Skip to content

Commit

Permalink
Merge pull request #13 from okjodom/node-nfo
Browse files Browse the repository at this point in the history
feat: get node info
  • Loading branch information
carlaKC authored Aug 16, 2023
2 parents 39dc8d1 + 4868878 commit 51147b0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 13 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ async fn main() -> anyhow::Result<()> {

for node in nodes {
let lnd = LndNode::new(node.address, node.macaroon, node.cert).await?;
clients.insert(node.id, Arc::new(lnd));

let node_info = lnd.get_info().await?;
println!("Node info {:?}", node_info);

clients.insert(node_info.pubkey, Arc::new(lnd));
}

let sim = Simulation::new(clients, activity);
Expand Down
3 changes: 3 additions & 0 deletions sim-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ serde_json = "1.0.104"
bitcoin = { version = "0.30.1", features=["serde"] }
lightning = { version = "0.0.116" }
tonic_lnd = { git = "https://github.com/fedimint/tonic_lnd", branch="master", features=["lightningrpc", "routerrpc"]}
async-trait = "0.1.73"
thiserror = "1.0.45"
log = "0.4.20"
47 changes: 41 additions & 6 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use async_trait::async_trait;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::PaymentHash;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc, time::SystemTime};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::SystemTime;
use thiserror::Error;

pub mod lnd;

Expand Down Expand Up @@ -35,15 +39,46 @@ pub struct ActivityDefinition {
pub amount_msat: u64,
}

// Phase 2: Event Queue
#[derive(Debug, Error)]
pub enum SimulationError {
#[error("Lightning Error: {0:?}")]
LightningError(#[from] LightningError),
#[error("Other: {0:?}")]
Error(#[from] anyhow::Error),
}

#[allow(dead_code)]
pub enum PaymentError {}
#[derive(Debug, Error)]
pub enum LightningError {
#[error("Node connection error {0}")]
ConnectionError(String),
#[error("Get info error {0}")]
GetInfoError(String),
#[error("Send payment error {0}")]
SendPaymentError(String),
}

#[derive(Debug, Clone)]
pub struct NodeInfo {
pub pubkey: PublicKey,
pub alias: String,
pub features: Vec<u32>,
}

/// LightningNode represents the functionality that is required to execute events on a lightning node.
#[async_trait]
pub trait LightningNode {
fn send_payment(&self, dest: PublicKey, amt_msat: u64) -> anyhow::Result<PaymentHash>;
fn track_payment(&self, hash: PaymentHash) -> Result<(), PaymentError>;
/// Get information about the node.
async fn get_info(&self) -> Result<NodeInfo, LightningError>;

/// Keysend payment worth `amount_msat` from a source node to the destination node.
async fn send_payment(
&self,
dest: PublicKey,
amount_msat: u64,
) -> Result<PaymentHash, LightningError>;

/// Track a payment with the specified hash.
async fn track_payment(&self, hash: PaymentHash) -> Result<(), LightningError>;
}

#[allow(dead_code)]
Expand Down
52 changes: 46 additions & 6 deletions sim-lib/src/lnd.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
use crate::{LightningNode, PaymentError};
use std::str::FromStr;

use crate::{LightningError, LightningNode, NodeInfo};
use async_trait::async_trait;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::PaymentHash;
use tonic_lnd::Client;
use tonic_lnd::{
lnrpc::{GetInfoRequest, GetInfoResponse},
Client,
};

#[allow(dead_code)]
pub struct LndNode {
client: Client,
}

impl LndNode {
pub async fn new(address: String, macaroon: String, cert: String) -> anyhow::Result<Self> {
let client = tonic_lnd::connect(address, cert, macaroon).await?;
pub async fn new(
address: String,
macaroon: String,
cert: String,
) -> Result<Self, LightningError> {
let client = tonic_lnd::connect(address, cert, macaroon)
.await
.map_err(|err| LightningError::ConnectionError(err.to_string()))?;
Ok(Self { client })
}
}

#[async_trait]
impl LightningNode for LndNode {
fn send_payment(&self, _dest: PublicKey, _amt_msat: u64) -> anyhow::Result<PaymentHash> {
async fn get_info(&self) -> Result<NodeInfo, LightningError> {
let mut client = self.client.clone();
let ln_client = client.lightning();

let GetInfoResponse {
identity_pubkey,
features,
alias,
..
} = ln_client
.get_info(GetInfoRequest {})
.await
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
.into_inner();

Ok(NodeInfo {
pubkey: PublicKey::from_str(&identity_pubkey)
.map_err(|err| LightningError::GetInfoError(err.to_string()))?,
features: features.keys().copied().collect(),
alias,
})
}

async fn send_payment(
&self,
_dest: PublicKey,
_amount_msat: u64,
) -> Result<PaymentHash, LightningError> {
unimplemented!()
}

fn track_payment(&self, _hash: PaymentHash) -> Result<(), PaymentError> {
async fn track_payment(&self, _hash: PaymentHash) -> Result<(), LightningError> {
unimplemented!()
}
}

0 comments on commit 51147b0

Please sign in to comment.