From 7c10fbed18701426fcce4307b2fb74194686b715 Mon Sep 17 00:00:00 2001 From: G8XSU <3442979+G8XSU@users.noreply.github.com> Date: Mon, 14 Oct 2024 19:04:33 -0700 Subject: [PATCH] Add impl for UpdateChannelConfig Api. --- server/src/api/mod.rs | 2 + server/src/api/update_channel_config.rs | 77 +++++++++++++++++++++++++ server/src/service.rs | 2 + 3 files changed, 81 insertions(+) create mode 100644 server/src/api/update_channel_config.rs diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index c1079f2..3f6a577 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -10,3 +10,5 @@ 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; diff --git a/server/src/api/update_channel_config.rs b/server/src/api/update_channel_config.rs new file mode 100644 index 0000000..46e45b2 --- /dev/null +++ b/server/src/api/update_channel_config.rs @@ -0,0 +1,77 @@ +use ldk_node::bitcoin::secp256k1::PublicKey; +use ldk_node::{ChannelConfig, 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, request: UpdateChannelConfigRequest, +) -> Result { + let user_channel_id: u128 = + request.user_channel_id.parse().map_err(|_| ldk_node::NodeError::InvalidChannelId)?; + + let channel_config = proto_to_channel_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, + Arc::new(channel_config), + ) + .map_err(ldk_node::NodeError::from)?; + + Ok(UpdateChannelConfigResponse {}) +} + +fn proto_to_channel_config(proto_channel_config: &protos::ChannelConfig) -> ChannelConfig { + let channel_config = ChannelConfig::new(); + + match proto_channel_config.forwarding_fee_proportional_millionths { + Some(forwarding_fee_proportional_millionths) => channel_config + .set_forwarding_fee_proportional_millionths(forwarding_fee_proportional_millionths), + None => {}, + } + + match proto_channel_config.forwarding_fee_base_msat { + Some(forwarding_fee_base_msat) => { + channel_config.set_forwarding_fee_base_msat(forwarding_fee_base_msat) + }, + None => {}, + } + + match proto_channel_config.cltv_expiry_delta { + Some(cltv_expiry_delta) => channel_config.set_cltv_expiry_delta(cltv_expiry_delta as u16), + None => {}, + } + + match proto_channel_config.force_close_avoidance_max_fee_satoshis { + Some(force_close_avoidance_max_fee_satoshis) => channel_config + .set_force_close_avoidance_max_fee_satoshis(force_close_avoidance_max_fee_satoshis), + None => {}, + } + + match proto_channel_config.accept_underpaying_htlcs { + Some(accept_underpaying_htlcs) => { + channel_config.set_accept_underpaying_htlcs(accept_underpaying_htlcs) + }, + None => {}, + } + + match &proto_channel_config.max_dust_htlc_exposure { + Some(max_dust_htlc_exposure) => match max_dust_htlc_exposure { + &MaxDustHtlcExposure::FixedLimitMsat(limit) => { + channel_config.set_max_dust_htlc_exposure_from_fixed_limit(limit) + }, + &MaxDustHtlcExposure::FeeRateMultiplier(multiplier) => { + channel_config.set_max_dust_htlc_exposure_from_fee_rate_multiplier(multiplier) + }, + }, + None => {}, + } + // FIXME: set max_dust_htlc_exposure + channel_config +} diff --git a/server/src/service.rs b/server/src/service.rs index 510131d..03c76f5 100644 --- a/server/src/service.rs +++ b/server/src/service.rs @@ -21,6 +21,7 @@ use crate::api::get_payment_details::{ handle_get_payment_details_request, GET_PAYMENT_DETAILS_PATH, }; use crate::api::list_channels::{handle_list_channels_request, LIST_CHANNELS_PATH}; +use crate::api::update_channel_config::{handle_update_channel_config_request, UPDATE_CHANNEL_CONFIG_PATH}; 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}; @@ -62,6 +63,7 @@ impl Service> 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)) },