Skip to content

Commit 6a60d9c

Browse files
committed
Pass effective capacity to scorer
Scorers could benefit from having the channel's EffectiveCapacity rather than a u64 msat value. For instance, ProbabilisticScorer can give a more accurate penalty when given the ExactLiquidity variant. Pass a struct wrapping the effective capacity, the proposed amount, and any in-flight HTLC value.
1 parent fc9b2e3 commit 6a60d9c

File tree

3 files changed

+401
-159
lines changed

3 files changed

+401
-159
lines changed

lightning-invoice/src/payment.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
//! # use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
3939
//! # use lightning::ln::channelmanager::{ChannelDetails, PaymentId, PaymentSendFailure};
4040
//! # use lightning::ln::msgs::LightningError;
41-
//! # use lightning::routing::scoring::Score;
4241
//! # use lightning::routing::network_graph::NodeId;
4342
//! # use lightning::routing::router::{Route, RouteHop, RouteParameters};
43+
//! # use lightning::routing::scoring::{ChannelUsage, Score};
4444
//! # use lightning::util::events::{Event, EventHandler, EventsProvider};
4545
//! # use lightning::util::logger::{Logger, Record};
4646
//! # use lightning::util::ser::{Writeable, Writer};
@@ -90,7 +90,7 @@
9090
//! # }
9191
//! # impl Score for FakeScorer {
9292
//! # fn channel_penalty_msat(
93-
//! # &self, _short_channel_id: u64, _send_amt: u64, _chan_amt: u64, _source: &NodeId, _target: &NodeId
93+
//! # &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
9494
//! # ) -> u64 { 0 }
9595
//! # fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
9696
//! # fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
@@ -534,6 +534,7 @@ mod tests {
534534
use lightning::ln::msgs::{ChannelMessageHandler, ErrorAction, LightningError};
535535
use lightning::routing::network_graph::NodeId;
536536
use lightning::routing::router::{PaymentParameters, Route, RouteHop};
537+
use lightning::routing::scoring::ChannelUsage;
537538
use lightning::util::test_utils::TestLogger;
538539
use lightning::util::errors::APIError;
539540
use lightning::util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
@@ -1327,7 +1328,7 @@ mod tests {
13271328

13281329
impl Score for TestScorer {
13291330
fn channel_penalty_msat(
1330-
&self, _short_channel_id: u64, _send_amt: u64, _chan_amt: u64, _source: &NodeId, _target: &NodeId
1331+
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
13311332
) -> u64 { 0 }
13321333

13331334
fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {

lightning/src/routing/router.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::secp256k1::key::PublicKey;
1717
use ln::channelmanager::ChannelDetails;
1818
use ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures};
1919
use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
20-
use routing::scoring::Score;
20+
use routing::scoring::{ChannelUsage, Score};
2121
use routing::network_graph::{DirectedChannelInfoWithUpdate, EffectiveCapacity, NetworkGraph, ReadOnlyNetworkGraph, NodeId, RoutingFees};
2222
use util::ser::{Writeable, Readable};
2323
use util::logger::{Level, Logger};
@@ -1039,10 +1039,16 @@ where L::Target: Logger {
10391039
}
10401040
}
10411041

1042-
let available_liquidity_msat = htlc_maximum_msat - used_liquidity_msat;
1043-
let path_penalty_msat = $next_hops_path_penalty_msat.saturating_add(
1044-
scorer.channel_penalty_msat(short_channel_id, amount_to_transfer_over_msat,
1045-
available_liquidity_msat, &$src_node_id, &$dest_node_id));
1042+
let channel_usage = ChannelUsage {
1043+
amount_msat: amount_to_transfer_over_msat,
1044+
inflight_htlc_msat: used_liquidity_msat,
1045+
effective_capacity: $candidate.effective_capacity(),
1046+
};
1047+
let channel_penalty_msat = scorer.channel_penalty_msat(
1048+
short_channel_id, &$src_node_id, &$dest_node_id, channel_usage
1049+
);
1050+
let path_penalty_msat = $next_hops_path_penalty_msat
1051+
.saturating_add(channel_penalty_msat);
10461052
let new_graph_node = RouteGraphNode {
10471053
node_id: $src_node_id,
10481054
lowest_fee_to_peer_through_node: total_fee_msat,
@@ -1254,9 +1260,16 @@ where L::Target: Logger {
12541260
short_channel_id: hop.short_channel_id,
12551261
})
12561262
.unwrap_or_else(|| CandidateRouteHop::PrivateHop { hint: hop });
1257-
let capacity_msat = candidate.effective_capacity().as_msat();
1263+
let channel_usage = ChannelUsage {
1264+
amount_msat: final_value_msat,
1265+
inflight_htlc_msat: 0,
1266+
effective_capacity: candidate.effective_capacity(),
1267+
};
1268+
let channel_penalty_msat = scorer.channel_penalty_msat(
1269+
hop.short_channel_id, &source, &target, channel_usage
1270+
);
12581271
aggregate_next_hops_path_penalty_msat = aggregate_next_hops_path_penalty_msat
1259-
.saturating_add(scorer.channel_penalty_msat(hop.short_channel_id, final_value_msat, capacity_msat, &source, &target));
1272+
.saturating_add(channel_penalty_msat);
12601273

12611274
aggregate_next_hops_cltv_delta = aggregate_next_hops_cltv_delta
12621275
.saturating_add(hop.cltv_expiry_delta as u32);
@@ -1712,7 +1725,7 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
17121725
mod tests {
17131726
use routing::network_graph::{NetworkGraph, NetGraphMsgHandler, NodeId};
17141727
use routing::router::{get_route, add_random_cltv_offset, default_node_features, PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees, DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA};
1715-
use routing::scoring::Score;
1728+
use routing::scoring::{ChannelUsage, Score};
17161729
use chain::transaction::OutPoint;
17171730
use chain::keysinterface::KeysInterface;
17181731
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
@@ -5053,7 +5066,7 @@ mod tests {
50535066
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), ::io::Error> { unimplemented!() }
50545067
}
50555068
impl Score for BadChannelScorer {
5056-
fn channel_penalty_msat(&self, short_channel_id: u64, _send_amt: u64, _capacity_msat: u64, _source: &NodeId, _target: &NodeId) -> u64 {
5069+
fn channel_penalty_msat(&self, short_channel_id: u64, _: &NodeId, _: &NodeId, _: ChannelUsage) -> u64 {
50575070
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
50585071
}
50595072

@@ -5071,7 +5084,7 @@ mod tests {
50715084
}
50725085

50735086
impl Score for BadNodeScorer {
5074-
fn channel_penalty_msat(&self, _short_channel_id: u64, _send_amt: u64, _capacity_msat: u64, _source: &NodeId, target: &NodeId) -> u64 {
5087+
fn channel_penalty_msat(&self, _: u64, _: &NodeId, target: &NodeId, _: ChannelUsage) -> u64 {
50755088
if *target == self.node_id { u64::max_value() } else { 0 }
50765089
}
50775090

0 commit comments

Comments
 (0)