Skip to content

Commit cf96b7d

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 7ae4f68 commit cf96b7d

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ pub enum ConnectStyle {
171171
/// Provides the full block via the `chain::Listen` interface. In the current code this is
172172
/// equivalent to `TransactionsFirst` with some additional assertions.
173173
FullBlockViaListen,
174+
/// Provides the full block via the `chain::Listen` interface, condensing multiple block
175+
/// disconnections into a single `blocks_disconnected` call.
176+
FullBlockDisconnectionsSkippingViaListen,
174177
}
175178

176179
impl ConnectStyle {
@@ -185,6 +188,7 @@ impl ConnectStyle {
185188
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks => true,
186189
ConnectStyle::TransactionsFirstReorgsOnlyTip => true,
187190
ConnectStyle::FullBlockViaListen => false,
191+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => false,
188192
}
189193
}
190194

@@ -199,14 +203,15 @@ impl ConnectStyle {
199203
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks => false,
200204
ConnectStyle::TransactionsFirstReorgsOnlyTip => false,
201205
ConnectStyle::FullBlockViaListen => false,
206+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => false,
202207
}
203208
}
204209

205210
fn random_style() -> ConnectStyle {
206211
use core::hash::{BuildHasher, Hasher};
207212
// Get a random value using the only std API to do so - the DefaultHasher
208213
let rand_val = std::collections::hash_map::RandomState::new().build_hasher().finish();
209-
let res = match rand_val % 9 {
214+
let res = match rand_val % 10 {
210215
0 => ConnectStyle::BestBlockFirst,
211216
1 => ConnectStyle::BestBlockFirstSkippingBlocks,
212217
2 => ConnectStyle::BestBlockFirstReorgsOnlyTip,
@@ -216,6 +221,7 @@ impl ConnectStyle {
216221
6 => ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks,
217222
7 => ConnectStyle::TransactionsFirstReorgsOnlyTip,
218223
8 => ConnectStyle::FullBlockViaListen,
224+
9 => ConnectStyle::FullBlockDisconnectionsSkippingViaListen,
219225
_ => unreachable!(),
220226
};
221227
eprintln!("Using Block Connection Style: {:?}", res);
@@ -319,7 +325,7 @@ fn do_connect_block_without_consistency_checks<'a, 'b, 'c, 'd>(node: &'a Node<'b
319325
node.node.transactions_confirmed(&block.header, &txdata, height);
320326
node.node.best_block_updated(&block.header, height);
321327
},
322-
ConnectStyle::FullBlockViaListen => {
328+
ConnectStyle::FullBlockViaListen|ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
323329
node.chain_monitor.chain_monitor.block_connected(&block, height);
324330
node.node.block_connected(&block, height);
325331
}
@@ -354,6 +360,13 @@ pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32)
354360
node.chain_monitor.chain_monitor.blocks_disconnected(best_block);
355361
Listen::blocks_disconnected(node.node, best_block);
356362
},
363+
ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
364+
if i == count - 1 {
365+
let best_block = BestBlock::new(orig.0.header.prev_blockhash, orig.1 - 1);
366+
node.chain_monitor.chain_monitor.blocks_disconnected(best_block);
367+
Listen::blocks_disconnected(node.node, best_block);
368+
}
369+
},
357370
ConnectStyle::BestBlockFirstSkippingBlocks|ConnectStyle::TransactionsFirstSkippingBlocks|
358371
ConnectStyle::HighlyRedundantTransactionsFirstSkippingBlocks|ConnectStyle::TransactionsDuplicativelyFirstSkippingBlocks => {
359372
if i == count - 1 {

lightning/src/ln/functional_tests.rs

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

2748-
if *nodes[1].connect_style.borrow() == ConnectStyle::FullBlockViaListen {
2749-
// We rely on the ability to connect a block redundantly, which isn't allowed via
2750-
// `chain::Listen`, so we never run the test if we randomly get assigned that
2751-
// connect_style.
2752-
return;
2748+
match *nodes[1].connect_style.borrow() {
2749+
ConnectStyle::FullBlockViaListen
2750+
| ConnectStyle::FullBlockDisconnectionsSkippingViaListen => {
2751+
// We rely on the ability to connect a block redundantly, which isn't allowed via
2752+
// `chain::Listen`, so we never run the test if we randomly get assigned that
2753+
// connect_style.
2754+
return;
2755+
},
2756+
_ => {},
27532757
}
27542758
let funding_tx = create_announced_chan_between_nodes(&nodes, 0, 1).3;
27552759
let error_message = "Channel force-closed";

0 commit comments

Comments
 (0)