@@ -6329,3 +6329,98 @@ fn test_announce_disable_channels() {
6329
6329
let msg_events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
6330
6330
assert_eq ! ( msg_events. len( ) , 0 ) ;
6331
6331
}
6332
+
6333
+ #[ test]
6334
+ fn test_bump_penalty_txn_on_revoked_commitment ( ) {
6335
+ // In case of penalty txn with too low feerates for getting into mempools, RBF-bump them to be sure
6336
+ // we're able to claim outputs on revoked commitment transaction before timelocks expiration
6337
+
6338
+ let nodes = create_network ( 2 , & [ None , None ] ) ;
6339
+
6340
+ let chan = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1000000 , 59000000 , LocalFeatures :: new ( ) , LocalFeatures :: new ( ) ) ;
6341
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 3000000 ) . 0 ;
6342
+ route_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 0 ] ) [ ..] , 3000000 ) . 0 ;
6343
+ let revoked_txn = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
6344
+ // Revoked commitment txn with 4 outputs : to_local, to_remote, 1 outgoing HTLC, 1 incoming HTLC
6345
+ assert_eq ! ( revoked_txn[ 0 ] . output. len( ) , 4 ) ;
6346
+ assert_eq ! ( revoked_txn[ 0 ] . input. len( ) , 1 ) ;
6347
+ assert_eq ! ( revoked_txn[ 0 ] . input[ 0 ] . previous_output. txid, chan. 3 . txid( ) ) ;
6348
+ let revoked_txid = revoked_txn[ 0 ] . txid ( ) ;
6349
+
6350
+ let mut penalty_sum = 0 ;
6351
+ for outp in revoked_txn[ 0 ] . output . iter ( ) {
6352
+ if outp. script_pubkey . is_v0_p2wsh ( ) {
6353
+ penalty_sum += outp. value ;
6354
+ }
6355
+ }
6356
+
6357
+ // Actually revoke tx by claiming a HTLC
6358
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , payment_preimage, 3_000_000 ) ;
6359
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
6360
+ nodes[ 1 ] . block_notifier . block_connected ( & Block { header, txdata : vec ! [ revoked_txn[ 0 ] . clone( ) ] } , 1 ) ;
6361
+
6362
+ // One or more justice tx should have been broadcast, check it
6363
+ let penalty_1;
6364
+ let feerate_1;
6365
+ {
6366
+ let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
6367
+ assert_eq ! ( node_txn. len( ) , 4 ) ; // justice tx (broadcasted from ChannelMonitor) * 2 (block-reparsing) + local commitment tx + local HTLC-timeout (broadcasted from ChannelManager)
6368
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 3 ] ) ;
6369
+ assert_eq ! ( node_txn[ 0 ] . input. len( ) , 3 ) ; // Penalty txn claims to_local, offered_htlc and received_htlc outputs
6370
+ assert_eq ! ( node_txn[ 0 ] . output. len( ) , 1 ) ;
6371
+ check_spends ! ( node_txn[ 0 ] , revoked_txn[ 0 ] . clone( ) ) ;
6372
+ let fee_1 = penalty_sum - node_txn[ 0 ] . output [ 0 ] . value ;
6373
+ feerate_1 = fee_1 * 1000 / node_txn[ 0 ] . get_weight ( ) as u64 ;
6374
+ penalty_1 = node_txn[ 0 ] . txid ( ) ;
6375
+ node_txn. clear ( ) ;
6376
+ } ;
6377
+
6378
+ // After exhaustion of height timer, a new bumped justice tx should have been broadcast, check it
6379
+ let header = connect_blocks ( & nodes[ 1 ] . block_notifier , 15 , 1 , true , header. bitcoin_hash ( ) ) ;
6380
+ let mut penalty_2 = penalty_1;
6381
+ let mut feerate_2 = 0 ;
6382
+ {
6383
+ let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
6384
+ assert_eq ! ( node_txn. len( ) , 1 ) ;
6385
+ if node_txn[ 0 ] . input [ 0 ] . previous_output . txid == revoked_txid {
6386
+ assert_eq ! ( node_txn[ 0 ] . input. len( ) , 3 ) ; // Penalty txn claims to_local, offered_htlc and received_htlc outputs
6387
+ assert_eq ! ( node_txn[ 0 ] . output. len( ) , 1 ) ;
6388
+ check_spends ! ( node_txn[ 0 ] , revoked_txn[ 0 ] . clone( ) ) ;
6389
+ penalty_2 = node_txn[ 0 ] . txid ( ) ;
6390
+ // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
6391
+ assert_ne ! ( penalty_2, penalty_1) ;
6392
+ let fee_2 = penalty_sum - node_txn[ 0 ] . output [ 0 ] . value ;
6393
+ feerate_2 = fee_2 * 1000 / node_txn[ 0 ] . get_weight ( ) as u64 ;
6394
+ // Verify 25% bump heuristic
6395
+ assert ! ( feerate_2 * 100 >= feerate_1 * 125 ) ;
6396
+ node_txn. clear ( ) ;
6397
+ }
6398
+ }
6399
+ assert_ne ! ( feerate_2, 0 ) ;
6400
+
6401
+ // After exhaustion of height timer for a 2nd time, a new bumped justice tx should have been broadcast, check it
6402
+ connect_blocks ( & nodes[ 1 ] . block_notifier , 15 , 16 , true , header) ;
6403
+ let penalty_3;
6404
+ let mut feerate_3 = 0 ;
6405
+ {
6406
+ let mut node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
6407
+ assert_eq ! ( node_txn. len( ) , 1 ) ;
6408
+ if node_txn[ 0 ] . input [ 0 ] . previous_output . txid == revoked_txid {
6409
+ assert_eq ! ( node_txn[ 0 ] . input. len( ) , 3 ) ; // Penalty txn claims to_local, offered_htlc and received_htlc outputs
6410
+ assert_eq ! ( node_txn[ 0 ] . output. len( ) , 1 ) ;
6411
+ check_spends ! ( node_txn[ 0 ] , revoked_txn[ 0 ] . clone( ) ) ;
6412
+ penalty_3 = node_txn[ 0 ] . txid ( ) ;
6413
+ // Verify new bumped tx is different from last claiming transaction, we don't want spurrious rebroadcast
6414
+ assert_ne ! ( penalty_3, penalty_2) ;
6415
+ let fee_3 = penalty_sum - node_txn[ 0 ] . output [ 0 ] . value ;
6416
+ feerate_3 = fee_3 * 1000 / node_txn[ 0 ] . get_weight ( ) as u64 ;
6417
+ // Verify 25% bump heuristic
6418
+ assert ! ( feerate_3 * 100 >= feerate_2 * 125 ) ;
6419
+ node_txn. clear ( ) ;
6420
+ }
6421
+ }
6422
+ assert_ne ! ( feerate_3, 0 ) ;
6423
+
6424
+ nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
6425
+ nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
6426
+ }
0 commit comments