Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lightning node data to info event #408

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/lightning/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod invoice;
use std::cmp::Ordering;

use crate::cli::settings::Settings;
use crate::error::MostroError;
Expand All @@ -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,
Expand Down Expand Up @@ -253,4 +252,38 @@ impl LndConnector {

Ok(())
}

pub async fn get_node_info(&mut self) -> Result<GetInfoResponse, MostroError> {
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<String>,
pub networks: Vec<String>,
pub uris: Vec<String>,
}

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(),
}
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@ use util::{get_nostr_client, invoice_subscribe};

static MOSTRO_CONFIG: OnceLock<Settings> = OnceLock::new();
static NOSTR_CLIENT: OnceLock<Client> = OnceLock::new();
static LN_STATUS: OnceLock<LnStatus> = OnceLock::new();

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -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!");
};
Comment on lines +90 to +94
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure fallback if LN node is unreachable.
Currently, a panic is triggered if the LN status fails to set. Consider a more graceful fallback if node connectivity fails, e.g., partial operation mode.


if let Ok(held_invoices) = find_held_invoices(&pool).await {
for invoice in held_invoices.iter() {
Expand Down
37 changes: 31 additions & 6 deletions src/nip33.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::lightning::LnStatus;
use crate::Settings;
use chrono::Duration;
use mostro_core::order::{Order, Status};
Expand Down Expand Up @@ -133,21 +134,17 @@ pub fn order_to_tags(order: &Order, reputation: Option<Rating>) -> 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(
Expand Down Expand Up @@ -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()],
Expand Down
7 changes: 4 additions & 3 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Unwrapping LN_STATUS can panic if LN status was never set.
Consider logging or gracefully handling missing LN status rather than unwrap(), as the node may be offline.

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,
Expand Down
Loading