@@ -3212,6 +3212,109 @@ mod tests {
3212
3212
// We can't continue, sadly, because our (1) now has a bogus signature
3213
3213
}
3214
3214
3215
+ #[ test]
3216
+ fn test_multi_flight_update_fee ( ) {
3217
+ let nodes = create_network ( 2 ) ;
3218
+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
3219
+ let channel_id = chan. 2 ;
3220
+
3221
+ macro_rules! get_feerate {
3222
+ ( $node: expr) => { {
3223
+ let chan_lock = $node. node. channel_state. lock( ) . unwrap( ) ;
3224
+ let chan = chan_lock. by_id. get( & channel_id) . unwrap( ) ;
3225
+ chan. get_feerate( )
3226
+ } }
3227
+ }
3228
+
3229
+ // A B
3230
+ // update_fee/commitment_signed ->
3231
+ // .- send (1) RAA and (2) commitment_signed
3232
+ // update_fee (never committed) ->
3233
+ // (3) update_fee ->
3234
+ // We have to manually generate the above update_fee, it is allowed by the protocol but we
3235
+ // don't track which updates correspond to which revoke_and_ack responses so we're in
3236
+ // AwaitingRAA mode and will not generate the update_fee yet.
3237
+ // <- (1) RAA delivered
3238
+ // (3) is generated and send (4) CS -.
3239
+ // Note that A cannot generate (4) prior to (1) being delivered as it otherwise doesn't
3240
+ // know the per_commitment_point to use for it.
3241
+ // <- (2) commitment_signed delivered
3242
+ // revoke_and_ack ->
3243
+ // B should send no response here
3244
+ // (4) commitment_signed delivered ->
3245
+ // <- RAA/commitment_signed delivered
3246
+ // revoke_and_ack ->
3247
+
3248
+ // First nodes[0] generates an update_fee
3249
+ let initial_feerate = get_feerate ! ( nodes[ 0 ] ) ;
3250
+ nodes[ 0 ] . node . update_fee ( channel_id, initial_feerate + 20 ) . unwrap ( ) ;
3251
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3252
+
3253
+ let events_0 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
3254
+ assert_eq ! ( events_0. len( ) , 1 ) ;
3255
+ let ( update_msg_1, commitment_signed_1) = match events_0[ 0 ] { // (1)
3256
+ Event :: UpdateHTLCs { updates : msgs:: CommitmentUpdate { ref update_fee, ref commitment_signed, .. } , .. } => {
3257
+ ( update_fee. as_ref ( ) . unwrap ( ) , commitment_signed)
3258
+ } ,
3259
+ _ => panic ! ( "Unexpected event" ) ,
3260
+ } ;
3261
+
3262
+ // Deliver first update_fee/commitment_signed pair, generating (1) and (2):
3263
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , update_msg_1) . unwrap ( ) ;
3264
+ let ( bs_revoke_msg, bs_commitment_signed) = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , commitment_signed_1) . unwrap ( ) ;
3265
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
3266
+
3267
+ // nodes[0] is awaiting a revoke from nodes[1] before it will create a new commitment
3268
+ // transaction:
3269
+ nodes[ 0 ] . node . update_fee ( channel_id, initial_feerate + 40 ) . unwrap ( ) ;
3270
+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
3271
+
3272
+ // Create the (3) update_fee message that nodes[0] will generate before it does...
3273
+ let mut update_msg_2 = msgs:: UpdateFee {
3274
+ channel_id : update_msg_1. channel_id . clone ( ) ,
3275
+ feerate_per_kw : ( initial_feerate + 30 ) as u32 ,
3276
+ } ;
3277
+
3278
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , & update_msg_2) . unwrap ( ) ;
3279
+
3280
+ update_msg_2. feerate_per_kw = ( initial_feerate + 40 ) as u32 ;
3281
+ // Deliver (3)
3282
+ nodes[ 1 ] . node . handle_update_fee ( & nodes[ 0 ] . node . get_our_node_id ( ) , & update_msg_2) . unwrap ( ) ;
3283
+
3284
+ // Deliver (1), generating (3) and (4)
3285
+ let as_second_update = nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_msg) . unwrap ( ) ;
3286
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3287
+ assert ! ( as_second_update. as_ref( ) . unwrap( ) . update_add_htlcs. is_empty( ) ) ;
3288
+ assert ! ( as_second_update. as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
3289
+ assert ! ( as_second_update. as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
3290
+ assert ! ( as_second_update. as_ref( ) . unwrap( ) . update_fail_malformed_htlcs. is_empty( ) ) ;
3291
+ // Check that the update_fee newly generated matches what we delivered:
3292
+ assert_eq ! ( as_second_update. as_ref( ) . unwrap( ) . update_fee. as_ref( ) . unwrap( ) . channel_id, update_msg_2. channel_id) ;
3293
+ assert_eq ! ( as_second_update. as_ref( ) . unwrap( ) . update_fee. as_ref( ) . unwrap( ) . feerate_per_kw, update_msg_2. feerate_per_kw) ;
3294
+
3295
+ // Deliver (2) commitment_signed
3296
+ let ( as_revoke_msg, as_commitment_signed) = nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , bs_commitment_signed. as_ref ( ) . unwrap ( ) ) . unwrap ( ) ;
3297
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3298
+ assert ! ( as_commitment_signed. is_none( ) ) ;
3299
+
3300
+ assert ! ( nodes[ 1 ] . node. handle_revoke_and_ack( & nodes[ 0 ] . node. get_our_node_id( ) , & as_revoke_msg) . unwrap( ) . is_none( ) ) ;
3301
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
3302
+
3303
+ // Delever (4)
3304
+ let ( bs_second_revoke, bs_second_commitment) = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_second_update. unwrap ( ) . commitment_signed ) . unwrap ( ) ;
3305
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
3306
+
3307
+ assert ! ( nodes[ 0 ] . node. handle_revoke_and_ack( & nodes[ 1 ] . node. get_our_node_id( ) , & bs_second_revoke) . unwrap( ) . is_none( ) ) ;
3308
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3309
+
3310
+ let ( as_second_revoke, as_second_commitment) = nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_second_commitment. unwrap ( ) ) . unwrap ( ) ;
3311
+ assert ! ( as_second_commitment. is_none( ) ) ;
3312
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
3313
+
3314
+ assert ! ( nodes[ 1 ] . node. handle_revoke_and_ack( & nodes[ 0 ] . node. get_our_node_id( ) , & as_second_revoke) . unwrap( ) . is_none( ) ) ;
3315
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
3316
+ }
3317
+
3215
3318
#[ test]
3216
3319
fn test_update_fee_vanilla ( ) {
3217
3320
let nodes = create_network ( 2 ) ;
0 commit comments