Skip to content

Commit 7ef7d92

Browse files
committed
f Make ChannelConfig a UniFFI interface
1 parent 6fff268 commit 7ef7d92

File tree

3 files changed

+106
-65
lines changed

3 files changed

+106
-65
lines changed

bindings/ldk_node.udl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,20 @@ dictionary PeerDetails {
204204
boolean is_connected;
205205
};
206206

207-
dictionary ChannelConfig {
208-
u32 forwarding_fee_proportional_millionths = 0;
209-
u32 forwarding_fee_base_msat = 1000;
210-
u16 cltv_expiry_delta = 72;
211-
MaxDustHTLCExposure max_dust_htlc_exposure;
212-
u64 force_close_avoidance_max_fee_satoshis = 1000;
213-
boolean accept_underpaying_htlcs = false;
214-
};
215-
216-
interface MaxDustHTLCExposure {
217-
[Name=from_fixed_limit]
218-
constructor(u64 limit);
219-
[Name=from_fee_multiplier]
220-
constructor(u64 multiplier);
207+
interface ChannelConfig {
208+
constructor();
209+
u32 forwarding_fee_proportional_millionths();
210+
void set_forwarding_fee_proportional_millionths(u32 value);
211+
u32 forwarding_fee_base_msat();
212+
void set_forwarding_fee_base_msat(u32 fee_msat);
213+
u16 cltv_expiry_delta();
214+
void set_cltv_expiry_delta(u16 value);
215+
u64 force_close_avoidance_max_fee_satoshis();
216+
void set_force_close_avoidance_max_fee_satoshis(u64 value_sat);
217+
boolean accept_underpaying_htlcs();
218+
void set_accept_underpaying_htlcs(boolean value);
219+
void set_max_dust_htlc_exposure_from_fixed_limit(u64 limit_msat);
220+
void set_max_dust_htlc_exposure_from_fee_rate_multiplier(u64 multiplier);
221221
};
222222

223223
enum LogLevel {

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ use payment_store::PaymentStore;
120120
pub use payment_store::{PaymentDetails, PaymentDirection, PaymentStatus};
121121
use peer_store::{PeerInfo, PeerStore};
122122
use types::{ChainMonitor, ChannelManager, KeysManager, NetworkGraph, PeerManager, Router, Scorer};
123-
pub use types::{ChannelDetails, ChannelId, MaxDustHTLCExposure, PeerDetails, UserChannelId};
123+
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId};
124124
use wallet::Wallet;
125125

126126
use logger::{log_debug, log_error, log_info, log_trace, FilesystemLogger, Logger};
@@ -890,7 +890,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
890890
/// Returns a temporary channel id.
891891
pub fn connect_open_channel(
892892
&self, node_id: PublicKey, address: NetAddress, channel_amount_sats: u64,
893-
push_to_counterparty_msat: Option<u64>, channel_config: Option<ChannelConfig>,
893+
push_to_counterparty_msat: Option<u64>, channel_config: Option<Arc<ChannelConfig>>,
894894
announce_channel: bool,
895895
) -> Result<(), Error> {
896896
let rt_lock = self.runtime.read().unwrap();
@@ -920,13 +920,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
920920
})
921921
})?;
922922

923+
let channel_config = (*(channel_config.unwrap_or_default())).clone().into();
923924
let user_config = UserConfig {
924925
channel_handshake_limits: Default::default(),
925926
channel_handshake_config: ChannelHandshakeConfig {
926927
announced_channel: announce_channel,
927928
..Default::default()
928929
},
929-
channel_config: channel_config.unwrap_or_default().into(),
930+
channel_config,
930931
..Default::default()
931932
};
932933

@@ -1030,10 +1031,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
10301031
/// Update the config for a previously opened channel.
10311032
pub fn update_channel_config(
10321033
&self, channel_id: &ChannelId, counterparty_node_id: PublicKey,
1033-
channel_config: ChannelConfig,
1034+
channel_config: Arc<ChannelConfig>,
10341035
) -> Result<(), Error> {
10351036
self.channel_manager
1036-
.update_channel_config(&counterparty_node_id, &[channel_id.0], &channel_config.into())
1037+
.update_channel_config(
1038+
&counterparty_node_id,
1039+
&[channel_id.0],
1040+
&(*channel_config).clone().into(),
1041+
)
10371042
.map_err(|_| Error::ChannelConfigUpdateFailed)
10381043
}
10391044

