1
1
use crate :: event:: EventQueue ;
2
2
use crate :: gossip:: GossipSource ;
3
3
use crate :: io;
4
- use crate :: io:: fs_store:: FilesystemStore ;
5
4
use crate :: io:: sqlite_store:: SqliteStore ;
6
- use crate :: io:: { KVStore , CHANNEL_MANAGER_PERSISTENCE_KEY , CHANNEL_MANAGER_PERSISTENCE_NAMESPACE } ;
7
5
use crate :: logger:: { log_error, FilesystemLogger , Logger } ;
8
6
use crate :: payment_store:: PaymentStore ;
9
7
use crate :: peer_store:: PeerStore ;
@@ -29,8 +27,14 @@ use lightning::routing::scoring::{
29
27
use lightning:: sign:: EntropySource ;
30
28
31
29
use 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
+ } ;
32
34
use lightning:: util:: ser:: ReadableArgs ;
33
35
36
+ use lightning_persister:: fs_store:: FilesystemStore ;
37
+
34
38
use lightning_transaction_sync:: EsploraSyncClient ;
35
39
36
40
use bdk:: bitcoin:: secp256k1:: Secp256k1 ;
@@ -48,6 +52,8 @@ use std::convert::TryInto;
48
52
use std:: default:: Default ;
49
53
use std:: fmt;
50
54
use std:: fs;
55
+ use std:: io:: Cursor ;
56
+ use std:: path:: PathBuf ;
51
57
use std:: sync:: { Arc , Mutex , RwLock } ;
52
58
use std:: time:: SystemTime ;
53
59
@@ -86,6 +92,8 @@ pub enum BuildError {
86
92
WriteFailed ,
87
93
/// We failed to access the given `storage_dir_path`.
88
94
StoragePathAccessFailed ,
95
+ /// We failed to setup our [`KVStore`].
96
+ KVStoreSetupFailed ,
89
97
/// We failed to setup the onchain wallet.
90
98
WalletSetupFailed ,
91
99
/// We failed to setup the logger.
@@ -103,6 +111,7 @@ impl fmt::Display for BuildError {
103
111
Self :: ReadFailed => write ! ( f, "Failed to read from store." ) ,
104
112
Self :: WriteFailed => write ! ( f, "Failed to write to store." ) ,
105
113
Self :: StoragePathAccessFailed => write ! ( f, "Failed to access the given storage path." ) ,
114
+ Self :: KVStoreSetupFailed => write ! ( f, "Failed to setup KVStore." ) ,
106
115
Self :: WalletSetupFailed => write ! ( f, "Failed to setup onchain wallet." ) ,
107
116
Self :: LoggerSetupFailed => write ! ( f, "Failed to setup the logger." ) ,
108
117
}
@@ -234,17 +243,26 @@ impl NodeBuilder {
234
243
let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
235
244
fs:: create_dir_all ( storage_dir_path. clone ( ) )
236
245
. 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
+ ) ;
238
254
self . build_with_store ( kv_store)
239
255
}
240
256
241
257
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
242
258
/// previously configured.
243
259
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
+
245
263
fs:: create_dir_all ( storage_dir_path. clone ( ) )
246
264
. 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) ) ;
248
266
self . build_with_store ( kv_store)
249
267
}
250
268
@@ -510,7 +528,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
510
528
) ) ;
511
529
512
530
// 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 (
514
532
Arc :: clone ( & kv_store) ,
515
533
Arc :: clone ( & keys_manager) ,
516
534
Arc :: clone ( & keys_manager) ,
@@ -536,9 +554,12 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
536
554
user_config. manually_accept_inbound_channels = true ;
537
555
}
538
556
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) ;
542
563
let channel_monitor_references =
543
564
channel_monitors. iter_mut ( ) . map ( |( _, chanmon) | chanmon) . collect ( ) ;
544
565
let read_args = ChannelManagerReadArgs :: new (
0 commit comments