Skip to content

Commit f3ce579

Browse files
Deterministic reconstruct_manager option in tests
We recently merged (test-only, for now) support for the ChannelManager reconstructing its set of pending HTLCs from Channel{Monitor} data, rather than using its own persisted maps. But because we want test coverage of both the new reconstruction codepaths as well as the old persisted map codepaths, in tests we would decide between those two sets of codepaths randomly. We now want to add some tests that require using the new codepaths, so here we add an option to explicitly set whether to reconstruct or not rather than choosing randomly.
1 parent bd312b8 commit f3ce579

3 files changed

Lines changed: 43 additions & 22 deletions

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17683,6 +17683,15 @@ pub struct ChannelManagerReadArgs<
1768317683
///
1768417684
/// This is not exported to bindings users because we have no HashMap bindings
1768517685
pub channel_monitors: HashMap<ChannelId, &'a ChannelMonitor<SP::EcdsaSigner>>,
17686+
17687+
/// Whether the `ChannelManager` should attempt to reconstruct its set of pending HTLCs from
17688+
/// `Channel{Monitor}` data rather than its own persisted maps, which is planned to become
17689+
/// the default behavior in upcoming versions.
17690+
///
17691+
/// If `None`, whether we reconstruct or use the legacy maps will be decided randomly during
17692+
/// `ChannelManager::from_channel_manager_data`.
17693+
#[cfg(test)]
17694+
pub reconstruct_manager_from_monitors: Option<bool>,
1768617695
}
1768717696

1768817697
impl<
@@ -17720,6 +17729,8 @@ impl<
1772017729
channel_monitors: hash_map_from_iter(
1772117730
channel_monitors.drain(..).map(|monitor| (monitor.channel_id(), monitor)),
1772217731
),
17732+
#[cfg(test)]
17733+
reconstruct_manager_from_monitors: None,
1772317734
}
1772417735
}
1772517736
}
@@ -18409,26 +18420,30 @@ impl<
1840918420
#[cfg(not(test))]
1841018421
let reconstruct_manager_from_monitors = false;
1841118422
#[cfg(test)]
18412-
let reconstruct_manager_from_monitors = {
18413-
use core::hash::{BuildHasher, Hasher};
18414-
18415-
match std::env::var("LDK_TEST_REBUILD_MGR_FROM_MONITORS") {
18416-
Ok(val) => match val.as_str() {
18417-
"1" => true,
18418-
"0" => false,
18419-
_ => panic!("LDK_TEST_REBUILD_MGR_FROM_MONITORS must be 0 or 1, got: {}", val),
18420-
},
18421-
Err(_) => {
18422-
let rand_val =
18423-
std::collections::hash_map::RandomState::new().build_hasher().finish();
18424-
if rand_val % 2 == 0 {
18425-
true
18426-
} else {
18427-
false
18428-
}
18429-
},
18430-
}
18431-
};
18423+
let reconstruct_manager_from_monitors =
18424+
args.reconstruct_manager_from_monitors.unwrap_or_else(|| {
18425+
use core::hash::{BuildHasher, Hasher};
18426+
18427+
match std::env::var("LDK_TEST_REBUILD_MGR_FROM_MONITORS") {
18428+
Ok(val) => match val.as_str() {
18429+
"1" => true,
18430+
"0" => false,
18431+
_ => panic!(
18432+
"LDK_TEST_REBUILD_MGR_FROM_MONITORS must be 0 or 1, got: {}",
18433+
val
18434+
),
18435+
},
18436+
Err(_) => {
18437+
let rand_val =
18438+
std::collections::hash_map::RandomState::new().build_hasher().finish();
18439+
if rand_val % 2 == 0 {
18440+
true
18441+
} else {
18442+
false
18443+
}
18444+
},
18445+
}
18446+
});
1843218447

1843318448
// If there's any preimages for forwarded HTLCs hanging around in ChannelMonitors we
1843418449
// should ensure we try them again on the inbound edge. We put them here and do so after we

lightning/src/ln/functional_test_utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,8 @@ impl<'a, 'b, 'c> Drop for Node<'a, 'b, 'c> {
909909
tx_broadcaster: &broadcaster,
910910
logger: &self.logger,
911911
channel_monitors,
912+
#[cfg(test)]
913+
reconstruct_manager_from_monitors: None,
912914
},
913915
)
914916
.unwrap();
@@ -1307,7 +1309,7 @@ fn check_claimed_htlcs_match_route<'a, 'b, 'c>(
13071309

13081310
pub fn _reload_node<'a, 'b, 'c>(
13091311
node: &'a Node<'a, 'b, 'c>, config: UserConfig, chanman_encoded: &[u8],
1310-
monitors_encoded: &[&[u8]],
1312+
monitors_encoded: &[&[u8]], _reconstruct_manager_from_monitors: Option<bool>,
13111313
) -> TestChannelManager<'b, 'c> {
13121314
let mut monitors_read = Vec::with_capacity(monitors_encoded.len());
13131315
for encoded in monitors_encoded {
@@ -1341,6 +1343,8 @@ pub fn _reload_node<'a, 'b, 'c>(
13411343
tx_broadcaster: node.tx_broadcaster,
13421344
logger: node.logger,
13431345
channel_monitors,
1346+
#[cfg(test)]
1347+
reconstruct_manager_from_monitors: _reconstruct_manager_from_monitors,
13441348
},
13451349
)
13461350
.unwrap()
@@ -1376,7 +1380,7 @@ macro_rules! reload_node {
13761380
$node.chain_monitor = &$new_chain_monitor;
13771381

13781382
$new_channelmanager =
1379-
_reload_node(&$node, $new_config, &chanman_encoded, $monitors_encoded);
1383+
_reload_node(&$node, $new_config, &chanman_encoded, $monitors_encoded, None);
13801384
$node.node = &$new_channelmanager;
13811385
$node.onion_messenger.set_offers_handler(&$new_channelmanager);
13821386
$node.onion_messenger.set_async_payments_handler(&$new_channelmanager);

lightning/src/ln/reload_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
437437
tx_broadcaster: nodes[0].tx_broadcaster,
438438
logger: &logger,
439439
channel_monitors: node_0_stale_monitors.iter().map(|monitor| { (monitor.channel_id(), monitor) }).collect(),
440+
reconstruct_manager_from_monitors: None,
440441
}) { } else {
441442
panic!("If the monitor(s) are stale, this indicates a bug and we should get an Err return");
442443
};
@@ -455,6 +456,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
455456
tx_broadcaster: nodes[0].tx_broadcaster,
456457
logger: &logger,
457458
channel_monitors: node_0_monitors.iter().map(|monitor| { (monitor.channel_id(), monitor) }).collect(),
459+
reconstruct_manager_from_monitors: None,
458460
}).unwrap();
459461
nodes_0_deserialized = nodes_0_deserialized_tmp;
460462
assert!(nodes_0_read.is_empty());

0 commit comments

Comments
 (0)