Skip to content

Commit d5b2812

Browse files
committed
Track the blocks a node has connected in the TestBroadcaster
1 parent e8c3d22 commit d5b2812

File tree

6 files changed

+60
-29
lines changed

6 files changed

+60
-29
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ mod tests {
238238
fn create_nodes(num_nodes: usize, persist_dir: String) -> Vec<Node> {
239239
let mut nodes = Vec::new();
240240
for i in 0..num_nodes {
241-
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
241+
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
242242
let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
243243
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
244244
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2920,7 +2920,7 @@ mod tests {
29202920
fn test_prune_preimages() {
29212921
let secp_ctx = Secp256k1::new();
29222922
let logger = Arc::new(TestLogger::new());
2923-
let broadcaster = Arc::new(TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
2923+
let broadcaster = Arc::new(TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
29242924
let fee_estimator = Arc::new(TestFeeEstimator { sat_per_kw: 253 });
29252925

29262926
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! here. See also the chanmon_fail_consistency fuzz test.
1414
1515
use bitcoin::blockdata::block::{Block, BlockHeader};
16+
use bitcoin::blockdata::constants::genesis_block;
1617
use bitcoin::hash_types::BlockHash;
1718
use bitcoin::network::constants::Network;
1819
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr};
@@ -30,6 +31,7 @@ use util::enforcing_trait_impls::EnforcingSigner;
3031
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
3132
use util::errors::APIError;
3233
use util::ser::{ReadableArgs, Writeable};
34+
use util::test_utils::TestBroadcaster;
3335

3436
use bitcoin::hashes::sha256::Hash as Sha256;
3537
use bitcoin::hashes::Hash;
@@ -40,6 +42,7 @@ use util::test_utils;
4042

4143
use prelude::*;
4244
use std::collections::HashMap;
45+
use std::sync::{Arc, Mutex};
4346

4447
// If persister_fail is true, we have the persister return a PermanentFailure
4548
// instead of the higher-level ChainMonitor.
@@ -107,6 +110,13 @@ fn test_monitor_and_persister_update_fail() {
107110
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
108111
let logger = test_utils::TestLogger::with_id(format!("node {}", 0));
109112
let persister = test_utils::TestPersister::new();
113+
let tx_broadcaster = TestBroadcaster {
114+
txn_broadcasted: Mutex::new(Vec::new()),
115+
// Because we will connect a block at height 200 below, we need the TestBroadcaster to know
116+
// that we are at height 200 so that it doesn't think we're violating the time lock
117+
// requirements of transactions broadcasted at that point.
118+
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 0); 200])),
119+
};
110120
let chain_mon = {
111121
let monitors = nodes[0].chain_monitor.chain_monitor.monitors.read().unwrap();
112122
let monitor = monitors.get(&outpoint).unwrap();
@@ -115,7 +125,7 @@ fn test_monitor_and_persister_update_fail() {
115125
let new_monitor = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
116126
&mut ::std::io::Cursor::new(&w.0), &test_utils::OnlyReadsKeysInterface {}).unwrap().1;
117127
assert!(new_monitor == *monitor);
118-
let chain_mon = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
128+
let chain_mon = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
119129
assert!(chain_mon.watch_channel(outpoint, new_monitor).is_ok());
120130
chain_mon
121131
};

lightning/src/ln/functional_test_utils.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use bitcoin::secp256k1::key::PublicKey;
4242
use prelude::*;
4343
use core::cell::RefCell;
4444
use std::rc::Rc;
45-
use std::sync::Mutex;
45+
use std::sync::{Arc, Mutex};
4646
use core::mem;
4747
use std::collections::HashMap;
4848

@@ -149,14 +149,14 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: &Block, s
149149
}
150150
}
151151
node.node.test_process_background_events();
152-
node.blocks.borrow_mut().push((block.header, height));
152+
node.blocks.lock().unwrap().push((block.header, height));
153153
}
154154

155155
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
156156
for i in 0..count {
157-
let orig_header = node.blocks.borrow_mut().pop().unwrap();
157+
let orig_header = node.blocks.lock().unwrap().pop().unwrap();
158158
assert!(orig_header.1 > 0); // Cannot disconnect genesis
159-
let prev_header = node.blocks.borrow().last().unwrap().clone();
159+
let prev_header = node.blocks.lock().unwrap().last().unwrap().clone();
160160

161161
match *node.connect_style.borrow() {
162162
ConnectStyle::FullBlockViaListen => {
@@ -178,7 +178,7 @@ pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32)
178178
}
179179

180180
pub fn disconnect_all_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
181-
let count = node.blocks.borrow_mut().len() as u32 - 1;
181+
let count = node.blocks.lock().unwrap().len() as u32 - 1;
182182
disconnect_blocks(node, count);
183183
}
184184

