From 8a51a792aa0967503c317531aef5ad336dc07d41 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Mon, 21 Nov 2022 17:07:44 -0500 Subject: [PATCH] Move DefaultRouter to router module --- lightning-background-processor/src/lib.rs | 2 +- lightning-invoice/src/utils.rs | 70 +---------------------- lightning/src/routing/router.rs | 65 ++++++++++++++++++++- 3 files changed, 68 insertions(+), 69 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index ece82bcaa..83f4aac13 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -580,13 +580,13 @@ mod tests { use lightning::ln::msgs::{ChannelMessageHandler, Init}; use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler}; use lightning::routing::gossip::{NetworkGraph, P2PGossipSync}; + use lightning::routing::router::DefaultRouter; use lightning::util::config::UserConfig; use lightning::util::events::{Event, MessageSendEventsProvider, MessageSendEvent}; use lightning::util::ser::Writeable; use lightning::util::test_utils; use lightning::util::persist::KVStorePersister; use lightning_invoice::payment::{InvoicePayer, Retry}; - use lightning_invoice::utils::DefaultRouter; use lightning_persister::FilesystemPersister; use std::fs; use std::path::PathBuf; diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 15ff9d6f4..b5ab740de 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -5,7 +5,7 @@ use crate::payment::Payer; use crate::{prelude::*, Description, InvoiceDescription, Sha256}; use bech32::ToBase32; -use bitcoin_hashes::{Hash, sha256}; +use bitcoin_hashes::Hash; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use lightning::chain::keysinterface::{Recipient, KeysInterface}; @@ -14,15 +14,12 @@ use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, P #[cfg(feature = "std")] use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; -use lightning::ln::msgs::LightningError; -use lightning::routing::gossip::{NetworkGraph, RoutingFees}; -use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router, ScorerAccountingForInFlightHtlcs}; -use lightning::routing::scoring::{LockableScore, Score}; +use lightning::routing::gossip::RoutingFees; +use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop}; use lightning::util::logger::Logger; use secp256k1::PublicKey; use core::ops::Deref; use core::time::Duration; -use crate::sync::Mutex; #[cfg(feature = "std")] /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice." @@ -524,67 +521,6 @@ fn filter_channels( .collect::>() } -/// A [`Router`] implemented using [`find_route`]. -pub struct DefaultRouter>, L: Deref, S: Deref> where - L::Target: Logger, - S::Target: for <'a> LockableScore<'a>, -{ - network_graph: G, - logger: L, - random_seed_bytes: Mutex<[u8; 32]>, - scorer: S -} - -impl>, L: Deref, S: Deref> DefaultRouter where - L::Target: Logger, - S::Target: for <'a> LockableScore<'a>, -{ - /// 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], scorer: S) -> Self { - let random_seed_bytes = Mutex::new(random_seed_bytes); - Self { network_graph, logger, random_seed_bytes, scorer } - } -} - -impl>, L: Deref, S: Deref> Router for DefaultRouter where - L::Target: Logger, - S::Target: for <'a> LockableScore<'a>, -{ - fn find_route( - &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, - inflight_htlcs: InFlightHtlcs - ) -> Result { - 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 - }; - - find_route( - payer, params, &self.network_graph, first_hops, &*self.logger, - &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs), - &random_seed_bytes - ) - } - - fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { - self.scorer.lock().payment_path_failed(path, short_channel_id); - } - - fn notify_payment_path_successful(&self, path: &[&RouteHop]) { - self.scorer.lock().payment_path_successful(path); - } - - fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { - self.scorer.lock().probe_successful(path); - } - - fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) { - self.scorer.lock().probe_failed(path, short_channel_id); - } -} - impl Payer for ChannelManager where M::Target: chain::Watch<::Signer>, diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index fa841d892..570bef7f8 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -13,23 +13,86 @@ //! interrogate it to get routes for your own payments. use bitcoin::secp256k1::PublicKey; +use bitcoin::hashes::Hash; +use bitcoin::hashes::sha256::Hash as Sha256; use crate::ln::PaymentHash; use crate::ln::channelmanager::{ChannelDetails, PaymentId}; use crate::ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures}; use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT}; use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees}; -use crate::routing::scoring::{ChannelUsage, Score}; +use crate::routing::scoring::{ChannelUsage, LockableScore, Score}; use crate::util::ser::{Writeable, Readable, Writer}; use crate::util::logger::{Level, Logger}; use crate::util::chacha20::ChaCha20; use crate::io; use crate::prelude::*; +use crate::sync::Mutex; use alloc::collections::BinaryHeap; use core::cmp; use core::ops::Deref; +/// A [`Router`] implemented using [`find_route`]. +pub struct DefaultRouter>, L: Deref, S: Deref> where + L::Target: Logger, + S::Target: for <'a> LockableScore<'a>, +{ + network_graph: G, + logger: L, + random_seed_bytes: Mutex<[u8; 32]>, + scorer: S +} + +impl>, L: Deref, S: Deref> DefaultRouter where + L::Target: Logger, + S::Target: for <'a> LockableScore<'a>, +{ + /// Creates a new router. + pub fn new(network_graph: G, logger: L, random_seed_bytes: [u8; 32], scorer: S) -> Self { + let random_seed_bytes = Mutex::new(random_seed_bytes); + Self { network_graph, logger, random_seed_bytes, scorer } + } +} + +impl>, L: Deref, S: Deref> Router for DefaultRouter where + L::Target: Logger, + S::Target: for <'a> LockableScore<'a>, +{ + fn find_route( + &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, + inflight_htlcs: InFlightHtlcs + ) -> Result { + let random_seed_bytes = { + let mut locked_random_seed_bytes = self.random_seed_bytes.lock().unwrap(); + *locked_random_seed_bytes = Sha256::hash(&*locked_random_seed_bytes).into_inner(); + *locked_random_seed_bytes + }; + + find_route( + payer, params, &self.network_graph, first_hops, &*self.logger, + &ScorerAccountingForInFlightHtlcs::new(&mut self.scorer.lock(), inflight_htlcs), + &random_seed_bytes + ) + } + + fn notify_payment_path_failed(&self, path: &[&RouteHop], short_channel_id: u64) { + self.scorer.lock().payment_path_failed(path, short_channel_id); + } + + fn notify_payment_path_successful(&self, path: &[&RouteHop]) { + self.scorer.lock().payment_path_successful(path); + } + + fn notify_payment_probe_successful(&self, path: &[&RouteHop]) { + self.scorer.lock().probe_successful(path); + } + + fn notify_payment_probe_failed(&self, path: &[&RouteHop], short_channel_id: u64) { + self.scorer.lock().probe_failed(path, short_channel_id); + } +} + /// A trait defining behavior for routing a payment. pub trait Router { /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. -- 2.39.5