Skip to content

Commit fea0bee

Browse files
committed
Add more robust functional test of Listen::blocks_disconnected
Now that the `Listen` interface allows blocks to be disconnected in batches rather than one at a time, we should test this. Here we add a new `ConnectStyle` for the functional test framework which tests doing so.
1 parent 2d3aaa5 commit fea0bee

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ pub enum ConnectStyle {
196196
/// Provides the full block via the `chain::Listen` interface. In the current code this is
197197
/// equivalent to `TransactionsFirst` with some additional assertions.
198198
FullBlockViaListen,
199+
/// Provides the full block via the `chain::Listen` interface, condensing multiple block
200+
/// disconnections into a single `blocks_disconnected` call.
201+
FullBlockDisconnectionsSkippingViaListen,
199202
}
200203

201204
impl ConnectStyle {
@@ -210,6 +213,7 @@ impl ConnectStyle {
210213
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks => true,
211214
ConnectStyle::TransactionsFirstReorgsOnlyTip => true,
212215
ConnectStyle::FullBlockViaListen => false,
216+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => false,
213217
}
214218
}
215219

@@ -224,14 +228,15 @@ impl ConnectStyle {
224228
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks => false,
225229
ConnectStyle::TransactionsFirstReorgsOnlyTip => false,
226230
ConnectStyle::FullBlockViaListen => false,
231+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => false,
227232
}
228233
}
229234

230235
fn random_style() -> ConnectStyle {
231236
use core::hash::{BuildHasher, Hasher};
232237
// Get a random value using the only std API to do so - the DefaultHasher
233238
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
234-
let res = match rand_val % 9 {
239+
let res = match rand_val % 10 {
235240
0 => ConnectStyle::BestBlockFirst,
236241
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
237242
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
@@ -241,6 +246,7 @@ impl ConnectStyle {
241246
6 => ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks,
242247
7 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
243248
8 => ConnectStyle::FullBlockViaListen,
249+
9 => ConnectStyle::FullBlockDisconnectionsSkippingViaListen,
244250
_ => unreachable!(),
245251
};
246252
eprintln!("Using Block Connection Style: {:?}", res);
@@ -371,7 +377,8 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(
371377
node.node.transactions_confirmed(&block.header, &txdata, height);
372378
node.node.best_block_updated(&block.header, height);
373379
},
374-
ConnectStyle::FullBlockViaListen => {
380+
ConnectStyle::FullBlockViaListen
381+
| ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
375382
node.chain_monitor.chain_monitor.block_connected(&block, height);
376383
node.node.block_connected(&block, height);
377384
},
@@ -432,6 +439,13 @@ pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32)
432439
node.chain_monitor.chain_monitor.blocks_disconnected(best_block);
433440
Listen::blocks_disconnected(node.node, best_block);
434441
},
442+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
443+
if i == count - 1 {
444+
let best_block = BestBlock::new(orig.0.header.prev_blockhash, orig.1 - 1);
445+
node.chain_monitor.chain_monitor.blocks_disconnected(best_block);
446+
Listen::blocks_disconnected(node.node, best_block);
447+
}
448+
},
435449
ConnectStyle::BestBlockFirstSkippingBlocks
436450
| ConnectStyle::TransactionsFirstSkippingBlocks
437451
| ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks

lightning/src/ln/functional_tests.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,11 +2328,15 @@ pub fn test_htlc_ignore_latest_remote_commitment() {
23282328
let node_a_id = nodes[0].node.get_our_node_id();
23292329
let node_b_id = nodes[1].node.get_our_node_id();
23302330

2331-
if *nodes[1].connect_style.borrow() == ConnectStyle::FullBlockViaListen {
2332-
// We rely on the ability to connect a block redundantly, which isn't allowed via
2333-
// `chain::Listen`, so we never run the test if we randomly get assigned that
2334-
// connect_style.
2335-
return;
2331+
match *nodes[1].connect_style.borrow() {
2332+
ConnectStyle::FullBlockViaListen
2333+
| ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
2334+
// We rely on the ability to connect a block redundantly, which isn't allowed via
2335+
// `chain::Listen`, so we never run the test if we randomly get assigned that
2336+
// connect_style.
2337+
return;
2338+
},
2339+
_ => {},
23362340
}
23372341
let funding_tx = create_announced_chan_between_nodes(&nodes, 0, 1).3;
23382342
let message = "Channel force-closed".to_owned();

0 commit comments

Comments
 (0)