diff --git a/src/lnurlp.rs b/src/lnurlp.rs index d7d3421..4277ce8 100644 --- a/src/lnurlp.rs +++ b/src/lnurlp.rs @@ -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, @@ -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( diff --git a/src/mint.rs b/src/mint.rs index fc27f0a..5fb1e45 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -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; @@ -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; async fn register_new_federation(&self, invite_code: InviteCode) -> anyhow::Result<()>; - async fn get_gateway(&self, id: FederationId) -> Option; } #[derive(Clone)] @@ -57,11 +57,6 @@ impl MultiMintWrapperTrait for MultiMintWrapper { Ok(()) } - - async fn get_gateway(&self, id: FederationId) -> Option { - let client = self.get_federation_client(id).await?; - select_gateway(&client).await - } } pub(crate) async fn setup_multimint( @@ -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::(); + 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 {