Skip to content

Commit 9ffe35c

Browse files
Fix scoring methods to score Paths instead of Vec<RouteHop>s
1 parent 35a3853 commit 9ffe35c

File tree

4 files changed

+169
-115
lines changed

4 files changed

+169
-115
lines changed

lightning-background-processor/src/lib.rs

+15-20
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
_ => {},
@@ -748,7 +743,7 @@ mod tests {
748743
use lightning::ln::msgs::{ChannelMessageHandler, Init};
749744
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
750745
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
751-
use lightning::routing::router::{DefaultRouter, RouteHop};
746+
use lightning::routing::router::{DefaultRouter, Path, RouteHop};
752747
use lightning::routing::scoring::{ChannelUsage, Score};
753748
use lightning::util::config::UserConfig;
754749
use lightning::util::ser::Writeable;
@@ -888,10 +883,10 @@ mod tests {
888883

889884
#[derive(Debug)]
890885
enum TestResult {
891-
PaymentFailure { path: Vec<RouteHop>, short_channel_id: u64 },
892-
PaymentSuccess { path: Vec<RouteHop> },
893-
ProbeFailure { path: Vec<RouteHop> },
894-
ProbeSuccess { path: Vec<RouteHop> },
886+
PaymentFailure { path: Path, short_channel_id: u64 },
887+
PaymentSuccess { path: Path },
888+
ProbeFailure { path: Path },
889+
ProbeSuccess { path: Path },
895890
}
896891

897892
impl TestScorer {
@@ -913,11 +908,11 @@ mod tests {
913908
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
914909
) -> u64 { unimplemented!(); }
915910

916-
fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
911+
fn payment_path_failed(&mut self, actual_path: &Path, actual_short_channel_id: u64) {
917912
if let Some(expectations) = &mut self.event_expectations {
918913
match expectations.pop_front().unwrap() {
919914
TestResult::PaymentFailure { path, short_channel_id } => {
920-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
915+
assert_eq!(actual_path, &path);
921916
assert_eq!(actual_short_channel_id, short_channel_id);
922917
},
923918
TestResult::PaymentSuccess { path } => {
@@ -933,14 +928,14 @@ mod tests {
933928
}
934929
}
935930

936-
fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
931+
fn payment_path_successful(&mut self, actual_path: &Path) {
937932
if let Some(expectations) = &mut self.event_expectations {
938933
match expectations.pop_front().unwrap() {
939934
TestResult::PaymentFailure { path, .. } => {
940935
panic!("Unexpected payment path failure: {:?}", path)
941936
},
942937
TestResult::PaymentSuccess { path } => {
943-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
938+
assert_eq!(actual_path, &path);
944939
},
945940
TestResult::ProbeFailure { path } => {
946941
panic!("Unexpected probe failure: {:?}", path)
@@ -952,7 +947,7 @@ mod tests {
952947
}
953948
}
954949

955-
fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) {
950+
fn probe_failed(&mut self, actual_path: &Path, _: u64) {
956951
if let Some(expectations) = &mut self.event_expectations {
957952
match expectations.pop_front().unwrap() {
958953
TestResult::PaymentFailure { path, .. } => {
@@ -962,15 +957,15 @@ mod tests {
962957
panic!("Unexpected payment path success: {:?}", path)
963958
},
964959
TestResult::ProbeFailure { path } => {
965-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
960+
assert_eq!(actual_path, &path);
966961
},
967962
TestResult::ProbeSuccess { path } => {
968963
panic!("Unexpected probe success: {:?}", path)
969964
}
970965
}
971966
}
972967
}
973-
fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
968+
fn probe_successful(&mut self, actual_path: &Path) {
974969
if let Some(expectations) = &mut self.event_expectations {
975970
match expectations.pop_front().unwrap() {
976971
TestResult::PaymentFailure { path, .. } => {
@@ -983,7 +978,7 @@ mod tests {
983978
panic!("Unexpected probe failure: {:?}", path)
984979
},
985980
TestResult::ProbeSuccess { path } => {
986-
assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
981+
assert_eq!(actual_path, &path);
987982
}
988983
}
989984
}
@@ -1431,14 +1426,14 @@ mod tests {
14311426
let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap();
14321427
let node_1_id = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
14331428

1434-
let path = vec![RouteHop {
1429+
let path = Path { hops: vec![RouteHop {
14351430
pubkey: node_1_id,
14361431
node_features: NodeFeatures::empty(),
14371432
short_channel_id: scored_scid,
14381433
channel_features: ChannelFeatures::empty(),
14391434
fee_msat: 0,
14401435
cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA as u32,
1441-
}];
1436+
}], blinded_tail: None };
14421437

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

lightning/src/routing/router.rs

+18-20
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)