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 implementation for UpdateChannelConfig Api. #16

Merged
merged 2 commits into from
Oct 22, 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
18 changes: 18 additions & 0 deletions protos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ pub struct OpenChannelResponse {
#[prost(bytes = "bytes", tag = "1")]
pub user_channel_id: ::prost::bytes::Bytes,
}
/// Update the config for a previously opened channel.
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.update_channel_config>
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UpdateChannelConfigRequest {
/// The hex-encoded local `user_channel_id` of this channel.
#[prost(string, tag = "1")]
pub user_channel_id: ::prost::alloc::string::String,
/// The hex-encoded public key of the counterparty node to update channel config with.
#[prost(string, tag = "2")]
pub counterparty_node_id: ::prost::alloc::string::String,
/// The updated channel configuration settings for a channel.
#[prost(message, optional, tag = "3")]
pub channel_config: ::core::option::Option<ChannelConfig>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UpdateChannelConfigResponse {}
/// ChannelConfig represents the configuration settings for a channel in a Lightning Network node.
/// See more: <https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelConfig.html>
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down
17 changes: 17 additions & 0 deletions protos/src/proto/ldk_node_server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ message OpenChannelResponse {
bytes user_channel_id = 1;
}

// Update the config for a previously opened channel.
// See more: https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.update_channel_config
message UpdateChannelConfigRequest {

// The hex-encoded local `user_channel_id` of this channel.
string user_channel_id = 1;

// The hex-encoded public key of the counterparty node to update channel config with.
string counterparty_node_id = 2;

// The updated channel configuration settings for a channel.
ChannelConfig channel_config = 3;
}

message UpdateChannelConfigResponse {
}

// ChannelConfig represents the configuration settings for a channel in a Lightning Network node.
// See more: https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelConfig.html
message ChannelConfig {
Expand Down
1 change: 1 addition & 0 deletions server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub(crate) mod list_payments;
pub(crate) mod onchain_receive;
pub(crate) mod onchain_send;
pub(crate) mod open_channel;
pub(crate) mod update_channel_config;
75 changes: 75 additions & 0 deletions server/src/api/update_channel_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
use ldk_node::{Node, UserChannelId};
use protos::channel_config::MaxDustHtlcExposure;
use protos::{UpdateChannelConfigRequest, UpdateChannelConfigResponse};
use std::str::FromStr;
use std::sync::Arc;

pub(crate) const UPDATE_CHANNEL_CONFIG_PATH: &str = "UpdateChannelConfig";

pub(crate) fn handle_update_channel_config_request(
node: Arc<Node>, request: UpdateChannelConfigRequest,
) -> Result<UpdateChannelConfigResponse, ldk_node::NodeError> {
let user_channel_id: u128 =
request.user_channel_id.parse().map_err(|_| ldk_node::NodeError::InvalidChannelId)?;

//FIXME: Use ldk/ldk-node's partial config update api.
let current_config = node
.list_channels()
.into_iter()
.find(|c| c.user_channel_id.0 == user_channel_id)
.ok_or_else(|| ldk_node::NodeError::InvalidChannelId)?
.config;
Comment on lines +17 to +23
Copy link

Choose a reason for hiding this comment

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

Should we do this now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Afaiu, Currently there is no api in ldk-node for partial config update, this will have to wait until next ldk-node release.

Since the functionality/user-interface here is equivalent, I don't think there is much point in waiting till then.
Created: lightningdevkit/ldk-node#385

We can remove this once that is complete.

Copy link

Choose a reason for hiding this comment

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

Ah, right!


let updated_channel_config =
build_updated_channel_config(current_config, request.channel_config.unwrap());

let counterparty_node_id = PublicKey::from_str(&request.counterparty_node_id)
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;
node.update_channel_config(
&UserChannelId(user_channel_id),
counterparty_node_id,
updated_channel_config,
)
.map_err(ldk_node::NodeError::from)?;

Ok(UpdateChannelConfigResponse {})
}

fn build_updated_channel_config(
current_config: ChannelConfig, proto_channel_config: protos::ChannelConfig,
) -> ChannelConfig {
let max_dust_htlc_exposure = proto_channel_config
.max_dust_htlc_exposure
.map(|max_dust_htlc_exposure| match max_dust_htlc_exposure {
MaxDustHtlcExposure::FixedLimitMsat(limit_msat) => {
MaxDustHTLCExposure::FixedLimit { limit_msat }
},
MaxDustHtlcExposure::FeeRateMultiplier(multiplier) => {
MaxDustHTLCExposure::FeeRateMultiplier { multiplier }
},
})
.unwrap_or(current_config.max_dust_htlc_exposure);

let cltv_expiry_delta = proto_channel_config
.cltv_expiry_delta.map(|c| u16::try_from(c).unwrap())
.unwrap_or(current_config.cltv_expiry_delta);

ChannelConfig {
forwarding_fee_proportional_millionths: proto_channel_config
.forwarding_fee_proportional_millionths
.unwrap_or(current_config.forwarding_fee_proportional_millionths),
forwarding_fee_base_msat: proto_channel_config
.forwarding_fee_base_msat
.unwrap_or(current_config.forwarding_fee_base_msat),
cltv_expiry_delta,
max_dust_htlc_exposure,
force_close_avoidance_max_fee_satoshis: proto_channel_config
.force_close_avoidance_max_fee_satoshis
.unwrap_or(current_config.force_close_avoidance_max_fee_satoshis),
accept_underpaying_htlcs: proto_channel_config
.accept_underpaying_htlcs
.unwrap_or(current_config.accept_underpaying_htlcs),
}
}
6 changes: 6 additions & 0 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use crate::api::list_payments::{handle_list_payments_request, LIST_PAYMENTS_PATH
use crate::api::onchain_receive::{handle_onchain_receive_request, ONCHAIN_RECEIVE_PATH};
use crate::api::onchain_send::{handle_onchain_send_request, ONCHAIN_SEND_PATH};
use crate::api::open_channel::{handle_open_channel, OPEN_CHANNEL_PATH};
use crate::api::update_channel_config::{
handle_update_channel_config_request, UPDATE_CHANNEL_CONFIG_PATH,
};

#[derive(Clone)]
pub struct NodeService {
Expand Down Expand Up @@ -62,6 +65,9 @@ impl Service<Request<Incoming>> for NodeService {
OPEN_CHANNEL_PATH => Box::pin(handle_request(node, req, handle_open_channel)),
CLOSE_CHANNEL_PATH => Box::pin(handle_request(node, req, handle_close_channel_request)),
LIST_CHANNELS_PATH => Box::pin(handle_request(node, req, handle_list_channels_request)),
UPDATE_CHANNEL_CONFIG_PATH => {
Box::pin(handle_request(node, req, handle_update_channel_config_request))
},
GET_PAYMENT_DETAILS_PATH => {
Box::pin(handle_request(node, req, handle_get_payment_details_request))
},
Expand Down