Skip to content

Commit 9b66e2c

Browse files
authored
Merge pull request lightningdevkit#3554 from wpaulino/channel-monitor-by-channel-id
Start tracking ChannelMonitors by channel ID in ChainMonitor and ChannelManager
2 parents 1434e9c + 717db82 commit 9b66e2c

16 files changed

+499
-469
lines changed

fuzz/src/chanmon_consistency.rs

+48-44
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct TestChainMonitor {
189189
Arc<TestPersister>,
190190
>,
191191
>,
192-
pub latest_monitors: Mutex<HashMap<OutPoint, LatestMonitorState>>,
192+
pub latest_monitors: Mutex<HashMap<ChannelId, LatestMonitorState>>,
193193
}
194194
impl TestChainMonitor {
195195
pub fn new(
@@ -213,12 +213,12 @@ impl TestChainMonitor {
213213
}
214214
impl chain::Watch<TestChannelSigner> for TestChainMonitor {
215215
fn watch_channel(
216-
&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>,
216+
&self, channel_id: ChannelId, monitor: channelmonitor::ChannelMonitor<TestChannelSigner>,
217217
) -> Result<chain::ChannelMonitorUpdateStatus, ()> {
218218
let mut ser = VecWriter(Vec::new());
219219
monitor.write(&mut ser).unwrap();
220220
let monitor_id = monitor.get_latest_update_id();
221-
let res = self.chain_monitor.watch_channel(funding_txo, monitor);
221+
let res = self.chain_monitor.watch_channel(channel_id, monitor);
222222
let state = match res {
223223
Ok(chain::ChannelMonitorUpdateStatus::Completed) => LatestMonitorState {
224224
persisted_monitor_id: monitor_id,
@@ -231,17 +231,17 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
231231
Ok(chain::ChannelMonitorUpdateStatus::UnrecoverableError) => panic!(),
232232
Err(()) => panic!(),
233233
};
234-
if self.latest_monitors.lock().unwrap().insert(funding_txo, state).is_some() {
234+
if self.latest_monitors.lock().unwrap().insert(channel_id, state).is_some() {
235235
panic!("Already had monitor pre-watch_channel");
236236
}
237237
res
238238
}
239239

240240
fn update_channel(
241-
&self, funding_txo: OutPoint, update: &channelmonitor::ChannelMonitorUpdate,
241+
&self, channel_id: ChannelId, update: &channelmonitor::ChannelMonitorUpdate,
242242
) -> chain::ChannelMonitorUpdateStatus {
243243
let mut map_lock = self.latest_monitors.lock().unwrap();
244-
let map_entry = map_lock.get_mut(&funding_txo).expect("Didn't have monitor on update call");
244+
let map_entry = map_lock.get_mut(&channel_id).expect("Didn't have monitor on update call");
245245
let latest_monitor_data = map_entry
246246
.pending_monitors
247247
.last()
@@ -265,7 +265,7 @@ impl chain::Watch<TestChannelSigner> for TestChainMonitor {
265265
.unwrap();
266266
let mut ser = VecWriter(Vec::new());
267267
deserialized_monitor.write(&mut ser).unwrap();
268-
let res = self.chain_monitor.update_channel(funding_txo, update);
268+
let res = self.chain_monitor.update_channel(channel_id, update);
269269
match res {
270270
chain::ChannelMonitorUpdateStatus::Completed => {
271271
map_entry.persisted_monitor_id = update.update_id;
@@ -711,9 +711,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
711711

712712
let mut monitors = new_hash_map();
713713
let mut old_monitors = $old_monitors.latest_monitors.lock().unwrap();
714-
for (outpoint, mut prev_state) in old_monitors.drain() {
714+
for (channel_id, mut prev_state) in old_monitors.drain() {
715715
monitors.insert(
716-
outpoint,
716+
channel_id,
717717
<(BlockHash, ChannelMonitor<TestChannelSigner>)>::read(
718718
&mut Cursor::new(&prev_state.persisted_monitor),
719719
(&*$keys_manager, &*$keys_manager),
@@ -725,11 +725,11 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
725725
// considering them discarded. LDK should replay these for us as they're stored in
726726
// the `ChannelManager`.
727727
prev_state.pending_monitors.clear();
728-
chain_monitor.latest_monitors.lock().unwrap().insert(outpoint, prev_state);
728+
chain_monitor.latest_monitors.lock().unwrap().insert(channel_id, prev_state);
729729
}
730730
let mut monitor_refs = new_hash_map();
731-
for (outpoint, monitor) in monitors.iter() {
732-
monitor_refs.insert(*outpoint, monitor);
731+
for (channel_id, monitor) in monitors.iter() {
732+
monitor_refs.insert(*channel_id, monitor);
733733
}
734734

735735
let read_args = ChannelManagerReadArgs {
@@ -752,9 +752,9 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
752752
.1,
753753
chain_monitor.clone(),
754754
);
755-
for (funding_txo, mon) in monitors.drain() {
755+
for (channel_id, mon) in monitors.drain() {
756756
assert_eq!(
757-
chain_monitor.chain_monitor.watch_channel(funding_txo, mon),
757+
chain_monitor.chain_monitor.watch_channel(channel_id, mon),
758758
Ok(ChannelMonitorUpdateStatus::Completed)
759759
);
760760
}
@@ -825,7 +825,6 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
825825
};
826826

827827
$source.handle_accept_channel($dest.get_our_node_id(), &accept_channel);
828-
let funding_output;
829828
{
830829
let mut events = $source.get_and_clear_pending_events();
831830
assert_eq!(events.len(), 1);
@@ -845,7 +844,6 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
845844
script_pubkey: output_script,
846845
}],
847846
};
848-
funding_output = OutPoint { txid: tx.compute_txid(), index: 0 };
849847
$source
850848
.funding_transaction_generated(
851849
temporary_channel_id,
@@ -890,13 +888,19 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
890888
$source.handle_funding_signed($dest.get_our_node_id(), &funding_signed);
891889
let events = $source.get_and_clear_pending_events();
892890
assert_eq!(events.len(), 1);
893-
if let events::Event::ChannelPending { ref counterparty_node_id, .. } = events[0] {
891+
let channel_id = if let events::Event::ChannelPending {
892+
ref counterparty_node_id,
893+
ref channel_id,
894+
..
895+
} = events[0]
896+
{
894897
assert_eq!(counterparty_node_id, &$dest.get_our_node_id());
898+
channel_id.clone()
895899
} else {
896900
panic!("Wrong event type");
897-
}
901+
};
898902

899-
funding_output
903+
channel_id
900904
}};
901905
}
902906

@@ -963,8 +967,8 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
963967

964968
let mut nodes = [node_a, node_b, node_c];
965969

966-
let chan_1_funding = make_channel!(nodes[0], nodes[1], keys_manager_b, 0);
967-
let chan_2_funding = make_channel!(nodes[1], nodes[2], keys_manager_c, 1);
970+
let chan_1_id = make_channel!(nodes[0], nodes[1], keys_manager_b, 0);
971+
let chan_2_id = make_channel!(nodes[1], nodes[2], keys_manager_c, 1);
968972

969973
for node in nodes.iter() {
970974
confirm_txn!(node);
@@ -1363,14 +1367,14 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
13631367
}
13641368
};
13651369

1366-
let complete_all_monitor_updates = |monitor: &Arc<TestChainMonitor>, chan_funding| {
1367-
if let Some(state) = monitor.latest_monitors.lock().unwrap().get_mut(chan_funding) {
1370+
let complete_all_monitor_updates = |monitor: &Arc<TestChainMonitor>, chan_id| {
1371+
if let Some(state) = monitor.latest_monitors.lock().unwrap().get_mut(chan_id) {
13681372
assert!(
13691373
state.pending_monitors.windows(2).all(|pair| pair[0].0 < pair[1].0),
13701374
"updates should be sorted by id"
13711375
);
13721376
for (id, data) in state.pending_monitors.drain(..) {
1373-
monitor.chain_monitor.channel_monitor_updated(*chan_funding, id).unwrap();
1377+
monitor.chain_monitor.channel_monitor_updated(*chan_id, id).unwrap();
13741378
if id > state.persisted_monitor_id {
13751379
state.persisted_monitor_id = id;
13761380
state.persisted_monitor = data;
@@ -1410,10 +1414,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
14101414
ChannelMonitorUpdateStatus::Completed
14111415
},
14121416

1413-
0x08 => complete_all_monitor_updates(&monitor_a, &chan_1_funding),
1414-
0x09 => complete_all_monitor_updates(&monitor_b, &chan_1_funding),
1415-
0x0a => complete_all_monitor_updates(&monitor_b, &chan_2_funding),
1416-
0x0b => complete_all_monitor_updates(&monitor_c, &chan_2_funding),
1417+
0x08 => complete_all_monitor_updates(&monitor_a, &chan_1_id),
1418+
0x09 => complete_all_monitor_updates(&monitor_b, &chan_1_id),
1419+
0x0a => complete_all_monitor_updates(&monitor_b, &chan_2_id),
1420+
0x0b => complete_all_monitor_updates(&monitor_c, &chan_2_id),
14171421

14181422
0x0c => {
14191423
if !chan_a_disconnected {
@@ -1683,21 +1687,21 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
16831687
nodes[2].maybe_update_chan_fees();
16841688
},
16851689

1686-
0xf0 => complete_monitor_update(&monitor_a, &chan_1_funding, &complete_first),
1687-
0xf1 => complete_monitor_update(&monitor_a, &chan_1_funding, &complete_second),
1688-
0xf2 => complete_monitor_update(&monitor_a, &chan_1_funding, &Vec::pop),
1690+
0xf0 => complete_monitor_update(&monitor_a, &chan_1_id, &complete_first),
1691+
0xf1 => complete_monitor_update(&monitor_a, &chan_1_id, &complete_second),
1692+
0xf2 => complete_monitor_update(&monitor_a, &chan_1_id, &Vec::pop),
16891693

1690-
0xf4 => complete_monitor_update(&monitor_b, &chan_1_funding, &complete_first),
1691-
0xf5 => complete_monitor_update(&monitor_b, &chan_1_funding, &complete_second),
1692-
0xf6 => complete_monitor_update(&monitor_b, &chan_1_funding, &Vec::pop),
1694+
0xf4 => complete_monitor_update(&monitor_b, &chan_1_id, &complete_first),
1695+
0xf5 => complete_monitor_update(&monitor_b, &chan_1_id, &complete_second),
1696+
0xf6 => complete_monitor_update(&monitor_b, &chan_1_id, &Vec::pop),
16931697

1694-
0xf8 => complete_monitor_update(&monitor_b, &chan_2_funding, &complete_first),
1695-
0xf9 => complete_monitor_update(&monitor_b, &chan_2_funding, &complete_second),
1696-
0xfa => complete_monitor_update(&monitor_b, &chan_2_funding, &Vec::pop),
1698+
0xf8 => complete_monitor_update(&monitor_b, &chan_2_id, &complete_first),
1699+
0xf9 => complete_monitor_update(&monitor_b, &chan_2_id, &complete_second),
1700+
0xfa => complete_monitor_update(&monitor_b, &chan_2_id, &Vec::pop),
16971701

1698-
0xfc => complete_monitor_update(&monitor_c, &chan_2_funding, &complete_first),
1699-
0xfd => complete_monitor_update(&monitor_c, &chan_2_funding, &complete_second),
1700-
0xfe => complete_monitor_update(&monitor_c, &chan_2_funding, &Vec::pop),
1702+
0xfc => complete_monitor_update(&monitor_c, &chan_2_id, &complete_first),
1703+
0xfd => complete_monitor_update(&monitor_c, &chan_2_id, &complete_second),
1704+
0xfe => complete_monitor_update(&monitor_c, &chan_2_id, &Vec::pop),
17011705

17021706
0xff => {
17031707
// Test that no channel is in a stuck state where neither party can send funds even
@@ -1711,10 +1715,10 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
17111715
*monitor_c.persister.update_ret.lock().unwrap() =
17121716
ChannelMonitorUpdateStatus::Completed;
17131717

1714-
complete_all_monitor_updates(&monitor_a, &chan_1_funding);
1715-
complete_all_monitor_updates(&monitor_b, &chan_1_funding);
1716-
complete_all_monitor_updates(&monitor_b, &chan_2_funding);
1717-
complete_all_monitor_updates(&monitor_c, &chan_2_funding);
1718+
complete_all_monitor_updates(&monitor_a, &chan_1_id);
1719+
complete_all_monitor_updates(&monitor_b, &chan_1_id);
1720+
complete_all_monitor_updates(&monitor_b, &chan_2_id);
1721+
complete_all_monitor_updates(&monitor_c, &chan_2_id);
17181722

17191723
// Next, make sure peers are all connected to each other
17201724
if chan_a_disconnected {

lightning-block-sync/src/init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ where
125125
///
126126
/// // Allow the chain monitor to watch any channels.
127127
/// let monitor = monitor_listener.0;
128-
/// chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
128+
/// chain_monitor.watch_channel(monitor.channel_id(), monitor);
129129
///
130130
/// // Create an SPV client to notify the chain monitor and channel manager of block events.
131131
/// let chain_poller = poll::ChainPoller::new(block_source, Network::Bitcoin);

0 commit comments

Comments
 (0)