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

Normalizes node features so they use LDK NodeFeatures #77

Merged
merged 1 commit into from
Aug 30, 2023
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
73 changes: 42 additions & 31 deletions sim-lib/src/cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,60 @@ pub struct ClnNode {

impl ClnNode {
pub async fn new(connection: ClnConnection) -> Result<Self, LightningError> {
let ca_pem = reader(&connection.ca_cert).await.map_err(|_| {
LightningError::ConnectionError("Cannot loads CA certificate".to_string())
})?;
let client_pem = reader(&connection.client_cert).await.map_err(|_| {
LightningError::ConnectionError("Cannot loads client certificate".to_string())
})?;
let client_key = reader(&connection.client_key)
.await
.map_err(|_| LightningError::ConnectionError("Cannot loads client key".to_string()))?;

let ca = Certificate::from_pem(ca_pem);
let ident = Identity::from_pem(client_pem, client_key);

let tls = ClientTlsConfig::new()
.domain_name("cln")
.identity(ident)
.ca_certificate(ca);

let channel = Channel::from_shared(connection.address.to_string())
.map_err(|err| LightningError::ConnectionError(err.to_string()))?
.tls_config(tls)
.map_err(|_| {
LightningError::ConnectionError("Cannot establish tls connection".to_string())
})?
.connect()
.await
.map_err(|_| {
LightningError::ConnectionError("Cannot connect to gRPC server".to_string())
})?;
let mut client = NodeClient::new(channel);

let GetinfoResponse { id, alias, .. } = client
.identity(Identity::from_pem(
reader(&connection.client_cert).await.map_err(|_| {
LightningError::ConnectionError("Cannot loads client certificate".to_string())
})?,
reader(&connection.client_key).await.map_err(|_| {
LightningError::ConnectionError("Cannot loads client key".to_string())
})?,
))
.ca_certificate(Certificate::from_pem(
reader(&connection.ca_cert).await.map_err(|_| {
LightningError::ConnectionError("Cannot loads CA certificate".to_string())
})?,
));

let mut client = NodeClient::new(
Channel::from_shared(connection.address.to_string())
.map_err(|err| LightningError::ConnectionError(err.to_string()))?
.tls_config(tls)
.map_err(|_| {
LightningError::ConnectionError("Cannot establish tls connection".to_string())
})?
.connect()
.await
.map_err(|_| {
LightningError::ConnectionError("Cannot connect to gRPC server".to_string())
})?,
);

let GetinfoResponse {
id,
alias,
our_features,
..
} = client
.getinfo(GetinfoRequest {})
.await
.map_err(|err| LightningError::GetInfoError(err.to_string()))?
.into_inner();

//FIXME: our_features is returning None, but it should not :S
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this comment still apply?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, and I'm not sure why :S

let features = if let Some(features) = our_features {
NodeFeatures::from_le_bytes(features.node)
} else {
NodeFeatures::empty()
};

Ok(Self {
client,
info: NodeInfo {
pubkey: PublicKey::from_slice(&id)
.map_err(|err| LightningError::GetInfoError(err.to_string()))?,
features: vec![],
features,
alias,
},
})
Expand Down
2 changes: 1 addition & 1 deletion sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub enum LightningError {
pub struct NodeInfo {
pub pubkey: PublicKey,
pub alias: String,
pub features: Vec<u32>,
pub features: NodeFeatures,
}

/// LightningNode represents the functionality that is required to execute events on a lightning node.
Expand Down
33 changes: 23 additions & 10 deletions sim-lib/src/lnd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashMap, str::FromStr};

use crate::{
Expand All @@ -14,7 +15,6 @@ use tonic_lnd::routerrpc::TrackPaymentRequest;
use tonic_lnd::{routerrpc::SendPaymentRequest, Client};
use triggered::Listener;

const KEYSEND_OPTIONAL: u32 = 55;
const KEYSEND_KEY: u64 = 5482373484;
const SEND_PAYMENT_TIMEOUT_SECS: i32 = 300;

Expand All @@ -24,6 +24,24 @@ pub struct LndNode {
info: NodeInfo,
}

// TODO: We could even generalize this to parse any type of Features
/// Parses the node features from the format returned by LND gRPC to LDK NodeFeatures
fn parse_node_features(features: HashSet<u32>) -> NodeFeatures {
let mut flags = vec![0; 256];

for f in features.into_iter() {
let byte_offset = (f / 8) as usize;
let mask = 1 << (f - 8 * byte_offset as u32);
if flags.len() <= byte_offset {
flags.resize(byte_offset + 1, 0u8);
}

flags[byte_offset] |= mask
}

NodeFeatures::from_le_bytes(flags)
}

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)
Expand All @@ -47,7 +65,7 @@ impl LndNode {
info: NodeInfo {
pubkey: PublicKey::from_str(&identity_pubkey)
.map_err(|err| LightningError::GetInfoError(err.to_string()))?,
features: features.keys().copied().collect(),
features: parse_node_features(features.keys().cloned().collect()),
alias,
},
})
Expand Down Expand Up @@ -171,15 +189,10 @@ impl LightningNode for LndNode {
.map_err(|err| LightningError::GetNodeInfoError(err.to_string()))?
.into_inner();

let mut nf = NodeFeatures::empty();

if let Some(node_info) = node_info.node {
// FIXME: We only care about the keysend feature now, but we should parse the whole feature vector
// into LDK's feature bitvector and properly construct NodeFeatures.
if node_info.features.contains_key(&KEYSEND_OPTIONAL) {
nf.set_keysend_optional()
}
Ok(nf)
Ok(parse_node_features(
node_info.features.keys().cloned().collect(),
))
} else {
Err(LightningError::GetNodeInfoError(
"Node not found".to_string(),
Expand Down
Loading