@@ -801,7 +801,7 @@ mod tests {
801
801
use lightning:: ln:: features:: { ChannelFeatures , NodeFeatures , InitFeatures } ;
802
802
use lightning:: ln:: functional_test_utils:: * ;
803
803
use lightning:: ln:: msgs:: { ChannelMessageHandler , ErrorAction , LightningError } ;
804
- use lightning:: routing:: gossip:: NodeId ;
804
+ use lightning:: routing:: gossip:: { EffectiveCapacity , NodeId } ;
805
805
use lightning:: routing:: router:: { PaymentParameters , Route , RouteHop } ;
806
806
use lightning:: routing:: scoring:: ChannelUsage ;
807
807
use lightning:: util:: test_utils:: TestLogger ;
@@ -1550,7 +1550,7 @@ mod tests {
1550
1550
}
1551
1551
1552
1552
#[ test]
1553
- fn inflight_data_trivial_test ( ) {
1553
+ fn inflight_map_data_trivial_test ( ) {
1554
1554
let event_handled = core:: cell:: RefCell :: new ( false ) ;
1555
1555
let event_handler = |_: & _ | { * event_handled. borrow_mut ( ) = true ; } ;
1556
1556
@@ -1590,7 +1590,57 @@ mod tests {
1590
1590
assert_eq ! ( inflight_map. get( & ( 2 , true ) ) , None ) ;
1591
1591
1592
1592
// Second path should still be inflight
1593
- assert_eq ! ( inflight_map. get( & ( 1 , true ) ) . unwrap( ) . clone( ) , 64 ) ;
1593
+ assert_eq ! ( inflight_map. get( & ( 1 , true ) ) . unwrap( ) . clone( ) , 64 )
1594
+ }
1595
+
1596
+ #[ test]
1597
+ fn inflight_usage_trivial_test ( ) {
1598
+ // First, let's just send a payment through, but only make sure one of the path completes
1599
+ let event_handled = core:: cell:: RefCell :: new ( false ) ;
1600
+ let event_handler = |_: & _ | { * event_handled. borrow_mut ( ) = true ; } ;
1601
+
1602
+ let payment_preimage = PaymentPreimage ( [ 1 ; 32 ] ) ;
1603
+ let payment_invoice = invoice ( payment_preimage) ;
1604
+ let payment_hash = Some ( PaymentHash ( payment_invoice. payment_hash ( ) . clone ( ) . into_inner ( ) ) ) ;
1605
+ let final_value_msat = payment_invoice. amount_milli_satoshis ( ) . unwrap ( ) ;
1606
+
1607
+ let payer = TestPayer :: new ( )
1608
+ . expect_send ( Amount :: ForInvoice ( 128 ) )
1609
+ . expect_send ( Amount :: ForInvoice ( 128 ) ) ;
1610
+ let final_value_msat = payment_invoice. amount_milli_satoshis ( ) . unwrap ( ) ;
1611
+ let route = TestRouter :: route_for_value ( final_value_msat) ;
1612
+ let router = TestRouter { } ;
1613
+ let scorer = RefCell :: new ( TestScorer :: new ( )
1614
+ // 1st invoice, 1st path
1615
+ . expect_usage ( ChannelUsage { amount_msat : 10 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1616
+ . expect_usage ( ChannelUsage { amount_msat : 20 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1617
+ . expect_usage ( ChannelUsage { amount_msat : 64 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1618
+ // 1st invoice, 2nd path
1619
+ . expect_usage ( ChannelUsage { amount_msat : 64 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1620
+ // 2nd invoice, 1st path
1621
+ . expect_usage ( ChannelUsage { amount_msat : 10 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1622
+ . expect_usage ( ChannelUsage { amount_msat : 20 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1623
+ . expect_usage ( ChannelUsage { amount_msat : 64 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1624
+ // 2nd invoice, 2nd path
1625
+ . expect_usage ( ChannelUsage { amount_msat : 128 , inflight_htlc_msat : 0 , effective_capacity : EffectiveCapacity :: Unknown } )
1626
+ ) ;
1627
+ let logger = TestLogger :: new ( ) ;
1628
+ let invoice_payer =
1629
+ InvoicePayer :: new ( & payer, router, & scorer, & logger, event_handler, Retry :: Attempts ( 0 ) ) ;
1630
+
1631
+ // Succeed 1st path, leave 2nd path inflight
1632
+ let payment_id = invoice_payer. pay_invoice ( & payment_invoice) . unwrap ( ) ;
1633
+ invoice_payer. handle_event ( & Event :: PaymentPathSuccessful {
1634
+ payment_id, payment_hash, path : route. paths [ 0 ] . clone ( )
1635
+ } ) ;
1636
+
1637
+ // Let's pay a second invoice that will be using the same path. This should trigger the
1638
+ // assertions that expect the last 4 ChannelUsage values above where TestScorer is initialized.
1639
+ // Particularly, the 2nd path of the 1st payment, since it is not yet complete, should still
1640
+ // have 64 msats inflight. Making the total inflight 128.
1641
+ let payment_preimage_2 = PaymentPreimage ( [ 2 ; 32 ] ) ;
1642
+ let payment_invoice_2 = invoice ( payment_preimage_2) ;
1643
+ invoice_payer. pay_invoice ( & payment_invoice_2) . unwrap ( ) ;
1594
1644
}
1595
1645
1596
1646
struct TestRouter ;
@@ -1658,9 +1708,22 @@ mod tests {
1658
1708
1659
1709
impl Router for TestRouter {
1660
1710
fn find_route < S : Score > (
1661
- & self , _payer : & PublicKey , route_params : & RouteParameters , _payment_hash : & PaymentHash ,
1662
- _first_hops : Option < & [ & ChannelDetails ] > , _scorer : & S
1711
+ & self , payer : & PublicKey , route_params : & RouteParameters , _payment_hash : & PaymentHash ,
1712
+ _first_hops : Option < & [ & ChannelDetails ] > , scorer : & S
1663
1713
) -> Result < Route , LightningError > {
1714
+ // Simulate calling the Scorer just as you would in find_route
1715
+ let route = Self :: route_for_value ( route_params. final_value_msat ) ;
1716
+ for path in route. paths {
1717
+ for hop in path {
1718
+ let usage = ChannelUsage {
1719
+ amount_msat : hop. fee_msat ,
1720
+ inflight_htlc_msat : 0 ,
1721
+ effective_capacity : EffectiveCapacity :: Unknown ,
1722
+ } ;
1723
+ scorer. channel_penalty_msat ( hop. short_channel_id , & NodeId :: from_pubkey ( payer) , & NodeId :: from_pubkey ( & hop. pubkey ) , usage) ;
1724
+ }
1725
+ }
1726
+
1664
1727
Ok ( Route {
1665
1728
payment_params : Some ( route_params. payment_params . clone ( ) ) , ..Self :: route_for_value ( route_params. final_value_msat )
1666
1729
} )
@@ -1680,6 +1743,7 @@ mod tests {
1680
1743
1681
1744
struct TestScorer {
1682
1745
expectations : Option < VecDeque < TestResult > > ,
1746
+ scorer_expectations : RefCell < VecDeque < ChannelUsage > > ,
1683
1747
}
1684
1748
1685
1749
#[ derive( Debug ) ]
@@ -1694,13 +1758,19 @@ mod tests {
1694
1758
fn new ( ) -> Self {
1695
1759
Self {
1696
1760
expectations : None ,
1761
+ scorer_expectations : RefCell :: new ( VecDeque :: new ( ) ) ,
1697
1762
}
1698
1763
}
1699
1764
1700
1765
fn expect ( mut self , expectation : TestResult ) -> Self {
1701
1766
self . expectations . get_or_insert_with ( || VecDeque :: new ( ) ) . push_back ( expectation) ;
1702
1767
self
1703
1768
}
1769
+
1770
+ fn expect_usage ( self , expectation : ChannelUsage ) -> Self {
1771
+ self . scorer_expectations . borrow_mut ( ) . push_back ( expectation) ;
1772
+ self
1773
+ }
1704
1774
}
1705
1775
1706
1776
#[ cfg( c_bindings) ]
@@ -1710,8 +1780,16 @@ mod tests {
1710
1780
1711
1781
impl Score for TestScorer {
1712
1782
fn channel_penalty_msat (
1713
- & self , _short_channel_id : u64 , _source : & NodeId , _target : & NodeId , _usage : ChannelUsage
1714
- ) -> u64 { 0 }
1783
+ & self , _short_channel_id : u64 , _source : & NodeId , _target : & NodeId , usage : ChannelUsage
1784
+ ) -> u64 {
1785
+ match self . scorer_expectations . borrow_mut ( ) . pop_front ( ) {
1786
+ Some ( expectation) => {
1787
+ assert_eq ! ( expectation. amount_msat, usage. amount_msat) ;
1788
+ } ,
1789
+ None => { } ,
1790
+ }
1791
+ 0
1792
+ }
1715
1793
1716
1794
fn payment_path_failed ( & mut self , actual_path : & [ & RouteHop ] , actual_short_channel_id : u64 ) {
1717
1795
if let Some ( expectations) = & mut self . expectations {
0 commit comments