Skip to content

Commit 6e5ecba

Browse files
committed
Enforce block connection ordering in unit and functional tests
This expands the assertions on block ordering to apply to `#[cfg(test)]` builds in addition to normal builds, requiring that unit and functional tests have syntactically-valid (ie the previous block hash pointer and the heights match the blocks) blockchains. This requires a reasonably nontrivial diff in the functional tests however it is mostly straightforward changes.
1 parent 2b868c7 commit 6e5ecba

File tree

5 files changed

+190
-307
lines changed

5 files changed

+190
-307
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,16 +3262,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32623262

32633263
let _persistence_guard = PersistenceNotifierGuard::new(&self.total_consistency_lock, &self.persistence_notifier);
32643264

3265-
3266-
// This assertion should be enforced in tests, however we have a number of tests that
3267-
// were written before this requirement and do not meet it.
3268-
#[cfg(not(test))]
3269-
{
3270-
assert_eq!(*self.last_block_hash.read().unwrap(), header.prev_blockhash,
3271-
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
3272-
assert_eq!(self.latest_block_height.load(Ordering::Acquire) as u64, height as u64 - 1,
3273-
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
3274-
}
3265+
assert_eq!(*self.last_block_hash.read().unwrap(), header.prev_blockhash,
3266+
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
3267+
assert_eq!(self.latest_block_height.load(Ordering::Acquire) as u64, height as u64 - 1,
3268+
"Blocks must be connected in chain-order - the connected header must build on the last connected header");
32753269
self.latest_block_height.store(height as usize, Ordering::Release);
32763270
*self.last_block_hash.write().unwrap() = block_hash;
32773271

lightning/src/ln/functional_test_utils.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,8 @@ use std::collections::HashMap;
4747
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 starting_block = node.best_block_info();
51-
let mut block = Block {
52-
header: BlockHeader { version: 0x20000000, prev_blockhash: starting_block.0, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
53-
txdata: Vec::new(),
54-
};
55-
block.txdata.push(tx.clone());
56-
let height = starting_block.1 + 1;
57-
connect_block(node, &block, height);
58-
for i in 2..CHAN_CONFIRM_DEPTH {
59-
block = Block {
60-
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 },
61-
txdata: vec![],
62-
};
63-
connect_block(node, &block, i + height);
64-
}
50+
confirm_transaction_at(node, tx, node.best_block_info().1 + 1);
51+
connect_blocks(node, CHAN_CONFIRM_DEPTH - 1, node.best_block_info().1, false, Default::default());
6552
}
6653
pub fn confirm_transaction_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &Transaction, conf_height: u32) {
6754
let starting_block = node.best_block_info();

0 commit comments

Comments
 (0)