Skip to content

Commit c915c99

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.
1 parent 42085b9 commit c915c99

File tree

2 files changed

+35
-134
lines changed

2 files changed

+35
-134
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,28 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(
393393
}
394394
}
395395

396+
pub fn provide_anchor_reserves<'a, 'b, 'c>(nodes: &[Node<'a, 'b, 'c>]) -> Transaction {
397+
let mut output = Vec::with_capacity(nodes.len());
398+
for node in nodes {
399+
output.push(TxOut {
400+
value: Amount::ONE_BTC,
401+
script_pubkey: node.wallet_source.get_change_script().unwrap(),
402+
});
403+
}
404+
let tx = Transaction {
405+
version: TxVersion::TWO,
406+
lock_time: LockTime::ZERO,
407+
input: vec![TxIn { ..Default::default() }],
408+
output,
409+
};
410+
let height = nodes[0].best_block_info().1 + 1;
411+
let block = create_dummy_block(nodes[0].best_block_hash(), height, vec![tx.clone()]);
412+
for node in nodes {
413+
do_connect_block_with_consistency_checks(node, block.clone(), false);
414+
}
415+
tx
416+
}
417+
396418
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
397419
call_claimable_balances(node);
398420
eprintln!(

lightning/src/ln/monitor_tests.rs

Lines changed: 13 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
//! Further functional tests which test blockchain reorganizations.
1313
14-
use crate::events::bump_transaction::sync::WalletSourceSync;
1514
use crate::sign::{ecdsa::EcdsaChannelSigner, OutputSpender, SignerProvider, SpendableOutputDescriptor};
1615
use crate::chain::channelmonitor::{ANTI_REORG_DELAY, ARCHIVAL_DELAY_BLOCKS,LATENCY_GRACE_PERIOD_BLOCKS, COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE, Balance, BalanceSource, ChannelMonitorUpdateStep};
1716
use crate::chain::transaction::OutPoint;
@@ -465,25 +464,7 @@ fn do_test_claim_value_force_close(anchors: bool, prev_commitment_tx: bool) {
465464
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config.clone()), Some(user_config)]);
466465
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
467466

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

488469
let (_, _, chan_id, funding_tx) =
489470
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 1_000_000);
@@ -872,25 +853,7 @@ fn do_test_balances_on_local_commitment_htlcs(anchors: bool) {
872853
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config.clone()), Some(user_config)]);
873854
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
874855

875-
let coinbase_tx = Transaction {
876-
version: Version::TWO,
877-
lock_time: LockTime::ZERO,
878-
input: vec![TxIn { ..Default::default() }],
879-
output: vec![
880-
TxOut {
881-
value: Amount::ONE_BTC,
882-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
883-
},
884-
TxOut {
885-
value: Amount::ONE_BTC,
886-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
887-
},
888-
],
889-
};
890-
if anchors {
891-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
892-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 1 }, coinbase_tx.output[1].value);
893-
}
856+
let coinbase_tx = provide_anchor_reserves(&nodes);
894857

895858
// Create a single channel with two pending HTLCs from nodes[0] to nodes[1], one which nodes[1]
896859
// knows the preimage for, one which it does not.
@@ -1655,25 +1618,7 @@ fn do_test_revoked_counterparty_htlc_tx_balances(anchors: bool) {
16551618
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config.clone()), Some(user_config)]);
16561619
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
16571620

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

16781623
// Create some initial channels
16791624
let (_, _, chan_id, funding_tx) =
@@ -1956,16 +1901,7 @@ fn do_test_revoked_counterparty_aggregated_claims(anchors: bool) {
19561901
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config.clone()), Some(user_config)]);
19571902
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
19581903

1959-
let coinbase_tx = Transaction {
1960-
version: Version::TWO,
1961-
lock_time: LockTime::ZERO,
1962-
input: vec![TxIn { ..Default::default() }],
1963-
output: vec![TxOut {
1964-
value: Amount::ONE_BTC,
1965-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
1966-
}],
1967-
};
1968-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
1904+
let coinbase_tx = provide_anchor_reserves(&nodes);
19691905

