Skip to content

Commit 1ba1c6d

Browse files
committed
Randomize the ConnectStyle during tests
We have a bunch of fancy infrastructure to ensure we can connect blocks using all our different connection interfaces, but we only bother to use it in a few select tests. This expands our use of `ConnectStyle` to most of our tests by simply randomizing the style in each test. This makes our tests non-deterministic, but we print the connection style at start so that it's easy to reproduce a failure deterministically.
1 parent 2ea5229 commit 1ba1c6d

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn confirm_transaction_at<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, tx: &T
7777
}
7878

7979
/// The possible ways we may notify a ChannelManager of a new block
80-
#[derive(Clone, Copy, PartialEq)]
80+
#[derive(Clone, Copy, Debug, PartialEq)]
8181
pub enum ConnectStyle {
8282
/// Calls best_block_updated first, detecting transactions in the block only after receiving the
8383
/// header and height information.
@@ -102,6 +102,26 @@ pub enum ConnectStyle {
102102
FullBlockViaListen,
103103
}
104104

105+
impl ConnectStyle {
106+
fn random_style() -> ConnectStyle {
107+
use std::hash::{BuildHasher, Hasher};
108+
// Get a random value using the only std AIP to do so - the DefaultHasher
109+
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
110+
let res = match rand_val % 7 {
111+
0 => ConnectStyle::BestBlockFirst,
112+
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
113+
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
114+
3 => ConnectStyle::TransactionsFirst,
115+
4 => ConnectStyle::TransactionsFirstSkippingBlocks,
116+
5 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
117+
6 => ConnectStyle::FullBlockViaListen,
118+
_ => unreachable!(),
119+
};
120+
#[cfg(feature = "std")] { eprintln!("Using Block Connection Style: {:?}", res); }
121+
res
122+
}
123+
}
124+
105125
pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) -> BlockHash {
106126
let skip_intermediaries = match *node.connect_style.borrow() {
107127
ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks|
@@ -142,6 +162,9 @@ fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
142162
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
143163
call_claimable_balances(node);
144164
let height = node.best_block_info().1 + 1;
165+
#[cfg(feature = "std")] {
166+
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
167+
}
145168
if !skip_intermediaries {
146169
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
147170
match *node.connect_style.borrow() {
@@ -172,6 +195,9 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, sk
172195

173196
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
174197
call_claimable_balances(node);
198+
#[cfg(feature = "std")] {
199+
eprintln!("Disconnecting {} blocks using Block Connection Style: {:?}", count, *node.connect_style.borrow());
200+
}
175201
for i in 0..count {
176202
let orig = node.blocks.lock().unwrap().pop().unwrap();
177203
assert!(orig.1 > 0); // Cannot disconnect genesis
@@ -1904,7 +1930,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
19041930
let mut nodes = Vec::new();
19051931
let chan_count = Rc::new(RefCell::new(0));
19061932
let payment_count = Rc::new(RefCell::new(0));
1907-
let connect_style = Rc::new(RefCell::new(ConnectStyle::FullBlockViaListen));
1933+
let connect_style = Rc::new(RefCell::new(ConnectStyle::random_style()));
19081934

19091935
for i in 0..node_count {
19101936
let net_graph_msg_handler = NetGraphMsgHandler::new(cfgs[i].network_graph, None, cfgs[i].logger);

0 commit comments

Comments
 (0)