@@ -47,8 +47,8 @@ use lightning::events::MessageSendEventsProvider;
47
47
use lightning:: ln:: channel:: FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE ;
48
48
use lightning:: ln:: channel_state:: ChannelDetails ;
49
49
use lightning:: ln:: channelmanager:: {
50
- ChainParameters , ChannelManager , ChannelManagerReadArgs , PaymentId , PaymentSendFailure ,
51
- RecipientOnionFields ,
50
+ ChainParameters , ChannelManager , ChannelManagerReadArgs , PaymentId ,
51
+ RecipientOnionFields , Retry
52
52
} ;
53
53
use lightning:: ln:: functional_test_utils:: * ;
54
54
use lightning:: ln:: msgs:: {
@@ -58,7 +58,7 @@ use lightning::ln::script::ShutdownScript;
58
58
use lightning:: ln:: types:: ChannelId ;
59
59
use lightning:: offers:: invoice:: UnsignedBolt12Invoice ;
60
60
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 } ;
62
62
use lightning:: sign:: {
63
63
EntropySource , InMemorySigner , KeyMaterial , NodeSigner , Recipient , SignerProvider ,
64
64
} ;
@@ -82,6 +82,7 @@ use bitcoin::secp256k1::{self, Message, PublicKey, Scalar, Secp256k1, SecretKey}
82
82
83
83
use lightning:: io:: Cursor ;
84
84
use std:: cmp:: { self , Ordering } ;
85
+ use std:: collections:: VecDeque ;
85
86
use std:: mem;
86
87
use std:: sync:: atomic;
87
88
use std:: sync:: { Arc , Mutex } ;
@@ -112,13 +113,18 @@ impl FeeEstimator for FuzzEstimator {
112
113
}
113
114
}
114
115
115
- struct FuzzRouter { }
116
+ struct FuzzRouter {
117
+ pub next_routes : Mutex < VecDeque < Route > > ,
118
+ }
116
119
117
120
impl Router for FuzzRouter {
118
121
fn find_route (
119
122
& self , _payer : & PublicKey , _params : & RouteParameters ,
120
123
_first_hops : Option < & [ & ChannelDetails ] > , _inflight_htlcs : InFlightHtlcs ,
121
124
) -> Result < Route , msgs:: LightningError > {
125
+ if let Some ( route) = self . next_routes . lock ( ) . unwrap ( ) . pop_front ( ) {
126
+ return Ok ( route)
127
+ }
122
128
Err ( msgs:: LightningError {
123
129
err : String :: from ( "Not implemented" ) ,
124
130
action : msgs:: ErrorAction :: IgnoreError ,
@@ -434,6 +440,29 @@ impl KeyProvider {
434
440
}
435
441
}
436
442
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
+
437
466
#[ inline]
438
467
fn check_api_err ( api_err : APIError , sendable_bounds_violated : bool ) {
439
468
match api_err {
@@ -460,34 +489,6 @@ fn check_api_err(api_err: APIError, sendable_bounds_violated: bool) {
460
489
} ,
461
490
}
462
491
}
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
- }
491
492
492
493
type ChanMan < ' a > = ChannelManager <
493
494
Arc < TestChainMonitor > ,
@@ -546,7 +547,8 @@ fn send_payment(
546
547
. find ( |chan| chan. short_channel_id == Some ( dest_chan_id) )
547
548
. map ( |chan| ( chan. next_outbound_htlc_minimum_msat , chan. next_outbound_htlc_limit_msat ) )
548
549
. 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 (
550
552
Route {
551
553
paths : vec ! [ Path {
552
554
hops: vec![ RouteHop {
@@ -561,19 +563,21 @@ fn send_payment(
561
563
blinded_tail: None ,
562
564
} ] ,
563
565
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 (
565
572
payment_hash,
566
573
RecipientOnionFields :: secret_only ( payment_secret) ,
567
574
PaymentId ( payment_id) ,
575
+ route_params,
576
+ Retry :: Attempts ( 0 )
568
577
) {
569
- check_payment_err ( err, amt > max_value_sendable || amt < min_value_sendable) ;
570
- false
578
+ panic ! ( "Errored with {:?} on initial payment send" , err) ;
571
579
} 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)
577
581
}
578
582
}
579
583
@@ -615,7 +619,8 @@ fn send_hop_payment(
615
619
. map ( |chan| ( chan. next_outbound_htlc_minimum_msat , chan. next_outbound_htlc_limit_msat ) )
616
620
. unwrap_or ( ( 0 , 0 ) ) ;
617
621
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 (
619
624
Route {
620
625
paths : vec ! [ Path {
621
626
hops: vec![
@@ -641,28 +646,30 @@ fn send_hop_payment(
641
646
blinded_tail: None ,
642
647
} ] ,
643
648
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 (
645
655
payment_hash,
646
656
RecipientOnionFields :: secret_only ( payment_secret) ,
647
657
PaymentId ( payment_id) ,
658
+ route_params,
659
+ Retry :: Attempts ( 0 )
648
660
) {
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) ;
652
662
} 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)
658
665
}
659
666
}
660
667
661
668
#[ inline]
662
669
pub fn do_test < Out : Output > ( data : & [ u8 ] , underlying_out : Out , anchors : bool ) {
663
670
let out = SearchingOutput :: new ( underlying_out) ;
664
671
let broadcast = Arc :: new ( TestBroadcaster { } ) ;
665
- let router = FuzzRouter { } ;
672
+ let router = FuzzRouter { next_routes : Mutex :: new ( VecDeque :: new ( ) ) } ;
666
673
667
674
macro_rules! make_node {
668
675
( $node_id: expr, $fee_estimator: expr) => { {
0 commit comments