Skip to content

Commit 151ec49

Browse files
Remove deprecated send_payment_with_route usage from fuzzing
This allows us to make the PaymentSendFailure error type private, as well as reduce the visibility of the vestigial send_payment_with_route method that was already made test and fuzz-only in a previous commit.
1 parent 9de59ca commit 151ec49

File tree

3 files changed

+71
-57
lines changed

3 files changed

+71
-57
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ use lightning::events::MessageSendEventsProvider;
4747
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4848
use lightning::ln::channel_state::ChannelDetails;
4949
use lightning::ln::channelmanager::{
50-
ChainParameters, ChannelManager, ChannelManagerReadArgs, PaymentId, PaymentSendFailure,
51-
RecipientOnionFields,
50+
ChainParameters, ChannelManager, ChannelManagerReadArgs, PaymentId,
51+
RecipientOnionFields, Retry
5252
};
5353
use lightning::ln::functional_test_utils::*;
5454
use lightning::ln::msgs::{
@@ -58,7 +58,7 @@ use lightning::ln::script::ShutdownScript;
5858
use lightning::ln::types::ChannelId;
5959
use lightning::offers::invoice::UnsignedBolt12Invoice;
6060
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
61-
use lightning::routing::router::{InFlightHtlcs, Path, Route, RouteHop, RouteParameters, Router};
61+
use lightning::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters, Router};
6262
use lightning::sign::{
6363
EntropySource, InMemorySigner, KeyMaterial, NodeSigner, Recipient, SignerProvider,
6464
};
@@ -82,6 +82,7 @@ use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}
8282

8383
use lightning::io::Cursor;
8484
use std::cmp::{self, Ordering};
85+
use std::collections::VecDeque;
8586
use std::mem;
8687
use std::sync::atomic;
8788
use std::sync::{Arc, Mutex};
@@ -112,13 +113,18 @@ impl FeeEstimator for FuzzEstimator {
112113
}
113114
}
114115

115-
struct FuzzRouter {}
116+
struct FuzzRouter {
117+
pub next_routes: Mutex<VecDeque<Route>>,
118+
}
116119

