Skip to content

Commit

Permalink
Add impl for UpdateChannelConfig Api.
Browse files Browse the repository at this point in the history
  • Loading branch information
G8XSU committed Oct 21, 2024
1 parent a440a85 commit 1533826
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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;

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

0 comments on commit 1533826

Please sign in to comment.