Skip to content

Commit 640af71

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 640af71

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 33 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,31 @@ pub enum ConnectStyle {
102102
FullBlockViaListen,
103103
}
104104

105+
impl ConnectStyle {
106+
fn random_style() -> ConnectStyle {
107+
#[cfg(feature = "std")] {
108+
use core::hash::{BuildHasher, Hasher};
109+
// Get a random value using the only std AIP to do so - the DefaultHasher
110+
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
111+
let res = match rand_val % 7 {
112+
0 => ConnectStyle::BestBlockFirst,
113+
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
114+
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
115+
3 => ConnectStyle::TransactionsFirst,
116+
4 => ConnectStyle::TransactionsFirstSkippingBlocks,
117+
5 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
118+
6 => ConnectStyle::FullBlockViaListen,
119+
_ => unreachable!(),
120+
};
121+
eprintln!("Using Block Connection Style: {:?}", res);
122+
res
123+
}
124+
#[cfg(not(feature = "std"))] {
125+
ConnectStyle::FullBlockViaListen
126+
}
127+
}
128+
}
129+
105130
pub fn connect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, depth: u32) -> BlockHash {
106131
let skip_intermediaries = match *node.connect_style.borrow() {
107132
ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks|
@@ -142,6 +167,9 @@ fn call_claimable_balances<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>) {
142167
fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, skip_intermediaries: bool) {
143168
call_claimable_balances(node);
144169
let height = node.best_block_info().1 + 1;
170+
#[cfg(feature = "std")] {
171+
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
172+
}
145173
if !skip_intermediaries {
146174
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
147175
match *node.connect_style.borrow() {
@@ -172,6 +200,9 @@ fn do_connect_block<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, block: Block, sk
172200

173201
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
174202
call_claimable_balances(node);
203+
#[cfg(feature = "std")] {
204+
eprintln!("Disconnecting {} blocks using Block Connection Style: {:?}", count, *node.connect_style.borrow());
205+
}
175206
for i in 0..count {
176207
let orig = node.blocks.lock().unwrap().pop().unwrap();
177208
assert!(orig.1 > 0); // Cannot disconnect genesis
@@ -1904,7 +1935,7 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
19041935
let mut nodes = Vec::new();
19051936
let chan_count = Rc::new(RefCell::new(0));
19061937
let payment_count = Rc::new(RefCell::new(0));
1907-
let connect_style = Rc::new(RefCell::new(ConnectStyle::FullBlockViaListen));
1938+
let connect_style = Rc::new(RefCell::new(ConnectStyle::random_style()));
19081939

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

0 commit comments

Comments
 (0)