11use crate :: event:: EventQueue ;
22use crate :: gossip:: GossipSource ;
33use crate :: io;
4- use crate :: io:: fs_store:: FilesystemStore ;
54use crate :: io:: sqlite_store:: SqliteStore ;
6- use crate :: io:: { KVStore , CHANNEL_MANAGER_PERSISTENCE_KEY , CHANNEL_MANAGER_PERSISTENCE_NAMESPACE } ;
75use crate :: logger:: { log_error, FilesystemLogger , Logger } ;
86use crate :: payment_store:: PaymentStore ;
97use crate :: peer_store:: PeerStore ;
@@ -29,8 +27,14 @@ use lightning::routing::scoring::{
2927use lightning:: sign:: EntropySource ;
3028
3129use lightning:: util:: config:: UserConfig ;
30+ use lightning:: util:: persist:: {
31+ read_channel_monitors, KVStore , CHANNEL_MANAGER_PERSISTENCE_KEY ,
32+ CHANNEL_MANAGER_PERSISTENCE_NAMESPACE , CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE ,
33+ } ;
3234use lightning:: util:: ser:: ReadableArgs ;
3335
36+ use lightning_persister:: fs_store:: FilesystemStore ;
37+
3438use lightning_transaction_sync:: EsploraSyncClient ;
3539
3640use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
@@ -48,6 +52,8 @@ use std::convert::TryInto;
4852use std:: default:: Default ;
4953use std:: fmt;
5054use std:: fs;
55+ use std:: io:: Cursor ;
56+ use std:: path:: PathBuf ;
5157use std:: sync:: { Arc , Mutex , RwLock } ;
5258use std:: time:: SystemTime ;
5359
@@ -86,6 +92,8 @@ pub enum BuildError {
8692 WriteFailed ,
8793 /// We failed to access the given `storage_dir_path`.
8894 StoragePathAccessFailed ,
95+ /// We failed to setup our [`KVStore`].
96+ KVStoreSetupFailed ,
8997 /// We failed to setup the onchain wallet.
9098 WalletSetupFailed ,
9199 /// We failed to setup the logger.
@@ -103,6 +111,7 @@ impl fmt::Display for BuildError {
103111 Self :: ReadFailed => write ! ( f, "Failed to read from store." ) ,
104112 Self :: WriteFailed => write ! ( f, "Failed to write to store." ) ,
105113 Self :: StoragePathAccessFailed => write ! ( f, "Failed to access the given storage path." ) ,
114+ Self :: KVStoreSetupFailed => write ! ( f, "Failed to setup KVStore." ) ,
106115 Self :: WalletSetupFailed => write ! ( f, "Failed to setup onchain wallet." ) ,
107116 Self :: LoggerSetupFailed => write ! ( f, "Failed to setup the logger." ) ,
108117 }
@@ -234,17 +243,26 @@ impl NodeBuilder {
234243 let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
235244 fs:: create_dir_all ( storage_dir_path. clone ( ) )
236245 . map_err ( |_| BuildError :: StoragePathAccessFailed ) ?;
237- let kv_store = Arc :: new ( SqliteStore :: new ( storage_dir_path. into ( ) ) ) ;
246+ let kv_store = Arc :: new (
247+ SqliteStore :: new (
248+ storage_dir_path. into ( ) ,
249+ Some ( io:: sqlite_store:: SQLITE_DB_FILE_NAME . to_string ( ) ) ,
250+ Some ( io:: sqlite_store:: KV_TABLE_NAME . to_string ( ) ) ,
251+ )
252+ . map_err ( |_| BuildError :: KVStoreSetupFailed ) ?,
253+ ) ;
238254 self . build_with_store ( kv_store)
239255 }
240256
241257 /// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
242258 /// previously configured.
243259 pub fn build_with_fs_store ( & self ) -> Result < Node < FilesystemStore > , BuildError > {
244- let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
260+ let mut storage_dir_path: PathBuf = self . config . storage_dir_path . clone ( ) . into ( ) ;
261+ storage_dir_path. push ( "fs_store" ) ;
262+
245263 fs:: create_dir_all ( storage_dir_path. clone ( ) )
246264 . map_err ( |_| BuildError :: StoragePathAccessFailed ) ?;
247- let kv_store = Arc :: new ( FilesystemStore :: new ( storage_dir_path. into ( ) ) ) ;
265+ let kv_store = Arc :: new ( FilesystemStore :: new ( storage_dir_path) ) ;
248266 self . build_with_store ( kv_store)
249267 }
250268
@@ -510,7 +528,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
510528 ) ) ;
511529
512530 // Read ChannelMonitor state from store
513- let mut channel_monitors = match io :: utils :: read_channel_monitors (
531+ let mut channel_monitors = match read_channel_monitors (
514532 Arc :: clone ( & kv_store) ,
515533 Arc :: clone ( & keys_manager) ,
516534 Arc :: clone ( & keys_manager) ,
@@ -536,9 +554,12 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
536554 user_config. manually_accept_inbound_channels = true ;
537555 }
538556 let channel_manager = {
539- if let Ok ( mut reader) =
540- kv_store. read ( CHANNEL_MANAGER_PERSISTENCE_NAMESPACE , CHANNEL_MANAGER_PERSISTENCE_KEY )
541- {
557+ if let Ok ( res) = kv_store. read (
558+ CHANNEL_MANAGER_PERSISTENCE_NAMESPACE ,
559+ CHANNEL_MANAGER_PERSISTENCE_SUB_NAMESPACE ,
560+ CHANNEL_MANAGER_PERSISTENCE_KEY ,
561+ ) {
562+ let mut reader = Cursor :: new ( res) ;
542563 let channel_monitor_references =
543564 channel_monitors. iter_mut ( ) . map ( |( _, chanmon) | chanmon) . collect ( ) ;
544565 let read_args = ChannelManagerReadArgs :: new (
0 commit comments