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 8, 2023
1 parent bdad3bc commit 9c5eea6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 68 deletions.
28 changes: 14 additions & 14 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,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
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ pub use error::Error as NodeError;
use error::Error;

pub use event::Event;
pub use types::NetAddress;
pub use types::ChannelConfig;
pub use types::NetAddress;

pub use io::utils::generate_entropy_mnemonic;

Expand All @@ -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, Scorer};
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId, MaxDustHTLCExposure};
pub use types::{ChannelDetails, ChannelId, PeerDetails, UserChannelId};
use wallet::Wallet;

use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};
Expand Down Expand Up @@ -889,7 +889,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 @@ -919,13 +919,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 @@ -1029,10 +1030,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
133 changes: 85 additions & 48 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use lightning::routing::gossip;
use lightning::routing::router::DefaultRouter;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters};
use lightning::sign::InMemorySigner;
use lightning::util::ser::{Hostname, Readable, Writeable, Writer};
use lightning::util::config::ChannelConfig as LdkChannelConfig;
use lightning::util::config::MaxDustHTLCExposure as LdkMaxDustHTLCExposure;
use lightning_net_tokio::SocketDescriptor; use lightning_transaction_sync::EsploraSyncClient;
use lightning::util::ser::{Hostname, Readable, Writeable, Writer};
use lightning_net_tokio::SocketDescriptor;
use lightning_transaction_sync::EsploraSyncClient;

use bitcoin::secp256k1::PublicKey;
use bitcoin::OutPoint;
Expand All @@ -22,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 @@ -396,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 @@ -442,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 9c5eea6

Please sign in to comment.