Skip to content

Commit 107c6c7

Browse files
authored
Merge pull request #1108 from TheBlueMatt/2021-10-persist-mon-blocks
Persist ChannelMonitors after new blocks are connected
2 parents 083828a + 6fb5bd3 commit 107c6c7

16 files changed

+627
-238
lines changed

fuzz/src/chanmon_consistency.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -855,22 +855,26 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
855855

856856
0x08 => {
857857
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
858-
nodes[0].channel_monitor_updated(&chan_1_funding, *id);
858+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
859+
nodes[0].process_monitor_events();
859860
}
860861
},
861862
0x09 => {
862863
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
863-
nodes[1].channel_monitor_updated(&chan_1_funding, *id);
864+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
865+
nodes[1].process_monitor_events();
864866
}
865867
},
866868
0x0a => {
867869
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
868-
nodes[1].channel_monitor_updated(&chan_2_funding, *id);
870+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
871+
nodes[1].process_monitor_events();
869872
}
870873
},
871874
0x0b => {
872875
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
873-
nodes[2].channel_monitor_updated(&chan_2_funding, *id);
876+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
877+
nodes[2].process_monitor_events();
874878
}
875879
},
876880

@@ -1071,22 +1075,26 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
10711075
// Test that no channel is in a stuck state where neither party can send funds even
10721076
// after we resolve all pending events.
10731077
// First make sure there are no pending monitor updates, resetting the error state
1074-
// and calling channel_monitor_updated for each monitor.
1078+
// and calling force_channel_monitor_updated for each monitor.
10751079
*monitor_a.persister.update_ret.lock().unwrap() = Ok(());
10761080
*monitor_b.persister.update_ret.lock().unwrap() = Ok(());
10771081
*monitor_c.persister.update_ret.lock().unwrap() = Ok(());
10781082

10791083
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1080-
nodes[0].channel_monitor_updated(&chan_1_funding, *id);
1084+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1085+
nodes[0].process_monitor_events();
10811086
}
10821087
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1083-
nodes[1].channel_monitor_updated(&chan_1_funding, *id);
1088+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
1089+
nodes[1].process_monitor_events();
10841090
}
10851091
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1086-
nodes[1].channel_monitor_updated(&chan_2_funding, *id);
1092+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1093+
nodes[1].process_monitor_events();
10871094
}
10881095
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1089-
nodes[2].channel_monitor_updated(&chan_2_funding, *id);
1096+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
1097+
nodes[2].process_monitor_events();
10901098
}
10911099

10921100
// Next, make sure peers are all connected to each other

fuzz/src/utils/test_persister.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use lightning::chain;
22
use lightning::chain::{chainmonitor, channelmonitor};
3+
use lightning::chain::chainmonitor::MonitorUpdateId;
34
use lightning::chain::transaction::OutPoint;
45
use lightning::util::enforcing_trait_impls::EnforcingSigner;
56

@@ -9,11 +10,11 @@ pub struct TestPersister {
910
pub update_ret: Mutex<Result<(), chain::ChannelMonitorUpdateErr>>,
1011
}
1112
impl chainmonitor::Persist<EnforcingSigner> for TestPersister {
12-
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
13+
fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
1314
self.update_ret.lock().unwrap().clone()
1415
}
1516

16-
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: &channelmonitor::ChannelMonitorUpdate, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
17+
fn update_persisted_channel(&self, _funding_txo: OutPoint, _update: &Option<channelmonitor::ChannelMonitorUpdate>, _data: &channelmonitor::ChannelMonitor<EnforcingSigner>, _update_id: MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
1718
self.update_ret.lock().unwrap().clone()
1819
}
1920
}

lightning-persister/src/lib.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,18 @@ impl FilesystemPersister {
159159
}
160160

161161
impl<ChannelSigner: Sign> chainmonitor::Persist<ChannelSigner> for FilesystemPersister {
162-
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
162+
// TODO: We really need a way for the persister to inform the user that its time to crash/shut
163+
// down once these start returning failure.
164+
// A PermanentFailure implies we need to shut down since we're force-closing channels without
165+
// even broadcasting!
166+
167+
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
163168
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
164169
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
165170
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
166171
}
167172

168-
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
173+
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &Option<ChannelMonitorUpdate>, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
169174
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
170175
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
171176
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
@@ -296,6 +301,8 @@ mod tests {
296301
nodes[1].node.force_close_channel(&chan.2).unwrap();
297302
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
298303
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
304+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
305+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
299306

300307
// Set the persister's directory to read-only, which should result in
301308
// returning a permanent failure when we then attempt to persist a
@@ -309,7 +316,7 @@ mod tests {
309316
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
310317
index: 0
311318
};
312-
match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
319+
match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
313320
Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
314321
_ => panic!("unexpected result from persisting new channel")
315322
}
@@ -333,6 +340,8 @@ mod tests {
333340
nodes[1].node.force_close_channel(&chan.2).unwrap();
334341
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
335342
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
343+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
344+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
336345

337346
// Create the persister with an invalid directory name and test that the
338347
// channel fails to open because the directories fail to be created. There
@@ -344,7 +353,7 @@ mod tests {
344353
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
345354
index: 0
346355
};
347-
match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
356+
match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
348357
Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
349358
_ => panic!("unexpected result from persisting new channel")
350359
}

0 commit comments

Comments
 (0)