From 5746830667486fde7df08318350eee477a51e035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Calder=C3=B3n?= Date: Fri, 27 Dec 2024 15:56:09 -0300 Subject: [PATCH] Add lightning node data to info event (#408) --- src/lightning/mod.rs | 39 ++++++++++++++++++++++++++++++++++++--- src/main.rs | 7 +++++++ src/nip33.rs | 37 +++++++++++++++++++++++++++++++------ src/scheduler.rs | 7 ++++--- 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/lightning/mod.rs b/src/lightning/mod.rs index ae3fbd87..511828e4 100644 --- a/src/lightning/mod.rs +++ b/src/lightning/mod.rs @@ -1,5 +1,4 @@ pub mod invoice; -use std::cmp::Ordering; use crate::cli::settings::Settings; use crate::error::MostroError; @@ -12,14 +11,14 @@ use fedimint_tonic_lnd::invoicesrpc::{ AddHoldInvoiceRequest, AddHoldInvoiceResp, CancelInvoiceMsg, CancelInvoiceResp, SettleInvoiceMsg, SettleInvoiceResp, }; -use fedimint_tonic_lnd::lnrpc::{invoice::InvoiceState, Payment}; +use fedimint_tonic_lnd::lnrpc::{invoice::InvoiceState, GetInfoRequest, GetInfoResponse, Payment}; use fedimint_tonic_lnd::routerrpc::{SendPaymentRequest, TrackPaymentRequest}; use fedimint_tonic_lnd::Client; use nostr_sdk::nostr::hashes::hex::FromHex; use nostr_sdk::nostr::secp256k1::rand::{self, RngCore}; +use std::cmp::Ordering; use tokio::sync::mpsc::Sender; use tracing::info; -// use tonic_lnd::lnrpc:: pub struct LndConnector { client: Client, @@ -253,4 +252,38 @@ impl LndConnector { Ok(()) } + + pub async fn get_node_info(&mut self) -> Result { + let info = self.client.lightning().get_info(GetInfoRequest {}).await; + + match info { + Ok(i) => Ok(i.into_inner()), + Err(e) => Err(MostroError::LnNodeError(e.to_string())), + } + } +} + +#[derive(Debug)] +pub struct LnStatus { + pub version: String, + pub node_pubkey: String, + pub commit_hash: String, + pub node_alias: String, + pub chains: Vec, + pub networks: Vec, + pub uris: Vec, +} + +impl LnStatus { + pub fn from_get_info_response(info: GetInfoResponse) -> Self { + Self { + version: info.version, + node_pubkey: info.identity_pubkey, + commit_hash: info.commit_hash, + node_alias: info.alias, + chains: info.chains.iter().map(|c| c.chain.to_string()).collect(), + networks: info.chains.iter().map(|c| c.network.to_string()).collect(), + uris: info.uris.iter().map(|u| u.to_string()).collect(), + } + } } diff --git a/src/main.rs b/src/main.rs index 49ddd707..bec7010e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ pub mod util; use crate::app::run; use crate::cli::settings::{init_global_settings, Settings}; use crate::cli::settings_init; +use crate::lightning::LnStatus; use anyhow::Result; use db::find_held_invoices; use lightning::LndConnector; @@ -32,6 +33,7 @@ use util::{get_nostr_client, invoice_subscribe}; static MOSTRO_CONFIG: OnceLock = OnceLock::new(); static NOSTR_CLIENT: OnceLock = OnceLock::new(); +static LN_STATUS: OnceLock = OnceLock::new(); #[tokio::main] async fn main() -> Result<()> { @@ -85,6 +87,11 @@ async fn main() -> Result<()> { client.subscribe(vec![subscription], None).await?; let mut ln_client = LndConnector::new().await?; + let ln_status = ln_client.get_node_info().await?; + let ln_status = LnStatus::from_get_info_response(ln_status); + if LN_STATUS.set(ln_status).is_err() { + panic!("No connection to LND node - shutting down Mostro!"); + }; if let Ok(held_invoices) = find_held_invoices(&pool).await { for invoice in held_invoices.iter() { diff --git a/src/nip33.rs b/src/nip33.rs index 1a668493..cc597e1a 100644 --- a/src/nip33.rs +++ b/src/nip33.rs @@ -1,3 +1,4 @@ +use crate::lightning::LnStatus; use crate::Settings; use chrono::Duration; use mostro_core::order::{Order, Status}; @@ -133,21 +134,17 @@ pub fn order_to_tags(order: &Order, reputation: Option) -> Tags { /// # Arguments /// /// -pub fn info_to_tags(mostro_pubkey: &PublicKey) -> Tags { +pub fn info_to_tags(ln_status: &LnStatus) -> Tags { let mostro_settings = Settings::get_mostro(); let ln_settings = Settings::get_ln(); let tags: Tags = Tags::new(vec![ - Tag::custom( - TagKind::Custom(Cow::Borrowed("mostro_pubkey")), - vec![mostro_pubkey.to_string()], - ), Tag::custom( TagKind::Custom(Cow::Borrowed("mostro_version")), vec![env!("CARGO_PKG_VERSION").to_string()], ), Tag::custom( - TagKind::Custom(Cow::Borrowed("mostro_commit_id")), + TagKind::Custom(Cow::Borrowed("mostro_commit_hash")), vec![env!("GIT_HASH").to_string()], ), Tag::custom( @@ -186,6 +183,34 @@ pub fn info_to_tags(mostro_pubkey: &PublicKey) -> Tags { TagKind::Custom(Cow::Borrowed("invoice_expiration_window")), vec![ln_settings.hold_invoice_expiration_window.to_string()], ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_version")), + vec![ln_status.version.to_string()], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_node_pubkey")), + vec![ln_status.node_pubkey.to_string()], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_commit_hash")), + vec![ln_status.commit_hash.to_string()], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_node_alias")), + vec![ln_status.node_alias.to_string()], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_chains")), + vec![ln_status.chains.join(",")], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_networks")), + vec![ln_status.networks.join(",")], + ), + Tag::custom( + TagKind::Custom(Cow::Borrowed("lnd_uris")), + vec![ln_status.uris.join(",")], + ), Tag::custom( TagKind::Custom(Cow::Borrowed("y")), vec!["mostrop2p".to_string()], diff --git a/src/scheduler.rs b/src/scheduler.rs index 1e66e8e4..338ff179 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -5,6 +5,7 @@ use crate::db::*; use crate::lightning::LndConnector; use crate::util; use crate::util::get_nostr_client; +use crate::LN_STATUS; use chrono::{TimeDelta, Utc}; use mostro_core::order::{Kind, Status}; @@ -69,13 +70,13 @@ async fn job_info_event_send() { Err(e) => return error!("{e}"), }; let interval = Settings::get_mostro().publish_mostro_info_interval as u64; - + let ln_status = LN_STATUS.get().unwrap(); tokio::spawn(async move { loop { info!("Sending info about mostro"); - let tags = crate::nip33::info_to_tags(&mostro_keys.public_key()); - let id = format!("info-{}", mostro_keys.public_key()); + let tags = crate::nip33::info_to_tags(ln_status); + let id = mostro_keys.public_key().to_string(); let info_ev = match crate::nip33::new_event(&mostro_keys, "", id, tags) { Ok(info) => info,