Skip to content

Commit

Permalink
Update gateway cache every hour
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Mar 26, 2024
1 parent 447bd35 commit ea7e473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/lnurlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::str::FromStr;

use crate::{
invoice::{spawn_invoice_subscription, InvoiceState},
mint::select_gateway,
models::{invoice::NewInvoice, zaps::NewZap},
routes::{LnurlCallbackParams, LnurlCallbackResponse, LnurlVerifyResponse},
State,
Expand Down Expand Up @@ -103,11 +104,9 @@ pub async fn lnurl_callback(

let invoice_index = user.invoice_index;

let gateway = state
.mm
.get_gateway(federation_id)
let gateway = select_gateway(&client)
.await
.ok_or(anyhow!("Not gateway configured for federation"))?;
.ok_or(anyhow!("No gateway found for federation"))?;

let (op_id, pr, preimage) = ln
.create_bolt11_invoice_for_user_tweaked(
Expand Down
27 changes: 20 additions & 7 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use fedimint_ln_client::LightningClientModule;
use fedimint_ln_common::LightningGateway;
use log::error;
use std::collections::HashMap;
use std::time::Duration;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::RwLock;

Expand All @@ -18,7 +19,6 @@ pub(crate) trait MultiMintWrapperTrait {
async fn check_has_federation(&self, id: FederationId) -> bool;
async fn get_federation_client(&self, id: FederationId) -> Option<ClientHandleArc>;
async fn register_new_federation(&self, invite_code: InviteCode) -> anyhow::Result<()>;
async fn get_gateway(&self, id: FederationId) -> Option<LightningGateway>;
}

#[derive(Clone)]
Expand Down Expand Up @@ -57,11 +57,6 @@ impl MultiMintWrapperTrait for MultiMintWrapper {

Ok(())
}

async fn get_gateway(&self, id: FederationId) -> Option<LightningGateway> {
let client = self.get_federation_client(id).await?;
select_gateway(&client).await
}
}

pub(crate) async fn setup_multimint(
Expand Down Expand Up @@ -95,7 +90,25 @@ pub(crate) async fn setup_multimint(
fm: Arc::new(RwLock::new(mm)),
};

Ok(Arc::new(mmw))
let mmw = Arc::new(mmw);

// spawn thread to update gateways periodically, check every hour
let mmw_clone = mmw.clone();
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(60 * 60)).await;
let mm = mmw_clone.fm.read().await;
let clients = mm.clients.lock().await;
for (_, client) in clients.iter() {
let ln = client.get_first_module::<LightningClientModule>();
if let Err(e) = ln.update_gateway_cache(true).await {
error!("Failed to update gateway cache: {e}");
}
}
}
});

Ok(mmw)
}

pub(crate) async fn select_gateway(client: &ClientHandleArc) -> Option<LightningGateway> {
Expand Down

0 comments on commit ea7e473

Please sign in to comment.