Skip to content

Commit

Permalink
f Make ChannelConfig a UniFFI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Aug 11, 2023
1 parent 6fff268 commit 7ef7d92
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 65 deletions.
28 changes: 14 additions & 14 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -890,7 +890,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
/// 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<u64>, channel_config: Option<ChannelConfig>,
push_to_counterparty_msat: Option<u64>, channel_config: Option<Arc<ChannelConfig>>,
announce_channel: bool,
) -> Result<(), Error> {
let rt_lock = self.runtime.read().unwrap();
Expand Down Expand Up @@ -920,13 +920,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
})
})?;

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()
};

Expand Down Expand Up @@ -1030,10 +1031,14 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
/// 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<ChannelConfig>,
) -> 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)
}

Expand Down
128 changes: 82 additions & 46 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K> = chainmonitor::ChainMonitor<
InMemorySigner,
Expand Down Expand Up @@ -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<MaxDustHTLCExposure>,
/// 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<LdkChannelConfig>,
}

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<LdkChannelConfig> 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<ChannelConfig> 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()
}
}

Expand All @@ -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))
}
}

0 comments on commit 7ef7d92

Please sign in to comment.