Skip to content

Commit 89ad059

Browse files
committed
Use an opaque type to describe monitor updates in Persist
In the next commit, we'll be originating monitor updates both from the ChainMonitor and from the ChannelManager, making simple sequential update IDs impossible. Further, the existing async monitor update API was somewhat hard to work with - instead of being able to generate monitor_updated callbacks whenever a persistence process finishes, you had to ensure you only did so at least once all previous updates had also been persisted. Here we eat the complexity for the user by moving to an opaque type for monitor updates, tracking which updates are in-flight for the user and only generating monitor-persisted events once all pending updates have been committed.
1 parent 4500270 commit 89ad059

File tree

6 files changed

+206
-103
lines changed

6 files changed

+206
-103
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -855,25 +855,25 @@ 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-
monitor_a.chain_monitor.channel_monitor_updated(chan_1_funding, *id);
858+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
859859
nodes[0].process_monitor_events();
860860
}
861861
},
862862
0x09 => {
863863
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
864-
monitor_b.chain_monitor.channel_monitor_updated(chan_1_funding, *id);
864+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
865865
nodes[1].process_monitor_events();
866866
}
867867
},
868868
0x0a => {
869869
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
870-
monitor_b.chain_monitor.channel_monitor_updated(chan_2_funding, *id);
870+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
871871
nodes[1].process_monitor_events();
872872
}
873873
},
874874
0x0b => {
875875
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
876-
monitor_c.chain_monitor.channel_monitor_updated(chan_2_funding, *id);
876+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
877877
nodes[2].process_monitor_events();
878878
}
879879
},
@@ -1075,25 +1075,25 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
10751075
// Test that no channel is in a stuck state where neither party can send funds even
10761076
// after we resolve all pending events.
10771077
// First make sure there are no pending monitor updates, resetting the error state
1078-
// and calling channel_monitor_updated for each monitor.
1078+
// and calling force_channel_monitor_updated for each monitor.
10791079
*monitor_a.persister.update_ret.lock().unwrap() = Ok(());
10801080
*monitor_b.persister.update_ret.lock().unwrap() = Ok(());
10811081
*monitor_c.persister.update_ret.lock().unwrap() = Ok(());
10821082

10831083
if let Some((id, _)) = monitor_a.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1084-
monitor_a.chain_monitor.channel_monitor_updated(chan_1_funding, *id);
1084+
monitor_a.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
10851085
nodes[0].process_monitor_events();
10861086
}
10871087
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_1_funding) {
1088-
monitor_b.chain_monitor.channel_monitor_updated(chan_1_funding, *id);
1088+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_1_funding, *id);
10891089
nodes[1].process_monitor_events();
10901090
}
10911091
if let Some((id, _)) = monitor_b.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1092-
monitor_b.chain_monitor.channel_monitor_updated(chan_2_funding, *id);
1092+
monitor_b.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
10931093
nodes[1].process_monitor_events();
10941094
}
10951095
if let Some((id, _)) = monitor_c.latest_monitors.lock().unwrap().get(&chan_2_funding) {
1096-
monitor_c.chain_monitor.channel_monitor_updated(chan_2_funding, *id);
1096+
monitor_c.chain_monitor.force_channel_monitor_updated(chan_2_funding, *id);
10971097
nodes[2].process_monitor_events();
10981098
}
10991099

fuzz/src/utils/test_persister.rs

Lines changed: 3 additions & 2 deletions
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: &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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ 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+
fn persist_new_channel(&self, funding_txo: OutPoint, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
163163
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
164164
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
165165
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
166166
}
167167

168-
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChannelSigner>) -> Result<(), chain::ChannelMonitorUpdateErr> {
168+
fn update_persisted_channel(&self, funding_txo: OutPoint, _update: &ChannelMonitorUpdate, monitor: &ChannelMonitor<ChannelSigner>, _update_id: chainmonitor::MonitorUpdateId) -> Result<(), chain::ChannelMonitorUpdateErr> {
169169
let filename = format!("{}_{}", funding_txo.txid.to_hex(), funding_txo.index);
170170
util::write_to_file(self.path_to_monitor_data(), filename, monitor)
171171
.map_err(|_| chain::ChannelMonitorUpdateErr::PermanentFailure)
@@ -296,6 +296,8 @@ mod tests {
296296
nodes[1].node.force_close_channel(&chan.2).unwrap();
297297
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
298298
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
299+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
300+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
299301

300302
// Set the persister's directory to read-only, which should result in
301303
// returning a permanent failure when we then attempt to persist a
@@ -309,7 +311,7 @@ mod tests {
309311
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
310312
index: 0
311313
};
312-
match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
314+
match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
313315
Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
314316
_ => panic!("unexpected result from persisting new channel")
315317
}
@@ -333,6 +335,8 @@ mod tests {
333335
nodes[1].node.force_close_channel(&chan.2).unwrap();
334336
check_closed_event!(nodes[1], 1, ClosureReason::HolderForceClosed);
335337
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
338+
let update_map = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap();
339+
let update_id = update_map.get(&added_monitors[0].0.to_channel_id()).unwrap();
336340

337341
// Create the persister with an invalid directory name and test that the
338342
// channel fails to open because the directories fail to be created. There
@@ -344,7 +348,7 @@ mod tests {
344348
txid: Txid::from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be").unwrap(),
345349
index: 0
346350
};
347-
match persister.persist_new_channel(test_txo, &added_monitors[0].1) {
351+
match persister.persist_new_channel(test_txo, &added_monitors[0].1, update_id.2) {
348352
Err(ChannelMonitorUpdateErr::PermanentFailure) => {},
349353
_ => panic!("unexpected result from persisting new channel")
350354
}

0 commit comments

Comments
 (0)