@@ -47,8 +47,8 @@ use lightning::events::MessageSendEventsProvider;
4747use lightning:: ln:: channel:: FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE ;
4848use lightning:: ln:: channel_state:: ChannelDetails ;
4949use lightning:: ln:: channelmanager:: {
50- ChainParameters , ChannelManager , ChannelManagerReadArgs , PaymentId , PaymentSendFailure ,
51- RecipientOnionFields ,
50+ ChainParameters , ChannelManager , ChannelManagerReadArgs , PaymentId ,
51+ RecipientOnionFields , Retry
5252} ;
5353use lightning:: ln:: functional_test_utils:: * ;
5454use lightning:: ln:: msgs:: {
@@ -58,7 +58,7 @@ use lightning::ln::script::ShutdownScript;
5858use lightning:: ln:: types:: ChannelId ;
5959use lightning:: offers:: invoice:: UnsignedBolt12Invoice ;
6060use 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 } ;
6262use lightning:: sign:: {
6363 EntropySource , InMemorySigner , KeyMaterial , NodeSigner , Recipient , SignerProvider ,
6464} ;
@@ -82,6 +82,7 @@ use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}
8282
8383use lightning:: io:: Cursor ;
8484use std:: cmp:: { self , Ordering } ;
85+ use std:: collections:: VecDeque ;
8586use std:: mem;
8687use std:: sync:: atomic;
8788use 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
117120impl 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]
438467fn 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
492493type 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]
662669pub 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) => { {
0 commit comments