Skip to content

Commit 88ea9f5

Browse files
committed
Switch to (beta) async persistence of ChannelMonitors
1 parent 557582d commit 88ea9f5

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

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};
@@ -61,6 +62,7 @@ use std::convert::TryInto;
6162
use std::fmt;
6263
use std::fs;
6364
use std::fs::File;
65+
use std::future::Future;
6466
use std::io::{BufReader, Write};
6567
use std::net::ToSocketAddrs;
6668
use std::path::Path;
@@ -141,15 +143,14 @@ type ChainMonitor = chainmonitor::ChainMonitor<
141143
Arc<BitcoindClient>,
142144
Arc<BitcoindClient>,
143145
Arc<FilesystemLogger>,
144-
Arc<
145-
MonitorUpdatingPersister<
146-
Arc<FilesystemStore>,
147-
Arc<FilesystemLogger>,
148-
Arc<KeysManager>,
149-
Arc<KeysManager>,
150-
Arc<BitcoindClient>,
151-
Arc<BitcoindClient>,
152-
>,
146+
chainmonitor::AsyncPersister<
147+
Arc<FilesystemStore>,
148+
TokioSpawner,
149+
Arc<FilesystemLogger>,
150+
Arc<KeysManager>,
151+
Arc<KeysManager>,
152+
Arc<BitcoindClient>,
153+
Arc<BitcoindClient>,
153154
>,
154155
Arc<KeysManager>,
155156
>;
@@ -212,6 +213,14 @@ pub(crate) type OutputSweeper = ldk_sweep::OutputSweeper<
212213
// Needed due to rust-lang/rust#63033.
213214
struct OutputSweeperWrapper(Arc<OutputSweeper>);
214215

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

650659
// Step 5: Initialize Persistence
651660
let fs_store = Arc::new(FilesystemStore::new(ldk_data_dir.clone().into()));
652-
let persister = Arc::new(MonitorUpdatingPersister::new(
661+
let persister = MonitorUpdatingPersisterAsync::new(
653662
Arc::clone(&fs_store),
663+
TokioSpawner,
654664
Arc::clone(&logger),
655665
1000,
656666
Arc::clone(&keys_manager),
657667
Arc::clone(&keys_manager),
658668
Arc::clone(&bitcoind_client),
659669
Arc::clone(&bitcoind_client),
660-
));
670+
);
661671
// Alternatively, you can use the `FilesystemStore` as a `Persist` directly, at the cost of
662672
// larger `ChannelMonitor` update writes (but no deletion or cleanup):
663673
//let persister = Arc::clone(&fs_store);
664674

665-
// Step 6: Initialize the ChainMonitor
666-
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new(
675+
// Step 6: Read ChannelMonitor state from disk
676+
let mut channelmonitors = persister.read_all_channel_monitors_with_updates().await.unwrap();
677+
// If you are using the `FilesystemStore` as a `Persist` directly, use
678+
// `lightning::util::persist::read_channel_monitors` like this:
679+
//read_channel_monitors(Arc::clone(&persister), Arc::clone(&keys_manager), Arc::clone(&keys_manager)).unwrap();
680+
681+
// Step 7: Initialize the ChainMonitor
682+
let chain_monitor: Arc<ChainMonitor> = Arc::new(chainmonitor::ChainMonitor::new_async_beta(
667683
None,
668684
Arc::clone(&broadcaster),
669685
Arc::clone(&logger),
670686
Arc::clone(&fee_estimator),
671-
Arc::clone(&persister),
687+
persister,
672688
Arc::clone(&keys_manager),
673689
keys_manager.get_peer_storage_key(),
674690
));
675691

676-
// Step 7: Read ChannelMonitor state from disk
677-
let mut channelmonitors = persister.read_all_channel_monitors_with_updates().unwrap();
678-
// If you are using the `FilesystemStore` as a `Persist` directly, use
679-
// `lightning::util::persist::read_channel_monitors` like this:
680-
//read_channel_monitors(Arc::clone(&persister), Arc::clone(&keys_manager), Arc::clone(&keys_manager)).unwrap();
681-
682692
// Step 8: Poll for the best chain tip, which may be used by the channel manager & spv client
683693
let polled_chain_tip = init::validate_best_block_header(bitcoind_client.as_ref())
684694
.await
@@ -837,6 +847,8 @@ async fn start_ldk() {
837847
// Step 14: Give ChannelMonitors to ChainMonitor
838848
for (_, (channel_monitor, _, _, _), _) in chain_listener_channel_monitors {
839849
let channel_id = channel_monitor.channel_id();
850+
// Note that this may not return `Completed` for ChannelMonitors which were last written by
851+
// a version of LDK prior to 0.1.
840852
assert_eq!(
841853
chain_monitor.load_existing_channel(channel_id, channel_monitor),
842854
Ok(ChannelMonitorUpdateStatus::Completed)

0 commit comments

Comments
 (0)