Skip to content

Commit

Permalink
Add lightning node data to info event (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch authored Dec 27, 2024
1 parent 1aac442 commit 5746830
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
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!");
};

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();
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

0 comments on commit 5746830

Please sign in to comment.