19701906
let (_, _, chan_id, funding_tx) =
19711907
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 100_000_000);
@@ -2246,25 +2182,7 @@ fn do_test_claimable_balance_correct_while_payment_pending(outbound_payment: boo
22462182
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[Some(user_config.clone()), Some(user_config.clone()), Some(user_config)]);
22472183
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
22482184

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

22692187
// Create a channel from A -> B
22702188
let (_, _, chan_ab_id, funding_tx_ab) =
@@ -2411,6 +2329,8 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24112329
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config.clone()), Some(config)]);
24122330
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
24132331

2332+
let coinbase_tx = provide_anchor_reserves(&nodes);
2333+
24142334
let (_, _, _, chan_id, funding_tx) = create_chan_between_nodes_with_value(
24152335
&nodes[0], &nodes[1], 1_000_000, 500_000_000
24162336
);
@@ -2429,17 +2349,6 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
24292349
false, [nodes[1].node.get_our_node_id()], 1000000);
24302350
check_added_monitors(&nodes[0], 1);
24312351

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

2455+
let coinbase_tx = provide_anchor_reserves(&nodes);
2456+
25462457
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes_with_value(
25472458
&nodes, 0, 1, 1_000_000, 500_000_000
25482459
);
@@ -2618,16 +2529,6 @@ fn do_test_yield_anchors_events(have_htlcs: bool) {
26182529
assert_eq!(holder_events.len(), 1);
26192530
let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() {
26202531
Event::BumpTransaction(event) => {
2621-
let coinbase_tx = Transaction {
2622-
version: Version::TWO,
2623-
lock_time: LockTime::ZERO,
2624-
input: vec![TxIn { ..Default::default() }],
2625-
output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2626-
value: Amount::ONE_BTC,
2627-
script_pubkey: nodes[0].wallet_source.get_change_script().unwrap(),
2628-
}],
2629-
};
2630-
nodes[0].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, coinbase_tx.output[0].value);
26312532
nodes[0].bump_tx_handler.handle_event(&event);
26322533
let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
26332534
assert_eq!(txn.len(), 2);
@@ -2743,6 +2644,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
27432644

27442645
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
27452646

2647+
let coinbase_tx = provide_anchor_reserves(&nodes);
2648+
27462649
let chan_a = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
27472650
let chan_b = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 20_000_000);
27482651

@@ -2801,18 +2704,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
28012704
assert_eq!(events.len(), 2);
28022705
let mut revoked_commitment_txs = Vec::with_capacity(events.len());
28032706
let mut anchor_txs = Vec::with_capacity(events.len());
2804-
for (idx, event) in events.into_iter().enumerate() {
2805-
let utxo_value = Amount::ONE_BTC * (idx + 1) as u64;
2806-
let coinbase_tx = Transaction {
2807-
version: Version::TWO,
2808-
lock_time: LockTime::ZERO,
2809-
input: vec![TxIn { ..Default::default() }],
2810-
output: vec![TxOut { // UTXO to attach fees to `anchor_tx`
2811-
value: utxo_value,
2812-
script_pubkey: nodes[1].wallet_source.get_change_script().unwrap(),
2813-
}],
2814-
};
2815-
nodes[1].wallet_source.add_utxo(bitcoin::OutPoint { txid: coinbase_tx.compute_txid(), vout: 0 }, utxo_value);
2707+
for event in events {
28162708
match event {
28172709
Event::BumpTransaction(event) => nodes[1].bump_tx_handler.handle_event(&event),
28182710
_ => panic!("Unexpected event"),
@@ -3130,20 +3022,7 @@ fn do_test_monitor_claims_with_random_signatures(anchors: bool, confirm_counterp
31303022
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_config.clone()), Some(user_config)]);
31313023
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
31323024

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

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

0 commit comments

Comments
 (0)