117120
impl Router for FuzzRouter {
118121
fn find_route(
119122
&self, _payer: &PublicKey, _params: &RouteParameters,
120123
_first_hops: Option<&[&ChannelDetails]>, _inflight_htlcs: InFlightHtlcs,
121124
) -> Result<Route, msgs::LightningError> {
125+
if let Some(route) = self.next_routes.lock().unwrap().pop_front() {
126+
return Ok(route)
127+
}
122128
Err(msgs::LightningError {
123129
err: String::from("Not implemented"),
124130
action: msgs::ErrorAction::IgnoreError,
@@ -434,6 +440,29 @@ impl KeyProvider {
434440
}
435441
}
436442

443+
// Returns a bool indicating whether the payment failed.
444+
#[inline]
445+
fn check_payment_send_events(source: &ChanMan, amt: u64, min_sendable: u64, max_sendable: u64) -> bool {
446+
let mut payment_failed = false;
447+
let events = source.get_and_clear_pending_events();
448+
assert!(events.len() == 2 || events.len() == 0);
449+
for ev in events {
450+
match ev {
451+
events::Event::PaymentPathFailed { failure: events::PathFailure::InitialSend { err }, .. } => {
452+
check_api_err(err, amt > max_sendable || amt < min_sendable);
453+
},
454+
events::Event::PaymentFailed { .. } => {},
455+
_ => panic!()
456+
};
457+
payment_failed = true;
458+
}
459+
// Note that while the max is a strict upper-bound, we can occasionally send substantially
460+
// below the minimum, with some gap which is unusable immediately below the minimum. Thus,
461+
// we don't check against min_value_sendable here.
462+
assert!(payment_failed || (amt <= max_sendable));
463+
payment_failed
464+
}
465+
437466
#[inline]
438467
fn check_api_err(api_err: APIError, sendable_bounds_violated: bool) {
439468
match api_err {
@@ -460,34 +489,6 @@ fn check_api_err(api_err: APIError, sendable_bounds_violated: bool) {
460489
},
461490
}
462491
}
463-
#[inline]
464-
fn check_payment_err(send_err: PaymentSendFailure, sendable_bounds_violated: bool) {
465-
match send_err {
466-
PaymentSendFailure::ParameterError(api_err) => {
467-
check_api_err(api_err, sendable_bounds_violated)
468-
},
469-
PaymentSendFailure::PathParameterError(per_path_results) => {
470-
for res in per_path_results {
471-
if let Err(api_err) = res {
472-
check_api_err(api_err, sendable_bounds_violated);
473-
}
474-
}
475-
},
476-
PaymentSendFailure::AllFailedResendSafe(per_path_results) => {
477-
for api_err in per_path_results {
478-
check_api_err(api_err, sendable_bounds_violated);
479-
}
480-
},
481-
PaymentSendFailure::PartialFailure { results, .. } => {
482-
for res in results {
483-
if let Err(api_err) = res {
484-
check_api_err(api_err, sendable_bounds_violated);
485-
}
486-
}
487-
},
488-
PaymentSendFailure::DuplicatePayment => panic!(),
489-
}
490-
}
491492

492493
type ChanMan<'a> = ChannelManager<
493494
Arc<TestChainMonitor>,
@@ -546,7 +547,8 @@ fn send_payment(
546547
.find(|chan| chan.short_channel_id == Some(dest_chan_id))
547548
.map(|chan| (chan.next_outbound_htlc_minimum_msat, chan.next_outbound_htlc_limit_msat))
548549
.unwrap_or((0, 0));
549-
if let Err(err) = source.send_payment_with_route(
550+
let mut next_routes = source.router.next_routes.lock().unwrap();
551+
next_routes.push_back(
550552
Route {
551553
paths: vec![Path {
552554
hops: vec![RouteHop {
@@ -561,19 +563,21 @@ fn send_payment(
561563
blinded_tail: None,
562564
}],
563565
route_params: None,
564-
},
566+
}
567+
);
568+
let route_params = RouteParameters::from_payment_params_and_value(
569+
PaymentParameters::from_node_id(source.get_our_node_id(), TEST_FINAL_CLTV), amt
570+
);
571+
if let Err(err) = source.send_payment(
565572
payment_hash,
566573
RecipientOnionFields::secret_only(payment_secret),
567574
PaymentId(payment_id),
575+
route_params,
576+
Retry::Attempts(0)
568577
) {
569-
check_payment_err(err, amt > max_value_sendable || amt < min_value_sendable);
570-
false
578+
panic!("Errored with {:?} on initial payment send", err);
571579
} else {
572-
// Note that while the max is a strict upper-bound, we can occasionally send substantially
573-
// below the minimum, with some gap which is unusable immediately below the minimum. Thus,
574-
// we don't check against min_value_sendable here.
575-
assert!(amt <= max_value_sendable);
576-
true
580+
check_payment_send_events(source, amt, min_value_sendable, max_value_sendable)
577581
}
578582
}
579583

@@ -615,7 +619,8 @@ fn send_hop_payment(
615619
.map(|chan| (chan.next_outbound_htlc_minimum_msat, chan.next_outbound_htlc_limit_msat))
616620
.unwrap_or((0, 0));
617621
let first_hop_fee = 50_000;
618-
if let Err(err) = source.send_payment_with_route(
622+
let mut next_routes = source.router.next_routes.lock().unwrap();
623+
next_routes.push_back(
619624
Route {
620625
paths: vec![Path {
621626
hops: vec![
@@ -641,28 +646,30 @@ fn send_hop_payment(
641646
blinded_tail: None,
642647
}],
643648
route_params: None,
644-
},
649+
}
650+
);
651+
let route_params = RouteParameters::from_payment_params_and_value(
652+
PaymentParameters::from_node_id(source.get_our_node_id(), TEST_FINAL_CLTV), amt
653+
);
654+
if let Err(err) = source.send_payment(
645655
payment_hash,
646656
RecipientOnionFields::secret_only(payment_secret),
647657
PaymentId(payment_id),
658+
route_params,
659+
Retry::Attempts(0)
648660
) {
649-
let sent_amt = amt + first_hop_fee;
650-
check_payment_err(err, sent_amt < min_value_sendable || sent_amt > max_value_sendable);
651-
false
661+
panic!("Errored with {:?} on initial payment send", err);
652662
} else {
653-
// Note that while the max is a strict upper-bound, we can occasionally send substantially
654-
// below the minimum, with some gap which is unusable immediately below the minimum. Thus,
655-
// we don't check against min_value_sendable here.
656-
assert!(amt + first_hop_fee <= max_value_sendable);
657-
true
663+
let sent_amt = amt + first_hop_fee;
664+
check_payment_send_events(source, sent_amt, min_value_sendable, max_value_sendable)
658665
}
659666
}
660667

661668
#[inline]
662669
pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out, anchors: bool) {
663670
let out = SearchingOutput::new(underlying_out);
664671
let broadcast = Arc::new(TestBroadcaster {});
665-
let router = FuzzRouter {};
672+
let router = FuzzRouter { next_routes: Mutex::new(VecDeque::new()) };
666673

667674
macro_rules! make_node {
668675
($node_id: expr, $fee_estimator: expr) => {{

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ use core::time::Duration;
125125
use core::ops::Deref;
126126
use bitcoin::hex::impl_fmt_traits;
127127
// Re-export this for use in the public API.
128-
pub use crate::ln::outbound_payment::{Bolt12PaymentError, PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
128+
pub use crate::ln::outbound_payment::{Bolt12PaymentError, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
129+
#[cfg(test)]
130+
pub(crate) use crate::ln::outbound_payment::PaymentSendFailure;
129131
use crate::ln::script::ShutdownScript;
130132

131133
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
@@ -2363,7 +2365,9 @@ where
23632365
fee_estimator: LowerBoundedFeeEstimator<F>,
23642366
chain_monitor: M,
23652367
tx_broadcaster: T,
2366-
#[allow(unused)]
2368+
#[cfg(fuzzing)]
2369+
pub router: R,
2370+
#[cfg(not(fuzzing))]
23672371
router: R,
23682372
message_router: MR,
23692373

@@ -4525,8 +4529,11 @@ where
45254529
// [`TestRouter::expect_find_route`] instead.
45264530
//
45274531
// [`TestRouter::expect_find_route`]: crate::util::test_utils::TestRouter::expect_find_route
4528-
#[cfg(any(test, fuzzing))]
4529-
pub fn send_payment_with_route(&self, route: Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, payment_id: PaymentId) -> Result<(), PaymentSendFailure> {
4532+
#[cfg(test)]
4533+
pub(crate) fn send_payment_with_route(
4534+
&self, route: Route, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
4535+
payment_id: PaymentId
4536+
) -> Result<(), PaymentSendFailure> {
45304537
let best_block_height = self.best_block.read().unwrap().height;
45314538
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
45324539
self.pending_outbound_payments

lightning/src/ln/outbound_payment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pub enum RetryableSendFailure {
482482
/// as the Err() type describing which state the payment is in, see the description of individual
483483
/// enum states for more.
484484
#[derive(Clone, Debug, PartialEq, Eq)]
485-
pub enum PaymentSendFailure {
485+
pub(crate) enum PaymentSendFailure {
486486
/// A parameter which was passed to send_payment was invalid, preventing us from attempting to
487487
/// send the payment at all.
488488
///

0 commit comments

Comments
 (0)