From: Jeffrey Czyz Date: Tue, 24 Aug 2021 05:14:10 +0000 (-0500) Subject: Implement Payer and Router for lightning crate X-Git-Tag: v0.0.103~8^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=010436d07d79672c96f5fecc8e948ba66303b735;p=rust-lightning Implement Payer and Router for lightning crate Implements Payer for ChannelManager and Rotuer for find_route, which can be used to parameterize InvoicePayer when needing payment retries. --- diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 02bb31dfa..ef885f203 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,14 +1,21 @@ //! Convenient utilities to create an invoice. + use {Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice}; +use payment::{Payer, Router}; + use bech32::ToBase32; use bitcoin_hashes::Hash; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use lightning::chain::keysinterface::{Sign, KeysInterface}; -use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY}; -use lightning::routing::network_graph::RoutingFees; -use lightning::routing::router::{RouteHint, RouteHintHop}; +use lightning::ln::{PaymentHash, PaymentSecret}; +use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY}; +use lightning::ln::msgs::LightningError; +use lightning::routing::network_graph::{NetworkGraph, RoutingFees}; +use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route}; +use lightning::routing::scorer::Scorer; use lightning::util::logger::Logger; +use secp256k1::key::PublicKey; use std::convert::TryInto; use std::ops::Deref; @@ -89,6 +96,58 @@ where } } +/// A [`Router`] implemented using [`find_route`]. +pub struct DefaultRouter where G: Deref, L::Target: Logger { + network_graph: G, + logger: L, +} + +impl DefaultRouter where G: Deref, L::Target: Logger { + /// Creates a new router using the given [`NetworkGraph`] and [`Logger`]. + pub fn new(network_graph: G, logger: L) -> Self { + Self { network_graph, logger } + } +} + +impl Router for DefaultRouter +where G: Deref, L::Target: Logger { + fn find_route( + &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&ChannelDetails]>, + ) -> Result { + let scorer = Scorer::default(); + find_route(payer, params, &*self.network_graph, first_hops, &*self.logger, &scorer) + } +} + +impl Payer for ChannelManager +where + M::Target: chain::Watch, + T::Target: BroadcasterInterface, + K::Target: KeysInterface, + F::Target: FeeEstimator, + L::Target: Logger, +{ + fn node_id(&self) -> PublicKey { + self.get_our_node_id() + } + + fn first_hops(&self) -> Vec { + self.list_usable_channels() + } + + fn send_payment( + &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option + ) -> Result { + self.send_payment(route, payment_hash, payment_secret) + } + + fn retry_payment( + &self, route: &Route, payment_id: PaymentId + ) -> Result<(), PaymentSendFailure> { + self.retry_payment(route, payment_id) + } +} + #[cfg(test)] mod test { use {Currency, Description, InvoiceDescription};