Skip to content

Commit 6ed1e5f

Browse files
Fix scoring methods to score Paths instead of Vec<RouteHop>s
1 parent b0d9cdd commit 6ed1e5f

File tree

4 files changed

+169
-115
lines changed

4 files changed

+169
-115
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,20 @@ fn update_scorer<'a, S: 'static + Deref<Target = SC> + Send + Sync, SC: 'a + Wri
236236
let mut score = scorer.lock();
237237
match event {
238238
Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
239-
let path = path.iter().collect::<Vec<_>>();
240239
score.payment_path_failed(&path, *scid);
241240
},
242241
Event::PaymentPathFailed { ref path, payment_failed_permanently: true, .. } => {
243242
// Reached if the destination explicitly failed it back. We treat this as a successful probe
244243
// because the payment made it all the way to the destination with sufficient liquidity.
245-
let path = path.iter().collect::<Vec<_>>();
246244
score.probe_successful(&path);
247245
},
248246
Event::PaymentPathSuccessful { path, .. } => {
249-
let path = path.iter().collect::<Vec<_>>();
250247
score.payment_path_successful(&path);
251248
},
252249
Event::ProbeSuccessful { path, .. } => {
253-
let path = path.iter().collect::<Vec<_>>();
254250
score.probe_successful(&path);
255251
},
256252
Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
257-
let path = path.iter().collect::<Vec<_>>();
258253
score.probe_failed(&path, *scid);
259254
},
260255
_ => {},
@@ -751,7 +746,7 @@ mod tests {
751746
use lightning::ln::msgs::{ChannelMessageHandler, Init};
752747
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
753748
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
754-
use lightning::routing::router::{DefaultRouter, RouteHop};
749+
use lightning::routing::router::{DefaultRouter, Path, RouteHop};
755750
use lightning::routing::scoring::{ChannelUsage, Score};
756751
use lightning::util::config::UserConfig;
757752
use lightning::util::ser::Writeable;
@@ -891,10 +886,10 @@ mod tests {
891886

892887
#[derive(Debug)]
893888
enum TestResult {
894-
PaymentFailure { path: Vec<RouteHop>, short_channel_id: u64 },
895-
PaymentSuccess { path: Vec<RouteHop> },
896-
ProbeFailure { path: Vec<RouteHop> },
897-
ProbeSuccess { path: Vec<RouteHop> },
889+
PaymentFailure { path: Path, short_channel_id: u64 },
890+
PaymentSuccess { path: Path },
891+
ProbeFailure { path: Path },
892+
ProbeSuccess { path: Path },
898893
}
899894

900895
impl TestScorer {
@@ -916,11 +911,11 @@ mod tests {
916911
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
917912
) -> u64 { unimplemented!(); }
918913

919-
fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
914+
fn payment_path_failed(&mut self, actual_path: &Path, actual_short_channel_id: u64) {
920915
if let Some(expectations) = &mut self.event_expectations {
921916
match expectations.pop_front().unwrap() {
922917
TestResult::PaymentFailure { path, short_channel_id } => {
923-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
918+
assert_eq!(actual_path, &path);
924919
assert_eq!(actual_short_channel_id, short_channel_id);
925920
},
926921
TestResult::PaymentSuccess { path } => {
@@ -936,14 +931,14 @@ mod tests {
936931
}
937932
}
938933

939-
fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
934+
fn payment_path_successful(&mut self, actual_path: &Path) {
940935
if let Some(expectations) = &mut self.event_expectations {
941936
match expectations.pop_front().unwrap() {
942937
TestResult::PaymentFailure { path, .. } => {
943938
panic!("Unexpected payment path failure: {:?}", path)
944939
},
945940
TestResult::PaymentSuccess { path } => {
946-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
941+
assert_eq!(actual_path, &path);
947942
},
948943
TestResult::ProbeFailure { path } => {
949944
panic!("Unexpected probe failure: {:?}", path)
@@ -955,7 +950,7 @@ mod tests {
955950
}
956951
}
957952

958-
fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) {
953+
fn probe_failed(&mut self, actual_path: &Path, _: u64) {
959954
if let Some(expectations) = &mut self.event_expectations {
960955
match expectations.pop_front().unwrap() {
961956
TestResult::PaymentFailure { path, .. } => {
@@ -965,15 +960,15 @@ mod tests {
965960
panic!("Unexpected payment path success: {:?}", path)
966961
},
967962
TestResult::ProbeFailure { path } => {
968-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
963+
assert_eq!(actual_path, &path);
969964
},
970965
TestResult::ProbeSuccess { path } => {
971966
panic!("Unexpected probe success: {:?}", path)
972967
}
973968
}
974969
}
975970
}
976-
fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
971+
fn probe_successful(&mut self, actual_path: &Path) {
977972
if let Some(expectations) = &mut self.event_expectations {
978973
match expectations.pop_front().unwrap() {
979974
TestResult::PaymentFailure { path, .. } => {
@@ -986,7 +981,7 @@ mod tests {
986981
panic!("Unexpected probe failure: {:?}", path)
987982
},
988983
TestResult::ProbeSuccess { path } => {
989-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
984+
assert_eq!(actual_path, &path);
990985
}
991986
}
992987
}
@@ -1510,14 +1505,14 @@ mod tests {
15101505
let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap();
15111506
let node_1_id = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
15121507

1513-
let path = vec![RouteHop {
1508+
let path = Path { hops: vec![RouteHop {
15141509
pubkey: node_1_id,
15151510
node_features: NodeFeatures::empty(),
15161511
short_channel_id: scored_scid,
15171512
channel_features: ChannelFeatures::empty(),
15181513
fee_msat: 0,
15191514
cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA as u32,
1520-
}];
1515+
}], blinded_tail: None };
15211516

15221517
$nodes[0].scorer.lock().unwrap().expect(TestResult::PaymentFailure { path: path.clone(), short_channel_id: scored_scid });
15231518
$nodes[0].node.push_pending_event(Event::PaymentPathFailed {

lightning/src/routing/router.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> {
137137
}
138138
}
139139

140-
fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
140+
fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
141141
self.scorer.payment_path_failed(path, short_channel_id)
142142
}
143143

144-
fn payment_path_successful(&mut self, path: &[&RouteHop]) {
144+
fn payment_path_successful(&mut self, path: &Path) {
145145
self.scorer.payment_path_successful(path)
146146
}
147147

148-
fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
148+
fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
149149
self.scorer.probe_failed(path, short_channel_id)
150150
}
151151

152-
fn probe_successful(&mut self, path: &[&RouteHop]) {
152+
fn probe_successful(&mut self, path: &Path) {
153153
self.scorer.probe_successful(path)
154154
}
155155
}
@@ -2251,13 +2251,13 @@ fn build_route_from_hops_internal<L: Deref>(
22512251
u64::max_value()
22522252
}
22532253

2254-
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
2254+
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
22552255

2256-
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
2256+
fn payment_path_successful(&mut self, _path: &Path) {}
22572257

2258-
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
2258+
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
22592259

2260-
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
2260+
fn probe_successful(&mut self, _path: &Path) {}
22612261
}
22622262

22632263
impl<'a> Writeable for HopScorer {
@@ -5268,10 +5268,10 @@ mod tests {
52685268
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
52695269
}
52705270

5271-
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5272-
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
5273-
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5274-
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
5271+
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5272+
fn payment_path_successful(&mut self, _path: &Path) {}
5273+
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5274+
fn probe_successful(&mut self, _path: &Path) {}
52755275
}
52765276

52775277
struct BadNodeScorer {
@@ -5288,10 +5288,10 @@ mod tests {
52885288
if *target == self.node_id { u64::max_value() } else { 0 }
52895289
}
52905290

5291-
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5292-
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
5293-
fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
5294-
fn probe_successful(&mut self, _path: &[&RouteHop]) {}
5291+
fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5292+
fn payment_path_successful(&mut self, _path: &Path) {}
5293+
fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
5294+
fn probe_successful(&mut self, _path: &Path) {}
52955295
}
52965296

52975297
#[test]
@@ -5955,14 +5955,12 @@ mod benches {
59555955
let amount = route.get_total_amount();
59565956
if amount < 250_000 {
59575957
for path in route.paths {
5958-
// fixed to use the whole Path in an upcoming commit
5959-
scorer.payment_path_successful(&path.hops.iter().collect::<Vec<_>>());
5958+
scorer.payment_path_successful(&path);
59605959
}
59615960
} else if amount > 750_000 {
59625961
for path in route.paths {
59635962
let short_channel_id = path.hops[path.hops.len() / 2].short_channel_id;
5964-
// fixed to use the whole Path in an upcoming commit
5965-
scorer.payment_path_failed(&path.hops.iter().collect::<Vec<_>>(), short_channel_id);
5963+
scorer.payment_path_failed(&path, short_channel_id);
59665964
}
59675965
}
59685966
}

0 commit comments

Comments
 (0)