@@ -3021,18 +3021,27 @@ fn test_blocked_chan_preimage_release() {
3021
3021
check_added_monitors ( & nodes[ 1 ] , 1 ) ; // We generate only a preimage monitor update
3022
3022
assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3023
3023
3024
- // Finish the CS dance between nodes[0] and nodes[1].
3025
- commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , as_htlc_fulfill_updates. commitment_signed, false ) ;
3024
+ // Finish the CS dance between nodes[0] and nodes[1]. Note that until the final RAA CS is held
3025
+ // until the full set of `ChannelMonitorUpdate`s on the nodes[1] <-> nodes[2] channel are
3026
+ // complete, while the preimage that we care about ensuring is on disk did make it there above,
3027
+ // the holding logic doesn't care about the type of update, it just cares that there is one.
3028
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_htlc_fulfill_updates. commitment_signed ) ;
3029
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3030
+ let ( a, raa) = do_main_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 0 ] , false ) ;
3031
+ assert ! ( a. is_none( ) ) ;
3032
+
3033
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & raa) ;
3026
3034
check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3035
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3027
3036
3028
3037
let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
3029
3038
assert_eq ! ( events. len( ) , 3 ) ;
3030
3039
if let Event :: PaymentSent { .. } = events[ 0 ] { } else { panic ! ( ) ; }
3031
3040
if let Event :: PaymentPathSuccessful { .. } = events[ 2 ] { } else { panic ! ( ) ; }
3032
3041
if let Event :: PaymentForwarded { .. } = events[ 1 ] { } else { panic ! ( ) ; }
3033
3042
3034
- // The event processing should release the last RAA update .
3035
- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3043
+ // The event processing should release the last RAA updates on both channels .
3044
+ check_added_monitors ( & nodes[ 1 ] , 2 ) ;
3036
3045
3037
3046
// When we fetch the next update the message getter will generate the next update for nodes[2],
3038
3047
// generating a further monitor update.
@@ -3043,3 +3052,123 @@ fn test_blocked_chan_preimage_release() {
3043
3052
commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , bs_htlc_fulfill_updates. commitment_signed, false ) ;
3044
3053
expect_payment_sent ( & nodes[ 2 ] , payment_preimage_2, None , true , true ) ;
3045
3054
}
3055
+
3056
+ fn do_test_inverted_mon_completion_order ( complete_bc_commitment_dance : bool ) {
3057
+ // When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
3058
+ // channel, we immediately claim the HTLC on the upstream channel, before even doing a
3059
+ // `commitment_signed` dance on the downstream channel. This implies that our
3060
+ // `ChannelMonitorUpdate`s "go out" in the right order - first we ensure we'll get our money,
3061
+ // then we write the update that resolves giving money on the downstream node. This is safe as
3062
+ // long as `ChannelMonitorUpdate`s complete in the order in which they are generated, but of
3063
+ // course this may not be the case. For asynchronous update writes, we have to ensure monitor
3064
+ // updates can block each other, preventing the inversion all together.
3065
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3066
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3067
+
3068
+ let persister;
3069
+ let new_chain_monitor;
3070
+ let nodes_1_deserialized;
3071
+
3072
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3073
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3074
+
3075
+ let chan_id_ab = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3076
+ let chan_id_bc = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3077
+
3078
+ // Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3079
+ // `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3080
+ // on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3081
+ let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 100_000 ) ;
3082
+
3083
+ let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3084
+
3085
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
3086
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3087
+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, 100_000 ) ;
3088
+
3089
+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3090
+ let cs_updates = get_htlc_update_msgs ( & nodes[ 2 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3091
+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. update_fulfill_htlcs [ 0 ] ) ;
3092
+
3093
+ // B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3094
+ // for it since the monitor update is marked in-progress.
3095
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3096
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3097
+
3098
+ // Now step the Commitment Signed Dance between B and C forward a bit (or fully), ensuring we
3099
+ // won't get the preimage when the nodes reconnect, at which point we have to ensure we get it
3100
+ // from the ChannelMonitor.
3101
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. commitment_signed ) ;
3102
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3103
+ if complete_bc_commitment_dance {
3104
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
3105
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) ;
3106
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3107
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_commitment_signed) ;
3108
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3109
+ let cs_raa = get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
3110
+
3111
+ // At this point node B still hasn't persisted the `ChannelMonitorUpdate` with the
3112
+ // preimage in the A <-> B channel, which will prevent it from persisting the
3113
+ // `ChannelMonitorUpdate` here to avoid "losing" the preimage.
3114
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_raa) ;
3115
+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3116
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3117
+ }
3118
+
3119
+ // Now reload node B
3120
+ let manager_b = nodes[ 1 ] . node . encode ( ) ;
3121
+
3122
+ let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
3123
+ reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
3124
+
3125
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3126
+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3127
+
3128
+ // If we used the latest ChannelManager to reload from, we should have both channels still
3129
+ // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3130
+ // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3131
+ // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3132
+ // complete after reconnecting to our peers.
3133
+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3134
+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3135
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3136
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3137
+
3138
+ // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3139
+ // the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3140
+ // expect to *not* receive the final RAA ChannelMonitorUpdate.
3141
+ if complete_bc_commitment_dance {
3142
+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3143
+ } else {
3144
+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , -2 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , true ) ) ;
3145
+ }
3146
+
3147
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3148
+
3149
+ // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3150
+ // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3151
+ // process.
3152
+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3153
+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
3154
+
3155
+ // When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3156
+ // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3157
+ // channel.
3158
+ let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
3159
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3160
+
3161
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
3162
+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
3163
+
3164
+ expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , false ) ;
3165
+
3166
+ // Finally, check that the payment was, ultimately, seen as sent by node A.
3167
+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
3168
+ }
3169
+
3170
+ #[ test]
3171
+ fn test_inverted_mon_completion_order ( ) {
3172
+ do_test_inverted_mon_completion_order ( true ) ;
3173
+ do_test_inverted_mon_completion_order ( false ) ;
3174
+ }
0 commit comments