Skip to content

Commit 4709221

Browse files
authored
Merge pull request #138 from tnull/2023-07-vss-prep
2 parents bcb01f6 + eb9b314 commit 4709221

File tree

8 files changed

+91
-74
lines changed

8 files changed

+91
-74
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ interface LDKNode {
8181
Invoice receive_variable_amount_payment([ByRef]string description, u32 expiry_secs);
8282
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
8383
[Throws=NodeError]
84-
boolean remove_payment([ByRef]PaymentHash payment_hash);
84+
void remove_payment([ByRef]PaymentHash payment_hash);
8585
sequence<PaymentDetails> list_payments();
8686
sequence<PeerDetails> list_peers();
8787
sequence<ChannelDetails> list_channels();

src/builder.rs

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,21 @@ impl NodeBuilder {
250250
pub fn build_with_store<K: KVStore + Sync + Send + 'static>(
251251
&self, kv_store: Arc<K>,
252252
) -> Result<Node<K>, BuildError> {
253+
let logger = setup_logger(&self.config)?;
254+
let seed_bytes = seed_bytes_from_config(
255+
&self.config,
256+
self.entropy_source_config.as_ref(),
257+
Arc::clone(&logger),
258+
)?;
253259
let config = Arc::new(self.config.clone());
254260

255-
let runtime = Arc::new(RwLock::new(None));
256261
build_with_store_internal(
257262
config,
258-
self.entropy_source_config.as_ref(),
259263
self.chain_data_source_config.as_ref(),
260264
self.gossip_source_config.as_ref(),
265+
seed_bytes,
266+
logger,
261267
kv_store,
262-
runtime,
263268
)
264269
}
265270
}
@@ -377,57 +382,27 @@ impl ArcedNodeBuilder {
377382

378383
/// Builds a [`Node`] instance according to the options previously configured.
379384
fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
380-
config: Arc<Config>, entropy_source_config: Option<&EntropySourceConfig>,
381-
chain_data_source_config: Option<&ChainDataSourceConfig>,
382-
gossip_source_config: Option<&GossipSourceConfig>, kv_store: Arc<K>,
383-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
385+
config: Arc<Config>, chain_data_source_config: Option<&ChainDataSourceConfig>,
386+
gossip_source_config: Option<&GossipSourceConfig>, seed_bytes: [u8; 64],
387+
logger: Arc<FilesystemLogger>, kv_store: Arc<K>,
384388
) -> Result<Node<K>, BuildError> {
385-
let ldk_data_dir = format!("{}/ldk", config.storage_dir_path);
386-
fs::create_dir_all(ldk_data_dir.clone()).map_err(|_| BuildError::StoragePathAccessFailed)?;
387-
388-
let bdk_data_dir = format!("{}/bdk", config.storage_dir_path);
389-
fs::create_dir_all(bdk_data_dir.clone()).map_err(|_| BuildError::StoragePathAccessFailed)?;
390-
391-
let log_dir = match &config.log_dir_path {
392-
Some(log_dir) => String::from(log_dir),
393-
None => config.storage_dir_path.clone() + "/logs",
394-
};
395-
396-
// Initialize the Logger
397-
let logger = Arc::new(
398-
FilesystemLogger::new(log_dir, config.log_level)
399-
.map_err(|_| BuildError::LoggerSetupFailed)?,
400-
);
401-
402389
// Initialize the on-chain wallet and chain access
403-
let seed_bytes = match entropy_source_config {
404-
Some(EntropySourceConfig::SeedBytes(bytes)) => bytes.clone(),
405-
Some(EntropySourceConfig::SeedFile(seed_path)) => {
406-
io::utils::read_or_generate_seed_file(seed_path, Arc::clone(&logger))
407-
.map_err(|_| BuildError::InvalidSeedFile)?
408-
}
409-
Some(EntropySourceConfig::Bip39Mnemonic { mnemonic, passphrase }) => match passphrase {
410-
Some(passphrase) => mnemonic.to_seed(passphrase),
411-
None => mnemonic.to_seed(""),
412-
},
413-
None => {
414-
// Default to read or generate from the default location generate a seed file.
415-
let seed_path = format!("{}/keys_seed", config.storage_dir_path);
416-
io::utils::read_or_generate_seed_file(&seed_path, Arc::clone(&logger))
417-
.map_err(|_| BuildError::InvalidSeedFile)?
418-
}
419-
};
420-
421390
let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network, &seed_bytes)
422-
.map_err(|_| BuildError::InvalidSeedBytes)?;
391+
.map_err(|e| {
392+
log_error!(logger, "Failed to derive master secret: {}", e);
393+
BuildError::InvalidSeedBytes
394+
})?;
423395

