Skip to content

Commit 3654169

Browse files
committed
Expand tests to cover serialization with pending background events
1 parent 4abedbb commit 3654169

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

lightning/src/ln/reorg_tests.rs

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99

1010
//! Further functional tests which test blockchain reorganizations.
1111
12-
use chain::channelmonitor::ANTI_REORG_DELAY;
12+
use chain::channelmonitor::{ANTI_REORG_DELAY, ChannelMonitor};
13+
use chain::Watch;
14+
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs};
1315
use ln::features::InitFeatures;
1416
use ln::msgs::{ChannelMessageHandler, ErrorAction, HTLCFailChannelUpdate};
17+
use util::config::UserConfig;
18+
use util::enforcing_trait_impls::EnforcingSigner;
1519
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
20+
use util::test_utils;
21+
use util::ser::{ReadableArgs, Writeable};
1622

1723
use bitcoin::blockdata::block::{Block, BlockHeader};
24+
use bitcoin::hash_types::BlockHash;
1825

19-
use std::default::Default;
26+
use std::collections::HashMap;
2027
use std::mem;
2128

2229
use ln::functional_test_utils::*;
@@ -182,13 +189,17 @@ fn test_onchain_htlc_timeout_delay_remote_commitment() {
182189
do_test_onchain_htlc_reorg(false, false);
183190
}
184191

185-
#[test]
186-
fn test_unconf_chan() {
187-
// After creating a chan between nodes, we disconnect all blocks previously seen to force a channel close on nodes[0] side
192+
fn do_test_unconf_chan(reload_node: bool) {
193+
// After creating a chan between nodes, we disconnect all blocks previously seen to force a
194+
// channel close on nodes[0] side. We also use this to provide very basic testing of logic
195+
// around freeing background events which store monitor updates during block_[dis]connected.
188196
let chanmon_cfgs = create_chanmon_cfgs(2);
189197
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
190198
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
191-
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
199+
let persister: test_utils::TestPersister;
200+
let new_chain_monitor: test_utils::TestChainMonitor;
201+
let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
202+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
192203
create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
193204

194205
let channel_state = nodes[0].node.channel_state.lock().unwrap();
@@ -207,11 +218,60 @@ fn test_unconf_chan() {
207218
nodes[0].node.block_disconnected(&headers.pop().unwrap());
208219
}
209220
check_closed_broadcast!(nodes[0], false);
221+
{
222+
let channel_state = nodes[0].node.channel_state.lock().unwrap();
223+
assert_eq!(channel_state.by_id.len(), 0);
224+
assert_eq!(channel_state.short_to_id.len(), 0);
225+
}
226+
227+
if reload_node {
228+
// Since we currently have a background event pending, it's good to test that we survive a
229+
// serialization roundtrip. Further, this tests the somewhat awkward edge-case of dropping
230+
// the Channel object from the ChannelManager, but still having a monitor event pending for
231+
// it when we go to deserialize, and then use the ChannelManager.
232+
let nodes_0_serialized = nodes[0].node.encode();
233+
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
234+
nodes[0].chain_monitor.chain_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write(&mut chan_0_monitor_serialized).unwrap();
235+
236+
persister = test_utils::TestPersister::new();
237+
let keys_manager = &chanmon_cfgs[0].keys_manager;
238+
new_chain_monitor = test_utils::TestChainMonitor::new(Some(nodes[0].chain_source), nodes[0].tx_broadcaster.clone(), nodes[0].logger, node_cfgs[0].fee_estimator, &persister, keys_manager);
239+
nodes[0].chain_monitor = &new_chain_monitor;
240+
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
241+
let (_, mut chan_0_monitor) = <(BlockHash, ChannelMonitor<EnforcingSigner>)>::read(
242+
&mut chan_0_monitor_read, keys_manager).unwrap();
243+
assert!(chan_0_monitor_read.is_empty());
244+
245+
let mut nodes_0_read = &nodes_0_serialized[..];
246+
let config = UserConfig::default();
247+
nodes_0_deserialized = {
248+
let mut channel_monitors = HashMap::new();
249+
channel_monitors.insert(chan_0_monitor.get_funding_txo().0, &mut chan_0_monitor);
250+
<(BlockHash, ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>)>::read(&mut nodes_0_read, ChannelManagerReadArgs {
251+
default_config: config,
252+
keys_manager,
253+
fee_estimator: node_cfgs[0].fee_estimator,
254+
chain_monitor: nodes[0].chain_monitor,
255+
tx_broadcaster: nodes[0].tx_broadcaster.clone(),
256+
logger: nodes[0].logger,
257+
channel_monitors,
258+
}).unwrap().1
259+
};
260+
nodes[0].node = &nodes_0_deserialized;
261+
assert!(nodes_0_read.is_empty());
262+
263+
nodes[0].chain_monitor.watch_channel(chan_0_monitor.get_funding_txo().0.clone(), chan_0_monitor).unwrap();
264+
check_added_monitors!(nodes[0], 1);
265+
}
266+
210267
nodes[0].node.test_process_background_events(); // Required to free the pending background monitor update
211268
check_added_monitors!(nodes[0], 1);
212-
let channel_state = nodes[0].node.channel_state.lock().unwrap();
213-
assert_eq!(channel_state.by_id.len(), 0);
214-
assert_eq!(channel_state.short_to_id.len(), 0);
269+
}
270+
271+
#[test]
272+
fn test_unconf_chan() {
273+
do_test_unconf_chan(true);
274+
do_test_unconf_chan(false);
215275
}
216276

217277
#[test]

0 commit comments

Comments
 (0)