Skip to content

Commit 585f8d8

Browse files
committed
Provide add probing interface to Scorer
1 parent e213ba5 commit 585f8d8

File tree

3 files changed

+107
-14
lines changed

3 files changed

+107
-14
lines changed

lightning-invoice/src/payment.rs

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
//! # ) -> u64 { 0 }
105105
//! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
106106
//! # fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
107+
//! # fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
108+
//! # fn probe_successful(&mut self, _path: &[&RouteHop]) {}
107109
//! # }
108110
//! #
109111
//! # struct FakeLogger {}
@@ -595,8 +597,11 @@ where
595597
// hop and then drop the event instead of handing it up to the user's event
596598
// handler.
597599
if self.payer.payment_is_probe(*payment_hash, *payment_id) {
598-
let scid = if *rejected_by_dest { u64::max_value() } else { *short_channel_id };
599-
self.scorer.lock().payment_path_failed(&path, scid);
600+
if *rejected_by_dest {
601+
self.scorer.lock().probe_successful(&path);
602+
} else {
603+
self.scorer.lock().probe_failed(&path, *short_channel_id);
604+
}
600605
return;
601606
}
602607
}
@@ -1345,7 +1350,7 @@ mod tests {
13451350
.expect_send(Amount::ForInvoice(final_value_msat))
13461351
.expect_send(Amount::OnRetry(final_value_msat / 2));
13471352
let router = TestRouter {};
1348-
let scorer = RefCell::new(TestScorer::new().expect(PaymentPath::Failure {
1353+
let scorer = RefCell::new(TestScorer::new().expect(ExpectedPaymentResult::PathFailure {
13491354
path: path.clone(), short_channel_id: path[0].short_channel_id,
13501355
}));
13511356
let logger = TestLogger::new();
@@ -1381,8 +1386,8 @@ mod tests {
13811386
let payer = TestPayer::new().expect_send(Amount::ForInvoice(final_value_msat));
13821387
let router = TestRouter {};
13831388
let scorer = RefCell::new(TestScorer::new()
1384-
.expect(PaymentPath::Success { path: route.paths[0].clone() })
1385-
.expect(PaymentPath::Success { path: route.paths[1].clone() })
1389+
.expect(ExpectedPaymentResult::PathSuccess { path: route.paths[0].clone() })
1390+
.expect(ExpectedPaymentResult::PathSuccess { path: route.paths[1].clone() })
13861391
);
13871392
let logger = TestLogger::new();
13881393
let invoice_payer =
@@ -1479,13 +1484,15 @@ mod tests {
14791484
}
14801485

14811486
struct TestScorer {
1482-
expectations: Option<VecDeque<PaymentPath>>,
1487+
expectations: Option<VecDeque<ExpectedPaymentResult>>,
14831488
}
14841489

14851490
#[derive(Debug)]
1486-
enum PaymentPath {
1487-
Failure { path: Vec<RouteHop>, short_channel_id: u64 },
1488-
Success { path: Vec<RouteHop> },
1491+
enum ExpectedPaymentResult {
1492+
PathFailure { path: Vec<RouteHop>, short_channel_id: u64 },
1493+
PathSuccess { path: Vec<RouteHop> },
1494+
ProbeFailure { path: Vec<RouteHop>, short_channel_id: u64 },
1495+
ProbeSuccess { path: Vec<RouteHop> },
14891496
}
14901497

14911498
impl TestScorer {
@@ -1495,7 +1502,7 @@ mod tests {
14951502
}
14961503
}
14971504

1498-
fn expect(mut self, expectation: PaymentPath) -> Self {
1505+
fn expect(mut self, expectation: ExpectedPaymentResult) -> Self {
14991506
self.expectations.get_or_insert_with(|| VecDeque::new()).push_back(expectation);
15001507
self
15011508
}
@@ -1514,13 +1521,19 @@ mod tests {
15141521
fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
15151522
if let Some(expectations) = &mut self.expectations {
15161523
match expectations.pop_front() {
1517-
Some(PaymentPath::Failure { path, short_channel_id }) => {
1524+
Some(ExpectedPaymentResult::PathFailure { path, short_channel_id }) => {
15181525
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
15191526
assert_eq!(actual_short_channel_id, short_channel_id);
15201527
},
1521-
Some(PaymentPath::Success { path }) => {
1528+
Some(ExpectedPaymentResult::PathSuccess { path }) => {
15221529
panic!("Unexpected successful payment path: {:?}", path)
15231530
},
1531+
Some(ExpectedPaymentResult::ProbeFailure { path, .. }) => {
1532+
panic!("Unexpected failed payment probe: {:?}", path)
1533+
},
1534+
Some(ExpectedPaymentResult::ProbeSuccess { path }) => {
1535+
panic!("Unexpected successful payment probe: {:?}", path)
1536+
},
15241537
None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
15251538
}
15261539
}
@@ -1529,10 +1542,56 @@ mod tests {
15291542
fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
15301543
if let Some(expectations) = &mut self.expectations {
15311544
match expectations.pop_front() {
1532-
Some(PaymentPath::Failure { path, .. }) => {
1545+
Some(ExpectedPaymentResult::PathFailure { path, .. }) => {
15331546
panic!("Unexpected payment path failure: {:?}", path)
15341547
},
1535-
Some(PaymentPath::Success { path }) => {
1548+
Some(ExpectedPaymentResult::PathSuccess { path }) => {
1549+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1550+
},
1551+
Some(ExpectedPaymentResult::ProbeFailure { path, .. }) => {
1552+
panic!("Unexpected failed payment probe: {:?}", path)
1553+
},
1554+
Some(ExpectedPaymentResult::ProbeSuccess { path }) => {
1555+
panic!("Unexpected successful payment probe: {:?}", path)
1556+
},
1557+
None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),
1558+
}
1559+
}
1560+
}
1561+
1562+
fn probe_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
1563+
if let Some(expectations) = &mut self.expectations {
1564+
match expectations.pop_front() {
1565+
Some(ExpectedPaymentResult::PathFailure { path, .. }) => {
1566+
panic!("Unexpected failed payment path: {:?}", path)
1567+
},
1568+
Some(ExpectedPaymentResult::PathSuccess { path }) => {
1569+
panic!("Unexpected successful payment path: {:?}", path)
1570+
},
1571+
Some(ExpectedPaymentResult::ProbeFailure { path, short_channel_id }) => {
1572+
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
1573+
assert_eq!(actual_short_channel_id, short_channel_id);
1574+
},
1575+
Some(ExpectedPaymentResult::ProbeSuccess { path }) => {
1576+
panic!("Unexpected successful payment probe: {:?}", path)
1577+
},
1578+
None => panic!("Unexpected payment_path_failed call: {:?}", actual_path),
1579+
}
1580+
}
1581+
}
1582+
fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
1583+
if let Some(expectations) = &mut self.expectations {
1584+
match expectations.pop_front() {
1585+
Some(ExpectedPaymentResult::PathFailure { path, .. }) => {
1586+
panic!("Unexpected payment path failure: {:?}", path)
1587+
},
1588+
Some(ExpectedPaymentResult::PathSuccess { path }) => {
1589+
panic!("Unexpected successful payment path: {:?}", path)
1590+
},
1591+
Some(ExpectedPaymentResult::ProbeFailure { path, .. }) => {
1592+
panic!("Unexpected failed payment probe: {:?}", path)
1593+
},
1594+
Some(ExpectedPaymentResult::ProbeSuccess { path }) => {
15361595
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
15371596
},
15381597
None => panic!("Unexpected payment_path_successful call: {:?}", actual_path),

lightning/src/routing/router.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,10 @@ fn build_route_from_hops_internal<L: Deref>(
18491849
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
18501850

18511851
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
1852+
1853+
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
1854+
1855+
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
18521856
}
18531857

18541858
impl<'a> Writeable for HopScorer {
@@ -5314,6 +5318,8 @@ mod tests {
53145318

53155319
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
53165320
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
5321+
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5322+
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
53175323
}
53185324

53195325
struct BadNodeScorer {
@@ -5332,6 +5338,8 @@ mod tests {
53325338

53335339
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
53345340
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
5341+
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5342+
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
53355343
}
53365344

53375345
#[test]

lightning/src/routing/scoring.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ pub trait Score $(: $supertrait)* {
102102

103103
/// Handles updating channel penalties after successfully routing along a path.
104104
fn payment_path_successful(&mut self, path: &[&RouteHop]);
105+
106+
/// Handles updating channel penalties after a probe over the given path failed.
107+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
108+
109+
/// Handles updating channel penalties after a probe over the given path succeeded.
110+
fn probe_successful(&mut self, path: &[&RouteHop]);
105111
}
106112

107113
impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
@@ -118,6 +124,14 @@ impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
118124
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
119125
self.deref_mut().payment_path_successful(path)
120126
}
127+
128+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
129+
self.deref_mut().probe_failed(path, short_channel_id)
130+
}
131+
132+
fn probe_successful(&mut self, path: &[&RouteHop]) {
133+
self.deref_mut().probe_successful(path)
134+
}
121135
}
122136
} }
123137

@@ -241,6 +255,10 @@ impl Score for FixedPenaltyScorer {
241255
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
242256

243257
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
258+
259+
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
260+
261+
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
244262
}
245263

246264
impl Writeable for FixedPenaltyScorer {
@@ -811,6 +829,14 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
811829
}
812830
}
813831
}
832+
833+
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
834+
self.payment_path_failed(path, short_channel_id)
835+
}
836+
837+
fn probe_successful(&mut self, path: &[&RouteHop]) {
838+
self.payment_path_failed(path, u64::max_value())
839+
}
814840
}
815841

816842
mod approx {

0 commit comments

Comments
 (0)