-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add impl for UpdateChannelConfig Api.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters