@@ -45,6 +45,7 @@ use bitcoin::blockdata::opcodes;
45
45
use bitcoin:: blockdata:: constants:: genesis_block;
46
46
use bitcoin:: network:: constants:: Network ;
47
47
use bitcoin:: consensus:: encode;
48
+ use bitcoin:: consensus:: serialize;
48
49
49
50
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
50
51
use bitcoin:: hashes:: Hash ;
@@ -1761,8 +1762,8 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
1761
1762
// Set the fee rate for the channel very high, to the point where the fundee
1762
1763
// sending any amount would result in a channel reserve violation. In this test
1763
1764
// we check that we would be prevented from sending an HTLC in this situation.
1764
- chanmon_cfgs[ 0 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 6000 } ;
1765
- chanmon_cfgs[ 1 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 6000 } ;
1765
+ chanmon_cfgs[ 0 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 6000 ) } ;
1766
+ chanmon_cfgs[ 1 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 6000 ) } ;
1766
1767
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1767
1768
let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1768
1769
let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
@@ -1793,8 +1794,8 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
1793
1794
// to channel reserve violation. This close could also happen if the fee went
1794
1795
// up a more realistic amount, but many HTLCs were outstanding at the time of
1795
1796
// the update_add_htlc.
1796
- chanmon_cfgs[ 0 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 6000 } ;
1797
- chanmon_cfgs[ 1 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 6000 } ;
1797
+ chanmon_cfgs[ 0 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 6000 ) } ;
1798
+ chanmon_cfgs[ 1 ] . fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 6000 ) } ;
1798
1799
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1799
1800
let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1800
1801
let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
@@ -4324,7 +4325,7 @@ fn test_no_txn_manager_serialize_deserialize() {
4324
4325
nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write_for_disk ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
4325
4326
4326
4327
logger = test_utils:: TestLogger :: new ( ) ;
4327
- fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
4328
+ fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } ;
4328
4329
utxo_pool = test_utils:: TestPool :: new ( ) ;
4329
4330
new_chan_monitor = test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , & logger, & fee_estimator, & utxo_pool) ;
4330
4331
nodes[ 0 ] . chan_monitor = & new_chan_monitor;
@@ -4433,7 +4434,7 @@ fn test_manager_serialize_deserialize_events() {
4433
4434
let mut chan_0_monitor_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
4434
4435
nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write_for_disk ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
4435
4436
4436
- fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
4437
+ fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } ;
4437
4438
logger = test_utils:: TestLogger :: new ( ) ;
4438
4439
utxo_pool = test_utils:: TestPool :: new ( ) ;
4439
4440
new_chan_monitor = test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , & logger, & fee_estimator, & utxo_pool) ;
@@ -4527,7 +4528,7 @@ fn test_simple_manager_serialize_deserialize() {
4527
4528
nodes[ 0 ] . chan_monitor . simple_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write_for_disk ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
4528
4529
4529
4530
logger = test_utils:: TestLogger :: new ( ) ;
4530
- fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
4531
+ fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } ;
4531
4532
utxo_pool = test_utils:: TestPool :: new ( ) ;
4532
4533
new_chan_monitor = test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , & logger, & fee_estimator, & utxo_pool) ;
4533
4534
nodes[ 0 ] . chan_monitor = & new_chan_monitor;
@@ -4607,7 +4608,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
4607
4608
}
4608
4609
4609
4610
logger = test_utils:: TestLogger :: new ( ) ;
4610
- fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
4611
+ fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } ;
4611
4612
utxo_pool = test_utils:: TestPool :: new ( ) ;
4612
4613
new_chan_monitor = test_utils:: TestChannelMonitor :: new ( nodes[ 0 ] . chain_monitor . clone ( ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , & logger, & fee_estimator, & utxo_pool) ;
4613
4614
nodes[ 0 ] . chan_monitor = & new_chan_monitor;
@@ -7386,7 +7387,7 @@ fn test_user_configurable_csv_delay() {
7386
7387
7387
7388
// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
7388
7389
let keys_manager: Arc < KeysInterface < ChanKeySigner = EnforcingChannelKeys > > = Arc :: new ( test_utils:: TestKeysInterface :: new ( & nodes[ 0 ] . node_seed , Network :: Testnet ) ) ;
7389
- if let Err ( error) = Channel :: new_outbound ( & & test_utils:: TestFeeEstimator { sat_per_kw : 253 } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , 1000000 , 1000000 , 0 , & low_our_to_self_config) {
7390
+ if let Err ( error) = Channel :: new_outbound ( & & test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , 1000000 , 1000000 , 0 , & low_our_to_self_config) {
7390
7391
match error {
7391
7392
APIError :: APIMisuseError { err } => { assert ! ( regex:: Regex :: new( r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks" ) . unwrap( ) . is_match( err. as_str( ) ) ) ; } ,
7392
7393
_ => panic ! ( "Unexpected event" ) ,
@@ -7397,7 +7398,7 @@ fn test_user_configurable_csv_delay() {
7397
7398
nodes[ 1 ] . node . create_channel ( nodes[ 0 ] . node . get_our_node_id ( ) , 1000000 , 1000000 , 42 , None ) . unwrap ( ) ;
7398
7399
let mut open_channel = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendOpenChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7399
7400
open_channel. to_self_delay = 200 ;
7400
- if let Err ( error) = Channel :: new_from_req ( & & test_utils:: TestFeeEstimator { sat_per_kw : 253 } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel, 0 , & low_our_to_self_config) {
7401
+ if let Err ( error) = Channel :: new_from_req ( & & test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel, 0 , & low_our_to_self_config) {
7401
7402
match error {
7402
7403
ChannelError :: Close ( err) => { assert ! ( regex:: Regex :: new( r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks" ) . unwrap( ) . is_match( err. as_str( ) ) ) ; } ,
7403
7404
_ => panic ! ( "Unexpected event" ) ,
@@ -7423,7 +7424,7 @@ fn test_user_configurable_csv_delay() {
7423
7424
nodes[ 1 ] . node . create_channel ( nodes[ 0 ] . node . get_our_node_id ( ) , 1000000 , 1000000 , 42 , None ) . unwrap ( ) ;
7424
7425
let mut open_channel = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendOpenChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
7425
7426
open_channel. to_self_delay = 200 ;
7426
- if let Err ( error) = Channel :: new_from_req ( & & test_utils:: TestFeeEstimator { sat_per_kw : 253 } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel, 0 , & high_their_to_self_config) {
7427
+ if let Err ( error) = Channel :: new_from_req ( & & test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } , & keys_manager, nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel, 0 , & high_their_to_self_config) {
7427
7428
match error {
7428
7429
ChannelError :: Close ( err) => { assert ! ( regex:: Regex :: new( r"They wanted our payments to be delayed by a needlessly long period\. Upper limit: \d+\. Actual: \d+" ) . unwrap( ) . is_match( err. as_str( ) ) ) ; } ,
7429
7430
_ => panic ! ( "Unexpected event" ) ,
@@ -7468,7 +7469,7 @@ fn test_data_loss_protect() {
7468
7469
let mut chan_monitor = <( BlockHash , ChannelMonitor < EnforcingChannelKeys > ) >:: read ( & mut :: std:: io:: Cursor :: new ( previous_chan_monitor_state. 0 ) ) . unwrap ( ) . 1 ;
7469
7470
chain_monitor = ChainWatchInterfaceUtil :: new ( Network :: Testnet ) ;
7470
7471
tx_broadcaster = test_utils:: TestBroadcaster { txn_broadcasted : Mutex :: new ( Vec :: new ( ) ) } ;
7471
- fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : 253 } ;
7472
+ fee_estimator = test_utils:: TestFeeEstimator { sat_per_kw : Mutex :: new ( 253 ) } ;
7472
7473
keys_manager = test_utils:: TestKeysInterface :: new ( & nodes[ 0 ] . node_seed , Network :: Testnet ) ;
7473
7474
utxo_pool = test_utils:: TestPool :: new ( ) ;
7474
7475
monitor = test_utils:: TestChannelMonitor :: new ( & chain_monitor, & tx_broadcaster, & logger, & fee_estimator, & utxo_pool) ;
@@ -8446,3 +8447,75 @@ fn test_concurrent_monitor_claim() {
8446
8447
check_spends ! ( txn[ 2 ] , chan_1. 3 ) ;
8447
8448
}
8448
8449
}
8450
+
8451
+ #[ test]
8452
+ fn test_basic_commitment_cpfp ( ) {
8453
+ // Node A sends a HTLC to Node B, who holds it. We propagate blocks until HTLC
8454
+ // reach its deadine and Node A must close the channnel. We observe a broadcast
8455
+ // of a commitment transaction and its anchor-output CPFP. After few blocks we
8456
+ // observe a feerate-bump of the CPFP.
8457
+
8458
+ // Create Alice and Bob
8459
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8460
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8461
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8462
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8463
+
8464
+ // Create a channel between Alice and Bob
8465
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
8466
+
8467
+ // Route a HTLC from Alice to Bob without settlement
8468
+ route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 9_000_000 ) . 0 ;
8469
+
8470
+ // Connect block on Alice's chain view until offered HTLC must be settled onchain
8471
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
8472
+ nodes[ 0 ] . block_notifier . block_connected ( & Block { header, txdata : vec ! [ ] } , 135 ) ;
8473
+ check_closed_broadcast ! ( nodes[ 0 ] , false ) ;
8474
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8475
+
8476
+ {
8477
+ let mut node_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8478
+ assert_eq ! ( node_txn. len( ) , 2 ) ;
8479
+
8480
+ // Verify 1st transaction spend channel funding
8481
+ check_spends ! ( node_txn[ 0 ] , chan_1. 3 ) ;
8482
+
8483
+ // Verify 2nd transaction is a HTLC-timeout
8484
+ assert_eq ! ( node_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , OFFERED_HTLC_SCRIPT_WEIGHT ) ;
8485
+ check_spends ! ( node_txn[ 1 ] , node_txn[ 0 ] ) ;
8486
+ node_txn. clear ( ) ;
8487
+ }
8488
+
8489
+ nodes[ 0 ] . chan_monitor . simple_monitor . fee_estimator . set_sat_per_kw ( 5000 ) ;
8490
+
8491
+ // Verify that CPFP logic is triggered after a feerate increase
8492
+ nodes[ 0 ] . block_notifier . block_connected ( & Block { header, txdata : vec ! [ ] } , 136 ) ;
8493
+ {
8494
+ let mut node_txn = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ;
8495
+ assert_eq ! ( node_txn. len( ) , 2 ) ;
8496
+
8497
+ // Verify 1st transaction spend channel funding
8498
+ check_spends ! ( node_txn[ 0 ] , chan_1. 3 ) ;
8499
+
8500
+ // Verify 2nd transaction is a Child-Pay-For-Parent
8501
+ assert_eq ! ( node_txn[ 1 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , ANCHOR_SCRIPT_WEIGHT ) ;
8502
+ assert_eq ! ( node_txn[ 1 ] . output. len( ) , 1 ) ;
8503
+ let mut check_anchor = false ;
8504
+ for ( idx, outp) in node_txn[ 0 ] . output . iter ( ) . enumerate ( ) {
8505
+ if outp. value == ANCHOR_OUTPUT_VALUE {
8506
+ let tx = serialize ( & node_txn[ 1 ] ) ;
8507
+ if let Ok ( _) = node_txn[ 0 ] . output [ idx] . script_pubkey . verify ( 0 , ANCHOR_OUTPUT_VALUE , tx. as_slice ( ) ) { check_anchor = true }
8508
+ }
8509
+ }
8510
+ if !check_anchor { panic ! ( "Invalid anchor output spent" ) ; }
8511
+
8512
+ // Verify that CPFP fee is correct. The effective fee must be superior or equal to
8513
+ // the required feerare multiply by the effective weight.
8514
+ // Effective weight is expected to differ from predicted weight as some fields
8515
+ // are inflated to prevent worst-case scenarios (73-bytes ECDSA sigs, count_tx_out, ..)
8516
+ let effective_package_weight = node_txn[ 0 ] . get_weight ( ) + node_txn[ 1 ] . get_weight ( ) ;
8517
+ let required_fee = 5000 * ( effective_package_weight as u64 ) / 1000 ;
8518
+ let effective_fee = ( test_utils:: TEST_BUMP_VALUE + ANCHOR_OUTPUT_VALUE ) - node_txn[ 1 ] . output [ 0 ] . value ;
8519
+ assert ! ( effective_fee >= required_fee) ;
8520
+ }
8521
+ }
0 commit comments