Skip to content

Commit 7fda0ca

Browse files
joschisanclaude
andcommitted
Return an error for an inverted probe amount range
A `debug_assert!` let an inverted range through in release builds. Reject it with `BuildError::InvalidProbeAmountRange` instead, from both the Rust builder and the uniffi one, like the other fallible builder setters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 32b4d7e commit 7fda0ca

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎bindings/ldk_node.udl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface ProbingConfigBuilder {
4040
void set_max_locked_msat(u64 max_msat);
4141
void set_diversity_penalty_msat(u64 penalty_msat);
4242
void set_cooldown(u64 secs);
43+
[Throws=BuildError]
4344
void set_amount_range_msat(u64 min_msat, u64 max_msat);
4445
ProbingConfig build();
4546
};

‎src/builder.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ pub enum BuildError {
192192
InvalidTorProxyAddress,
193193
/// The provided alias is invalid.
194194
InvalidNodeAlias,
195+
/// The given probe amount range is invalid, i.e., its minimum exceeds its maximum.
196+
InvalidProbeAmountRange,
195197
/// An attempt to setup a runtime has failed.
196198
RuntimeSetupFailed,
197199
/// We failed to read data from the [`KVStore`].
@@ -254,6 +256,7 @@ impl fmt::Display for BuildError {
254256
Self::LoggerSetupFailed => write!(f, "Failed to setup the logger."),
255257
Self::ChainSourceSetupFailed => write!(f, "Failed to setup the chain source."),
256258
Self::InvalidNodeAlias => write!(f, "Given node alias is invalid."),
259+
Self::InvalidProbeAmountRange => write!(f, "Given probe amount range is invalid."),
257260
Self::NetworkMismatch => {
258261
write!(f, "Given network does not match the node's previously configured network.")
259262
},

‎src/probing.rs‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use lightning::routing::router::{
7878
use lightning_invoice::DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA;
7979
use lightning_types::features::{ChannelFeatures, NodeFeatures};
8080

81+
use crate::builder::BuildError;
8182
use crate::config::{
8283
DEFAULT_MAX_PROBE_AMOUNT_MSAT, DEFAULT_MAX_PROBE_LOCKED_MSAT, DEFAULT_MIN_PROBE_AMOUNT_MSAT,
8384
DEFAULT_PROBED_NODE_COOLDOWN_SECS, DEFAULT_PROBING_INTERVAL_SECS, MIN_PROBING_INTERVAL,
@@ -270,11 +271,18 @@ impl ProbingConfigBuilder {
270271
/// more liquidity per in-flight probe — size `max_locked_msat` accordingly.
271272
///
272273
/// Defaults to 1 000 000 - 10 000 000 msat (1k - 10k sats).
273-
pub fn amount_range_msat(&mut self, min_msat: u64, max_msat: u64) -> &mut Self {
274-
debug_assert!(min_msat <= max_msat, "min_msat must not exceed max_msat");
274+
///
275+
/// Returns [`BuildError::InvalidProbeAmountRange`] if `min_msat` exceeds `max_msat`.
276+
pub fn amount_range_msat(
277+
&mut self, min_msat: u64, max_msat: u64,
278+
) -> Result<&mut Self, BuildError> {
279+
if min_msat > max_msat {
280+
return Err(BuildError::InvalidProbeAmountRange);
281+
}
282+
275283
self.min_amount_msat = min_msat;
276284
self.max_amount_msat = max_msat;
277-
self
285+
Ok(self)
278286
}
279287

280288
/// Builds the [`ProbingConfig`].
@@ -364,8 +372,10 @@ impl ArcedProbingConfigBuilder {
364372
/// own probe amounts when building paths.
365373
///
366374
/// Defaults to 1 000 000 - 10 000 000 msat (1k - 10k sats).
367-
pub fn set_amount_range_msat(&self, min_msat: u64, max_msat: u64) {
368-
self.inner.write().expect("lock").amount_range_msat(min_msat, max_msat);
375+
///
376+
/// Returns [`BuildError::InvalidProbeAmountRange`] if `min_msat` exceeds `max_msat`.
377+
pub fn set_amount_range_msat(&self, min_msat: u64, max_msat: u64) -> Result<(), BuildError> {
378+
self.inner.write().expect("lock").amount_range_msat(min_msat, max_msat).map(|_| ())
369379
}
370380

371381
/// Builds the [`ProbingConfig`].
@@ -879,3 +889,23 @@ pub(crate) async fn run_prober(prober: Arc<Prober>, mut stop_rx: tokio::sync::wa
879889
}
880890
}
881891
}
892+
893+
#[cfg(test)]
894+
mod tests {
895+
use super::*;
896+
897+
#[test]
898+
fn amount_range_rejects_min_above_max() {
899+
let mut builder = ProbingConfigBuilder::random_walk(3);
900+
901+
assert_eq!(
902+
builder.amount_range_msat(2_000, 1_000).err(),
903+
Some(BuildError::InvalidProbeAmountRange)
904+
);
905+
906+
builder.amount_range_msat(1_000, 1_000).expect("an equal minimum and maximum is valid");
907+
908+
let config = builder.build();
909+
assert_eq!((config.min_amount_msat, config.max_amount_msat), (1_000, 1_000));
910+
}
911+
}

0 commit comments

Comments
 (0)