Store full blocks in the test blockchain tracker instead of headers

This commit is contained in:
Matt Corallo 2022-05-15 18:47:32 +00:00
parent e5c988e00c
commit 12e8e9f3db
5 changed files with 32 additions and 28 deletions

View file

@ -103,7 +103,7 @@ fn test_monitor_and_persister_update_fail() {
// Because we will connect a block at height 200 below, we need the TestBroadcaster to know
// that we are at height 200 so that it doesn't think we're violating the time lock
// requirements of transactions broadcasted at that point.
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 200); 200])),
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 200); 200])),
};
let chain_mon = {
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();

View file

@ -109,18 +109,20 @@ pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) ->
};
assert!(depth >= 1);
for i in 1..depth {
do_connect_block(node, &block, skip_intermediaries);
let prev_blockhash = block.header.block_hash();
do_connect_block(node, block, skip_intermediaries);
block = Block {
header: BlockHeader { version: 0x20000000, prev_blockhash: block.header.block_hash(), merkle_root: Default::default(), time: height + i, bits: 42, nonce: 42 },
header: BlockHeader { version: 0x20000000, prev_blockhash, merkle_root: Default::default(), time: height + i, bits: 42, nonce: 42 },
txdata: vec![],
};
}
connect_block(node, &block);
block.header.block_hash()
let hash = block.header.block_hash();
do_connect_block(node, block, false);
hash
}
pub fn connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block) {
do_connect_block(node, block, false);
do_connect_block(node, block.clone(), false);
}
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>) {
}
}
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block, skip_intermediaries: bool) {
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
call_claimable_balances(node);
let height = node.best_block_info().1 + 1;
if !skip_intermediaries {
@ -158,30 +160,30 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block, s
}
call_claimable_balances(node);
node.node.test_process_background_events();
node.blocks.lock().unwrap().push((block.header, height));
node.blocks.lock().unwrap().push((block, height));
}
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
call_claimable_balances(node);
for i in 0..count {
let orig_header = node.blocks.lock().unwrap().pop().unwrap();
assert!(orig_header.1 > 0); // Cannot disconnect genesis
let prev_header = node.blocks.lock().unwrap().last().unwrap().clone();
let orig = node.blocks.lock().unwrap().pop().unwrap();
assert!(orig.1 > 0); // Cannot disconnect genesis
let prev = node.blocks.lock().unwrap().last().unwrap().clone();
match *node.connect_style.borrow() {
ConnectStyle::FullBlockViaListen => {
node.chain_monitor.chain_monitor.block_disconnected(&orig_header.0, orig_header.1);
Listen::block_disconnected(node.node, &orig_header.0, orig_header.1);
node.chain_monitor.chain_monitor.block_disconnected(&orig.0.header, orig.1);
Listen::block_disconnected(node.node, &orig.0.header, orig.1);
},
ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks => {
if i == count - 1 {
node.chain_monitor.chain_monitor.best_block_updated(&prev_header.0, prev_header.1);
node.node.best_block_updated(&prev_header.0, prev_header.1);
node.chain_monitor.chain_monitor.best_block_updated(&prev.0.header, prev.1);
node.node.best_block_updated(&prev.0.header, prev.1);
}
},
_ => {
node.chain_monitor.chain_monitor.best_block_updated(&prev_header.0, prev_header.1);
node.node.best_block_updated(&prev_header.0, prev_header.1);
node.chain_monitor.chain_monitor.best_block_updated(&prev.0.header, prev.1);
node.node.best_block_updated(&prev.0.header, prev.1);
},
}
call_claimable_balances(node);
@ -227,7 +229,7 @@ pub struct Node<'a, 'b: 'a, 'c: 'b> {
pub network_payment_count: Rc<RefCell<u8>>,
pub network_chan_count: Rc<RefCell<u32>>,
pub logger: &'c test_utils::TestLogger,
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
pub connect_style: Rc<RefCell<ConnectStyle>>,
}
impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
@ -238,7 +240,7 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
self.blocks.lock().unwrap().last().map(|(a, b)| (a.block_hash(), *b)).unwrap()
}
pub fn get_block_header(&self, height: u32) -> BlockHeader {
self.blocks.lock().unwrap()[height as usize].0
self.blocks.lock().unwrap()[height as usize].0.header
}
}
@ -1815,7 +1817,7 @@ pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
for i in 0..node_count {
let tx_broadcaster = test_utils::TestBroadcaster {
txn_broadcasted: Mutex::new(Vec::new()),
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 0)])),
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 0)])),
};
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
let chain_source = test_utils::TestChainSource::new(Network::Testnet);

View file

@ -8711,10 +8711,11 @@ fn test_update_err_monitor_lockdown() {
watchtower
};
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
let block = Block { header, txdata: vec![] };
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
// transaction lock time requirements here.
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (header, 0));
watchtower.chain_monitor.block_connected(&Block { header, txdata: vec![] }, 200);
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 0));
watchtower.chain_monitor.block_connected(&block, 200);
// Try to update ChannelMonitor
assert!(nodes[1].node.claim_funds(preimage));
@ -8772,10 +8773,11 @@ fn test_concurrent_monitor_claim() {
watchtower
};
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
let block = Block { header, txdata: vec![] };
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
// transaction lock time requirements here.
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (header, 0));
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
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));
watchtower_alice.chain_monitor.block_connected(&block, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
// Watchtower Alice should have broadcast a commitment/HTLC-timeout
{

View file

@ -482,7 +482,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
if confirm_before_reload {
let best_block = nodes[0].blocks.lock().unwrap().last().unwrap().clone();
nodes[0].node.best_block_updated(&best_block.0, best_block.1);
nodes[0].node.best_block_updated(&best_block.0.header, best_block.1);
}
// Create a new channel on which to retry the payment before we fail the payment via the

View file

@ -31,7 +31,7 @@ use bitcoin::blockdata::constants::genesis_block;
use bitcoin::blockdata::transaction::{Transaction, TxOut};
use bitcoin::blockdata::script::{Builder, Script};
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::block::BlockHeader;
use bitcoin::blockdata::block::Block;
use bitcoin::network::constants::Network;
use bitcoin::hash_types::{BlockHash, Txid};
@ -224,11 +224,11 @@ impl<Signer: keysinterface::Sign> chainmonitor::Persist<Signer> for TestPersiste
pub struct TestBroadcaster {
pub txn_broadcasted: Mutex<Vec<Transaction>>,
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
pub blocks: Arc<Mutex<Vec<(Block, u32)>>>,
}
impl TestBroadcaster {
pub fn new(blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>) -> TestBroadcaster {
pub fn new(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> TestBroadcaster {
TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks }
}
}