|
| 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 | +} |
0 commit comments