Skip to content

Commit 9ce78ae

Browse files
authored
Merge pull request #16 from G8XSU/update-fees
2 parents 7242146 + 1533826 commit 9ce78ae

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

protos/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ pub struct OpenChannelResponse {
229229
#[prost(bytes = "bytes", tag = "1")]
230230
pub user_channel_id: ::prost::bytes::Bytes,
231231
}
232+
/// Update the config for a previously opened channel.
233+
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.update_channel_config>
234+
#[allow(clippy::derive_partial_eq_without_eq)]
235+
#[derive(Clone, PartialEq, ::prost::Message)]
236+
pub struct UpdateChannelConfigRequest {
237+
/// The hex-encoded local `user_channel_id` of this channel.
238+
#[prost(string, tag = "1")]
239+
pub user_channel_id: ::prost::alloc::string::String,
240+
/// The hex-encoded public key of the counterparty node to update channel config with.
241+
#[prost(string, tag = "2")]
242+
pub counterparty_node_id: ::prost::alloc::string::String,
243+
/// The updated channel configuration settings for a channel.
244+
#[prost(message, optional, tag = "3")]
245+
pub channel_config: ::core::option::Option<ChannelConfig>,
246+
}
247+
#[allow(clippy::derive_partial_eq_without_eq)]
248+
#[derive(Clone, PartialEq, ::prost::Message)]
249+
pub struct UpdateChannelConfigResponse {}
232250
/// ChannelConfig represents the configuration settings for a channel in a Lightning Network node.
233251
/// See more: <https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelConfig.html>
234252
#[allow(clippy::derive_partial_eq_without_eq)]

protos/src/proto/ldk_node_server.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ message OpenChannelResponse {
220220
bytes user_channel_id = 1;
221221
}
222222

