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