Skip to content

Commit 1e2ae31

Browse files
committed
XXX(breaks tests): Fix block connection ordering in a number of functional tests
Many functional tests rely on being able to call block_connected arbitrarily, jumping back in time to confirm a transaction at a specific height. Instead, this takes us one step towards having a well-formed blockchain in the functional tests.
1 parent 4673fd7 commit 1e2ae31

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

lightning-persister/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ mod tests {
241241
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
242242
assert_eq!(node_txn.len(), 1);
243243

244-
let header = BlockHeader { version: 0x20000000, prev_blockhash: *nodes[0].last_block_hash.lock().unwrap(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
244+
let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].last_block.lock().unwrap().0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
245245
connect_block(&nodes[1], &Block { header, txdata: vec![node_txn[0].clone(), node_txn[0].clone()]}, CHAN_CONFIRM_DEPTH);
246246
check_closed_broadcast!(nodes[1], false);
247247
check_added_monitors!(nodes[1], 1);

lightning/src/ln/functional_test_utils.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,50 @@ use std::sync::Mutex;
4444
use std::mem;
4545
use std::collections::HashMap;
4646

47-
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
47+
pub const CHAN_CONFIRM_DEPTH: u32 = 10;
4848

4949
pub fn confirm_transaction<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &Transaction) {
50-
let dummy_tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() };
51-
let dummy_tx_count = tx.version as usize;
5250
let mut block = Block {
53-
header: BlockHeader { version: 0x20000000, prev_blockhash: genesis_block(Network::Testnet).header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
54-
txdata: vec![dummy_tx; dummy_tx_count],
51+
header: BlockHeader { version: 0x20000000, prev_blockhash: node.last_block.lock().unwrap().0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
52+
txdata: Vec::new(),
5553
};
5654
block.txdata.push(tx.clone());
57-
connect_block(node, &block, 1);
55+
let height = node.last_block.lock().unwrap().1 + 1;
56+
connect_block(node, &block, height);
5857
for i in 2..CHAN_CONFIRM_DEPTH {
5958
block = Block {
6059
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
6160
txdata: vec![],
6261
};
62+
connect_block(node, &block, i + height);
63+
}
64+
}
65+
pub fn confirm_transaction_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &Transaction, conf_height: u32) {
66+
let mut block = Block {
67+
header: BlockHeader { version: 0x20000000, prev_blockhash: node.last_block.lock().unwrap().0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
68+
txdata: Vec::new(),
69+
};
70+
let height = node.last_block.lock().unwrap().1 + 1;
71+
assert!(height <= conf_height);
72+
for i in height..conf_height {
6373
connect_block(node, &block, i);
74+
block = Block {
75+
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
76+
txdata: vec![],
77+
};
78+
}
79+
80+
for _ in 0..*node.network_chan_count.borrow() { // Make sure we don't end up with channels at the same short id by offsetting by chan_count
81+
block.txdata.push(Transaction { version: 0, lock_time: 0, input: Vec::new(), output: Vec::new() });
82+
}
83+
block.txdata.push(tx.clone());
84+
connect_block(node, &block, conf_height);
85+
for i in 1..CHAN_CONFIRM_DEPTH {
86+
block = Block {
87+
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
88+
txdata: vec![],
89+
};
90+
connect_block(node, &block, i + conf_height);
6491
}
6592
}
6693

@@ -85,14 +112,14 @@ pub fn connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block,
85112
node.chain_monitor.chain_monitor.block_connected(&block.header, &txdata, height);
86113
node.node.block_connected(&block.header, &txdata, height);
87114
node.node.test_process_background_events();
88-
*node.last_block_hash.lock().unwrap() = block.header.block_hash();
115+
*node.last_block.lock().unwrap() = (block.header.block_hash(), height);
89116
}
90117

91118
pub fn disconnect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, header: &BlockHeader, height: u32) {
92119
node.chain_monitor.chain_monitor.block_disconnected(header, height);
93120
node.node.block_disconnected(header);
94121
node.node.test_process_background_events();
95-
*node.last_block_hash.lock().unwrap() = header.prev_blockhash;
122+
*node.last_block.lock().unwrap() = (header.prev_blockhash, height - 1);
96123
}
97124