src/types.rs

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::convert::TryFrom;
2323
use std::fmt::Display;
2424
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
2525
use std::str::FromStr;
26-
use std::sync::{Arc, Mutex};
26+
use std::sync::{Arc, Mutex, RwLock};
2727

2828
pub(crate) type ChainMonitor<K> = chainmonitor::ChainMonitor<
2929
InMemorySigner,
@@ -397,44 +397,97 @@ impl Readable for NetAddress {
397397
}
398398

399399
/// Options which apply on a per-channel basis.
400+
///
401+
/// See documentation of [`LdkChannelConfig`] for details.
402+
#[derive(Debug)]
400403
pub struct ChannelConfig {
401-
/// See documentation of [`LdkChannelConfig::forwarding_fee_proportional_millionths`].
402-
pub forwarding_fee_proportional_millionths: u32,
403-
/// See documentation of [`LdkChannelConfig::forwarding_fee_base_msat`].
404-
pub forwarding_fee_base_msat: u32,
405-
/// See documentation of [`LdkChannelConfig::cltv_expiry_delta`].
406-
pub cltv_expiry_delta: u16,
407-
/// See documentation of [`LdkChannelConfig::max_dust_htlc_exposure`].
408-
pub max_dust_htlc_exposure: Arc<MaxDustHTLCExposure>,
409-
/// See documentation of [`LdkChannelConfig::force_close_avoidance_max_fee_satoshis`].
410-
pub force_close_avoidance_max_fee_satoshis: u64,
411-
/// See documentation of [`LdkChannelConfig::accept_underpaying_htlcs`].
412-
pub accept_underpaying_htlcs: bool,
404+
inner: RwLock<LdkChannelConfig>,
405+
}
406+
407+
impl Clone for ChannelConfig {
408+
fn clone(&self) -> Self {
409+
self.inner.read().unwrap().clone().into()
410+
}
411+
}
412+
413+
impl ChannelConfig {
414+
/// Constructs a new `ChannelConfig`.
415+
pub fn new() -> Self {
416+
Self::default()
417+
}
418+
419+
/// Returns the set `forwarding_fee_proportional_millionths`.
420+
pub fn forwarding_fee_proportional_millionths(&self) -> u32 {
421+
self.inner.read().unwrap().forwarding_fee_proportional_millionths
422+
}
423+
424+
/// Sets the `forwarding_fee_proportional_millionths`.
425+
pub fn set_forwarding_fee_proportional_millionths(&self, value: u32) {
426+
self.inner.write().unwrap().forwarding_fee_proportional_millionths = value;
427+
}
428+
429+
/// Returns the set `forwarding_fee_base_msat`.
430+
pub fn forwarding_fee_base_msat(&self) -> u32 {
431+
self.inner.read().unwrap().forwarding_fee_base_msat
432+
}
433+
434+
/// Sets the `forwarding_fee_base_msat`.
435+
pub fn set_forwarding_fee_base_msat(&self, fee_msat: u32) {
436+
self.inner.write().unwrap().forwarding_fee_base_msat = fee_msat;
437+
}
438+
439+
/// Returns the set `cltv_expiry_delta`.
440+
pub fn cltv_expiry_delta(&self) -> u16 {
441+
self.inner.read().unwrap().cltv_expiry_delta
442+
}
443+
444+
/// Sets the `cltv_expiry_delta`.
445+
pub fn set_cltv_expiry_delta(&self, value: u16) {
446+
self.inner.write().unwrap().cltv_expiry_delta = value;
447+
}
448+
449+
/// Returns the set `force_close_avoidance_max_fee_satoshis`.
450+
pub fn force_close_avoidance_max_fee_satoshis(&self) -> u64 {
451+
self.inner.read().unwrap().force_close_avoidance_max_fee_satoshis
452+
}
453+
454+
/// Sets the `force_close_avoidance_max_fee_satoshis`.
455+
pub fn set_force_close_avoidance_max_fee_satoshis(&self, value_sat: u64) {
456+
self.inner.write().unwrap().force_close_avoidance_max_fee_satoshis = value_sat;
457+
}
458+
459+
/// Returns the set `accept_underpaying_htlcs`.
460+
pub fn accept_underpaying_htlcs(&self) -> bool {
461+
self.inner.read().unwrap().accept_underpaying_htlcs
462+
}
463+
464+
/// Sets the `accept_underpaying_htlcs`.
465+
pub fn set_accept_underpaying_htlcs(&self, value: bool) {
466+
self.inner.write().unwrap().accept_underpaying_htlcs = value;
467+
}
468+
469+
/// Sets the `max_dust_htlc_exposure` from a fixed limit.
470+
pub fn set_max_dust_htlc_exposure_from_fixed_limit(&self, limit_msat: u64) {
471+
self.inner.write().unwrap().max_dust_htlc_exposure =
472+
LdkMaxDustHTLCExposure::FixedLimitMsat(limit_msat);
473+
}
474+
475+
/// Sets the `max_dust_htlc_exposure` from a fee rate multiplier.
476+
pub fn set_max_dust_htlc_exposure_from_fee_rate_multiplier(&self, multiplier: u64) {
477+
self.inner.write().unwrap().max_dust_htlc_exposure =
478+
LdkMaxDustHTLCExposure::FeeRateMultiplier(multiplier);
479+
}
413480
}
414481

