Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ typedef dictionary TorConfig;

typedef interface NodeEntropy;

typedef interface ProbingConfig;

typedef enum WordCount;

[Remote]
Expand All @@ -32,6 +34,18 @@ interface LogWriter {
void log(LogRecord record);
};

interface ProbingConfigBuilder {
[Name=high_degree]
constructor(u64 top_node_count);
[Name=random_walk]
constructor(u64 max_hops);
void set_interval(u64 secs);
void set_max_locked_msat(u64 max_msat);
void set_diversity_penalty_msat(u64 penalty_msat);
void set_cooldown(u64 secs);
ProbingConfig build();
};

interface Builder {
constructor();
[Name=from_config]
Expand Down Expand Up @@ -61,6 +75,7 @@ interface Builder {
[Throws=BuildError]
void set_async_payments_role(AsyncPaymentsRole? role);
void set_wallet_recovery_mode();
void set_probing_config(ProbingConfig config);
[Throws=BuildError]
Node build(NodeEntropy node_entropy);
[Throws=BuildError]
Expand Down
97 changes: 94 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::config::{
default_user_config, may_announce_channel, AnnounceError, AsyncPaymentsRole,
BitcoindRestClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig, HRNResolverConfig,
TorConfig, DEFAULT_ESPLORA_SERVER_URL, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
DEFAULT_MAX_PROBE_AMOUNT_MSAT, DEFAULT_MIN_PROBE_AMOUNT_MSAT,
};
use crate::connection::ConnectionManager;
use crate::entropy::NodeEntropy;
Expand All @@ -76,6 +77,10 @@ use crate::logger::{log_error, LdkLogger, LogLevel, LogWriter, Logger};
use crate::message_handler::NodeCustomMessageHandler;
use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox;
use crate::peer_store::PeerStore;
use crate::probing::{
HighDegreeStrategy, Prober, ProbingConfig, ProbingStrategy, ProbingStrategyKind,
RandomWalkStrategy,
};
use crate::runtime::{Runtime, RuntimeSpawner};
use crate::tx_broadcaster::TransactionBroadcaster;
use crate::types::{
Expand Down Expand Up @@ -292,6 +297,7 @@ pub struct NodeBuilder {
runtime_handle: Option<tokio::runtime::Handle>,
pathfinding_scores_sync_config: Option<PathfindingScoresSyncConfig>,
recovery_mode: bool,
probing_config: Option<ProbingConfig>,
}

impl NodeBuilder {
Expand All @@ -310,16 +316,19 @@ impl NodeBuilder {
let runtime_handle = None;
let pathfinding_scores_sync_config = None;
let recovery_mode = false;
let async_payments_role = None;
let probing_config = None;
Self {
config,
chain_data_source_config,
gossip_source_config,
liquidity_source_config,
log_writer_config,
runtime_handle,
async_payments_role: None,
async_payments_role,
pathfinding_scores_sync_config,
recovery_mode,
probing_config,
}
}

Expand Down Expand Up @@ -625,6 +634,31 @@ impl NodeBuilder {
self
}

/// Configures background probing.
///
/// Use [`ProbingConfigBuilder`] to build the configuration:
Comment thread
randomlogin marked this conversation as resolved.
/// ```no_run
/// # #[cfg(not(feature = "uniffi"))]
/// # {
/// use std::time::Duration;
/// use ldk_node::Builder;
/// use ldk_node::probing::ProbingConfigBuilder;
///
/// let mut builder = Builder::new();
/// builder.set_probing_config(
/// ProbingConfigBuilder::high_degree(100)
/// .interval(Duration::from_secs(30))
/// .build()
/// );
/// # }
/// ```
///
/// [`ProbingConfigBuilder`]: crate::probing::ProbingConfigBuilder
pub fn set_probing_config(&mut self, config: ProbingConfig) -> &mut Self {
self.probing_config = Some(config);
self
}

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
pub fn build(&self, node_entropy: NodeEntropy) -> Result<Node, BuildError> {
Expand Down Expand Up @@ -864,6 +898,7 @@ impl NodeBuilder {
self.gossip_source_config.as_ref(),
self.liquidity_source_config.as_ref(),
self.pathfinding_scores_sync_config.as_ref(),
self.probing_config.as_ref(),
self.async_payments_role,
self.recovery_mode,
seed_bytes,
Expand Down Expand Up @@ -1164,6 +1199,15 @@ impl ArcedNodeBuilder {
self.inner.write().expect("lock").set_wallet_recovery_mode();
}

/// Configures background probing.
///
/// Use [`ProbingConfigBuilder`] to build the configuration.
///
/// [`ProbingConfigBuilder`]: crate::probing::ProbingConfigBuilder
pub fn set_probing_config(&self, config: Arc<ProbingConfig>) {
self.inner.write().expect("lock").set_probing_config((*config).clone());
}

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
pub fn build(&self, node_entropy: Arc<NodeEntropy>) -> Result<Arc<Node>, BuildError> {
Expand Down Expand Up @@ -1359,8 +1403,9 @@ fn build_with_store_internal(
gossip_source_config: Option<&GossipSourceConfig>,
liquidity_source_config: Option<&LiquiditySourceConfig>,
pathfinding_scores_sync_config: Option<&PathfindingScoresSyncConfig>,
async_payments_role: Option<AsyncPaymentsRole>, recovery_mode: bool, seed_bytes: [u8; 64],
runtime: Arc<Runtime>, logger: Arc<Logger>, kv_store: Arc<DynStore>,
probing_config: Option<&ProbingConfig>, async_payments_role: Option<AsyncPaymentsRole>,
recovery_mode: bool, seed_bytes: [u8; 64], runtime: Arc<Runtime>, logger: Arc<Logger>,
kv_store: Arc<DynStore>,
) -> Result<Node, BuildError> {
optionally_install_rustls_cryptoprovider();

Expand Down Expand Up @@ -2152,6 +2197,51 @@ fn build_with_store_internal(
_leak_checker.0.push(Arc::downgrade(&wallet) as Weak<dyn Any + Send + Sync>);
}

let prober = probing_config.map(|probing_cfg| {
let strategy: Arc<dyn ProbingStrategy> = match &probing_cfg.kind {
ProbingStrategyKind::HighDegree { top_node_count } => {
// Dedicated router for probing so the diversity penalty doesn't interfere
// with real payments; shares the scorer so probe results still train it.
let mut probing_fee_params = ProbabilisticScoringFeeParameters::default();
if let Some(penalty) = probing_cfg.diversity_penalty_msat {
probing_fee_params.probing_diversity_penalty_msat = penalty;
}
let probing_router = Arc::new(DefaultRouter::new(
Arc::clone(&network_graph),
Arc::clone(&logger),
Arc::clone(&keys_manager),
Arc::clone(&scorer),
probing_fee_params,
));
Arc::new(HighDegreeStrategy::new(
Arc::clone(&network_graph),
Arc::clone(&channel_manager),
probing_router,
*top_node_count,
DEFAULT_MIN_PROBE_AMOUNT_MSAT,
DEFAULT_MAX_PROBE_AMOUNT_MSAT,
probing_cfg.cooldown,
config.probing_liquidity_limit_multiplier,
))
},
ProbingStrategyKind::RandomWalk { max_hops } => Arc::new(RandomWalkStrategy::new(
Arc::clone(&network_graph),
Arc::clone(&channel_manager),
*max_hops,
DEFAULT_MIN_PROBE_AMOUNT_MSAT,
DEFAULT_MAX_PROBE_AMOUNT_MSAT,
)),
ProbingStrategyKind::Custom(s) => Arc::clone(s),
};
Arc::new(Prober {
channel_manager: Arc::clone(&channel_manager),
logger: Arc::clone(&logger),
strategy,
interval: probing_cfg.interval,
max_locked_msat: probing_cfg.max_locked_msat,
})
});

Ok(Node {
runtime,
stop_sender,
Expand Down Expand Up @@ -2185,6 +2275,7 @@ fn build_with_store_internal(
om_mailbox,
async_payments_role,
hrn_resolver,
prober,
#[cfg(cycle_tests)]
_leak_checker,
})
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS: u64 = 80;
const DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS: u64 = 30;
const DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS: u64 = 60 * 10;
const DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER: u64 = 3;
pub(crate) const DEFAULT_PROBING_INTERVAL_SECS: u64 = 10;
pub(crate) const MIN_PROBING_INTERVAL: Duration = Duration::from_millis(100);
pub(crate) const DEFAULT_PROBED_NODE_COOLDOWN_SECS: u64 = 60 * 60; // 1 hour
pub(crate) const DEFAULT_MAX_PROBE_LOCKED_MSAT: u64 = 100_000_000; // 100k sats
pub(crate) const DEFAULT_MIN_PROBE_AMOUNT_MSAT: u64 = 1_000_000; // 1k sats
pub(crate) const DEFAULT_MAX_PROBE_AMOUNT_MSAT: u64 = 10_000_000; // 10k sats
const DEFAULT_ANCHOR_PER_CHANNEL_RESERVE_SATS: u64 = 25_000;

// The default timeout after which we abort a wallet syncing operation.
Expand Down
29 changes: 20 additions & 9 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use crate::payment::store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
};
use crate::payment::PaymentMetadata;
use crate::probing::Prober;
use crate::runtime::Runtime;
use crate::types::{
CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet,
Expand Down Expand Up @@ -537,12 +538,13 @@ where
payment_store: Arc<PaymentStore>,
peer_store: Arc<PeerStore<L>>,
keys_manager: Arc<KeysManager>,
runtime: Arc<Runtime>,
logger: L,
config: Arc<Config>,
static_invoice_store: Option<StaticInvoiceStore>,
onion_messenger: Arc<OnionMessenger>,
om_mailbox: Option<Arc<OnionMessageMailbox>>,
prober: Option<Arc<Prober>>,
runtime: Arc<Runtime>,
logger: L,
config: Arc<Config>,
}

impl<L: Deref + Clone + Sync + Send + 'static> EventHandler<L>
Expand All @@ -558,7 +560,7 @@ where
payment_store: Arc<PaymentStore>, peer_store: Arc<PeerStore<L>>,
keys_manager: Arc<KeysManager>, static_invoice_store: Option<StaticInvoiceStore>,
onion_messenger: Arc<OnionMessenger>, om_mailbox: Option<Arc<OnionMessageMailbox>>,
runtime: Arc<Runtime>, logger: L, config: Arc<Config>,
prober: Option<Arc<Prober>>, runtime: Arc<Runtime>, logger: L, config: Arc<Config>,
) -> Self {
Self {
event_queue,
Expand All @@ -572,12 +574,13 @@ where
payment_store,
peer_store,
keys_manager,
logger,
runtime,
config,
static_invoice_store,
onion_messenger,
om_mailbox,
prober,
runtime,
logger,
config,
}
}

Expand Down Expand Up @@ -1210,8 +1213,16 @@ where

LdkEvent::PaymentPathSuccessful { .. } => {},
LdkEvent::PaymentPathFailed { .. } => {},
LdkEvent::ProbeSuccessful { .. } => {},
LdkEvent::ProbeFailed { .. } => {},
LdkEvent::ProbeSuccessful { path, payment_id, .. } => {
if let Some(prober) = &self.prober {
prober.handle_background_probe_successful(&path, payment_id);
}
},
LdkEvent::ProbeFailed { path, payment_id, .. } => {
if let Some(prober) = &self.prober {
prober.handle_background_probe_failed(&path, payment_id);
}
},
LdkEvent::HTLCHandlingFailed { failure_type, .. } => {
if let Some(liquidity_source) = self.liquidity_source.as_ref() {
liquidity_source.handle_htlc_handling_failed(failure_type).await;
Expand Down
1 change: 1 addition & 0 deletions src/ffi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub use crate::entropy::{generate_entropy_mnemonic, NodeEntropy, WordCount};
use crate::error::Error;
pub use crate::liquidity::LSPS1OrderStatus;
pub use crate::logger::{LogLevel, LogRecord, LogWriter};
pub use crate::probing::ProbingConfig;
use crate::{hex_utils, SocketAddress, UserChannelId};

uniffi::custom_type!(PublicKey, String, {
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ pub mod logger;
mod message_handler;
pub mod payment;
mod peer_store;
pub mod probing;
mod runtime;
mod scoring;
mod tx_broadcaster;
mod types;
mod util;
mod wallet;

use std::default::Default;
Expand Down Expand Up @@ -172,6 +174,9 @@ use payment::{
UnifiedPayment,
};
use peer_store::{PeerInfo, PeerStore};
#[cfg(feature = "uniffi")]
pub use probing::ArcedProbingConfigBuilder as ProbingConfigBuilder;
use probing::{run_prober, Prober};
use runtime::Runtime;
pub use tokio;
use types::{
Expand Down Expand Up @@ -247,6 +252,7 @@ pub struct Node {
om_mailbox: Option<Arc<OnionMessageMailbox>>,
async_payments_role: Option<AsyncPaymentsRole>,
hrn_resolver: HRNResolver,
prober: Option<Arc<Prober>>,
#[cfg(cycle_tests)]
_leak_checker: LeakChecker,
}
Expand Down Expand Up @@ -605,11 +611,19 @@ impl Node {
static_invoice_store,
Arc::clone(&self.onion_messenger),
self.om_mailbox.clone(),
self.prober.clone(),
Arc::clone(&self.runtime),
Arc::clone(&self.logger),
Arc::clone(&self.config),
));

if let Some(prober) = self.prober.clone() {
let stop_rx = self.stop_sender.subscribe();
self.runtime.spawn_cancellable_background_task(async move {
run_prober(prober, stop_rx).await;
});
}

// Setup background processing
let background_persister = Arc::clone(&self.kv_store);
let background_event_handler = Arc::clone(&event_handler);
Expand Down Expand Up @@ -1096,6 +1110,11 @@ impl Node {
))
}

/// Returns a reference to the [`Prober`], or `None` if no probing strategy is configured.
pub fn prober(&self) -> Option<&Prober> {
self.prober.as_deref()
}

/// Retrieve a list of known channels.
pub fn list_channels(&self) -> Vec<ChannelDetails> {
self.channel_manager.list_channels().into_iter().map(|c| c.into()).collect()
Expand Down
Loading