Skip to content

Commit c9efeff

Browse files
committed
Add a test utility to provide nodes with anchor reserves
In a number of tests we require available UTXOs to do HTLC anchor claims by bringing our own fees. We previously wrote that out in each test, which is somewhat verbose, so here we simply add a test utility that gives each node a full BTC in a single UTXO. Trivial conflicts resolved in: * lightning/src/ln/monitor_tests.rs
1 parent 64f678d commit c9efeff

File tree

2 files changed

+36
-134
lines changed

2 files changed

+36
-134
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,28 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b
335335
}
336336
}
337337

338+
pub fn provide_anchor_reserves<'a, 'b, 'c>(nodes: &[Node<'a, 'b, 'c>]) -> Transaction {
339+
let mut output = Vec::with_capacity(nodes.len());
340+
for node in nodes {
341+
output.push(TxOut {
342+
value: Amount::ONE_BTC,
343+
script_pubkey: node.wallet_source.get_change_script().unwrap(),
344+
});
345+
}
346+
let tx = Transaction {
347+
version: TxVersion::TWO,
348+
lock_time: LockTime::ZERO,
349+
input: vec![TxIn { ..Default::default() }],
350+
output,
351+
};
352+
let height = nodes[0].best_block_info().1 + 1;
353+
let block = create_dummy_block(nodes[0].best_block_hash(), height, vec![tx.clone()]);
354+
for node in nodes {
355+
do_connect_block_with_consistency_checks(node, block.clone(), false);
356+
}
357+
tx
358+
}
359+
338360
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
339361
call_claimable_balances(node);
340362
eprintln!("Disconnecting {} blocks using Block Connection Style: {:?}", count, *node.connect_style.borrow());

lightning/src/ln/monitor_tests.rs

Lines changed: 14 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::sign::{ecdsa::EcdsaChannelSigner, OutputSpender, SpendableOutputDescr
1313
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ARCHIVAL_DELAY_BLOCKS,LATENCY_GRACE_PERIOD_BLOCKS, COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE, Balance, BalanceSource, ChannelMonitorUpdateStep};
1414
use crate::chain::transaction::OutPoint;
1515
use crate::chain::chaininterface::{ConfirmationTarget, LowerBoundedFeeEstimator, compute_feerate_sat_per_1000_weight};
16-
use crate::events::bump_transaction::{BumpTransactionEvent, WalletSource};
16+
use crate::events::bump_transaction::BumpTransactionEvent;
1717
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
1818
use crate::ln::channel;
1919
use crate::ln::types::ChannelId;
@@ -462,25 +462,7 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
462462
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
463463
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
464464

465-
let coinbase_tx = Transaction {
466-
version: Version::TWO,
467-
lock_time: LockTime::ZERO,
468-
input: vec![TxIn { ..Default::default() }],
469-
output: vec![
470-
TxOut {
471-
value: Amount::ONE_BTC,
472-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
473-
},
474-
TxOut {
475-
value: Amount::ONE_BTC,
476-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
477-
},
478-
],
479-
};
480-
if anchors {
481-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
482-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 1 }, coinbase_tx.output[1].value);
483-
}
465+
let coinbase_tx = provide_anchor_reserves(&nodes);
484466

485467
let (_, _, chan_id, funding_tx) =
486468
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
@@ -865,25 +847,7 @@ fn do_test_balances_on_local_commitment_htlcs(anchors: bool) {
865847
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
866848
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
867849

868-
let coinbase_tx = Transaction {
869-
version: Version::TWO,
870-
lock_time: LockTime::ZERO,
871-
input: vec![TxIn { ..Default::default() }],
872-
output: vec![
873-
TxOut {
874-
value: Amount::ONE_BTC,
875-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
876-
},
877-
TxOut {
878-
value: Amount::ONE_BTC,
879-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
880-
},
881-
],
882-
};
883-
if anchors {
884-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
885-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 1 }, coinbase_tx.output[1].value);
886-
}
850+
let coinbase_tx = provide_anchor_reserves(&nodes);
887851

888852
// Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
889853
// knows the preimage for, one which it does not.
@@ -1650,25 +1614,7 @@ fn do_test_revoked_counterparty_htlc_tx_balances(anchors: bool) {
16501614
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
16511615
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
16521616

1653-
let coinbase_tx = Transaction {
1654-
version: Version::TWO,
1655-
lock_time: LockTime::ZERO,
1656-
input: vec![TxIn { ..Default::default() }],
1657-
output: vec![
1658-
TxOut {
1659-
value: Amount::ONE_BTC,
1660-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
1661-
},
1662-
TxOut {
1663-
value: Amount::ONE_BTC,
1664-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
1665-
},
1666-
],
1667-
};
1668-
if anchors {
1669-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
1670-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 1 }, coinbase_tx.output[1].value);
1671-
}
1617+
let coinbase_tx = provide_anchor_reserves(&nodes);
16721618

