@@ -93,7 +93,9 @@ impl OutboundJITChannelState {
93
93
}
94
94
}
95
95
96
- fn htlc_intercepted ( & self , htlc : InterceptedHTLC ) -> Result < Self , ChannelStateError > {
96
+ fn htlc_intercepted (
97
+ & self , htlc : InterceptedHTLC , config : & LSPS2ServiceConfig ,
98
+ ) -> Result < Self , ChannelStateError > {
97
99
match self {
98
100
OutboundJITChannelState :: AwaitingPayment {
99
101
htlcs,
@@ -107,24 +109,46 @@ impl OutboundJITChannelState {
107
109
let total_expected_outbound_amount_msat =
108
110
htlcs. iter ( ) . map ( |htlc| htlc. expected_outbound_amount_msat ) . sum ( ) ;
109
111
110
- let expected_payment_size_msat =
111
- payment_size_msat. unwrap_or ( total_expected_outbound_amount_msat) ;
112
+ let ( expected_payment_size_msat, mpp_mode) =
113
+ if let Some ( payment_size_msat) = payment_size_msat {
114
+ ( * payment_size_msat, true )
115
+ } else {
116
+ debug_assert_eq ! ( htlcs. len( ) , 1 ) ;
117
+ if htlcs. len ( ) != 1 {
118
+ return Err ( ChannelStateError (
119
+ format ! ( "Paying via multiple HTLCs is disallowed in \" no-MPP+var-invoice\" mode." )
120
+ ) ) ;
121
+ }
122
+ ( total_expected_outbound_amount_msat, false )
123
+ } ;
124
+
125
+ if expected_payment_size_msat < config. min_payment_size_msat
126
+ || expected_payment_size_msat > config. max_payment_size_msat
127
+ {
128
+ return Err ( ChannelStateError (
129
+ format ! ( "Payment size violates our limits: expected_payment_size_msat = {}, min_payment_size_msat = {}, max_payment_size_msat = {}" ,
130
+ expected_payment_size_msat,
131
+ config. min_payment_size_msat,
132
+ config. max_payment_size_msat
133
+ ) ) ) ;
134
+ }
112
135
113
136
let opening_fee_msat = compute_opening_fee (
114
137
expected_payment_size_msat,
115
138
* min_fee_msat,
116
139
( * proportional_fee) . into ( ) ,
117
140
) . ok_or ( ChannelStateError (
118
- format ! ( "Could not compute valid opening fee with min_fee_msat = {}, proportional = {}, and total_expected_outbound_amount_msat = {}" ,
141
+ format ! ( "Could not compute valid opening fee with min_fee_msat = {}, proportional = {}, and expected_payment_size_msat = {}" ,
119
142
min_fee_msat,
120
143
proportional_fee,
121
- total_expected_outbound_amount_msat
144
+ expected_payment_size_msat
122
145
)
123
146
) ) ?;
124
147
125
148
let amt_to_forward_msat =
126
149
expected_payment_size_msat. saturating_sub ( opening_fee_msat) ;
127
150
151
+ // Go ahead and open the channel if we intercepted sufficient HTLCs.
128
152
if total_expected_outbound_amount_msat >= expected_payment_size_msat
129
153
&& amt_to_forward_msat > 0
130
154
{
@@ -134,21 +158,22 @@ impl OutboundJITChannelState {
134
158
amt_to_forward_msat,
135
159
} )
136
160
} else {
137
- // payment size being specified means MPP is supported
138
- if payment_size_msat. is_some ( ) {
161
+ if mpp_mode {
139
162
Ok ( OutboundJITChannelState :: AwaitingPayment {
140
163
min_fee_msat : * min_fee_msat,
141
164
proportional_fee : * proportional_fee,
142
165
htlcs,
143
166
payment_size_msat : * payment_size_msat,
144
167
} )
145
168
} else {
146
- Err ( ChannelStateError ( "HTLC is too small to pay opening fee" . to_string ( ) ) )
169
+ Err ( ChannelStateError (
170
+ "Intercepted HTLC is too small to pay opening fee" . to_string ( ) ,
171
+ ) )
147
172
}
148
173
}
149
174
}
150
175
state => Err ( ChannelStateError ( format ! (
151
- "Invoice params received when JIT Channel was in state: {:?}" ,
176
+ "Intercepted HTLC when JIT Channel was in state: {:?}" ,
152
177
state
153
178
) ) ) ,
154
179
}
@@ -186,9 +211,9 @@ impl OutboundJITChannel {
186
211
}
187
212
188
213
fn htlc_intercepted (
189
- & mut self , htlc : InterceptedHTLC ,
214
+ & mut self , htlc : InterceptedHTLC , config : & LSPS2ServiceConfig ,
190
215
) -> Result < Option < ( u64 , u64 ) > , LightningError > {
191
- self . state = self . state . htlc_intercepted ( htlc) ?;
216
+ self . state = self . state . htlc_intercepted ( htlc, config ) ?;
192
217
193
218
match & self . state {
194
219
OutboundJITChannelState :: AwaitingPayment { .. } => {
@@ -447,7 +472,7 @@ where
447
472
peer_state. outbound_channels_by_intercept_scid . get_mut ( & intercept_scid)
448
473
{
449
474
let htlc = InterceptedHTLC { intercept_id, expected_outbound_amount_msat } ;
450
- match jit_channel. htlc_intercepted ( htlc) {
475
+ match jit_channel. htlc_intercepted ( htlc, & self . config ) {
451
476
Ok ( Some ( ( opening_fee_msat, amt_to_forward_msat) ) ) => {
452
477
self . enqueue_event ( Event :: LSPS2Service (
453
478
LSPS2ServiceEvent :: OpenChannel {
0 commit comments