Skip to content

Min cost flow router impelementation for Pickhardt Payments #1668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link
Contributor

@tnull tnull Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't check in local files. In order to avoid this, you may want to add .vscode to your local gitignore file.

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
15 changes: 15 additions & 0 deletions lightning-invoice/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
//! # fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
//! # fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
//! # fn probe_successful(&mut self, _path: &[&RouteHop]) {}
//! # fn estimated_channel_liquidity_range(&self,scid:u64,target: &NodeId) -> Option<(u64,u64)> {
//! # None
//! # }
//! # }
//! #
//! # struct FakeLogger {}
Expand Down Expand Up @@ -250,6 +253,15 @@ impl<'a, S: Score> Score for AccountForInFlightHtlcs<'a, S> {
fn probe_successful(&mut self, path: &[&RouteHop]) {
self.scorer.probe_successful(path)
}

fn estimated_channel_liquidity_range(&self,scid:u64,source: &NodeId, target: &NodeId) -> Option<(u64,u64)> {
if let Some(used_liquidity) = self.inflight_htlcs.get(&(scid, source < target)) {
self.scorer.estimated_channel_liquidity_range(scid, source, target).map(
|(min, max)| (min.saturating_sub(*used_liquidity), max.saturating_sub(*used_liquidity)))
} else {
self.scorer.estimated_channel_liquidity_range(scid, source, target)
}
}
}

/// Storing minimal payment attempts information required for determining if a outbound payment can
Expand Down Expand Up @@ -1992,6 +2004,9 @@ mod tests {
}
}
}
fn estimated_channel_liquidity_range(&self,scid:u64,source: &NodeId, target: &NodeId) -> Option<(u64,u64)> {
None
}
}

impl Drop for TestScorer {
Expand Down
35 changes: 35 additions & 0 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,41 @@ where L::Target: Logger {
}
}


/// Pickhardt [`Router`] implemented using an alterhative [`find_route`] implementation that computes min cost flow.
pub struct MinCostFlowRouter<G: Deref<Target = NetworkGraph<L>>, L: Deref> where L::Target: Logger {
network_graph: G,
logger: L,
random_seed_bytes: Mutex<[u8; 32]>,
}

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> MinCostFlowRouter<G, L> where L::Target: Logger {
/// Creates a new router using the given [`NetworkGraph`], a [`Logger`], and a randomness source
/// `random_seed_bytes`.
pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32]) -> Self {
let random_seed_bytes = Mutex::new(random_seed_bytes);
Self { network_graph, logger, random_seed_bytes }
}
}

use lightning::routing::min_cost_flow_router;

impl<G: Deref<Target = NetworkGraph<L>>, L: Deref> Router for MinCostFlowRouter<G, L>
where L::Target: Logger {
fn find_route<S:Score>(
&self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash,
first_hops: Option<&[&ChannelDetails]>, scorer: &S
) -> Result<Route, LightningError> {
let random_seed_bytes = {
let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap();
*locked_random_seed_bytes = sha256::Hash::hash(&*locked_random_seed_bytes).into_inner();
*locked_random_seed_bytes
};
min_cost_flow_router::find_route(
payer, params, &self.network_graph, first_hops, &*self.logger, scorer, &random_seed_bytes)
}
}

impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Payer for ChannelManager<Signer, M, T, K, F, L>
where
M::Target: chain::Watch<Signer>,
Expand Down
Loading