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 15, 2024
1 parent d647505 commit 7c10fbe
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
77 changes: 77 additions & 0 deletions server/src/api/update_channel_config.rs
Original file line number Diff line number Diff line change
@@ -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<Node>, request: UpdateChannelConfigRequest,
) -> Result<UpdateChannelConfigResponse, ldk_node::NodeError> {
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
}
2 changes: 2 additions & 0 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -62,6 +63,7 @@ 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 7c10fbe

Please sign in to comment.