Skip to content

Commit 12e8e9f

Browse files
committed
Store full blocks in the test blockchain tracker instead of headers
1 parent e5c988e commit 12e8e9f

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn test_monitor_and_persister_update_fail() {
103103
// Because we will connect a block at height 200 below, we need the TestBroadcaster to know
104104
// that we are at height 200 so that it doesn't think we're violating the time lock
105105
// requirements of transactions broadcasted at that point.
106-
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 200); 200])),
106+
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 200); 200])),
107107
};
108108
let chain_mon = {
109109
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();

lightning/src/ln/functional_test_utils.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,20 @@ pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) ->
109109
};
110110
assert!(depth >= 1);
111111
for i in 1..depth {
112-
do_connect_block(node, &block, skip_intermediaries);
112+
let prev_blockhash = block.header.block_hash();
113+
do_connect_block(node, block, skip_intermediaries);
113114
block = Block {
114-
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: height + i, bits: 42, nonce: 42 },
115+
header: BlockHeader { version: 0x20000000, prev_blockhash, merkle_root: Default::default(), time: height + i, bits: 42, nonce: 42 },
115116
txdata: vec![],
116117
};
117118
}
118-
connect_block(node, &block);
119-
block.header.block_hash()
119+
let hash = block.header.block_hash();
120+
do_connect_block(node, block, false);
121+
hash
120122
}
121123

122124
pub fn connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block) {
123-
do_connect_block(node, block, false);
125+
do_connect_block(node, block.clone(), false);
124126
}
125127

126128
fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
@@ -130,7 +132,7 @@ fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
130132
}
131133
}
132134

133-
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block, skip_intermediaries: bool) {
135+
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
134136
call_claimable_balances(node);
135137
let height = node.best_block_info().1 + 1;
136138
if !skip_intermediaries {
@@ -158,30 +160,30 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block, s
158160
}
159161
call_claimable_balances(node);
160162
node.node.test_process_background_events();
161-
node.blocks.lock().unwrap().push((block.header, height));
163+
node.blocks.lock().unwrap().push((block, height));
162164
}
163165

164166
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
165167
call_claimable_balances(node);
166168
for i in 0..count {
167-
let orig_header = node.blocks.lock().unwrap().pop().unwrap();
168-
assert!(orig_header.1 > 0); // Cannot disconnect genesis
169-
let prev_header = node.blocks.lock().unwrap().last().unwrap().clone();
169+
let orig = node.blocks.lock().unwrap().pop().unwrap();
170+
assert!(orig.1 > 0); // Cannot disconnect genesis
171+
let prev = node.blocks.lock().unwrap().last().unwrap().clone();
170172

171173
match *node.connect_style.borrow() {
172174
ConnectStyle::FullBlockViaListen => {
173-
node.chain_monitor.chain_monitor.block_disconnected(&orig_header.0, orig_header.1);
174-
Listen::block_disconnected(node.node, &orig_header.0, orig_header.1);
175+
node.chain_monitor.chain_monitor.block_disconnected(&orig.0.header, orig.1);
176+
Listen::block_disconnected(node.node, &orig.0.header, orig.1);
175177
},
176178
ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks => {
177179
if i == count - 1 {
178-
node.chain_monitor.chain_monitor.best_block_updated(&prev_header.0, prev_header.1);
179-
node.node.best_block_updated(&prev_header.0, prev_header.1);
180+
node.chain_monitor.chain_monitor.best_block_updated(&prev.0.header, prev.1);
181+
node.node.best_block_updated(&prev.0.header, prev.1);
180182
}
181183
},
182184
_ => {
183-
node.chain_monitor.chain_monitor.best_block_updated(&prev_header.0, prev_header.1);
184-
node.node.best_block_updated(&prev_header.0, prev_header.1);
185+
node.chain_monitor.chain_monitor.best_block_updated(&prev.0.header, prev.1);
186+
node.node.best_block_updated(&prev.0.header, prev.1);
185187
},
186188
}
187189
call_claimable_balances(node);
@@ -227,7 +229,7 @@ pub struct Node<'a, 'b: 'a, 'c: 'b> {
227229
pub network_payment_count: Rc<RefCell<u8>>,
228230
pub network_chan_count: Rc<RefCell<u32>>,
229231
pub logger: &'c test_utils::TestLogger,
230-
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
232+
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
231233
pub connect_style: Rc<RefCell<ConnectStyle>>,
232234
}
233235
impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
@@ -238,7 +240,7 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
238240
self.blocks.lock().unwrap().last().map(|(a, b)| (a.block_hash(), *b)).unwrap()
239241
}
240242
pub fn get_block_header(&self, height: u32) -> BlockHeader {
241-
self.blocks.lock().unwrap()[height as usize].0
243+
self.blocks.lock().unwrap()[height as usize].0.header
242244
}
243245
}
244246

@@ -1815,7 +1817,7 @@ pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
18151817
for i in 0..node_count {
18161818
let tx_broadcaster = test_utils::TestBroadcaster {
18171819
txn_broadcasted: Mutex::new(Vec::new()),
1818-
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 0)])),
1820+
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 0)])),
18191821
};
18201822
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
18211823
let chain_source = test_utils::TestChainSource::new(Network::Testnet);

lightning/src/ln/functional_tests.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8711,10 +8711,11 @@ fn test_update_err_monitor_lockdown() {
87118711
watchtower
87128712
};
87138713
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8714+
let block = Block { header, txdata: vec![] };
87148715
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
87158716
// transaction lock time requirements here.
8716-
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (header, 0));
8717-
watchtower.chain_monitor.block_connected(&Block { header, txdata: vec![] }, 200);
8717+
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 0));
8718+
watchtower.chain_monitor.block_connected(&block, 200);
87188719

87198720
// Try to update ChannelMonitor
87208721
assert!(nodes[1].node.claim_funds(preimage));
@@ -8772,10 +8773,11 @@ fn test_concurrent_monitor_claim() {
87728773
watchtower
87738774
};
87748775
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8776+
let block = Block { header, txdata: vec![] };
87758777
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
87768778
// transaction lock time requirements here.
8777-
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (header, 0));
8778-
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8779+
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (block.clone(), 0));
8780+
watchtower_alice.chain_monitor.block_connected(&block, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
87798781

87808782
// Watchtower Alice should have broadcast a commitment/HTLC-timeout
87818783
{

lightning/src/ln/payment_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
482482

483483
if confirm_before_reload {
484484
let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone();
485-
nodes[0].node.best_block_updated(&best_block.0, best_block.1);
485+
nodes[0].node.best_block_updated(&best_block.0.header, best_block.1);
486486
}
487487

488488
// Create a new channel on which to retry the payment before we fail the payment via the

lightning/src/util/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::blockdata::constants::genesis_block;
3131
use bitcoin::blockdata::transaction::{Transaction, TxOut};
3232
use bitcoin::blockdata::script::{Builder, Script};
3333
use bitcoin::blockdata::opcodes;
34-
use bitcoin::blockdata::block::BlockHeader;
34+
use bitcoin::blockdata::block::Block;
3535
use bitcoin::network::constants::Network;
3636
use bitcoin::hash_types::{BlockHash, Txid};
3737

@@ -224,11 +224,11 @@ impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersiste
224224

225225
pub struct TestBroadcaster {
226226
pub txn_broadcasted: Mutex<Vec<Transaction>>,
227-
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
227+
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
228228
}
229229

230230
impl TestBroadcaster {
231-
pub fn new(blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>) -> TestBroadcaster {
231+
pub fn new(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> TestBroadcaster {
232232
TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks }
233233
}
234234
}

0 commit comments

Comments
 (0)