16731619
// Create some initial channels
16741620
let (_, _, chan_id, funding_tx) =
@@ -1951,16 +1897,7 @@ fn do_test_revoked_counterparty_aggregated_claims(anchors: bool) {
19511897
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
19521898
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19531899

1954-
let coinbase_tx = Transaction {
1955-
version: Version::TWO,
1956-
lock_time: LockTime::ZERO,
1957-
input: vec![TxIn { ..Default::default() }],
1958-
output: vec![TxOut {
1959-
value: Amount::ONE_BTC,
1960-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
1961-
}],
1962-
};
1963-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
1900+
let coinbase_tx = provide_anchor_reserves(&nodes);
19641901

19651902
let (_, _, chan_id, funding_tx) =
19661903
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
@@ -2241,25 +2178,7 @@ fn do_test_claimable_balance_correct_while_payment_pending(outbound_payment: boo
22412178
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(user_config), Some(user_config), Some(user_config)]);
22422179
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
22432180

2244-
let coinbase_tx = Transaction {
2245-
version: Version::TWO,
2246-
lock_time: LockTime::ZERO,
2247-
input: vec![TxIn { ..Default::default() }],
2248-
output: vec![
2249-
TxOut {
2250-
value: Amount::ONE_BTC,
2251-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
2252-
},
2253-
TxOut {
2254-
value: Amount::ONE_BTC,
2255-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
2256-
},
2257-
],
2258-
};
2259-
if anchors {
2260-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
2261-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 1 }, coinbase_tx.output[1].value);
2262-
}
2181+
provide_anchor_reserves(&nodes);
22632182

22642183
// Create a channel from A -> B
22652184
let (_, _, chan_ab_id, funding_tx_ab) =
@@ -2406,6 +2325,8 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24062325
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), Some(config)]);
24072326
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
24082327

2328+
let coinbase_tx = provide_anchor_reserves(&nodes);
2329+
24092330
let (_, _, _, chan_id, funding_tx) = create_chan_between_nodes_with_value(
24102331
&nodes[0], &nodes[1], 1_000_000, 500_000_000
24112332
);
@@ -2424,17 +2345,6 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24242345
false, [nodes[1].node.get_our_node_id()], 1000000);
24252346
check_added_monitors(&nodes[0], 1);
24262347

2427-
let coinbase_tx = Transaction {
2428-
version: Version::TWO,
2429-
lock_time: LockTime::ZERO,
2430-
input: vec![TxIn { ..Default::default() }],
2431-
output: vec![TxOut { // UTXO to attach fees to `htlc_tx` on anchors
2432-
value: Amount::ONE_BTC,
2433-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
2434-
}],
2435-
};
2436-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
2437-
24382348
// Set up a helper closure we'll use throughout our test. We should only expect retries without
24392349
// bumps if fees have not increased after a block has been connected (assuming the height timer
24402350
// re-evaluates at every block) or after `ChainMonitor::rebroadcast_pending_claims` is called.
@@ -2538,6 +2448,8 @@ fn do_test_yield_anchors_events(have_htlcs: bool) {
25382448
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]);
25392449
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
25402450

2451+
let coinbase_tx = provide_anchor_reserves(&nodes);
2452+
25412453
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(
25422454
&nodes, 0, 1, 1_000_000, 500_000_000
25432455
);
@@ -2613,16 +2525,6 @@ fn do_test_yield_anchors_events(have_htlcs: bool) {
26132525
assert_eq!(holder_events.len(), 1);
26142526
let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
26152527
Event::BumpTransaction(event) => {
2616-
let coinbase_tx = Transaction {
2617-
version: Version::TWO,
2618-
lock_time: LockTime::ZERO,
2619-
input: vec![TxIn { ..Default::default() }],
2620-
output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2621-
value: Amount::ONE_BTC,
2622-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
2623-
}],
2624-
};
2625-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
26262528
nodes[0].bump_tx_handler.handle_event(&event);
26272529
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
26282530
assert_eq!(txn.len(), 2);
@@ -2738,6 +2640,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
27382640

27392641
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
27402642

2643+
let coinbase_tx = provide_anchor_reserves(&nodes);
2644+
27412645
let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
27422646
let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
27432647

@@ -2796,18 +2700,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
27962700
assert_eq!(events.len(), 2);
27972701
let mut revoked_commitment_txs = Vec::with_capacity(events.len());
27982702
let mut anchor_txs = Vec::with_capacity(events.len());
2799-
for (idx, event) in events.into_iter().enumerate() {
2800-
let utxo_value = Amount::ONE_BTC * (idx + 1) as u64;
2801-
let coinbase_tx = Transaction {
2802-
version: Version::TWO,
2803-
lock_time: LockTime::ZERO,
2804-
input: vec![TxIn { ..Default::default() }],
2805-
output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2806-
value: utxo_value,
2807-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
2808-
}],
2809-
};
2810-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, utxo_value);
2703+
for event in events {
28112704
match event {
28122705
Event::BumpTransaction(event) => nodes[1].bump_tx_handler.handle_event(&event),
28132706
_ => panic!("Unexpected event"),
@@ -3125,20 +3018,7 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp
31253018
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config), Some(user_config)]);
31263019
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
31273020

3128-
let coinbase_tx = Transaction {
3129-
version: Version::TWO,
3130-
lock_time: LockTime::ZERO,
3131-
input: vec![TxIn { ..Default::default() }],
3132-
output: vec![
3133-
TxOut {
3134-
value: Amount::ONE_BTC,
3135-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
3136-
},
3137-
],
3138-
};
3139-
if anchors {
3140-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
3141-
}
3021+
let coinbase_tx = provide_anchor_reserves(&nodes);
31423022

31433023
// Open a channel and route a payment. We'll let it timeout to claim it.
31443024
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);

0 commit comments

Comments
 (0)