Skip to content

Commit 7e7abb5

Browse files
committed
Add test yielding anchor-related events
1 parent e519299 commit 7e7abb5

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

lightning/src/ln/monitor_tests.rs

+150
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99

1010
//! Further functional tests which test blockchain reorganizations.
1111
12+
#[cfg(anchors)]
13+
use crate::chain::keysinterface::BaseSign;
14+
#[cfg(anchors)]
15+
use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
1216
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
1317
use crate::chain::transaction::OutPoint;
1418
use crate::chain::chaininterface::LowerBoundedFeeEstimator;
1519
use crate::ln::channel;
20+
#[cfg(anchors)]
21+
use crate::ln::chan_utils;
1622
use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, PaymentId};
1723
use crate::ln::msgs::ChannelMessageHandler;
24+
#[cfg(anchors)]
25+
use crate::util::config::UserConfig;
26+
#[cfg(anchors)]
27+
use crate::util::events::BumpTransactionEvent;
1828
use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
1929

2030
use bitcoin::blockdata::script::Builder;
2131
use bitcoin::blockdata::opcodes;
2232
use bitcoin::secp256k1::Secp256k1;
33+
#[cfg(anchors)]
34+
use bitcoin::{Amount, Script, TxIn, TxOut, PackedLockTime};
2335
use bitcoin::Transaction;
2436

2537
use crate::prelude::*;
@@ -1666,3 +1678,141 @@ fn test_revoked_counterparty_aggregated_claims() {
16661678
assert!(nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
16671679
assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
16681680
}
1681+
1682+
#[cfg(anchors)]
1683+
#[test]
1684+
fn test_yield_anchors_events() {
1685+
// Tests that two parties supporting anchor outputs can open a channel, route payments over
1686+
// it, and finalize its resolution uncooperatively. Once the HTLCs are locked in, one side will
1687+
// force close once the HTLCs expire. The force close should stem from an event emitted by LDK,
1688+
// allowing the consumer to provide additional fees to the commitment transaction to be
1689+
// broadcast. Once the commitment transaction confirms, events for the HTLC resolution should be
1690+
// emitted by LDK, such that the consumer can attach fees to the zero fee HTLC transactions.
1691+
let secp = Secp256k1::new();
1692+
let mut chanmon_cfgs = create_chanmon_cfgs(2);
1693+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1694+
let mut anchors_config = UserConfig::default();
1695+
anchors_config.channel_handshake_config.announced_channel = true;
1696+
anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
1697+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
1698+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1699+
1700+
let chan_id = create_announced_chan_between_nodes_with_value(
1701+
&nodes, 0, 1, 1_000_000, 500_000_000
1702+
).2;
1703+
route_payment(&nodes[0], &[&nodes[1]], 1_000_000);
1704+
let (payment_preimage, payment_hash, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1705+
1706+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
1707+
1708+
connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1);
1709+
check_closed_broadcast!(&nodes[0], true);
1710+
assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
1711+
1712+
get_monitor!(nodes[0], chan_id).provide_payment_preimage(
1713+
&payment_hash, &payment_preimage, &node_cfgs[0].tx_broadcaster,
1714+
&LowerBoundedFeeEstimator::new(node_cfgs[0].fee_estimator), &nodes[0].logger
1715+
);
1716+
1717+
let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1718+
assert_eq!(holder_events.len(), 1);
1719+
let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
1720+
Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => {
1721+
assert_eq!(commitment_tx.input.len(), 1);
1722+
assert_eq!(commitment_tx.output.len(), 6);
1723+
let mut anchor_tx = Transaction {
1724+
version: 2,
1725+
lock_time: PackedLockTime::ZERO,
1726+
input: vec![
1727+
TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() },
1728+
TxIn { ..Default::default() },
1729+
],
1730+
output: vec![TxOut {
1731+
value: Amount::ONE_BTC.to_sat(),
1732+
script_pubkey: Script::new_op_return(&[]),
1733+
}],
1734+
};
1735+
let signer = nodes[0].keys_manager.derive_channel_keys(
1736+
anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id,
1737+
);
1738+
let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap();
1739+
anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness(
1740+
&signer.pubkeys().funding_pubkey, &funding_sig
1741+
);
1742+
(commitment_tx, anchor_tx)
1743+
},
1744+
_ => panic!("Unexpected event"),
1745+
};
1746+
1747+
mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]);
1748+
check_added_monitors!(nodes[0], 1);
1749+
1750+
let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1751+
// Certain block ConnectStyle's cause an extra ChannelClose event to be emitted since the best
1752+
// block is being updated prior to the confirmed transactions.
1753+
match *nodes[0].connect_style.borrow() {
1754+
ConnectStyle::BestBlockFirst|ConnectStyle::BestBlockFirstReorgsOnlyTip|ConnectStyle::BestBlockFirstSkippingBlocks => {
1755+
assert_eq!(holder_events.len(), 3);
1756+
if let Event::BumpTransaction(BumpTransactionEvent::ChannelClose { .. }) = holder_events.remove(0) {}
1757+
else { panic!("unexpected event"); }
1758+
1759+
},
1760+
_ => assert_eq!(holder_events.len(), 2),
1761+
};
1762+
let mut htlc_txs = Vec::with_capacity(2);
1763+
for event in holder_events {
1764+
match event {
1765+
Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, .. }) => {
1766+
assert_eq!(htlc_descriptors.len(), 1);
1767+
let htlc_descriptor = &htlc_descriptors[0];
1768+
let signer = nodes[0].keys_manager.derive_channel_keys(
1769+
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1770+
);
1771+
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
1772+
let mut htlc_tx = Transaction {
1773+
version: 2,
1774+
lock_time: if htlc_descriptor.htlc.offered {
1775+
PackedLockTime(htlc_descriptor.htlc.cltv_expiry)
1776+
} else {
1777+
PackedLockTime::ZERO
1778+
},
1779+
input: vec![
1780+
htlc_descriptor.unsigned_tx_input(), // HTLC input
1781+
TxIn { ..Default::default() } // Fee input
1782+
],
1783+
output: vec![
1784+
htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1785+
TxOut { // Fee input change
1786+
value: Amount::ONE_BTC.to_sat(),
1787+
script_pubkey: Script::new_op_return(&[]),
1788+
}
1789+
]
1790+
};
1791+
let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1792+
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1793+
htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
1794+
htlc_txs.push(htlc_tx);
1795+
},
1796+
_ => panic!("Unexpected event"),
1797+
}
1798+
}
1799+
1800+
mine_transactions(&nodes[0], &[&htlc_txs[0], &htlc_txs[1]]);
1801+
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
1802+
1803+
assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty());
1804+
1805+
connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32);
1806+
1807+
let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events();
1808+
assert_eq!(holder_events.len(), 3);
1809+
for event in holder_events {
1810+
match event {
1811+
Event::SpendableOutputs { .. } => {},
1812+
_ => panic!("Unexpected event"),
1813+
}
1814+
}
1815+
1816+
// Clear the remaining events as they're not relevant to what we're testing.
1817+
nodes[0].node.get_and_clear_pending_events();
1818+
}

0 commit comments

Comments
 (0)