Skip to content

Commit

Permalink
Allows aliases within node definition in the config file
Browse files Browse the repository at this point in the history
Also makes sure that, if a public key is provided, it matches the one obtained
by the backend
  • Loading branch information
sr-gi committed Sep 15, 2023
1 parent 9263a08 commit 345ce66
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 24 deletions.
16 changes: 8 additions & 8 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ async fn main() -> anyhow::Result<()> {
// TODO: We should simplify this into two minimal branches plus shared logging and inserting into the list
match connection {
NodeConnection::LND(c) => {
let node_id = c.id;
let lnd = LndNode::new(c).await?;
let node_info = lnd.get_info();

log::info!(
"Connected to {} - Node ID: {}.",
lnd.get_info().alias,
lnd.get_info().pubkey
node_info.alias,
node_info.pubkey
);

clients.insert(node_id, Arc::new(Mutex::new(lnd)));
clients.insert(node_info.pubkey, Arc::new(Mutex::new(lnd)));
}
NodeConnection::CLN(c) => {
let node_id = c.id;
let cln = ClnNode::new(c).await?;
let node_info = cln.get_info();

log::info!(
"Connected to {} - Node ID: {}.",
cln.get_info().alias,
cln.get_info().pubkey
node_info.alias,
node_info.pubkey
);

clients.insert(node_id, Arc::new(Mutex::new(cln)));
clients.insert(node_info.pubkey, Arc::new(Mutex::new(cln)));
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion sim-lib/src/cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ClnNode {

let GetinfoResponse {
id,
alias,
mut alias,
our_features,
..
} = client
Expand All @@ -66,6 +66,20 @@ impl ClnNode {
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
.into_inner();

let node_id = PublicKey::from_slice(&id).unwrap();

match connection.id {
crate::NodeId::PublicKey(pk) => {
if pk != node_id {
return Err(LightningError::ValidationError(format!(
"The provided node id does not match the one returned by the backend ({} != {})",
pk, node_id,
)));
}
}
crate::NodeId::Alias(a) => alias = Some(a),
}

//FIXME: our_features is returning None, but it should not :S
let features = if let Some(features) = our_features {
NodeFeatures::from_le_bytes(features.node)
Expand Down
24 changes: 16 additions & 8 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ pub enum NodeConnection {
CLN(ClnConnection),
}

#[derive(Serialize, Debug, Clone)]
pub enum NodeId {
PublicKey(PublicKey),
Alias(String),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LndConnection {
pub id: PublicKey,
#[serde(with = "serializers::serde_node_id")]
pub id: NodeId,
pub address: String,
#[serde(deserialize_with = "serializers::deserialize_path")]
pub macaroon: String,
Expand All @@ -41,7 +48,8 @@ pub struct LndConnection {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClnConnection {
pub id: PublicKey,
#[serde(with = "serializers::serde_node_id")]
pub id: NodeId,
pub address: String,
#[serde(deserialize_with = "serializers::deserialize_path")]
pub ca_cert: String,
Expand Down Expand Up @@ -85,19 +93,19 @@ pub enum SimulationError {
// Phase 2: Event Queue
#[derive(Debug, Error)]
pub enum LightningError {
#[error("Node connection error {0}")]
#[error("Node connection error: {0}")]
ConnectionError(String),
#[error("Get info error {0}")]
#[error("Get info error: {0}")]
GetInfoError(String),
#[error("Send payment error {0}")]
#[error("Send payment error: {0}")]
SendPaymentError(String),
#[error("Track payment error {0}")]
#[error("Track payment error: {0}")]
TrackPaymentError(String),
#[error("Invalid payment hash")]
InvalidPaymentHash,
#[error("Get node info error {0}")]
#[error("Get node info error: {0}")]
GetNodeInfoError(String),
#[error("Config validation failed {0}")]
#[error("Config validation failed: {0}")]
ValidationError(String),
#[error("RPC error: {0:?}")]
RpcError(#[from] tonic_lnd::tonic::Status),
Expand Down
24 changes: 19 additions & 5 deletions sim-lib/src/lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ fn parse_node_features(features: HashSet<u32>) -> NodeFeatures {
}

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

let GetInfoResponse {
identity_pubkey,
features,
alias,
mut alias,
..
} = client
.lightning()
Expand All @@ -59,6 +60,19 @@ impl LndNode {
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
.into_inner();

let node_id = PublicKey::from_str(&identity_pubkey).unwrap();
match connection.id {
crate::NodeId::PublicKey(pk) => {
if pk != node_id {
return Err(LightningError::ValidationError(format!(
"The provided node id does not match the one returned by the backend ({} != {})",
pk, node_id,
)));
}
}
crate::NodeId::Alias(a) => alias = a,
}

Ok(Self {
client,
info: NodeInfo {
Expand Down
33 changes: 31 additions & 2 deletions sim-lib/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use expanduser::expanduser;
use serde::Deserialize;

pub mod serde_payment_hash {

use super::*;
use lightning::ln::PaymentHash;
use serde::Deserialize;

pub fn serialize<S>(hash: &PaymentHash, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -28,6 +27,36 @@ pub mod serde_payment_hash {
}
}

pub mod serde_node_id {
use super::*;
use std::str::FromStr;

use crate::NodeId;
use bitcoin::secp256k1::PublicKey;

pub fn serialize<S>(id: &NodeId, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&match id {
NodeId::PublicKey(p) => p.to_string(),
NodeId::Alias(s) => s.to_string(),
})
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<NodeId, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if let Ok(pk) = PublicKey::from_str(&s) {
Ok(NodeId::PublicKey(pk))
} else {
Ok(NodeId::Alias(s))
}
}
}

pub fn deserialize_path<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down

0 comments on commit 345ce66

Please sign in to comment.