Skip to content

Commit

Permalink
Merge pull request fedimint#6476 from joschisan/lnv2_touch_ups
Browse files Browse the repository at this point in the history
chore: make the code a bit cleaner
  • Loading branch information
tvolk131 authored Nov 28, 2024
2 parents 4d58f6a + f02364b commit a9a2146
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
29 changes: 9 additions & 20 deletions modules/fedimint-lnv2-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,7 @@ impl ServerModule for Lightning {
.await
.unwrap_or(0);

ensure!(
current_vote < vote,
"Unix time vote is redundant {current_vote} < {vote}"
);
ensure!(current_vote < vote, "Unix time vote is redundant");

Ok(())
}
Expand Down Expand Up @@ -647,21 +644,17 @@ impl Lightning {
.collect::<Vec<u64>>()
.await;

while counts.len() < num_peers.total() {
counts.push(0);
}

assert_eq!(counts.len(), num_peers.total());

counts.sort_unstable();

assert!(counts.first() <= counts.last());
counts.reverse();

assert!(counts.last() <= counts.first());

// The block count we select guarantees that any threshold of correct peers can
// increase the consensus block count and any consensus block count has been
// confirmed by a threshold of peers.

counts[num_peers.max_evil()]
counts.get(num_peers.threshold() - 1).copied().unwrap_or(0)
}

async fn consensus_unix_time(&self, dbtx: &mut DatabaseTransaction<'_>) -> u64 {
Expand All @@ -674,21 +667,17 @@ impl Lightning {
.collect::<Vec<u64>>()
.await;

while times.len() < num_peers.total() {
times.push(0);
}

assert_eq!(times.len(), num_peers.total());

times.sort_unstable();

assert!(times.first() <= times.last());
times.reverse();

assert!(times.last() <= times.first());

// The unix time we select guarantees that any threshold of correct peers can
// advance the consensus unix time and any consensus unix time has been
// confirmed by a threshold of peers.

times[num_peers.max_evil()]
times.get(num_peers.threshold() - 1).copied().unwrap_or(0)
}

async fn await_incoming_contract(
Expand Down
14 changes: 6 additions & 8 deletions modules/fedimint-lnv2-tests/src/bin/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use devimint::devfed::DevJitFed;
use devimint::federation::Client;
use devimint::version_constants::VERSION_0_5_0_ALPHA;
Expand Down Expand Up @@ -168,7 +166,6 @@ async fn test_payments(dev_fed: &DevJitFed) -> anyhow::Result<()> {
let lnd = dev_fed.lnd().await?;

let (hold_preimage, hold_invoice, hold_payment_hash) = lnd.create_hold_invoice(60000).await?;
let hold_invoice = Bolt11Invoice::from_str(&hold_invoice).expect("Could not parse invoice");

let gateway_pairs = [(gw_lnd, gw_ldk), (gw_ldk, gw_lnd)];

Expand All @@ -193,7 +190,7 @@ async fn test_payments(dev_fed: &DevJitFed) -> anyhow::Result<()> {
test_send(
&client,
&gw_send.addr,
&invoice,
&invoice.to_string(),
FinalSendOperationState::Refunded,
)
.await?;
Expand All @@ -219,7 +216,7 @@ async fn test_payments(dev_fed: &DevJitFed) -> anyhow::Result<()> {
test_send(
&client,
&gw_send.addr,
&invoice,
&invoice.to_string(),
FinalSendOperationState::Success,
)
.await?;
Expand All @@ -241,7 +238,7 @@ async fn test_payments(dev_fed: &DevJitFed) -> anyhow::Result<()> {
test_send(
&client,
&gw_send.addr,
&invoice,
&invoice.to_string(),
FinalSendOperationState::Success,
)
.await?;
Expand All @@ -264,6 +261,7 @@ async fn test_payments(dev_fed: &DevJitFed) -> anyhow::Result<()> {
}

info!("Testing LDK Gateway can pay HOLD invoice...");

try_join!(
test_send(
&client,
Expand Down Expand Up @@ -338,7 +336,7 @@ async fn receive(
async fn test_send(
client: &Client,
gateway: &String,
invoice: &Bolt11Invoice,
invoice: &String,
final_state: FinalSendOperationState,
) -> anyhow::Result<()> {
let send_op = serde_json::from_value::<OperationId>(
Expand All @@ -347,7 +345,7 @@ async fn test_send(
"module",
"lnv2",
"send",
invoice.to_string(),
invoice,
"--gateway",
gateway
)
Expand Down

0 comments on commit a9a2146

Please sign in to comment.