@@ -9208,6 +9208,125 @@ fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
9208
9208
do_test_tx_confirmed_skipping_blocks_immediate_broadcast ( true ) ;
9209
9209
}
9210
9210
9211
+ #[ test]
9212
+ fn test_forwardable_regen ( ) {
9213
+ // Tests that if we reload a ChannelManager while forwards are pending we will regenerate the
9214
+ // PendingHTLCsForwardable event automatically, ensuring we don't forget to forward/receive
9215
+ // HTLCs.
9216
+ // We test it for both payment receipt and payment forwarding.
9217
+
9218
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
9219
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
9220
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
9221
+ let persister: test_utils:: TestPersister ;
9222
+ let new_chain_monitor: test_utils:: TestChainMonitor ;
9223
+ let nodes_1_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
9224
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
9225
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
9226
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
9227
+
9228
+ // First send a payment to nodes[1]
9229
+ let ( route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 1 ] , 100_000 ) ;
9230
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) . unwrap ( ) ;
9231
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
9232
+
9233
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
9234
+ assert_eq ! ( events. len( ) , 1 ) ;
9235
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
9236
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
9237
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
9238
+
9239
+ expect_pending_htlcs_forwardable_ignore ! ( nodes[ 1 ] ) ;
9240
+
9241
+ // Next send a payment which is forwarded by nodes[1]
9242
+ let ( route_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 2 ] , 200_000 ) ;
9243
+ nodes[ 0 ] . node . send_payment ( & route_2, payment_hash_2, & Some ( payment_secret_2) ) . unwrap ( ) ;
9244
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
9245
+
9246
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
9247
+ assert_eq ! ( events. len( ) , 1 ) ;
9248
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
9249
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
9250
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
9251
+
9252
+ // There is already a PendingHTLCsForwardable event "pending" so another one will not be
9253
+ // generated
9254
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
9255
+
9256
+ // Now restart nodes[1] and make sure it regenerates a single PendingHTLCsForwardable
9257
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
9258
+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
9259
+
9260
+ let nodes_1_serialized = nodes[ 1 ] . node . encode ( ) ;
9261
+ let mut chan_0_monitor_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
9262
+ let mut chan_1_monitor_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
9263
+ {
9264
+ let monitors = nodes[ 1 ] . chain_monitor . chain_monitor . monitors . read ( ) . unwrap ( ) ;
9265
+ let mut monitor_iter = monitors. iter ( ) ;
9266
+ monitor_iter. next ( ) . unwrap ( ) . 1 . write ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
9267
+ monitor_iter. next ( ) . unwrap ( ) . 1 . write ( & mut chan_1_monitor_serialized) . unwrap ( ) ;
9268
+ }
9269
+
9270
+ persister = test_utils:: TestPersister :: new ( ) ;
9271
+ let keys_manager = & chanmon_cfgs[ 1 ] . keys_manager ;
9272
+ new_chain_monitor = test_utils:: TestChainMonitor :: new ( Some ( nodes[ 1 ] . chain_source ) , nodes[ 1 ] . tx_broadcaster . clone ( ) , nodes[ 1 ] . logger , node_cfgs[ 1 ] . fee_estimator , & persister, keys_manager) ;
9273
+ nodes[ 1 ] . chain_monitor = & new_chain_monitor;
9274
+
9275
+ let mut chan_0_monitor_read = & chan_0_monitor_serialized. 0 [ ..] ;
9276
+ let ( _, mut chan_0_monitor) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read (
9277
+ & mut chan_0_monitor_read, keys_manager) . unwrap ( ) ;
9278
+ assert ! ( chan_0_monitor_read. is_empty( ) ) ;
9279
+ let mut chan_1_monitor_read = & chan_1_monitor_serialized. 0 [ ..] ;
9280
+ let ( _, mut chan_1_monitor) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read (
9281
+ & mut chan_1_monitor_read, keys_manager) . unwrap ( ) ;
9282
+ assert ! ( chan_1_monitor_read. is_empty( ) ) ;
9283
+
9284
+ let mut nodes_1_read = & nodes_1_serialized[ ..] ;
9285
+ let ( _, nodes_1_deserialized_tmp) = {
9286
+ let mut channel_monitors = HashMap :: new ( ) ;
9287
+ channel_monitors. insert ( chan_0_monitor. get_funding_txo ( ) . 0 , & mut chan_0_monitor) ;
9288
+ channel_monitors. insert ( chan_1_monitor. get_funding_txo ( ) . 0 , & mut chan_1_monitor) ;
9289
+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_1_read, ChannelManagerReadArgs {
9290
+ default_config : UserConfig :: default ( ) ,
9291
+ keys_manager,
9292
+ fee_estimator : node_cfgs[ 1 ] . fee_estimator ,
9293
+ chain_monitor : nodes[ 1 ] . chain_monitor ,
9294
+ tx_broadcaster : nodes[ 1 ] . tx_broadcaster . clone ( ) ,
9295
+ logger : nodes[ 1 ] . logger ,
9296
+ channel_monitors,
9297
+ } ) . unwrap ( )
9298
+ } ;
9299
+ nodes_1_deserialized = nodes_1_deserialized_tmp;
9300
+ assert ! ( nodes_1_read. is_empty( ) ) ;
9301
+
9302
+ assert ! ( nodes[ 1 ] . chain_monitor. watch_channel( chan_0_monitor. get_funding_txo( ) . 0 , chan_0_monitor) . is_ok( ) ) ;
9303
+ assert ! ( nodes[ 1 ] . chain_monitor. watch_channel( chan_1_monitor. get_funding_txo( ) . 0 , chan_1_monitor) . is_ok( ) ) ;
9304
+ nodes[ 1 ] . node = & nodes_1_deserialized;
9305
+ check_added_monitors ! ( nodes[ 1 ] , 2 ) ;
9306
+
9307
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
9308
+ // Note that nodes[1] and nodes[2] resend their funding_locked here since they haven't updated
9309
+ // the commitment state.
9310
+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( true , true ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
9311
+
9312
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
9313
+
9314
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
9315
+ expect_payment_received ! ( nodes[ 1 ] , payment_hash, payment_secret, 100_000 ) ;
9316
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
9317
+
9318
+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
9319
+ assert_eq ! ( events. len( ) , 1 ) ;
9320
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
9321
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
9322
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , payment_event. commitment_msg, false ) ;
9323
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
9324
+ expect_payment_received ! ( nodes[ 2 ] , payment_hash_2, payment_secret_2, 200_000 ) ;
9325
+
9326
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage) ;
9327
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , payment_preimage_2) ;
9328
+ }
9329
+
9211
9330
#[ test]
9212
9331
fn test_keysend_payments_to_public_node ( ) {
9213
9332
let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
0 commit comments