Skip to content

Commit 8c76305

Browse files
committed
Switch to (beta) async persistence of ChannelMonitors
1 parent 614cbb7 commit 8c76305

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

src/main.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ use lightning::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
4040
use lightning::util::config::UserConfig;
4141
use lightning::util::hash_tables::hash_map::Entry;
4242
use lightning::util::hash_tables::HashMap;
43+
use lightning::util::native_async::FutureSpawner;
4344
use lightning::util::persist::{
44-
self, KVStore, MonitorUpdatingPersister, OUTPUT_SWEEPER_PERSISTENCE_KEY,
45+
self, KVStore, MonitorUpdatingPersisterAsync, OUTPUT_SWEEPER_PERSISTENCE_KEY,
4546
OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE, OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE,
4647
};
4748
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
@@ -59,6 +60,7 @@ use std::convert::TryInto;
5960
use std::fmt;
6061
use std::fs;
6162
use std::fs::File;
63+
use std::future::Future;
6264
use std::io::{BufReader, Write};
6365
use std::net::ToSocketAddrs;
6466
use std::path::Path;
@@ -139,15 +141,14 @@ type ChainMonitor = chainmonitor::ChainMonitor<
139141
Arc<BitcoindClient>,
140142
Arc<BitcoindClient>,
141143
Arc<FilesystemLogger>,
142-
Arc<
143-
MonitorUpdatingPersister<
144-
Arc<FilesystemStore>,
145-
Arc<FilesystemLogger>,
146-
Arc<KeysManager>,
147-
Arc<KeysManager>,
148-
Arc<BitcoindClient>,
149-
Arc<BitcoindClient>,
150-
>,
144+
chainmonitor::AsyncPersister<
145+
Arc<FilesystemStore>,
146+
TokioSpawner,
147+
Arc<FilesystemLogger>,
148+
Arc<KeysManager>,
149+
Arc<KeysManager>,
150+
Arc<BitcoindClient>,
151+
Arc<BitcoindClient>,
151152
>,
152153
Arc<KeysManager>,
153154
>;
@@ -210,6 +211,14 @@ pub(crate) type OutputSweeper = ldk_sweep::OutputSweeper<
210211
// Needed due to rust-lang/rust#63033.
211212
struct OutputSweeperWrapper(Arc<OutputSweeper>);
212213

214+
// Trivially bridge the LDK FutureSpawner trait to tokio
215+
struct TokioSpawner;
216+
impl FutureSpawner for TokioSpawner{
217+
fn spawn<T: Future<Output = ()> + Send + 'static>(&self, future: T) {
218+
tokio::spawn(future);
219+
}
220+
}
221+
213222
fn handle_ldk_events<'a>(
214223
channel_manager: Arc<ChannelManager>, bitcoind_client: &'a BitcoindClient,
215224
network_graph: &'a NetworkGraph, keys_manager: &'a KeysManager,
@@ -679,36 +688,37 @@ async fn start_ldk() {
679688

680689
// Step 5: Initialize Persistence
681690
let fs_store = Arc::new(FilesystemStore::new(ldk_data_dir.clone().into()));
682-
let persister = Arc::new(MonitorUpdatingPersister::new(
691+
let persister = MonitorUpdatingPersisterAsync::new(
683692
Arc::clone(&fs_store),
693+
TokioSpawner,
684694
Arc::clone(&logger),
685695
1000,
686696
Arc::clone(&keys_manager),
687697
Arc::clone(&keys_manager),
688698
Arc::clone(&bitcoind_client),
689699
Arc::clone(&bitcoind_client),
690-
));
700+
);
691701
// Alternatively, you can use the `FilesystemStore` as a `Persist` directly, at the cost of
692702
// larger `ChannelMonitor` update writes (but no deletion or cleanup):
693703
//let persister = Arc::clone(&fs_store);
694704

695-
// Step 6: Initialize the ChainMonitor
696-
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
705+
// Step 6: Read ChannelMonitor state from disk
706+
let mut channelmonitors = persister.read_all_channel_monitors_with_updates().await.unwrap();
707+
// If you are using the `FilesystemStore` as a `Persist` directly, use
708+
// `lightning::util::persist::read_channel_monitors` like this:
709+
// read_channel_monitors(Arc::clone(&persister), Arc::clone(&keys_manager), Arc::clone(&keys_manager)).unwrap();
710+
711+
// Step 7: Initialize the ChainMonitor
712+
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new_async_beta(
697713
None,
698714
Arc::clone(&broadcaster),
699715
Arc::clone(&logger),
700716
Arc::clone(&fee_estimator),
701-
Arc::clone(&persister),
717+
persister,
702718
Arc::clone(&keys_manager),
703719
keys_manager.get_peer_storage_key(),
704720
));
705721

706-
// Step 7: Read ChannelMonitor state from disk
707-
let mut channelmonitors = persister.read_all_channel_monitors_with_updates().unwrap();
708-
// If you are using the `FilesystemStore` as a `Persist` directly, use
709-
// `lightning::util::persist::read_channel_monitors` like this:
710-
//read_channel_monitors(Arc::clone(&persister), Arc::clone(&keys_manager), Arc::clone(&keys_manager)).unwrap();
711-
712722
// Step 8: Poll for the best chain tip, which may be used by the channel manager & spv client
713723
let polled_chain_tip = init::validate_best_block_header(bitcoind_client.as_ref())
714724
.await
@@ -870,6 +880,8 @@ async fn start_ldk() {
870880
// Step 14: Give ChannelMonitors to ChainMonitor
871881
for (_, (channel_monitor, _, _, _), _) in chain_listener_channel_monitors {
872882
let channel_id = channel_monitor.channel_id();
883+
// Note that this may not return `Completed` for ChannelMonitors which were last written by
884+
// a version of LDK prior to 0.1.
873885
assert_eq!(
874886
chain_monitor.load_existing_monitor(channel_id, channel_monitor),
875887
Ok(ChannelMonitorUpdateStatus::Completed)

0 commit comments

Comments
 (0)