Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
set preferred federation gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyRonning committed Jan 27, 2024
1 parent e86fd7f commit 6f95223
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ use lightning::{
};
use lightning_invoice::Bolt11Invoice;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::sync::atomic::{AtomicU32, Ordering};
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{
str::FromStr,
sync::atomic::{AtomicU32, Ordering},
};

// The amount of time in milliseconds to wait for
// checking the status of a fedimint payment. This
Expand Down Expand Up @@ -223,6 +226,75 @@ impl<S: MutinyStorage> FederationClient<S> {
return Err(MutinyError::NetworkMismatch);
}

// Set active gateway preference
let lightning_module = fedimint_client.get_first_module::<LightningClientModule>();
let gateways = lightning_module
.fetch_registered_gateways()
.await
.map_err(|e| {
log_warn!(
logger,
"Could not fetch gateways from federation {}: {e}",
federation_info.federation_id()
)
});

if let Ok(gateways) = gateways {
let mut active_choice: Option<bitcoin::secp256k1::PublicKey> = None;
for g in gateways {
let g_id = g.info.gateway_id;

// if the gateway node ID matches what we expect for our signet/mainnet
// these take the highest priority
if g_id
== bitcoin::secp256k1::PublicKey::from_str(
"0256f5ef1d986e9abf559651b7167de28bfd954683cd0f14703be12d1421aedc55",
)
.expect("should be valid pubkey")
&& fedimint_client.federation_id() // our signet federation
== FederationId::from_str(
"c8d423964c7ad944d30f57359b6e5b260e211dcfdb945140e28d4df51fd572d2",
)
.expect("should be a valid federation id")
{
active_choice = Some(g_id);
break;
} else if g.info.gateway_id
== bitcoin::secp256k1::PublicKey::from_str(
"025b9f090d3daab012346701f27d1c220d6d290f6b498255cddc492c255532a09d",
)
.expect("should be valid pubkey")
&& fedimint_client.federation_id() // our mainnet federation
== FederationId::from_str(
"c36038cce5a97e3467f03336fa8e7e3410960b81d1865cda2a609f70a8f51efb",
)
.expect("should be a valid federation id")
{
active_choice = Some(g_id);
break;
}

// TODO if vetted, set assigned
// TODO if not vetted, make sure fee is high enough
}

if let Some(a) = active_choice {
log_info!(
logger,
"Setting active gateway for federation {}: {:?}",
federation_info.federation_id(),
a
);
let _ = lightning_module.set_active_gateway(&a).await.map_err(|e| {
log_warn!(
logger,
"Could not set gateway for federation {}: {e}",
federation_info.federation_id()
)
});
}
}

log_debug!(logger, "Built fedimint client");
Ok(FederationClient {
uuid,
Expand Down

0 comments on commit 6f95223

Please sign in to comment.