From: Matt Corallo Date: Sun, 15 May 2022 18:47:32 +0000 (+0000) Subject: Store full blocks in the test blockchain tracker instead of headers X-Git-Tag: v0.0.107~14^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=12e8e9f3dbd47928bf26f402e01f7a0f4157ed76;p=rust-lightning Store full blocks in the test blockchain tracker instead of headers --- diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index 58fe30ba2..5706870e6 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -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(); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index b4807ea68..cb8a02d28 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -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>, pub network_chan_count: Rc>, pub logger: &'c test_utils::TestLogger, - pub blocks: Arc>>, + pub blocks: Arc>>, pub connect_style: Rc>, } 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 { 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); diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 3961b7077..6f5304b81 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -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 { diff --git a/lightning/src/ln/payment_tests.rs b/lightning/src/ln/payment_tests.rs index 46d5d22b4..1e5495a35 100644 --- a/lightning/src/ln/payment_tests.rs +++ b/lightning/src/ln/payment_tests.rs @@ -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 diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 5e9591d8e..ac1baaf0d 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -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 chainmonitor::Persist for TestPersiste pub struct TestBroadcaster { pub txn_broadcasted: Mutex>, - pub blocks: Arc>>, + pub blocks: Arc>>, } impl TestBroadcaster { - pub fn new(blocks: Arc>>) -> TestBroadcaster { + pub fn new(blocks: Arc>>) -> TestBroadcaster { TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks } } }