From 7ef7d92cacdcd4f03308a740c9fd4629110c78c5 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 8 Aug 2023 10:57:11 +0200 Subject: [PATCH] f Make `ChannelConfig` a UniFFI `interface` --- bindings/ldk_node.udl | 28 ++++----- src/lib.rs | 15 +++-- src/types.rs | 128 +++++++++++++++++++++++++++--------------- 3 files changed, 106 insertions(+), 65 deletions(-) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 8e03ba42e..6bbdd95d7 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -204,20 +204,20 @@ dictionary PeerDetails { boolean is_connected; }; -dictionary ChannelConfig { - u32 forwarding_fee_proportional_millionths = 0; - u32 forwarding_fee_base_msat = 1000; - u16 cltv_expiry_delta = 72; - MaxDustHTLCExposure max_dust_htlc_exposure; - u64 force_close_avoidance_max_fee_satoshis = 1000; - boolean accept_underpaying_htlcs = false; -}; - -interface MaxDustHTLCExposure { - [Name=from_fixed_limit] - constructor(u64 limit); - [Name=from_fee_multiplier] - constructor(u64 multiplier); +interface ChannelConfig { + constructor(); + u32 forwarding_fee_proportional_millionths(); + void set_forwarding_fee_proportional_millionths(u32 value); + u32 forwarding_fee_base_msat(); + void set_forwarding_fee_base_msat(u32 fee_msat); + u16 cltv_expiry_delta(); + void set_cltv_expiry_delta(u16 value); + u64 force_close_avoidance_max_fee_satoshis(); + void set_force_close_avoidance_max_fee_satoshis(u64 value_sat); + boolean accept_underpaying_htlcs(); + void set_accept_underpaying_htlcs(boolean value); + void set_max_dust_htlc_exposure_from_fixed_limit(u64 limit_msat); + void set_max_dust_htlc_exposure_from_fee_rate_multiplier(u64 multiplier); }; enum LogLevel { diff --git a/src/lib.rs b/src/lib.rs index 3aa9af0da..b06f0a2ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,7 +120,7 @@ use payment_store::PaymentStore; pub use payment_store::{PaymentDetails, PaymentDirection, PaymentStatus}; use peer_store::{PeerInfo, PeerStore}; use types::{ChainMonitor, ChannelManager, KeysManager, NetworkGraph, PeerManager, Router, Scorer}; -pub use types::{ChannelDetails, ChannelId, MaxDustHTLCExposure, PeerDetails, UserChannelId}; +pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId}; use wallet::Wallet; use logger::{log_debug, log_error, log_info, log_trace, FilesystemLogger, Logger}; @@ -890,7 +890,7 @@ impl Node { /// Returns a temporary channel id. pub fn connect_open_channel( &self, node_id: PublicKey, address: NetAddress, channel_amount_sats: u64, - push_to_counterparty_msat: Option, channel_config: Option, + push_to_counterparty_msat: Option, channel_config: Option>, announce_channel: bool, ) -> Result<(), Error> { let rt_lock = self.runtime.read().unwrap(); @@ -920,13 +920,14 @@ impl Node { }) })?; + let channel_config = (*(channel_config.unwrap_or_default())).clone().into(); let user_config = UserConfig { channel_handshake_limits: Default::default(), channel_handshake_config: ChannelHandshakeConfig { announced_channel: announce_channel, ..Default::default() }, - channel_config: channel_config.unwrap_or_default().into(), + channel_config, ..Default::default() }; @@ -1030,10 +1031,14 @@ impl Node { /// Update the config for a previously opened channel. pub fn update_channel_config( &self, channel_id: &ChannelId, counterparty_node_id: PublicKey, - channel_config: ChannelConfig, + channel_config: Arc, ) -> Result<(), Error> { self.channel_manager - .update_channel_config(&counterparty_node_id, &[channel_id.0], &channel_config.into()) + .update_channel_config( + &counterparty_node_id, + &[channel_id.0], + &(*channel_config).clone().into(), + ) .map_err(|_| Error::ChannelConfigUpdateFailed) } diff --git a/src/types.rs b/src/types.rs index 5bc023487..b031dec69 100644 --- a/src/types.rs +++ b/src/types.rs @@ -23,7 +23,7 @@ use std::convert::TryFrom; use std::fmt::Display; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; use std::str::FromStr; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; pub(crate) type ChainMonitor = chainmonitor::ChainMonitor< InMemorySigner, @@ -397,44 +397,97 @@ impl Readable for NetAddress { } /// Options which apply on a per-channel basis. +/// +/// See documentation of [`LdkChannelConfig`] for details. +#[derive(Debug)] pub struct ChannelConfig { - /// See documentation of [`LdkChannelConfig::forwarding_fee_proportional_millionths`]. - pub forwarding_fee_proportional_millionths: u32, - /// See documentation of [`LdkChannelConfig::forwarding_fee_base_msat`]. - pub forwarding_fee_base_msat: u32, - /// See documentation of [`LdkChannelConfig::cltv_expiry_delta`]. - pub cltv_expiry_delta: u16, - /// See documentation of [`LdkChannelConfig::max_dust_htlc_exposure`]. - pub max_dust_htlc_exposure: Arc, - /// See documentation of [`LdkChannelConfig::force_close_avoidance_max_fee_satoshis`]. - pub force_close_avoidance_max_fee_satoshis: u64, - /// See documentation of [`LdkChannelConfig::accept_underpaying_htlcs`]. - pub accept_underpaying_htlcs: bool, + inner: RwLock, +} + +impl Clone for ChannelConfig { + fn clone(&self) -> Self { + self.inner.read().unwrap().clone().into() + } +} + +impl ChannelConfig { + /// Constructs a new `ChannelConfig`. + pub fn new() -> Self { + Self::default() + } + + /// Returns the set `forwarding_fee_proportional_millionths`. + pub fn forwarding_fee_proportional_millionths(&self) -> u32 { + self.inner.read().unwrap().forwarding_fee_proportional_millionths + } + + /// Sets the `forwarding_fee_proportional_millionths`. + pub fn set_forwarding_fee_proportional_millionths(&self, value: u32) { + self.inner.write().unwrap().forwarding_fee_proportional_millionths = value; + } + + /// Returns the set `forwarding_fee_base_msat`. + pub fn forwarding_fee_base_msat(&self) -> u32 { + self.inner.read().unwrap().forwarding_fee_base_msat + } + + /// Sets the `forwarding_fee_base_msat`. + pub fn set_forwarding_fee_base_msat(&self, fee_msat: u32) { + self.inner.write().unwrap().forwarding_fee_base_msat = fee_msat; + } + + /// Returns the set `cltv_expiry_delta`. + pub fn cltv_expiry_delta(&self) -> u16 { + self.inner.read().unwrap().cltv_expiry_delta + } + + /// Sets the `cltv_expiry_delta`. + pub fn set_cltv_expiry_delta(&self, value: u16) { + self.inner.write().unwrap().cltv_expiry_delta = value; + } + + /// Returns the set `force_close_avoidance_max_fee_satoshis`. + pub fn force_close_avoidance_max_fee_satoshis(&self) -> u64 { + self.inner.read().unwrap().force_close_avoidance_max_fee_satoshis + } + + /// Sets the `force_close_avoidance_max_fee_satoshis`. + pub fn set_force_close_avoidance_max_fee_satoshis(&self, value_sat: u64) { + self.inner.write().unwrap().force_close_avoidance_max_fee_satoshis = value_sat; + } + + /// Returns the set `accept_underpaying_htlcs`. + pub fn accept_underpaying_htlcs(&self) -> bool { + self.inner.read().unwrap().accept_underpaying_htlcs + } + + /// Sets the `accept_underpaying_htlcs`. + pub fn set_accept_underpaying_htlcs(&self, value: bool) { + self.inner.write().unwrap().accept_underpaying_htlcs = value; + } + + /// Sets the `max_dust_htlc_exposure` from a fixed limit. + pub fn set_max_dust_htlc_exposure_from_fixed_limit(&self, limit_msat: u64) { + self.inner.write().unwrap().max_dust_htlc_exposure = + LdkMaxDustHTLCExposure::FixedLimitMsat(limit_msat); + } + + /// Sets the `max_dust_htlc_exposure` from a fee rate multiplier. + pub fn set_max_dust_htlc_exposure_from_fee_rate_multiplier(&self, multiplier: u64) { + self.inner.write().unwrap().max_dust_htlc_exposure = + LdkMaxDustHTLCExposure::FeeRateMultiplier(multiplier); + } } impl From for ChannelConfig { fn from(value: LdkChannelConfig) -> Self { - Self { - forwarding_fee_proportional_millionths: value.forwarding_fee_proportional_millionths, - forwarding_fee_base_msat: value.forwarding_fee_base_msat, - cltv_expiry_delta: value.cltv_expiry_delta, - max_dust_htlc_exposure: Arc::new(MaxDustHTLCExposure(value.max_dust_htlc_exposure)), - force_close_avoidance_max_fee_satoshis: value.force_close_avoidance_max_fee_satoshis, - accept_underpaying_htlcs: value.accept_underpaying_htlcs, - } + Self { inner: RwLock::new(value) } } } impl From for LdkChannelConfig { fn from(value: ChannelConfig) -> Self { - Self { - forwarding_fee_proportional_millionths: value.forwarding_fee_proportional_millionths, - forwarding_fee_base_msat: value.forwarding_fee_base_msat, - cltv_expiry_delta: value.cltv_expiry_delta, - max_dust_htlc_exposure: value.max_dust_htlc_exposure.0.clone(), - force_close_avoidance_max_fee_satoshis: value.force_close_avoidance_max_fee_satoshis, - accept_underpaying_htlcs: value.accept_underpaying_htlcs, - } + *value.inner.read().unwrap() } } @@ -443,20 +496,3 @@ impl Default for ChannelConfig { LdkChannelConfig::default().into() } } - -/// Options for how to set the max dust HTLC exposure allowed on a channel. -/// -/// See documentation of [`LdkMaxDustHTLCExposure`] for details. -pub struct MaxDustHTLCExposure(pub LdkMaxDustHTLCExposure); - -impl MaxDustHTLCExposure { - /// See documentation of [`LdkMaxDustHTLCExposure::FixedLimitMsat`] for details. - pub fn from_fixed_limit(limit_msat: u64) -> Self { - Self(LdkMaxDustHTLCExposure::FixedLimitMsat(limit_msat)) - } - - /// See documentation of [`LdkMaxDustHTLCExposure::FeeRateMultiplier`] for details. - pub fn from_fee_multiplier(multiplier: u64) -> Self { - Self(LdkMaxDustHTLCExposure::FeeRateMultiplier(multiplier)) - } -}