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 ;
10
8
use crate :: types:: {
11
- ChainMonitor , ChannelManager , FakeMessageRouter , GossipSync , KeysManager , NetAddress ,
12
- NetworkGraph , OnionMessenger , PeerManager ,
9
+ ChainMonitor , ChannelManager , FakeMessageRouter , GossipSync , KeysManager , NetworkGraph ,
10
+ OnionMessenger , PeerManager , SocketAddress ,
13
11
} ;
14
12
use crate :: wallet:: Wallet ;
15
13
use crate :: LogLevel ;
@@ -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_PRIMARY_NAMESPACE , CHANNEL_MANAGER_PERSISTENCE_SECONDARY_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
@@ -80,12 +86,16 @@ pub enum BuildError {
80
86
InvalidSeedFile ,
81
87
/// The current system time is invalid, clocks might have gone backwards.
82
88
InvalidSystemTime ,
89
+ /// The a read channel monitor is invalid.
90
+ InvalidChannelMonitor ,
83
91
/// We failed to read data from the [`KVStore`].
84
92
ReadFailed ,
85
93
/// We failed to write data to the [`KVStore`].
86
94
WriteFailed ,
87
95
/// We failed to access the given `storage_dir_path`.
88
96
StoragePathAccessFailed ,
97
+ /// We failed to setup our [`KVStore`].
98
+ KVStoreSetupFailed ,
89
99
/// We failed to setup the onchain wallet.
90
100
WalletSetupFailed ,
91
101
/// We failed to setup the logger.
@@ -100,9 +110,13 @@ impl fmt::Display for BuildError {
100
110
Self :: InvalidSystemTime => {
101
111
write ! ( f, "System time is invalid. Clocks might have gone back in time." )
102
112
}
113
+ Self :: InvalidChannelMonitor => {
114
+ write ! ( f, "Failed to watch a deserialzed ChannelMonitor" )
115
+ }
103
116
Self :: ReadFailed => write ! ( f, "Failed to read from store." ) ,
104
117
Self :: WriteFailed => write ! ( f, "Failed to write to store." ) ,
105
118
Self :: StoragePathAccessFailed => write ! ( f, "Failed to access the given storage path." ) ,
119
+ Self :: KVStoreSetupFailed => write ! ( f, "Failed to setup KVStore." ) ,
106
120
Self :: WalletSetupFailed => write ! ( f, "Failed to setup onchain wallet." ) ,
107
121
Self :: LoggerSetupFailed => write ! ( f, "Failed to setup the logger." ) ,
108
122
}
@@ -155,8 +169,6 @@ impl NodeBuilder {
155
169
}
156
170
157
171
/// 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.
160
172
pub fn set_entropy_seed_bytes ( & mut self , seed_bytes : Vec < u8 > ) -> Result < & mut Self , BuildError > {
161
173
if seed_bytes. len ( ) != WALLET_KEYS_SEED_LEN {
162
174
return Err ( BuildError :: InvalidSeedBytes ) ;
@@ -217,7 +229,7 @@ impl NodeBuilder {
217
229
}
218
230
219
231
/// 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 {
221
233
self . config . listening_address = Some ( listening_address) ;
222
234
self
223
235
}
@@ -234,17 +246,26 @@ impl NodeBuilder {
234
246
let storage_dir_path = self . config . storage_dir_path . clone ( ) ;
235
247
fs:: create_dir_all ( storage_dir_path. clone ( ) )
236
248
. 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
+ ) ;
238
257
self . build_with_store ( kv_store)
239
258
}
240
259
241
260
/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
242
261
/// previously configured.
243
262
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
+
245
266
fs:: create_dir_all ( storage_dir_path. clone ( ) )
246
267
. 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) ) ;
248
269
self . build_with_store ( kv_store)
249
270
}
250
271
@@ -353,7 +374,7 @@ impl ArcedNodeBuilder {
353
374
}
354
375
355
376
/// 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 ) {
357
378
self . inner . write ( ) . unwrap ( ) . set_listening_address ( listening_address) ;
358
379
}
359
380
@@ -510,7 +531,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
510
531
) ) ;
511
532
512
533
// 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 (
514
535
Arc :: clone ( & kv_store) ,
515
536
Arc :: clone ( & keys_manager) ,
516
537
Arc :: clone ( & keys_manager) ,
@@ -536,9 +557,12 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
536
557
user_config. manually_accept_inbound_channels = true ;
537
558
}
538
559
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) ;
542
566
let channel_monitor_references =
543
567
channel_monitors. iter_mut ( ) . map ( |( _, chanmon) | chanmon) . collect ( ) ;
544
568
let read_args = ChannelManagerReadArgs :: new (
@@ -589,7 +613,10 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
589
613
// Give ChannelMonitors to ChainMonitor
590
614
for ( _blockhash, channel_monitor) in channel_monitors. into_iter ( ) {
591
615
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
+ } ) ?;
593
620
}
594
621
595
622
// Initialize the PeerManager
@@ -726,7 +753,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
726
753
gossip_source,
727
754
kv_store,
728
755
logger,
729
- router,
756
+ _router : router,
730
757
scorer,
731
758
peer_store,
732
759
payment_store,
0 commit comments