X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=b5ab740de5fc507844a1cd6d6f580de5bb3a487d;hb=8a51a792aa0967503c317531aef5ad336dc07d41;hp=5d54e376bed98be05ba12b237820be2b9ef59bc0;hpb=89f162c168bc934f64a663f31ea12793568c55b1;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 5d54e376..b5ab740d 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,11 +1,11 @@ //! Convenient utilities to create an invoice. use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError}; -use crate::payment::{Payer, ScoringRouter}; +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, NodeId, RoutingFees}; -use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router}; -use lightning::routing::scoring::{ChannelUsage, 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,72 +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 - ) - } -} - -impl>, L: Deref, S: Deref> ScoringRouter for DefaultRouter where - L::Target: Logger, - S::Target: for <'a> LockableScore<'a>, -{ - 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>, @@ -632,54 +563,6 @@ where fn inflight_htlcs(&self) -> InFlightHtlcs { self.compute_inflight_htlcs() } } - -/// Used to store information about all the HTLCs that are inflight across all payment attempts. -pub(crate) struct ScorerAccountingForInFlightHtlcs<'a, S: Score> { - scorer: &'a mut S, - /// Maps a channel's short channel id and its direction to the liquidity used up. - inflight_htlcs: InFlightHtlcs, -} - -impl<'a, S: Score> ScorerAccountingForInFlightHtlcs<'a, S> { - pub(crate) fn new(scorer: &'a mut S, inflight_htlcs: InFlightHtlcs) -> Self { - ScorerAccountingForInFlightHtlcs { - scorer, - inflight_htlcs - } - } -} - -#[cfg(c_bindings)] -impl<'a, S:Score> lightning::util::ser::Writeable for ScorerAccountingForInFlightHtlcs<'a, S> { - fn write(&self, writer: &mut W) -> Result<(), lightning::io::Error> { self.scorer.write(writer) } -} - -impl<'a, S: Score> Score for ScorerAccountingForInFlightHtlcs<'a, S> { - fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 { - if let Some(used_liqudity) = self.inflight_htlcs.used_liquidity_msat( - source, target, short_channel_id - ) { - let usage = ChannelUsage { - inflight_htlc_msat: usage.inflight_htlc_msat + used_liqudity, - ..usage - }; - - self.scorer.channel_penalty_msat(short_channel_id, source, target, usage) - } else { - self.scorer.channel_penalty_msat(short_channel_id, source, target, usage) - } - } - - fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() } - - fn payment_path_successful(&mut self, _path: &[&RouteHop]) { unreachable!() } - - fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) { unreachable!() } - - fn probe_successful(&mut self, _path: &[&RouteHop]) { unreachable!() } -} - - #[cfg(test)] mod test { use core::time::Duration;