@@ -3038,8 +3038,8 @@ fn test_blocked_chan_preimage_release() {
30383038 let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
30393039 let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
30403040
3041- create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3042- create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3041+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
3042+ let chan_id_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
30433043
30443044 send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 5_000_000 ) ;
30453045
@@ -3068,20 +3068,29 @@ fn test_blocked_chan_preimage_release() {
30683068 let as_htlc_fulfill_updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
30693069 nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_htlc_fulfill_updates. update_fulfill_htlcs [ 0 ] ) ;
30703070 check_added_monitors ( & nodes[ 1 ] , 1 ) ; // We generate only a preimage monitor update
3071+ assert ! ( get_monitor!( nodes[ 1 ] , chan_id_2) . get_stored_preimages( ) . contains_key( & payment_hash_2) ) ;
30713072 assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
30723073
3073- // Finish the CS dance between nodes[0] and nodes[1].
3074- do_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 0 ] , & as_htlc_fulfill_updates. commitment_signed , false , false ) ;
3074+ // Finish the CS dance between nodes[0] and nodes[1]. Note that until the event handling, the
3075+ // update_fulfill_htlc + CS is held, even though the preimage is already on disk for the
3076+ // channel.
3077+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_htlc_fulfill_updates. commitment_signed ) ;
3078+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3079+ let ( a, raa) = do_main_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 0 ] , false ) ;
3080+ assert ! ( a. is_none( ) ) ;
3081+
3082+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & raa) ;
30753083 check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3084+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
30763085
30773086 let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
30783087 assert_eq ! ( events. len( ) , 3 ) ;
30793088 if let Event :: PaymentSent { .. } = events[ 0 ] { } else { panic ! ( ) ; }
30803089 if let Event :: PaymentPathSuccessful { .. } = events[ 2 ] { } else { panic ! ( ) ; }
30813090 if let Event :: PaymentForwarded { .. } = events[ 1 ] { } else { panic ! ( ) ; }
30823091
3083- // The event processing should release the last RAA update .
3084- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3092+ // The event processing should release the last RAA updates on both channels .
3093+ check_added_monitors ( & nodes[ 1 ] , 2 ) ;
30853094
30863095 // When we fetch the next update the message getter will generate the next update for nodes[2],
30873096 // generating a further monitor update.
@@ -3092,3 +3101,128 @@ fn test_blocked_chan_preimage_release() {
30923101 do_commitment_signed_dance ( & nodes[ 2 ] , & nodes[ 1 ] , & bs_htlc_fulfill_updates. commitment_signed , false , false ) ;
30933102 expect_payment_sent ( & nodes[ 2 ] , payment_preimage_2, None , true , true ) ;
30943103}
3104+
3105+ fn do_test_inverted_mon_completion_order ( complete_bc_commitment_dance : bool ) {
3106+ // When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
3107+ // channel, we immediately claim the HTLC on the upstream channel, before even doing a
3108+ // `commitment_signed` dance on the downstream channel. This implies that our
3109+ // `ChannelMonitorUpdate`s are generated in the right order - first we ensure we'll get our
3110+ // money, then we write the update that resolves the downstream node claiming their money. This
3111+ // is safe as long as `ChannelMonitorUpdate`s complete in the order in which they are
3112+ // generated, but of course this may not be the case. For asynchronous update writes, we have
3113+ // to ensure monitor updates can block each other, preventing the inversion all together.
3114+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3115+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3116+
3117+ let persister;
3118+ let new_chain_monitor;
3119+ let nodes_1_deserialized;
3120+
3121+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3122+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3123+
3124+ let chan_id_ab = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3125+ let chan_id_bc = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3126+
3127+ // Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3128+ // `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3129+ // on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3130+ let ( payment_preimage, payment_hash, ..) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 100_000 ) ;
3131+
3132+ let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3133+
3134+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
3135+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3136+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, 100_000 ) ;
3137+
3138+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3139+ let cs_updates = get_htlc_update_msgs ( & nodes[ 2 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3140+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. update_fulfill_htlcs [ 0 ] ) ;
3141+
3142+ // B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3143+ // for it since the monitor update is marked in-progress.
3144+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3145+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3146+
3147+ // Now step the Commitment Signed Dance between B and C forward a bit (or fully), ensuring we
3148+ // won't get the preimage when the nodes reconnect and we have to get it from the
3149+ // ChannelMonitor.
3150+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. commitment_signed ) ;
3151+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3152+ if complete_bc_commitment_dance {
3153+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
3154+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) ;
3155+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3156+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_commitment_signed) ;
3157+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3158+ let cs_raa = get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
3159+
3160+ // At this point node B still hasn't persisted the `ChannelMonitorUpdate` with the
3161+ // preimage in the A <-> B channel, which will prevent it from persisting the
3162+ // `ChannelMonitorUpdate` for the B<->C channel here to avoid "losing" the preimage.
3163+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_raa) ;
3164+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3165+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3166+ }
3167+
3168+ // Now reload node B
3169+ let manager_b = nodes[ 1 ] . node . encode ( ) ;
3170+
3171+ let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
3172+ reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
3173+
3174+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3175+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3176+
3177+ // If we used the latest ChannelManager to reload from, we should have both channels still
3178+ // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3179+ // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3180+ // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3181+ // complete after reconnecting to our peers.
3182+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3183+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3184+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3185+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3186+
3187+ // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3188+ // the end go ahead and do that, though the
3189+ // `pending_responding_commitment_signed_dup_monitor` in `reconnect_args` indicates that we
3190+ // expect to *not* receive the final RAA ChannelMonitorUpdate.
3191+ if complete_bc_commitment_dance {
3192+ reconnect_nodes ( ReconnectArgs :: new ( & nodes[ 1 ] , & nodes[ 2 ] ) ) ;
3193+ } else {
3194+ let mut reconnect_args = ReconnectArgs :: new ( & nodes[ 1 ] , & nodes[ 2 ] ) ;
3195+ reconnect_args. pending_responding_commitment_signed . 1 = true ;
3196+ reconnect_args. pending_responding_commitment_signed_dup_monitor . 1 = true ;
3197+ reconnect_args. pending_raa = ( false , true ) ;
3198+ reconnect_nodes ( reconnect_args) ;
3199+ }
3200+
3201+ reconnect_nodes ( ReconnectArgs :: new ( & nodes[ 0 ] , & nodes[ 1 ] ) ) ;
3202+
3203+ // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3204+ // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3205+ // process.
3206+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3207+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
3208+
3209+ // When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3210+ // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3211+ // channel.
3212+ let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
3213+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3214+
3215+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
3216+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
3217+
3218+ expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , false ) ;
3219+
3220+ // Finally, check that the payment was, ultimately, seen as sent by node A.
3221+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
3222+ }
3223+
3224+ #[ test]
3225+ fn test_inverted_mon_completion_order ( ) {
3226+ do_test_inverted_mon_completion_order ( true ) ;
3227+ do_test_inverted_mon_completion_order ( false ) ;
3228+ }
0 commit comments