223+
// Update the config for a previously opened channel.
224+
// See more: https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.update_channel_config
225+
message UpdateChannelConfigRequest {
226+
227+
// The hex-encoded local `user_channel_id` of this channel.
228+
string user_channel_id = 1;
229+
230+
// The hex-encoded public key of the counterparty node to update channel config with.
231+
string counterparty_node_id = 2;
232+
233+
// The updated channel configuration settings for a channel.
234+
ChannelConfig channel_config = 3;
235+
}
236+
237+
message UpdateChannelConfigResponse {
238+
}
239+
223240
// ChannelConfig represents the configuration settings for a channel in a Lightning Network node.
224241
// See more: https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelConfig.html
225242
message ChannelConfig {

server/src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub(crate) mod list_payments;
1010
pub(crate) mod onchain_receive;
1111
pub(crate) mod onchain_send;
1212
pub(crate) mod open_channel;
13+
pub(crate) mod update_channel_config;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use ldk_node::bitcoin::secp256k1::PublicKey;
2+
use ldk_node::config::{ChannelConfig, MaxDustHTLCExposure};
3+
use ldk_node::{Node, UserChannelId};
4+
use protos::channel_config::MaxDustHtlcExposure;
5+
use protos::{UpdateChannelConfigRequest, UpdateChannelConfigResponse};
6+
use std::str::FromStr;
7+
use std::sync::Arc;
8+
9+
pub(crate) const UPDATE_CHANNEL_CONFIG_PATH: &str = "UpdateChannelConfig";
10+
11+
pub(crate) fn handle_update_channel_config_request(
12+
node: Arc<Node>, request: UpdateChannelConfigRequest,
13+
) -> Result<UpdateChannelConfigResponse, ldk_node::NodeError> {
14+
let user_channel_id: u128 =
15+
request.user_channel_id.parse().map_err(|_| ldk_node::NodeError::InvalidChannelId)?;
16+
17+
//FIXME: Use ldk/ldk-node's partial config update api.
18+
let current_config = node
19+
.list_channels()
20+
.into_iter()
21+
.find(|c| c.user_channel_id.0 == user_channel_id)
22+
.ok_or_else(|| ldk_node::NodeError::InvalidChannelId)?
23+
.config;
24+
25+
let updated_channel_config =
26+
build_updated_channel_config(current_config, request.channel_config.unwrap());
27+
28+
let counterparty_node_id = PublicKey::from_str(&request.counterparty_node_id)
29+
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;
30+
node.update_channel_config(
31+
&UserChannelId(user_channel_id),
32+
counterparty_node_id,
33+
updated_channel_config,
34+
)
35+
.map_err(ldk_node::NodeError::from)?;
36+
37+
Ok(UpdateChannelConfigResponse {})
38+
}
39+
40+
fn build_updated_channel_config(
41+
current_config: ChannelConfig, proto_channel_config: protos::ChannelConfig,
42+
) -> ChannelConfig {
43+
let max_dust_htlc_exposure = proto_channel_config
44+
.max_dust_htlc_exposure
45+
.map(|max_dust_htlc_exposure| match max_dust_htlc_exposure {
46+
MaxDustHtlcExposure::FixedLimitMsat(limit_msat) => {
47+
MaxDustHTLCExposure::FixedLimit { limit_msat }
48+
},
49+
MaxDustHtlcExposure::FeeRateMultiplier(multiplier) => {
50+
MaxDustHTLCExposure::FeeRateMultiplier { multiplier }
51+
},
52+
})
53+
.unwrap_or(current_config.max_dust_htlc_exposure);
54+
55+
let cltv_expiry_delta = proto_channel_config
56+
.cltv_expiry_delta.map(|c| u16::try_from(c).unwrap())
57+
.unwrap_or(current_config.cltv_expiry_delta);
58+
59+
ChannelConfig {
60+
forwarding_fee_proportional_millionths: proto_channel_config
61+
.forwarding_fee_proportional_millionths
62+
.unwrap_or(current_config.forwarding_fee_proportional_millionths),
63+
forwarding_fee_base_msat: proto_channel_config
64+
.forwarding_fee_base_msat
65+
.unwrap_or(current_config.forwarding_fee_base_msat),
66+
cltv_expiry_delta,
67+
max_dust_htlc_exposure,
68+
force_close_avoidance_max_fee_satoshis: proto_channel_config
69+
.force_close_avoidance_max_fee_satoshis
70+
.unwrap_or(current_config.force_close_avoidance_max_fee_satoshis),
71+
accept_underpaying_htlcs: proto_channel_config
72+
.accept_underpaying_htlcs
73+
.unwrap_or(current_config.accept_underpaying_htlcs),
74+
}
75+
}

server/src/service.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use crate::api::list_payments::{handle_list_payments_request, LIST_PAYMENTS_PATH
2525
use crate::api::onchain_receive::{handle_onchain_receive_request, ONCHAIN_RECEIVE_PATH};
2626
use crate::api::onchain_send::{handle_onchain_send_request, ONCHAIN_SEND_PATH};
2727
use crate::api::open_channel::{handle_open_channel, OPEN_CHANNEL_PATH};
28+
use crate::api::update_channel_config::{
29+
handle_update_channel_config_request, UPDATE_CHANNEL_CONFIG_PATH,
30+
};
2831

2932
#[derive(Clone)]
3033
pub struct NodeService {
@@ -62,6 +65,9 @@ impl Service<Request<Incoming>> for NodeService {
6265
OPEN_CHANNEL_PATH => Box::pin(handle_request(node, req, handle_open_channel)),
6366
CLOSE_CHANNEL_PATH => Box::pin(handle_request(node, req, handle_close_channel_request)),
6467
LIST_CHANNELS_PATH => Box::pin(handle_request(node, req, handle_list_channels_request)),
68+
UPDATE_CHANNEL_CONFIG_PATH => {
69+
Box::pin(handle_request(node, req, handle_update_channel_config_request))
70+
},
6571
GET_PAYMENT_DETAILS_PATH => {
6672
Box::pin(handle_request(node, req, handle_get_payment_details_request))
6773
},

0 commit comments

Comments
 (0)