415482
impl From<LdkChannelConfig> for ChannelConfig {
416483
fn from(value: LdkChannelConfig) -> Self {
417-
Self {
418-
forwarding_fee_proportional_millionths: value.forwarding_fee_proportional_millionths,
419-
forwarding_fee_base_msat: value.forwarding_fee_base_msat,
420-
cltv_expiry_delta: value.cltv_expiry_delta,
421-
max_dust_htlc_exposure: Arc::new(MaxDustHTLCExposure(value.max_dust_htlc_exposure)),
422-
force_close_avoidance_max_fee_satoshis: value.force_close_avoidance_max_fee_satoshis,
423-
accept_underpaying_htlcs: value.accept_underpaying_htlcs,
424-
}
484+
Self { inner: RwLock::new(value) }
425485
}
426486
}
427487

428488
impl From<ChannelConfig> for LdkChannelConfig {
429489
fn from(value: ChannelConfig) -> Self {
430-
Self {
431-
forwarding_fee_proportional_millionths: value.forwarding_fee_proportional_millionths,
432-
forwarding_fee_base_msat: value.forwarding_fee_base_msat,
433-
cltv_expiry_delta: value.cltv_expiry_delta,
434-
max_dust_htlc_exposure: value.max_dust_htlc_exposure.0.clone(),
435-
force_close_avoidance_max_fee_satoshis: value.force_close_avoidance_max_fee_satoshis,
436-
accept_underpaying_htlcs: value.accept_underpaying_htlcs,
437-
}
490+
*value.inner.read().unwrap()
438491
}
439492
}
440493

@@ -443,20 +496,3 @@ impl Default for ChannelConfig {
443496
LdkChannelConfig::default().into()
444497
}
445498
}
446-
447-
/// Options for how to set the max dust HTLC exposure allowed on a channel.
448-
///
449-
/// See documentation of [`LdkMaxDustHTLCExposure`] for details.
450-
pub struct MaxDustHTLCExposure(pub LdkMaxDustHTLCExposure);
451-
452-
impl MaxDustHTLCExposure {
453-
/// See documentation of [`LdkMaxDustHTLCExposure::FixedLimitMsat`] for details.
454-
pub fn from_fixed_limit(limit_msat: u64) -> Self {
455-
Self(LdkMaxDustHTLCExposure::FixedLimitMsat(limit_msat))
456-
}
457-
458-
/// See documentation of [`LdkMaxDustHTLCExposure::FeeRateMultiplier`] for details.
459-
pub fn from_fee_multiplier(multiplier: u64) -> Self {
460-
Self(LdkMaxDustHTLCExposure::FeeRateMultiplier(multiplier))
461-
}
462-
}

0 commit comments

Comments
 (0)