@@ -231,6 +231,24 @@ impl ChannelState {
231
231
. remove ( hash)
232
232
. ok_or ( ForwardingError :: PaymentHashNotFound ( * hash) )
233
233
}
234
+
235
+ // Updates channel state to account for the resolution of an outgoing in-flight HTLC. If the HTLC failed, the
236
+ // balance is failed back to the channel's local balance. If not, the in-flight balance is settled to the other
237
+ // node, so there is no operation.
238
+ fn settle_outgoing_htlc ( & mut self , amt : u64 , success : bool ) {
239
+ if !success {
240
+ self . local_balance_msat += amt
241
+ }
242
+ }
243
+
244
+ // Updates channel state to account for the resolution of an incoming in-flight HTLC. If the HTLC succeeded,
245
+ // the balance is settled to the channel's local balance. If not, the in-flight balance is failed back to the
246
+ // other node, so there is no operation.
247
+ fn settle_incoming_htlc ( & mut self , amt : u64 , success : bool ) {
248
+ if success {
249
+ self . local_balance_msat += amt
250
+ }
251
+ }
234
252
}
235
253
236
254
/// Represents a simulated channel, and is responsible for managing addition and removal of HTLCs from the channel and
@@ -348,20 +366,13 @@ impl SimulatedChannel {
348
366
amount_msat : u64 ,
349
367
success : bool ,
350
368
) -> Result < ( ) , ForwardingError > {
351
- // Successful payments push balance to the receiver, failures return it to the sender.
352
- let ( sender_delta_msat, receiver_delta_msat) = if success {
353
- ( 0 , amount_msat)
354
- } else {
355
- ( amount_msat, 0 )
356
- } ;
357
-
358
369
if sending_node == & self . node_1 . policy . pubkey {
359
- self . node_1 . local_balance_msat += sender_delta_msat ;
360
- self . node_2 . local_balance_msat += receiver_delta_msat ;
370
+ self . node_1 . settle_outgoing_htlc ( amount_msat , success ) ;
371
+ self . node_2 . settle_incoming_htlc ( amount_msat , success ) ;
361
372
Ok ( ( ) )
362
373
} else if sending_node == & self . node_2 . policy . pubkey {
363
- self . node_2 . local_balance_msat += sender_delta_msat ;
364
- self . node_1 . local_balance_msat += receiver_delta_msat ;
374
+ self . node_2 . settle_outgoing_htlc ( amount_msat , success ) ;
375
+ self . node_1 . settle_incoming_htlc ( amount_msat , success ) ;
365
376
Ok ( ( ) )
366
377
} else {
367
378
Err ( ForwardingError :: NodeNotFound ( * sending_node) )
0 commit comments