424396
let wallet_name = bdk::wallet::wallet_name_from_descriptor(
425397
Bip84(xprv, bdk::KeychainKind::External),
426398
Some(Bip84(xprv, bdk::KeychainKind::Internal)),
427399
config.network,
428400
&Secp256k1::new(),
429401
)
430-
.map_err(|_| BuildError::WalletSetupFailed)?;
402+
.map_err(|e| {
403+
log_error!(logger, "Failed to derive wallet name: {}", e);
404+
BuildError::WalletSetupFailed
405+
})?;
431406

432407
let database_path = format!("{}/bdk_wallet_{}.sqlite", config.storage_dir_path, wallet_name);
433408
let database = SqliteDatabase::new(database_path);
@@ -438,7 +413,10 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
438413
config.network,
439414
database,
440415
)
441-
.map_err(|_| BuildError::WalletSetupFailed)?;
416+
.map_err(|e| {
417+
log_error!(logger, "Failed to set up wallet: {}", e);
418+
BuildError::WalletSetupFailed
419+
})?;
442420

443421
let (blockchain, tx_sync) = match chain_data_source_config {
444422
Some(ChainDataSourceConfig::Esplora(server_url)) => {
@@ -459,6 +437,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
459437
}
460438
};
461439

440+
let runtime = Arc::new(RwLock::new(None));
462441
let wallet =
463442
Arc::new(Wallet::new(blockchain, bdk_wallet, Arc::clone(&runtime), Arc::clone(&logger)));
464443

@@ -472,9 +451,11 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
472451
));
473452

474453
// Initialize the KeysManager
475-
let cur_time = SystemTime::now()
476-
.duration_since(SystemTime::UNIX_EPOCH)
477-
.map_err(|_| BuildError::InvalidSystemTime)?;
454+
let cur_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_err(|e| {
455+
log_error!(logger, "Failed to get current time: {}", e);
456+
BuildError::InvalidSystemTime
457+
})?;
458+
478459
let ldk_seed_bytes: [u8; 32] = xprv.private_key.secret_bytes();
479460
let keys_manager = Arc::new(KeysManager::new(
480461
&ldk_seed_bytes,
@@ -615,10 +596,6 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
615596
));
616597
let ephemeral_bytes: [u8; 32] = keys_manager.get_secure_random_bytes();
617598

618-
let cur_time = SystemTime::now()
619-
.duration_since(SystemTime::UNIX_EPOCH)
620-
.map_err(|_| BuildError::InvalidSystemTime)?;
621-
622599
// Initialize the GossipSource
623600
// Use the configured gossip source, if the user set one, otherwise default to P2PNetwork.
624601
let gossip_source_config = gossip_source_config.unwrap_or(&GossipSourceConfig::P2PNetwork);
@@ -634,7 +611,10 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
634611
Arc::clone(&kv_store),
635612
Arc::clone(&logger),
636613
)
637-
.map_err(|_| BuildError::WriteFailed)?;
614+
.map_err(|e| {
615+
log_error!(logger, "Failed writing to store: {}", e);
616+
BuildError::WriteFailed
617+
})?;
638618
p2p_source
639619
}
640620
GossipSourceConfig::RapidGossipSync(rgs_server) => {
@@ -670,9 +650,17 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
670650
}
671651
};
672652

653+
let cur_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_err(|e| {
654+
log_error!(logger, "Failed to get current time: {}", e);
655+
BuildError::InvalidSystemTime
656+
})?;
657+
673658
let peer_manager = Arc::new(PeerManager::new(
674659
msg_handler,
675-
cur_time.as_secs().try_into().map_err(|_| BuildError::InvalidSystemTime)?,
660+
cur_time.as_secs().try_into().map_err(|e| {
661+
log_error!(logger, "Failed to get current time: {}", e);
662+
BuildError::InvalidSystemTime
663+
})?,
676664
&ephemeral_bytes,
677665
Arc::clone(&logger),
678666
IgnoringMessageHandler {},
@@ -736,3 +724,38 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
736724
payment_store,
737725
})
738726
}
727+
728+
fn setup_logger(config: &Config) -> Result<Arc<FilesystemLogger>, BuildError> {
729+
let log_dir = match &config.log_dir_path {
730+
Some(log_dir) => String::from(log_dir),
731+
None => config.storage_dir_path.clone() + "/logs",
732+
};
733+
734+
Ok(Arc::new(
735+
FilesystemLogger::new(log_dir, config.log_level)
736+
.map_err(|_| BuildError::LoggerSetupFailed)?,
737+
))
738+
}
739+
740+
fn seed_bytes_from_config(
741+
config: &Config, entropy_source_config: Option<&EntropySourceConfig>,
742+
logger: Arc<FilesystemLogger>,
743+
) -> Result<[u8; 64], BuildError> {
744+
match entropy_source_config {
745+
Some(EntropySourceConfig::SeedBytes(bytes)) => Ok(bytes.clone()),
746+
Some(EntropySourceConfig::SeedFile(seed_path)) => {
747+
Ok(io::utils::read_or_generate_seed_file(&seed_path, Arc::clone(&logger))
748+
.map_err(|_| BuildError::InvalidSeedFile)?)
749+
}
750+
Some(EntropySourceConfig::Bip39Mnemonic { mnemonic, passphrase }) => match passphrase {
751+
Some(passphrase) => Ok(mnemonic.to_seed(passphrase)),
752+
None => Ok(mnemonic.to_seed("")),
753+
},
754+
None => {
755+
// Default to read or generate from the default location generate a seed file.
756+
let seed_path = format!("{}/keys_seed", config.storage_dir_path);
757+
Ok(io::utils::read_or_generate_seed_file(&seed_path, Arc::clone(&logger))
758+
.map_err(|_| BuildError::InvalidSeedFile)?)
759+
}
760+
}
761+
}

