Skip to content

Commit

Permalink
add set method on CombinedScorer to overwrite local data
Browse files Browse the repository at this point in the history
This commit expands on the previously introduced merge method by
offering a way to simply replace the local scores by the liquidity
information that is obtained from an external source.
  • Loading branch information
joostjager committed Feb 6, 2025
1 parent a4993a4 commit 3808c85
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lightning/src/routing/scoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,14 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref + Clone> CombinedScore
self.scorer.channel_liquidities.insert(scid, liquidity);
}
}

/// Set scorer state to match the provided external channel liquidity information. The state for channels that are
/// not present in the external data set will remain unchanged.
pub fn set(&mut self, external_scores: ChannelLiquidities) {
for (scid, liquidity) in external_scores.0 {
self.scorer.channel_liquidities.insert(scid, liquidity);
}
}
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> ScoreLookUp for CombinedScorer<G, L> where L::Target: Logger {
Expand Down Expand Up @@ -3962,6 +3970,19 @@ mod tests {
},
};

let logger_rc = Rc::new(&logger);

let mut external_liquidity = ChannelLiquidity::new(Duration::ZERO);
external_liquidity.as_directed_mut(&source_node_id(), &target_node_id(), 1_000).successful(
1000,
Duration::ZERO,
format_args!("test channel"),
logger_rc.as_ref(),
);

let mut external_scores = ChannelLiquidities::new();
external_scores.insert(42, external_liquidity);

{
let network_graph = network_graph.read_only();
let channel = network_graph.channel(42).unwrap();
Expand All @@ -3971,16 +3992,7 @@ mod tests {

let penalty = combined_scorer.channel_penalty_msat(&candidate, usage, &params);

let mut external_liquidity = ChannelLiquidity::new(Duration::ZERO);
let logger_rc = Rc::new(&logger); // Why necessary and not above for the network graph?
external_liquidity
.as_directed_mut(&source_node_id(), &target_node_id(), 1_000)
.successful(1000, Duration::ZERO, format_args!("test channel"), logger_rc.as_ref());

let mut external_scores = ChannelLiquidities::new();

external_scores.insert(42, external_liquidity);
combined_scorer.merge(external_scores, Duration::ZERO);
combined_scorer.merge(external_scores.clone(), Duration::ZERO);

let penalty_after_merge =
combined_scorer.channel_penalty_msat(&candidate, usage, &params);
Expand All @@ -3993,6 +4005,13 @@ mod tests {
let liquidity_range =
combined_scorer.scorer.estimated_channel_liquidity_range(42, &target_node_id());
assert_eq!(liquidity_range.unwrap(), (0, 300));

// Now set (overwrite) the scorer state with the external data which should lead to an even greater liquidity
// range. Just the success from the external source is now considered.
combined_scorer.set(external_scores);
let liquidity_range =
combined_scorer.scorer.estimated_channel_liquidity_range(42, &target_node_id());
assert_eq!(liquidity_range.unwrap(), (0, 0));
}
}

Expand Down

0 comments on commit 3808c85

Please sign in to comment.