98125
pub struct TestChanMonCfg {
@@ -125,7 +152,7 @@ pub struct Node<'a, 'b: 'a, 'c: 'b> {
125152
pub network_payment_count: Rc<RefCell<u8>>,
126153
pub network_chan_count: Rc<RefCell<u32>>,
127154
pub logger: &'c test_utils::TestLogger,
128-
pub last_block_hash: Mutex<BlockHash>,
155+
pub last_block: Mutex<(BlockHash, u32)>,
129156
}
130157

131158
impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
@@ -419,8 +446,8 @@ pub fn create_chan_between_nodes_with_value_init<'a, 'b, 'c>(node_a: &Node<'a, '
419446
tx
420447
}
421448

422-
pub fn create_chan_between_nodes_with_value_confirm_first<'a, 'b, 'c, 'd>(node_recv: &'a Node<'b, 'c, 'c>, node_conf: &'a Node<'b, 'c, 'd>, tx: &Transaction) {
423-
confirm_transaction(node_conf, tx);
449+
pub fn create_chan_between_nodes_with_value_confirm_first<'a, 'b, 'c, 'd>(node_recv: &'a Node<'b, 'c, 'c>, node_conf: &'a Node<'b, 'c, 'd>, tx: &Transaction, conf_height: u32) {
450+
confirm_transaction_at(node_conf, tx, conf_height);
424451
node_recv.node.handle_funding_locked(&node_conf.node.get_our_node_id(), &get_event_msg!(node_conf, MessageSendEvent::SendFundingLocked, node_recv.node.get_our_node_id()));
425452
}
426453

@@ -445,8 +472,9 @@ pub fn create_chan_between_nodes_with_value_confirm_second<'a, 'b, 'c>(node_recv
445472
}
446473

447474
pub fn create_chan_between_nodes_with_value_confirm<'a, 'b, 'c, 'd>(node_a: &'a Node<'b, 'c, 'd>, node_b: &'a Node<'b, 'c, 'd>, tx: &Transaction) -> ((msgs::FundingLocked, msgs::AnnouncementSignatures), [u8; 32]) {
448-
create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx);
449-
confirm_transaction(node_a, tx);
475+
let conf_height = std::cmp::max(node_a.last_block.lock().unwrap().1 + 1, node_b.last_block.lock().unwrap().1 + 1);
476+
create_chan_between_nodes_with_value_confirm_first(node_a, node_b, tx, conf_height);
477+
confirm_transaction_at(node_a, tx, conf_height);
450478
create_chan_between_nodes_with_value_confirm_second(node_b, node_a)
451479
}
452480

@@ -1023,7 +1051,7 @@ pub fn claim_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route:
10231051
claim_payment_along_route(origin_node, expected_route, false, our_payment_preimage, expected_amount);
10241052
}
10251053

1026-
pub const TEST_FINAL_CLTV: u32 = 32;
1054+
pub const TEST_FINAL_CLTV: u32 = 100;
10271055

10281056
pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash) {
10291057
let net_graph_msg_handler = &origin_node.net_graph_msg_handler;
@@ -1192,14 +1220,15 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
11921220
keys_manager: &cfgs[i].keys_manager, node: &chan_mgrs[i], net_graph_msg_handler,
11931221
node_seed: cfgs[i].node_seed, network_chan_count: chan_count.clone(),
11941222
network_payment_count: payment_count.clone(), logger: cfgs[i].logger,
1195-
last_block_hash: Mutex::new(genesis_block(Network::Testnet).header.block_hash()),
1223+
last_block: Mutex::new((genesis_block(Network::Testnet).header.block_hash(), 0)),
11961224
})
11971225
}
11981226

11991227
nodes
12001228
}
12011229

1202-
pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
1230+
// Note that the following only works for CLTV values up to 128
1231+
pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 137; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
12031232
pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
12041233