@@ -212,15 +212,15 @@ pub struct Node<'a, 'b: 'a, 'c: 'b> {
212212
pub network_payment_count: Rc<RefCell<u8>>,
213213
pub network_chan_count: Rc<RefCell<u32>>,
214214
pub logger: &'c test_utils::TestLogger,
215-
pub blocks: RefCell<Vec<(BlockHeader, u32)>>,
215+
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
216216
pub connect_style: Rc<RefCell<ConnectStyle>>,
217217
}
218218
impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
219219
pub fn best_block_hash(&self) -> BlockHash {
220-
self.blocks.borrow_mut().last().unwrap().0.block_hash()
220+
self.blocks.lock().unwrap().last().unwrap().0.block_hash()
221221
}
222222
pub fn best_block_info(&self) -> (BlockHash, u32) {
223-
self.blocks.borrow_mut().last().map(|(a, b)| (a.block_hash(), *b)).unwrap()
223+
self.blocks.lock().unwrap().last().map(|(a, b)| (a.block_hash(), *b)).unwrap()
224224
}
225225
}
226226

@@ -296,7 +296,8 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
296296
fee_estimator: &test_utils::TestFeeEstimator { sat_per_kw: 253 },
297297
chain_monitor: self.chain_monitor,
298298
tx_broadcaster: &test_utils::TestBroadcaster {
299-
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone())
299+
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone()),
300+
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
300301
},
301302
logger: &test_utils::TestLogger::new(),
302303
channel_monitors,
@@ -305,7 +306,8 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
305306

306307
let persister = test_utils::TestPersister::new();
307308
let broadcaster = test_utils::TestBroadcaster {
308-
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone())
309+
txn_broadcasted: Mutex::new(self.tx_broadcaster.txn_broadcasted.lock().unwrap().clone()),
310+
blocks: Arc::new(Mutex::new(self.tx_broadcaster.blocks.lock().unwrap().clone())),
309311
};
310312
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
311313
let chain_monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &broadcaster, &self.logger, &feeest, &persister, &self.keys_manager);
@@ -1284,7 +1286,10 @@ pub fn fail_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route:
12841286
pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
12851287
let mut chan_mon_cfgs = Vec::new();
12861288
for i in 0..node_count {
1287-
let tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())};
1289+
let tx_broadcaster = test_utils::TestBroadcaster {
1290+
txn_broadcasted: Mutex::new(Vec::new()),
1291+
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet).header, 0)])),
1292+
};
12881293
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
12891294
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
12901295
let logger = test_utils::TestLogger::with_id(format!("node {}", i));
@@ -1345,7 +1350,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
13451350
keys_manager: &cfgs[i].keys_manager, node: &chan_mgrs[i], net_graph_msg_handler,
13461351
node_seed: cfgs[i].node_seed, network_chan_count: chan_count.clone(),
13471352
network_payment_count: payment_count.clone(), logger: cfgs[i].logger,
1348-
blocks: RefCell::new(vec![(genesis_block(Network::Testnet).header, 0)]),
1353+
blocks: Arc::clone(&cfgs[i].tx_broadcaster.blocks),
13491354
connect_style: Rc::clone(&connect_style),
13501355
})
13511356
}

lightning/src/ln/functional_tests.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use prelude::*;
5454
use alloc::collections::BTreeSet;
5555
use std::collections::{HashMap, HashSet};
5656
use core::default::Default;
57-
use std::sync::Mutex;
57+
use std::sync::{Arc, Mutex};
5858

5959
use ln::functional_test_utils::*;
6060
use ln::chan_utils::CommitmentTransaction;
@@ -2750,7 +2750,8 @@ fn test_htlc_on_chain_success() {
27502750
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
27512751

27522752
// Ensure all nodes are at the same height
2753-
let node_max_height = std::cmp::max(nodes[0].blocks.borrow().len(), std::cmp::max(nodes[1].blocks.borrow().len(), nodes[2].blocks.borrow().len())) as u32;
2753+
let node_max_height = std::cmp::max(nodes[0].blocks.lock().unwrap().len(),
2754+
std::cmp::max(nodes[1].blocks.lock().unwrap().len(), nodes[2].blocks.lock().unwrap().len())) as u32;
27542755
connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
27552756
connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
27562757
connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
@@ -4523,7 +4524,7 @@ fn test_dup_htlc_onchain_fails_on_reload() {
45234524
// Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
45244525
// which the current ChannelMonitor has not seen), the ChannelManager's de-duplication of
45254526
// payment events should kick in, leaving us with no pending events here.
4526-
nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, nodes[0].blocks.borrow().len() as u32 - 1);
4527+
nodes[0].chain_monitor.chain_monitor.block_connected(&claim_block, nodes[0].blocks.lock().unwrap().len() as u32 - 1);
45274528
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
45284529
}
45294530