src/io/fs_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl KVStore for FilesystemStore {
136136
Ok(())
137137
}
138138

139-
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool> {
139+
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
140140
let mut outer_lock = self.locks.lock().unwrap();
141141
let lock_key = (namespace.to_string(), key.to_string());
142142
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key.clone()).or_default());
@@ -148,7 +148,7 @@ impl KVStore for FilesystemStore {
148148
dest_file_path.push(key);
149149

150150
if !dest_file_path.is_file() {
151-
return Ok(false);
151+
return Ok(());
152152
}
153153

154154
fs::remove_file(&dest_file_path)?;
@@ -190,7 +190,7 @@ impl KVStore for FilesystemStore {
190190
// Garbage collect all lock entries that are not referenced anymore.
191191
outer_lock.retain(|_, v| Arc::strong_count(&v) > 1);
192192

193-
Ok(true)
193+
Ok(())
194194
}
195195

196196
fn list(&self, namespace: &str) -> std::io::Result<Vec<String>> {

src/io/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ pub trait KVStore: KVStorePersister {
6969
/// Will create the given `namespace` if not already present in the store.
7070
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()>;
7171
/// Removes any data that had previously been persisted under the given `key`.
72-
///
73-
/// Returns `true` if the `key` was present in the given `namespace`, and `false` otherwise.
74-
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool>;
72+
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()>;
7573
/// Returns a list of keys that are stored under the given `namespace`.
7674
///
7775
/// Will return an empty list if the `namespace` is unknown.

src/io/sqlite_store.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ impl KVStore for SqliteStore {
116116
})
117117
}
118118

119-
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool> {
119+
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
120120
let locked_conn = self.connection.lock().unwrap();
121121

122122
let sql = format!("DELETE FROM {} WHERE namespace=:namespace AND key=:key;", KV_TABLE_NAME);
123-
let changes = locked_conn
123+
locked_conn
124124
.execute(
125125
&sql,
126126
named_params! {
@@ -132,10 +132,7 @@ impl KVStore for SqliteStore {
132132
let msg = format!("Failed to delete key {}/{}: {}", namespace, key, e);
133133
std::io::Error::new(std::io::ErrorKind::Other, msg)
134134
})?;
135-
136-
let was_present = changes != 0;
137-
138-
Ok(was_present)
135+
Ok(())
139136
}
140137

141138
fn list(&self, namespace: &str) -> std::io::Result<Vec<String>> {

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,9 +1445,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
14451445
}
14461446

14471447
/// Remove the payment with the given hash from the store.
1448-
///
1449-
/// Returns `true` if the payment was present and `false` otherwise.
1450-
pub fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<bool, Error> {
1448+
pub fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
14511449
self.payment_store.remove(&payment_hash)
14521450
}
14531451

src/payment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ where
121121
Ok(updated)
122122
}
123123

124-
pub(crate) fn remove(&self, hash: &PaymentHash) -> Result<bool, Error> {
124+
pub(crate) fn remove(&self, hash: &PaymentHash) -> Result<(), Error> {
125125
let store_key = hex_utils::to_string(&hash.0);
126126
self.kv_store.remove(PAYMENT_INFO_PERSISTENCE_NAMESPACE, &store_key).map_err(|e| {
127127
log_error!(

src/test/utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ impl KVStore for TestStore {
9696
Ok(())
9797
}
9898

99-
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool> {
99+
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
100100
match self.persisted_bytes.write().unwrap().entry(namespace.to_string()) {
101101
hash_map::Entry::Occupied(mut e) => {
102102
self.did_persist.store(true, Ordering::SeqCst);
103-
Ok(e.get_mut().remove(&key.to_string()).is_some())
103+
e.get_mut().remove(&key.to_string());
104+
Ok(())
104105
}
105-
hash_map::Entry::Vacant(_) => Ok(false),
106+
hash_map::Entry::Vacant(_) => Ok(()),
106107
}
107108
}
108109

0 commit comments

Comments
 (0)