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 ;
108use crate :: types:: {
11- ChainMonitor , ChannelManager , FakeMessageRouter , GossipSync , KeysManager , NetAddress ,
12- NetworkGraph , OnionMessenger , PeerManager ,
9+ ChainMonitor , ChannelManager , FakeMessageRouter , GossipSync , KeysManager , NetworkGraph ,
10+ OnionMessenger , PeerManager , SocketAddress ,
1311} ;
1412use crate :: wallet:: Wallet ;
1513use crate :: LogLevel ;
@@ -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_PRIMARY_NAMESPACE , CHANNEL_MANAGER_PERSISTENCE_SECONDARY_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
@@ -80,12 +86,16 @@ pub enum BuildError {
8086 InvalidSeedFile ,
8187 /// The current system time is invalid, clocks might have gone backwards.
8288 InvalidSystemTime ,
89+ /// The a read channel monitor is invalid.
90+ InvalidChannelMonitor ,
8391 /// We failed to read data from the [`KVStore`].
8492 ReadFailed ,
8593 /// We failed to write data to the [`KVStore`].
8694 WriteFailed ,
8795 /// We failed to access the given `storage_dir_path`.
8896 StoragePathAccessFailed ,
97+ /// We failed to setup our [`KVStore`].
98+ KVStoreSetupFailed ,
8999 /// We failed to setup the onchain wallet.
90100 WalletSetupFailed ,
91101 /// We failed to setup the logger.
@@ -100,9 +110,13 @@ impl fmt::Display for BuildError {
100110 Self :: InvalidSystemTime => {
101111 write ! ( f, "System time is invalid. Clocks might have gone back in time." )
102112 }
113+ Self :: InvalidChannelMonitor => {
114+ write ! ( f, "Failed to watch a deserialzed ChannelMonitor" )
115+ }
103116 Self :: ReadFailed => write ! ( f, "Failed to read from store." ) ,
104117 Self :: WriteFailed => write ! ( f, "Failed to write to store." ) ,
105118 Self :: StoragePathAccessFailed => write ! ( f, "Failed to access the given storage path." ) ,
119+ Self :: KVStoreSetupFailed => write ! ( f, "Failed to setup KVStore." ) ,
106120 Self :: WalletSetupFailed => write ! ( f, "Failed to setup onchain wallet." ) ,
107121 Self :: LoggerSetupFailed => write ! ( f, "Failed to setup the logger." ) ,
108122 }
@@ -155,8 +169,6 @@ impl NodeBuilder {
155169 }
156170
157171 /// Configures the [`Node`] instance to source its wallet entropy from the given 64 seed bytes.
158- ///
159- /// **Note:** Panics if the length of the given `seed_bytes` differs from 64.
160172 pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : Vec < u8 > ) -> Result < & mut Self , BuildError > {
161173 if seed_bytes. len ( ) != WALLET_KEYS_SEED_LEN {
162174 return Err ( BuildError :: InvalidSeedBytes ) ;
@@ -217,7 +229,7 @@ impl NodeBuilder {
217229 }
218230
219231 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
220- pub fn set_listening_address ( & mut self , listening_address : NetAddress ) -> & mut Self {
232+ pub fn set_listening_address ( & mut self , listening_address : SocketAddress ) -> & mut Self {
221233 self . config . listening_address = Some ( listening_address) ;
222234 self
223235 }
@@ -234,17 +246,26 @@ impl NodeBuilder {
234246 let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
235247 fs:: create_dir_all ( storage_dir_path. clone ( ) )
236248 . map_err ( |_| BuildError :: StoragePathAccessFailed ) ?;
237- let kv_store = Arc :: new ( SqliteStore :: new ( storage_dir_path. into ( ) ) ) ;
249+ let kv_store = Arc :: new (
250+ SqliteStore :: new (
251+ storage_dir_path. into ( ) ,
252+ Some ( io:: sqlite_store:: SQLITE_DB_FILE_NAME . to_string ( ) ) ,
253+ Some ( io:: sqlite_store:: KV_TABLE_NAME . to_string ( ) ) ,
254+ )
255+ . map_err ( |_| BuildError :: KVStoreSetupFailed ) ?,
256+ ) ;
238257 self . build_with_store ( kv_store)
239258 }
240259
241260 /// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
242261 /// previously configured.
243262 pub fn build_with_fs_store ( & self ) -> Result < Node < FilesystemStore > , BuildError > {
244- let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
263+ let mut storage_dir_path: PathBuf = self . config . storage_dir_path . clone ( ) . into ( ) ;
264+ storage_dir_path. push ( "fs_store" ) ;
265+
245266 fs:: create_dir_all ( storage_dir_path. clone ( ) )
246267 . map_err ( |_| BuildError :: StoragePathAccessFailed ) ?;
247- let kv_store = Arc :: new ( FilesystemStore :: new ( storage_dir_path. into ( ) ) ) ;
268+ let kv_store = Arc :: new ( FilesystemStore :: new ( storage_dir_path) ) ;
248269 self . build_with_store ( kv_store)
249270 }
250271
@@ -353,7 +374,7 @@ impl ArcedNodeBuilder {
353374 }
354375
355376 /// Sets the IP address and TCP port on which [`Node`] will listen for incoming network connections.
356- pub fn set_listening_address ( & self , listening_address : NetAddress ) {
377+ pub fn set_listening_address ( & self , listening_address : SocketAddress ) {
357378 self . inner . write ( ) . unwrap ( ) . set_listening_address ( listening_address) ;
358379 }
359380
@@ -510,7 +531,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
510531 ) ) ;
511532
512533 // Read ChannelMonitor state from store
513- let mut channel_monitors = match io :: utils :: read_channel_monitors (
534+ let mut channel_monitors = match read_channel_monitors (
514535 Arc :: clone ( & kv_store) ,
515536 Arc :: clone ( & keys_manager) ,
516537 Arc :: clone ( & keys_manager) ,
@@ -536,9 +557,12 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
536557 user_config. manually_accept_inbound_channels = true ;
537558 }
538559 let channel_manager = {
539- if let Ok ( mut reader) =
540- kv_store. read ( CHANNEL_MANAGER_PERSISTENCE_NAMESPACE , CHANNEL_MANAGER_PERSISTENCE_KEY )
541- {
560+ if let Ok ( res) = kv_store. read (
561+ CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE ,
562+ CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE ,
563+ CHANNEL_MANAGER_PERSISTENCE_KEY ,
564+ ) {
565+ let mut reader = Cursor :: new ( res) ;
542566 let channel_monitor_references =
543567 channel_monitors. iter_mut ( ) . map ( |( _, chanmon) | chanmon) . collect ( ) ;
544568 let read_args = ChannelManagerReadArgs :: new (
@@ -589,7 +613,10 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
589613 // Give ChannelMonitors to ChainMonitor
590614 for ( _blockhash, channel_monitor) in channel_monitors. into_iter ( ) {
591615 let funding_outpoint = channel_monitor. get_funding_txo ( ) . 0 ;
592- chain_monitor. watch_channel ( funding_outpoint, channel_monitor) ;
616+ chain_monitor. watch_channel ( funding_outpoint, channel_monitor) . map_err ( |e| {
617+ log_error ! ( logger, "Failed to watch channel monitor: {:?}" , e) ;
618+ BuildError :: InvalidChannelMonitor
619+ } ) ?;
593620 }
594621
595622 // Initialize the PeerManager
@@ -726,7 +753,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
726753 gossip_source,
727754 kv_store,
728755 logger,
729- router,
756+ _router : router,
730757 scorer,
731758 peer_store,
732759 payment_store,
0 commit comments