Skip to content

Commit bc5c8af

Browse files
committed
Add test setup and first end-to-end test
1 parent efcb3b7 commit bc5c8af

File tree

8 files changed

+251
-36
lines changed

8 files changed

+251
-36
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ futures = "0.3"
4949
serde_json = { version = "1.0" }
5050
tokio = { version = "1", features = [ "full" ] }
5151

52+
[dev-dependencies]
53+
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
54+
electrum-client = "0.12.0"
55+
once_cell = "1.16.0"
5256

5357
[profile.release]
5458
panic = "abort"

src/error.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ impl From<bdk::Error> for Error {
7171
}
7272

7373
impl From<lightning_transaction_sync::TxSyncError> for Error {
74-
fn from(e: lightning_transaction_sync::TxSyncError) -> Self {
75-
match e {
76-
_ => Self::TxSyncFailed,
77-
}
74+
fn from(_e: lightning_transaction_sync::TxSyncError) -> Self {
75+
Self::TxSyncFailed
7876
}
7977
}

src/io_utils.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::sync::Arc;
1212

1313
pub(crate) fn read_or_generate_seed_file(config: &Config) -> [u8; 32] {
1414
let keys_seed_path = format!("{}/keys_seed", config.storage_dir_path);
15-
let keys_seed = if Path::new(&keys_seed_path).exists() {
16-
let seed = fs::read(keys_seed_path.clone()).expect("Failed to read keys seed file");
15+
if Path::new(&keys_seed_path).exists() {
16+
let seed = fs::read(keys_seed_path).expect("Failed to read keys seed file");
1717
assert_eq!(seed.len(), 32);
1818
let mut key = [0; 32];
1919
key.copy_from_slice(&seed);
@@ -22,14 +22,11 @@ pub(crate) fn read_or_generate_seed_file(config: &Config) -> [u8; 32] {
2222
let mut key = [0; 32];
2323
thread_rng().fill_bytes(&mut key);
2424

25-
let mut f =
26-
fs::File::create(keys_seed_path.clone()).expect("Failed to create keys seed file");
25+
let mut f = fs::File::create(keys_seed_path).expect("Failed to create keys seed file");
2726
f.write_all(&key).expect("Failed to write node keys seed to disk");
2827
f.sync_all().expect("Failed to sync node keys seed to disk");
2928
key
30-
};
31-
32-
keys_seed
29+
}
3330
}
3431

3532
pub(crate) fn read_network_graph(

src/lib.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ mod hex_utils;
3030
mod io_utils;
3131
mod logger;
3232
mod peer_store;
33+
#[cfg(test)]
34+
mod tests;
3335
mod types;
3436
mod wallet;
3537

@@ -206,19 +208,18 @@ impl Builder {
206208
.expect("Failed to read wallet master key");
207209

208210
let wallet_name = bdk::wallet::wallet_name_from_descriptor(
209-
Bip84(xprv.clone(), bdk::KeychainKind::External),
210-
Some(Bip84(xprv.clone(), bdk::KeychainKind::Internal)),
211+
Bip84(xprv, bdk::KeychainKind::External),
212+
Some(Bip84(xprv, bdk::KeychainKind::Internal)),
211213
config.network,
212214
&Secp256k1::new(),
213215
)
214216
.expect("Failed to derive on-chain wallet name");
215217
let database = sled::open(bdk_data_dir).expect("Failed to open BDK database");
216-
let database =
217-
database.open_tree(wallet_name.clone()).expect("Failed to open BDK database");
218+
let database = database.open_tree(wallet_name).expect("Failed to open BDK database");
218219

219220
let bdk_wallet = bdk::Wallet::new(
220-
Bip84(xprv.clone(), bdk::KeychainKind::External),
221-
Some(Bip84(xprv.clone(), bdk::KeychainKind::Internal)),
221+
Bip84(xprv, bdk::KeychainKind::External),
222+
Some(Bip84(xprv, bdk::KeychainKind::Internal)),
222223
config.network,
223224
database,
224225
)
@@ -285,7 +286,7 @@ impl Builder {
285286
let mut user_config = UserConfig::default();
286287
user_config.channel_handshake_limits.force_announced_channel_preference = false;
287288
let channel_manager = {
288-
if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir.clone())) {
289+
if let Ok(mut f) = fs::File::open(format!("{}/manager", ldk_data_dir)) {
289290
let mut channel_monitor_mut_references = Vec::new();
290291
for (_, channel_monitor) in channel_monitors.iter_mut() {
291292
channel_monitor_mut_references.push(channel_monitor);
@@ -316,7 +317,7 @@ impl Builder {
316317
network: config.network,
317318
best_block: BestBlock::new(dummy_block_hash, 0),
318319
};
319-
let fresh_channel_manager = channelmanager::ChannelManager::new(
320+
channelmanager::ChannelManager::new(
320321
Arc::clone(&wallet),
321322
Arc::clone(&chain_monitor),
322323
Arc::clone(&wallet),
@@ -327,8 +328,7 @@ impl Builder {
327328
Arc::clone(&keys_manager),
328329
user_config,
329330
chain_params,
330-
);
331-
fresh_channel_manager
331+
)
332332
}
333333
};
334334

@@ -380,7 +380,7 @@ impl Builder {
380380

381381
// Step 14: Restore event handler from disk or create a new one.
382382
let event_queue = if let Ok(mut f) =
383-
fs::File::open(format!("{}/{}", ldk_data_dir.clone(), event::EVENTS_PERSISTENCE_KEY))
383+
fs::File::open(format!("{}/{}", ldk_data_dir, event::EVENTS_PERSISTENCE_KEY))
384384
{
385385
Arc::new(
386386
EventQueue::read(&mut f, Arc::clone(&persister))
@@ -390,11 +390,9 @@ impl Builder {
390390
Arc::new(EventQueue::new(Arc::clone(&persister)))
391391
};
392392

393-
let peer_store = if let Ok(mut f) = fs::File::open(format!(
394-
"{}/{}",
395-
ldk_data_dir.clone(),
396-
peer_store::PEER_INFO_PERSISTENCE_KEY
397-
)) {
393+
let peer_store = if let Ok(mut f) =
394+
fs::File::open(format!("{}/{}", ldk_data_dir, peer_store::PEER_INFO_PERSISTENCE_KEY))
395+
{
398396
Arc::new(
399397
PeerInfoStorage::read(&mut f, Arc::clone(&persister))
400398
.expect("Failed to read peer information from disk."),
@@ -643,7 +641,7 @@ impl Node {
643641
if peer_info.pubkey == node_id {
644642
let _ = do_connect_peer(
645643
peer_info.pubkey,
646-
peer_info.address.clone(),
644+
peer_info.address,
647645
Arc::clone(&connect_pm),
648646
Arc::clone(&connect_logger),
649647
)
@@ -859,8 +857,8 @@ impl Node {
859857
}
860858
};
861859

862-
let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
863-
let payment_secret = Some(invoice.payment_secret().clone());
860+
let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
861+
let payment_secret = Some(*invoice.payment_secret());
864862

865863
let mut outbound_payments_lock = self.outbound_payments.lock().unwrap();
866864
outbound_payments_lock.insert(
@@ -957,20 +955,20 @@ impl Node {
957955
}
958956
};
959957

960-
let payment_hash = PaymentHash(invoice.payment_hash().clone().into_inner());
958+
let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
961959
inbound_payments_lock.insert(
962960
payment_hash,
963961
PaymentInfo {
964962
preimage: None,
965-
secret: Some(invoice.payment_secret().clone()),
963+
secret: Some(*invoice.payment_secret()),
966964
status: PaymentStatus::Pending,
967965
amount_msat,
968966
},
969967
);
970968
Ok(invoice)
971969
}
972970

973-
/// Query for information about the status of a specific payment.
971+
/// Query for information about the status of a specific payment.
974972
pub fn payment_info(&self, payment_hash: &[u8; 32]) -> Option<PaymentInfo> {
975973
let payment_hash = PaymentHash(*payment_hash);
976974

src/peer_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<K: KVStorePersister> PeerInfoStorage<K> {
4242
.persist(PEER_INFO_PERSISTENCE_KEY, &*locked_peers)
4343
.map_err(|_| Error::PersistenceFailed)?;
4444

45-
return Ok(());
45+
Ok(())
4646
}
4747

4848
pub(crate) fn remove_peer(&self, peer_pubkey: &PublicKey) -> Result<(), Error> {
@@ -54,7 +54,7 @@ impl<K: KVStorePersister> PeerInfoStorage<K> {
5454
.persist(PEER_INFO_PERSISTENCE_KEY, &*locked_peers)
5555
.map_err(|_| Error::PersistenceFailed)?;
5656

57-
return Ok(());
57+
Ok(())
5858
}
5959

6060
pub(crate) fn peers(&self) -> Vec<PeerInfo> {
@@ -150,7 +150,7 @@ impl TryFrom<String> for PeerInfo {
150150
type Error = Error;
151151

152152
fn try_from(peer_pubkey_and_ip_addr: String) -> Result<Self, Self::Error> {
153-
let mut pubkey_and_addr = peer_pubkey_and_ip_addr.split("@");
153+
let mut pubkey_and_addr = peer_pubkey_and_ip_addr.split('@');
154154
let pubkey = pubkey_and_addr.next();
155155
let peer_addr_str = pubkey_and_addr.next();
156156
if pubkey.is_none() || peer_addr_str.is_none() {

0 commit comments

Comments
 (0)