9
9
10
10
//! Further functional tests which test blockchain reorganizations.
11
11
12
- use crate :: sign:: { ChannelSigner , EcdsaChannelSigner } ;
12
+ use crate :: sign:: EcdsaChannelSigner ;
13
13
use crate :: chain:: channelmonitor:: { ANTI_REORG_DELAY , LATENCY_GRACE_PERIOD_BLOCKS , Balance } ;
14
14
use crate :: chain:: transaction:: OutPoint ;
15
- use crate :: chain:: chaininterface:: LowerBoundedFeeEstimator ;
16
- use crate :: events:: bump_transaction:: BumpTransactionEvent ;
15
+ use crate :: chain:: chaininterface:: { LowerBoundedFeeEstimator , compute_feerate_sat_per_1000_weight } ;
16
+ use crate :: events:: bump_transaction:: { BumpTransactionEvent , WalletSource } ;
17
17
use crate :: events:: { Event , MessageSendEvent , MessageSendEventsProvider , ClosureReason , HTLCDestination } ;
18
18
use crate :: ln:: channel;
19
- use crate :: ln:: chan_utils;
20
19
use crate :: ln:: channelmanager:: { BREAKDOWN_TIMEOUT , ChannelManager , PaymentId , RecipientOnionFields } ;
21
20
use crate :: ln:: msgs:: ChannelMessageHandler ;
22
21
use crate :: util:: config:: UserConfig ;
@@ -1743,49 +1742,43 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
1743
1742
check_closed_event ( & nodes[ 0 ] , 1 , ClosureReason :: CommitmentTxConfirmed , false ) ;
1744
1743
check_added_monitors ( & nodes[ 0 ] , 1 ) ;
1745
1744
1745
+ let coinbase_tx = Transaction {
1746
+ version : 2 ,
1747
+ lock_time : PackedLockTime :: ZERO ,
1748
+ input : vec ! [ TxIn { ..Default :: default ( ) } ] ,
1749
+ output : vec ! [ TxOut { // UTXO to attach fees to `htlc_tx` on anchors
1750
+ value: Amount :: ONE_BTC . to_sat( ) ,
1751
+ script_pubkey: nodes[ 0 ] . wallet_source. get_change_script( ) . unwrap( ) ,
1752
+ } ] ,
1753
+ } ;
1754
+ nodes[ 0 ] . wallet_source . add_utxo ( bitcoin:: OutPoint { txid : coinbase_tx. txid ( ) , vout : 0 } , coinbase_tx. output [ 0 ] . value ) ;
1755
+
1746
1756
// Set up a helper closure we'll use throughout our test. We should only expect retries without
1747
1757
// bumps if fees have not increased after a block has been connected (assuming the height timer
1748
1758
// re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
1749
1759
let mut prev_htlc_tx_feerate = None ;
1750
1760
let mut check_htlc_retry = |should_retry : bool , should_bump : bool | -> Option < Transaction > {
1751
1761
let ( htlc_tx, htlc_tx_feerate) = if anchors {
1752
1762
assert ! ( nodes[ 0 ] . tx_broadcaster. txn_broadcast( ) . is_empty( ) ) ;
1753
- let mut events = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
1763
+ let events = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
1754
1764
assert_eq ! ( events. len( ) , if should_retry { 1 } else { 0 } ) ;
1755
1765
if !should_retry {
1756
1766
return None ;
1757
1767
}
1758
- #[ allow( unused_assignments) ]
1759
- let mut tx = Transaction {
1760
- version : 2 ,
1761
- lock_time : bitcoin:: PackedLockTime :: ZERO ,
1762
- input : vec ! [ ] ,
1763
- output : vec ! [ ] ,
1764
- } ;
1765
- #[ allow( unused_assignments) ]
1766
- let mut feerate = 0 ;
1767
- feerate = if let Event :: BumpTransaction ( BumpTransactionEvent :: HTLCResolution {
1768
- target_feerate_sat_per_1000_weight, mut htlc_descriptors, tx_lock_time, ..
1769
- } ) = events. pop ( ) . unwrap ( ) {
1770
- let secp = Secp256k1 :: new ( ) ;
1771
- assert_eq ! ( htlc_descriptors. len( ) , 1 ) ;
1772
- let descriptor = htlc_descriptors. pop ( ) . unwrap ( ) ;
1773
- assert_eq ! ( descriptor. commitment_txid, commitment_txn[ 0 ] . txid( ) ) ;
1774
- let htlc_output_idx = descriptor. htlc . transaction_output_index . unwrap ( ) as usize ;
1775
- assert ! ( htlc_output_idx < commitment_txn[ 0 ] . output. len( ) ) ;
1776
- tx. lock_time = tx_lock_time;
1777
- // Note that we don't care about actually making the HTLC transaction meet the
1778
- // feerate for the test, we just want to make sure the feerates we receive from
1779
- // the events never decrease.
1780
- tx. input . push ( descriptor. unsigned_tx_input ( ) ) ;
1781
- tx. output . push ( descriptor. tx_output ( & secp) ) ;
1782
- let signer = descriptor. derive_channel_signer ( & nodes[ 0 ] . keys_manager ) ;
1783
- let our_sig = signer. sign_holder_htlc_transaction ( & mut tx, 0 , & descriptor, & secp) . unwrap ( ) ;
1784
- let witness_script = descriptor. witness_script ( & secp) ;
1785
- tx. input [ 0 ] . witness = descriptor. tx_input_witness ( & our_sig, & witness_script) ;
1786
- target_feerate_sat_per_1000_weight as u64
1787
- } else { panic ! ( "unexpected event" ) ; } ;
1788
- ( tx, feerate)
1768
+ match & events[ 0 ] {
1769
+ Event :: BumpTransaction ( event) => {
1770
+ nodes[ 0 ] . bump_tx_handler . handle_event ( & event) ;
1771
+ let mut txn = nodes[ 0 ] . tx_broadcaster . unique_txn_broadcast ( ) ;
1772
+ assert_eq ! ( txn. len( ) , 1 ) ;
1773
+ let htlc_tx = txn. pop ( ) . unwrap ( ) ;
1774
+ check_spends ! ( & htlc_tx, & commitment_txn[ 0 ] , & coinbase_tx) ;
1775
+ let htlc_tx_fee = HTLC_AMT_SAT + coinbase_tx. output [ 0 ] . value -
1776
+ htlc_tx. output . iter ( ) . map ( |output| output. value ) . sum :: < u64 > ( ) ;
1777
+ let htlc_tx_weight = htlc_tx. weight ( ) as u64 ;
1778
+ ( htlc_tx, compute_feerate_sat_per_1000_weight ( htlc_tx_fee, htlc_tx_weight) )
1779
+ }
1780
+ _ => panic ! ( "Unexpected event" ) ,
1781
+ }
1789
1782
} else {
1790
1783
assert ! ( nodes[ 0 ] . chain_monitor. chain_monitor. get_and_clear_pending_events( ) . is_empty( ) ) ;
1791
1784
let mut txn = nodes[ 0 ] . tx_broadcaster . txn_broadcast ( ) ;
@@ -1796,8 +1789,8 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
1796
1789
let htlc_tx = txn. pop ( ) . unwrap ( ) ;
1797
1790
check_spends ! ( htlc_tx, commitment_txn[ 0 ] ) ;
1798
1791
let htlc_tx_fee = HTLC_AMT_SAT - htlc_tx. output [ 0 ] . value ;
1799
- let htlc_tx_feerate = htlc_tx_fee * 1000 / htlc_tx. weight ( ) as u64 ;
1800
- ( htlc_tx, htlc_tx_feerate )
1792
+ let htlc_tx_weight = htlc_tx. weight ( ) as u64 ;
1793
+ ( htlc_tx, compute_feerate_sat_per_1000_weight ( htlc_tx_fee , htlc_tx_weight ) )
1801
1794
} ;
1802
1795
if should_bump {
1803
1796
assert ! ( htlc_tx_feerate > prev_htlc_tx_feerate. take( ) . unwrap( ) ) ;
@@ -1837,9 +1830,11 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
1837
1830
1838
1831
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
1839
1832
mine_transaction ( & nodes[ 0 ] , & htlc_tx) ;
1840
- // If we have a `ConnectStyle` that advertises the new block first without the transasctions ,
1833
+ // If we have a `ConnectStyle` that advertises the new block first without the transactions ,
1841
1834
// we'll receive an extra bumped claim.
1842
1835
if nodes[ 0 ] . connect_style . borrow ( ) . updates_best_block_first ( ) {
1836
+ nodes[ 0 ] . wallet_source . add_utxo ( bitcoin:: OutPoint { txid : coinbase_tx. txid ( ) , vout : 0 } , coinbase_tx. output [ 0 ] . value ) ;
1837
+ nodes[ 0 ] . wallet_source . remove_utxo ( bitcoin:: OutPoint { txid : htlc_tx. txid ( ) , vout : 1 } ) ;
1843
1838
check_htlc_retry ( true , anchors) ;
1844
1839
}
1845
1840
nodes[ 0 ] . chain_monitor . chain_monitor . rebroadcast_pending_claims ( ) ;
@@ -1860,7 +1855,6 @@ fn test_yield_anchors_events() {
1860
1855
// allowing the consumer to provide additional fees to the commitment transaction to be
1861
1856
// broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1862
1857
// emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1863
- let secp = Secp256k1 :: new ( ) ;
1864
1858
let mut chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1865
1859
let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1866
1860
let mut anchors_config = UserConfig :: default ( ) ;
@@ -1891,26 +1885,23 @@ fn test_yield_anchors_events() {
1891
1885
let mut holder_events = nodes[ 0 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
1892
1886
assert_eq ! ( holder_events. len( ) , 1 ) ;
1893
1887
let ( commitment_tx, anchor_tx) = match holder_events. pop ( ) . unwrap ( ) {
1894
- Event :: BumpTransaction ( BumpTransactionEvent :: ChannelClose { commitment_tx, anchor_descriptor, .. } ) => {
1895
- assert_eq ! ( commitment_tx. input. len( ) , 1 ) ;
1896
- assert_eq ! ( commitment_tx. output. len( ) , 6 ) ;
1897
- let mut anchor_tx = Transaction {
1888
+ Event :: BumpTransaction ( event) => {
1889
+ let coinbase_tx = Transaction {
1898
1890
version : 2 ,
1899
1891
lock_time : PackedLockTime :: ZERO ,
1900
- input : vec ! [
1901
- TxIn { previous_output: anchor_descriptor. outpoint, ..Default :: default ( ) } ,
1902
- TxIn { ..Default :: default ( ) } ,
1903
- ] ,
1904
- output : vec ! [ TxOut {
1892
+ input : vec ! [ TxIn { ..Default :: default ( ) } ] ,
1893
+ output : vec ! [ TxOut { // UTXO to attach fees to `anchor_tx`
1905
1894
value: Amount :: ONE_BTC . to_sat( ) ,
1906
- script_pubkey: Script :: new_op_return ( & [ ] ) ,
1895
+ script_pubkey: nodes [ 0 ] . wallet_source . get_change_script ( ) . unwrap ( ) ,
1907
1896
} ] ,
1908
1897
} ;
1909
- let signer = anchor_descriptor. derive_channel_signer ( & nodes[ 0 ] . keys_manager ) ;
1910
- let funding_sig = signer. sign_holder_anchor_input ( & mut anchor_tx, 0 , & secp) . unwrap ( ) ;
1911
- anchor_tx. input [ 0 ] . witness = chan_utils:: build_anchor_input_witness (
1912
- & signer. pubkeys ( ) . funding_pubkey , & funding_sig
1913
- ) ;
1898
+ nodes[ 0 ] . wallet_source . add_utxo ( bitcoin:: OutPoint { txid : coinbase_tx. txid ( ) , vout : 0 } , coinbase_tx. output [ 0 ] . value ) ;
1899
+ nodes[ 0 ] . bump_tx_handler . handle_event ( & event) ;
1900
+ let mut txn = nodes[ 0 ] . tx_broadcaster . unique_txn_broadcast ( ) ;
1901
+ assert_eq ! ( txn. len( ) , 2 ) ;
1902
+ let anchor_tx = txn. pop ( ) . unwrap ( ) ;
1903
+ let commitment_tx = txn. pop ( ) . unwrap ( ) ;
1904
+ check_spends ! ( anchor_tx, coinbase_tx, commitment_tx) ;
1914
1905
( commitment_tx, anchor_tx)
1915
1906
} ,
1916
1907
_ => panic ! ( "Unexpected event" ) ,
@@ -1934,28 +1925,12 @@ fn test_yield_anchors_events() {
1934
1925
let mut htlc_txs = Vec :: with_capacity ( 2 ) ;
1935
1926
for event in holder_events {
1936
1927
match event {
1937
- Event :: BumpTransaction ( BumpTransactionEvent :: HTLCResolution { htlc_descriptors, tx_lock_time, .. } ) => {
1938
- assert_eq ! ( htlc_descriptors. len( ) , 1 ) ;
1939
- let htlc_descriptor = & htlc_descriptors[ 0 ] ;
1940
- let mut htlc_tx = Transaction {
1941
- version : 2 ,
1942
- lock_time : tx_lock_time,
1943
- input : vec ! [
1944
- htlc_descriptor. unsigned_tx_input( ) , // HTLC input
1945
- TxIn { ..Default :: default ( ) } // Fee input
1946
- ] ,
1947
- output : vec ! [
1948
- htlc_descriptor. tx_output( & secp) , // HTLC output
1949
- TxOut { // Fee input change
1950
- value: Amount :: ONE_BTC . to_sat( ) ,
1951
- script_pubkey: Script :: new_op_return( & [ ] ) ,
1952
- }
1953
- ]
1954
- } ;
1955
- let signer = htlc_descriptor. derive_channel_signer ( & nodes[ 0 ] . keys_manager ) ;
1956
- let our_sig = signer. sign_holder_htlc_transaction ( & mut htlc_tx, 0 , htlc_descriptor, & secp) . unwrap ( ) ;
1957
- let witness_script = htlc_descriptor. witness_script ( & secp) ;
1958
- htlc_tx. input [ 0 ] . witness = htlc_descriptor. tx_input_witness ( & our_sig, & witness_script) ;
1928
+ Event :: BumpTransaction ( event) => {
1929
+ nodes[ 0 ] . bump_tx_handler . handle_event ( & event) ;
1930
+ let mut txn = nodes[ 0 ] . tx_broadcaster . unique_txn_broadcast ( ) ;
1931
+ assert_eq ! ( txn. len( ) , 1 ) ;
1932
+ let htlc_tx = txn. pop ( ) . unwrap ( ) ;
1933
+ check_spends ! ( htlc_tx, commitment_tx, anchor_tx) ;
1959
1934
htlc_txs. push ( htlc_tx) ;
1960
1935
} ,
1961
1936
_ => panic ! ( "Unexpected event" ) ,
@@ -2060,7 +2035,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
2060
2035
check_added_monitors ( & nodes[ 1 ] , 2 ) ;
2061
2036
check_closed_event ! ( & nodes[ 1 ] , 2 , ClosureReason :: OutdatedChannelManager ) ;
2062
2037
let ( revoked_commitment_a, revoked_commitment_b) = {
2063
- let txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
2038
+ let txn = nodes[ 1 ] . tx_broadcaster . unique_txn_broadcast ( ) ;
2064
2039
assert_eq ! ( txn. len( ) , 2 ) ;
2065
2040
assert_eq ! ( txn[ 0 ] . output. len( ) , 6 ) ; // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
2066
2041
assert_eq ! ( txn[ 1 ] . output. len( ) , 6 ) ; // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
@@ -2079,71 +2054,32 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
2079
2054
assert ! ( nodes[ 0 ] . chain_monitor. chain_monitor. get_and_clear_pending_events( ) . is_empty( ) ) ;
2080
2055
let events = nodes[ 1 ] . chain_monitor . chain_monitor . get_and_clear_pending_events ( ) ;
2081
2056
assert_eq ! ( events. len( ) , 2 ) ;
2082
- let anchor_tx = {
2083
- let secret_key = SecretKey :: from_slice ( & [ 1 ; 32 ] ) . unwrap ( ) ;
2084
- let public_key = PublicKey :: new ( secret_key. public_key ( & secp) ) ;
2085
- let fee_utxo_script = Script :: new_v0_p2wpkh ( & public_key. wpubkey_hash ( ) . unwrap ( ) ) ;
2057
+ let mut anchor_txs = Vec :: with_capacity ( events. len ( ) ) ;
2058
+ for ( idx, event) in events. into_iter ( ) . enumerate ( ) {
2059
+ let utxo_value = Amount :: ONE_BTC . to_sat ( ) * ( idx + 1 ) as u64 ;
2086
2060
let coinbase_tx = Transaction {
2087
2061
version : 2 ,
2088
2062
lock_time : PackedLockTime :: ZERO ,
2089
2063
input : vec ! [ TxIn { ..Default :: default ( ) } ] ,
2090
2064
output : vec ! [ TxOut { // UTXO to attach fees to `anchor_tx`
2091
- value: Amount :: ONE_BTC . to_sat ( ) ,
2092
- script_pubkey: fee_utxo_script . clone ( ) ,
2065
+ value: utxo_value ,
2066
+ script_pubkey: nodes [ 1 ] . wallet_source . get_change_script ( ) . unwrap ( ) ,
2093
2067
} ] ,
2094
2068
} ;
2095
- let mut anchor_tx = Transaction {
2096
- version : 2 ,
2097
- lock_time : PackedLockTime :: ZERO ,
2098
- input : vec ! [
2099
- TxIn { // Fee input
2100
- previous_output: bitcoin:: OutPoint { txid: coinbase_tx. txid( ) , vout: 0 } ,
2101
- ..Default :: default ( )
2102
- } ,
2103
- ] ,
2104
- output : vec ! [ TxOut { // Fee input change
2105
- value: coinbase_tx. output[ 0 ] . value / 2 ,
2106
- script_pubkey: Script :: new_op_return( & [ ] ) ,
2107
- } ] ,
2108
- } ;
2109
- let mut signers = Vec :: with_capacity ( 2 ) ;
2110
- for event in events {
2111
- match event {
2112
- Event :: BumpTransaction ( BumpTransactionEvent :: ChannelClose { anchor_descriptor, .. } ) => {
2113
- anchor_tx. input . push ( TxIn {
2114
- previous_output : anchor_descriptor. outpoint ,
2115
- ..Default :: default ( )
2116
- } ) ;
2117
- let signer = anchor_descriptor. derive_channel_signer ( & nodes[ 1 ] . keys_manager ) ;
2118
- signers. push ( signer) ;
2119
- } ,
2120
- _ => panic ! ( "Unexpected event" ) ,
2121
- }
2122
- }
2123
- for ( i, signer) in signers. into_iter ( ) . enumerate ( ) {
2124
- let anchor_idx = i + 1 ;
2125
- let funding_sig = signer. sign_holder_anchor_input ( & mut anchor_tx, anchor_idx, & secp) . unwrap ( ) ;
2126
- anchor_tx. input [ anchor_idx] . witness = chan_utils:: build_anchor_input_witness (
2127
- & signer. pubkeys ( ) . funding_pubkey , & funding_sig
2128
- ) ;
2129
- }
2130
- let fee_utxo_sig = {
2131
- let witness_script = Script :: new_p2pkh ( & public_key. pubkey_hash ( ) ) ;
2132
- let sighash = hash_to_message ! ( & SighashCache :: new( & anchor_tx) . segwit_signature_hash(
2133
- 0 , & witness_script, coinbase_tx. output[ 0 ] . value, EcdsaSighashType :: All
2134
- ) . unwrap( ) [ ..] ) ;
2135
- let sig = sign ( & secp, & sighash, & secret_key) ;
2136
- let mut sig = sig. serialize_der ( ) . to_vec ( ) ;
2137
- sig. push ( EcdsaSighashType :: All as u8 ) ;
2138
- sig
2069
+ nodes[ 1 ] . wallet_source . add_utxo ( bitcoin:: OutPoint { txid : coinbase_tx. txid ( ) , vout : 0 } , utxo_value) ;
2070
+ match event {
2071
+ Event :: BumpTransaction ( event) => nodes[ 1 ] . bump_tx_handler . handle_event ( & event) ,
2072
+ _ => panic ! ( "Unexpected event" ) ,
2139
2073
} ;
2140
- anchor_tx. input [ 0 ] . witness = Witness :: from_vec ( vec ! [ fee_utxo_sig, public_key. to_bytes( ) ] ) ;
2141
- check_spends ! ( anchor_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b) ;
2142
- anchor_tx
2074
+ let txn = nodes[ 1 ] . tx_broadcaster . txn_broadcast ( ) ;
2075
+ assert_eq ! ( txn. len( ) , 2 ) ;
2076
+ let ( commitment_tx, anchor_tx) = ( & txn[ 0 ] , & txn[ 1 ] ) ;
2077
+ check_spends ! ( anchor_tx, coinbase_tx, commitment_tx) ;
2078
+ anchor_txs. push ( anchor_tx. clone ( ) ) ;
2143
2079
} ;
2144
2080
2145
2081
for node in & nodes {
2146
- mine_transactions ( node, & [ & revoked_commitment_a, & revoked_commitment_b, & anchor_tx ] ) ;
2082
+ mine_transactions ( node, & [ & revoked_commitment_a, & anchor_txs [ 0 ] , & revoked_commitment_b, & anchor_txs [ 1 ] ] ) ;
2147
2083
}
2148
2084
check_added_monitors ! ( & nodes[ 0 ] , 2 ) ;
2149
2085
check_closed_broadcast ( & nodes[ 0 ] , 2 , true ) ;
@@ -2213,6 +2149,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
2213
2149
} ;
2214
2150
let mut descriptors = Vec :: with_capacity ( 4 ) ;
2215
2151
for event in events {
2152
+ // We don't use the `BumpTransactionEventHandler` here because it does not support
2153
+ // creating one transaction from multiple `HTLCResolution` events.
2216
2154
if let Event :: BumpTransaction ( BumpTransactionEvent :: HTLCResolution { mut htlc_descriptors, tx_lock_time, .. } ) = event {
2217
2155
assert_eq ! ( htlc_descriptors. len( ) , 2 ) ;
2218
2156
for htlc_descriptor in & htlc_descriptors {
0 commit comments