12051234
#[derive(PartialEq)]

lightning/src/ln/functional_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ fn do_test_sanity_on_in_flight_opens(steps: u8) {
497497
};
498498

499499
if steps & 0x0f == 6 { return; }
500-
create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx);
500+
create_chan_between_nodes_with_value_confirm_first(&nodes[0], &nodes[1], &tx, 2);
501501

502502
if steps & 0x0f == 7 { return; }
503503
confirm_transaction(&nodes[0], &tx);
@@ -2663,12 +2663,12 @@ fn claim_htlc_outputs_shared_tx() {
26632663
claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage_1, 3_000_000);
26642664

26652665
{
2666-
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2667-
connect_block(&nodes[0], &Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
2666+
let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].last_block.lock().unwrap().0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
2667+
connect_block(&nodes[0], &Block { header, txdata: vec![revoked_local_txn[0].clone()] }, CHAN_CONFIRM_DEPTH + 1);
26682668
check_added_monitors!(nodes[0], 1);
2669-
connect_block(&nodes[1], &Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
2669+
connect_block(&nodes[1], &Block { header, txdata: vec![revoked_local_txn[0].clone()] }, CHAN_CONFIRM_DEPTH + 1);
26702670
check_added_monitors!(nodes[1], 1);
2671-
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1, 1, true, header.block_hash());
2671+
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1, CHAN_CONFIRM_DEPTH + 1, true, header.block_hash());
26722672
expect_payment_failed!(nodes[1], payment_hash_2, true);
26732673

26742674
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();

lightning/src/ln/reorg_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ fn test_set_outpoints_partial_claiming() {
331331

332332
// Connect blocks on node A commitment transaction
333333
let header_101 = BlockHeader { version: 0x20000000, prev_blockhash: block_hash_100, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
334-
connect_block(&nodes[0], &Block { header: header_101, txdata: vec![remote_txn[0].clone()] }, 101);
334+
connect_block(&nodes[0], &Block { header: header_101, txdata: vec![remote_txn[0].clone()] }, CHAN_CONFIRM_DEPTH + 1);
335335
check_closed_broadcast!(nodes[0], false);
336336
check_added_monitors!(nodes[0], 1);
337337
// Verify node A broadcast tx claiming both HTLCs
@@ -364,7 +364,7 @@ fn test_set_outpoints_partial_claiming() {
364364

365365
// Broadcast partial claim on node A, should regenerate a claiming tx with HTLC dropped
366366
let header_102 = BlockHeader { version: 0x20000000, prev_blockhash: header_101.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
367-
connect_block(&nodes[0], &Block { header: header_102, txdata: vec![partial_claim_tx.clone()] }, 102);
367+
connect_block(&nodes[0], &Block { header: header_102, txdata: vec![partial_claim_tx.clone()] }, CHAN_CONFIRM_DEPTH + 2);
368368
{
369369
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
370370
assert_eq!(node_txn.len(), 1);
@@ -375,7 +375,7 @@ fn test_set_outpoints_partial_claiming() {
375375
nodes[0].node.get_and_clear_pending_msg_events();
376376

377377
// Disconnect last block on node A, should regenerate a claiming tx with HTLC dropped
378-
disconnect_block(&nodes[0], &header_102, 102);
378+
disconnect_block(&nodes[0], &header_102, CHAN_CONFIRM_DEPTH + 2);
379379
{
380380
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
381381
assert_eq!(node_txn.len(), 1);
@@ -385,7 +385,7 @@ fn test_set_outpoints_partial_claiming() {
385385
}
386386

387387
//// Disconnect one more block and then reconnect multiple no transaction should be generated
388-
disconnect_block(&nodes[0], &header_101, 101);
388+
disconnect_block(&nodes[0], &header_101, CHAN_CONFIRM_DEPTH + 1);
389389
connect_blocks(&nodes[1], 15, 101, false, block_hash_100);
390390
{
391391
let mut node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();

0 commit comments

Comments
 (0)