@@ -5244,7 +5245,8 @@ fn test_onchain_to_onchain_claim() {
52445245
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
52455246

52465247
// Ensure all nodes are at the same height
5247-
let node_max_height = std::cmp::max(nodes[0].blocks.borrow().len(), std::cmp::max(nodes[1].blocks.borrow().len(), nodes[2].blocks.borrow().len())) as u32;
5248+
let node_max_height = std::cmp::max(nodes[0].blocks.lock().unwrap().len(),
5249+
std::cmp::max(nodes[1].blocks.lock().unwrap().len(), nodes[2].blocks.lock().unwrap().len())) as u32;
52485250
connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
52495251
connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
52505252
connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
@@ -5357,7 +5359,9 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
53575359
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
53585360
create_announced_chan_between_nodes(&nodes, 2, 3, InitFeatures::known(), InitFeatures::known());
53595361

5360-
let node_max_height = std::cmp::max(std::cmp::max(nodes[0].blocks.borrow().len(), nodes[1].blocks.borrow().len()), std::cmp::max(nodes[2].blocks.borrow().len(), nodes[3].blocks.borrow().len())) as u32;
5362+
let node_max_height = std::cmp::max(
5363+
std::cmp::max(nodes[0].blocks.lock().unwrap().len(), nodes[1].blocks.lock().unwrap().len()),
5364+
std::cmp::max(nodes[2].blocks.lock().unwrap().len(), nodes[3].blocks.lock().unwrap().len())) as u32;
53615365
connect_blocks(&nodes[0], node_max_height - nodes[0].best_block_info().1);
53625366
connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
53635367
connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
@@ -7689,7 +7693,7 @@ fn test_data_loss_protect() {
76897693
logger = test_utils::TestLogger::with_id(format!("node {}", 0));
76907694
let mut chain_monitor = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(&mut ::std::io::Cursor::new(previous_chain_monitor_state.0), keys_manager).unwrap().1;
76917695
chain_source = test_utils::TestChainSource::new(Network::Testnet);
7692-
tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())};
7696+
tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
76937697
fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: 253 };
76947698
persister = test_utils::TestPersister::new();
76957699
monitor = test_utils::TestChainMonitor::new(Some(&chain_source), &tx_broadcaster, &logger, &fee_estimator, &persister, keys_manager);
@@ -8474,14 +8478,18 @@ fn test_secret_timeout() {
84748478
if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(100_000), 2, 0) {
84758479
assert_eq!(err, "Duplicate payment hash");
84768480
} else { panic!(); }
8477-
let mut block = Block {
8478-
header: BlockHeader {
8479-
version: 0x2000000,
8480-
prev_blockhash: nodes[1].blocks.borrow().last().unwrap().0.block_hash(),
8481-
merkle_root: Default::default(),
8482-
time: nodes[1].blocks.borrow().len() as u32 + 7200, bits: 42, nonce: 42 },
8483-
txdata: vec![],
8484-
};
8481+
let mut block;
8482+
{
8483+
let node_1_blocks = nodes[1].blocks.lock().unwrap();
8484+
block = Block {
8485+
header: BlockHeader {
8486+
version: 0x2000000,
8487+
prev_blockhash: node_1_blocks.last().unwrap().0.block_hash(),
8488+
merkle_root: Default::default(),
8489+
time: node_1_blocks.len() as u32 + 7200, bits: 42, nonce: 42 },
8490+
txdata: vec![],
8491+
};
8492+
}
84858493
connect_block(&nodes[1], &block);
84868494
if let Err(APIError::APIMisuseError { err }) = nodes[1].node.create_inbound_payment_for_hash(payment_hash, Some(100_000), 2, 0) {
84878495
assert_eq!(err, "Duplicate payment hash");
@@ -8630,6 +8638,9 @@ fn test_update_err_monitor_lockdown() {
86308638
watchtower
86318639
};
86328640
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8641+
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8642+
// transaction lock time requirements here.
8643+
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (header, 0));
86338644
watchtower.chain_monitor.block_connected(&Block { header, txdata: vec![] }, 200);
86348645

86358646
// Try to update ChannelMonitor
@@ -8689,6 +8700,9 @@ fn test_concurrent_monitor_claim() {
86898700
watchtower
86908701
};
86918702
let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
8703+
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8704+
// transaction lock time requirements here.
8705+
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (header, 0));
86928706
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
86938707

86948708
// Watchtower Alice should have broadcast a commitment/HTLC-timeout

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use bitcoin::blockdata::constants::genesis_block;
2828
use bitcoin::blockdata::transaction::{Transaction, TxOut};
2929
use bitcoin::blockdata::script::{Builder, Script};
3030
use bitcoin::blockdata::opcodes;
31+
use bitcoin::blockdata::block::BlockHeader;
3132
use bitcoin::network::constants::Network;
3233
use bitcoin::hash_types::{BlockHash, Txid};
3334

@@ -201,6 +202,7 @@ impl<Signer: keysinterface::Sign> channelmonitor::Persist<Signer> for TestPersis
201202

202203
pub struct TestBroadcaster {
203204
pub txn_broadcasted: Mutex<Vec<Transaction>>,
205+
pub blocks: Arc<Mutex<Vec<(BlockHeader, u32)>>>,
204206
}
205207
impl chaininterface::BroadcasterInterface for TestBroadcaster {
206208
fn broadcast_transaction(&self, tx: &Transaction) {

0 commit comments

Comments
 (0)