Skip to content

Commit 6e932b6

Browse files
committed
Integrate BumpTransactionEventHandler into existing anchor tests
1 parent c01a635 commit 6e932b6

File tree

1 file changed

+70
-132
lines changed

1 file changed

+70
-132
lines changed

lightning/src/ln/monitor_tests.rs

+70-132
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99

1010
//! Further functional tests which test blockchain reorganizations.
1111
12-
use crate::sign::{ChannelSigner, EcdsaChannelSigner};
12+
use crate::sign::EcdsaChannelSigner;
1313
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, LATENCY_GRACE_PERIOD_BLOCKS, Balance};
1414
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};
1717
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
1818
use crate::ln::channel;
19-
use crate::ln::chan_utils;
2019
use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, PaymentId, RecipientOnionFields};
2120
use crate::ln::msgs::ChannelMessageHandler;
2221
use crate::util::config::UserConfig;
@@ -1743,49 +1742,43 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
17431742
check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false);
17441743
check_added_monitors(&nodes[0], 1);
17451744

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+
17461756
// Set up a helper closure we'll use throughout our test. We should only expect retries without
17471757
// bumps if fees have not increased after a block has been connected (assuming the height timer
17481758
// re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
17491759
let mut prev_htlc_tx_feerate = None;
17501760
let mut check_htlc_retry = |should_retry: bool, should_bump: bool| -> Option<Transaction> {
17511761
let (htlc_tx, htlc_tx_feerate) = if anchors {
17521762
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();
17541764
assert_eq!(events.len(), if should_retry { 1 } else { 0 });
17551765
if !should_retry {
17561766
return None;
17571767
}
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+
}
17891782
} else {
17901783
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
17911784
let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
@@ -1796,8 +1789,8 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
17961789
let htlc_tx = txn.pop().unwrap();
17971790
check_spends!(htlc_tx, commitment_txn[0]);
17981791
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))
18011794
};
18021795
if should_bump {
18031796
assert!(htlc_tx_feerate > prev_htlc_tx_feerate.take().unwrap());
@@ -1837,9 +1830,11 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
18371830

18381831
// Mine the HTLC transaction to ensure we don't retry claims while they're confirmed.
18391832
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,
18411834
// we'll receive an extra bumped claim.
18421835
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 });
18431838
check_htlc_retry(true, anchors);
18441839
}
18451840
nodes[0].chain_monitor.chain_monitor.rebroadcast_pending_claims();
@@ -1860,7 +1855,6 @@ fn test_yield_anchors_events() {
18601855
// allowing the consumer to provide additional fees to the commitment transaction to be
18611856
// broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
18621857
// emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1863-
let secp = Secp256k1::new();
18641858
let mut chanmon_cfgs = create_chanmon_cfgs(2);
18651859
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
18661860
let mut anchors_config = UserConfig::default();
@@ -1891,26 +1885,23 @@ fn test_yield_anchors_events() {
18911885
let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
18921886
assert_eq!(holder_events.len(), 1);
18931887
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 {
18981890
version: 2,
18991891
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`
19051894
value: Amount::ONE_BTC.to_sat(),
1906-
script_pubkey: Script::new_op_return(&[]),
1895+
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
19071896
}],
19081897
};
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);
19141905
(commitment_tx, anchor_tx)
19151906
},
19161907
_ => panic!("Unexpected event"),
@@ -1934,28 +1925,12 @@ fn test_yield_anchors_events() {
19341925
let mut htlc_txs = Vec::with_capacity(2);
19351926
for event in holder_events {
19361927
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);
19591934
htlc_txs.push(htlc_tx);
19601935
},
19611936
_ => panic!("Unexpected event"),
@@ -2060,7 +2035,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
20602035
check_added_monitors(&nodes[1], 2);
20612036
check_closed_event!(&nodes[1], 2, ClosureReason::OutdatedChannelManager);
20622037
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();
20642039
assert_eq!(txn.len(), 2);
20652040
assert_eq!(txn[0].output.len(), 6); // 2 HTLC outputs + 1 to_self output + 1 to_remote output + 2 anchor outputs
20662041
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() {
20792054
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
20802055
let events = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events();
20812056
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;
20862060
let coinbase_tx = Transaction {
20872061
version: 2,
20882062
lock_time: PackedLockTime::ZERO,
20892063
input: vec![TxIn { ..Default::default() }],
20902064
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(),
20932067
}],
20942068
};
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"),
21392073
};
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());
21432079
};
21442080

21452081
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]]);
21472083
}
21482084
check_added_monitors!(&nodes[0], 2);
21492085
check_closed_broadcast(&nodes[0], 2, true);
@@ -2213,6 +2149,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
22132149
};
22142150
let mut descriptors = Vec::with_capacity(4);
22152151
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.
22162154
if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut htlc_descriptors, tx_lock_time, .. }) = event {
22172155
assert_eq!(htlc_descriptors.len(), 2);
22182156
for htlc_descriptor in &htlc_descriptors {

0 commit comments

Comments
 (0)