X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=65bf4fb80c08ec2ae257ca6c89769ed82873a218;hb=605d30eb034a879db4d5719730c88074b9347026;hp=5b16da45993ac221088bc1b8fd170a2716f360bd;hpb=a257906743d528c32c862b053b652d4b728aa990;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 5b16da45..65bf4fb8 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,7 +1,7 @@ //! Convenient utilities to create an invoice. use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError}; -use crate::payment::{InFlightHtlcs, Payer, Router}; +use crate::payment::{Payer, ScoringRouter}; use crate::{prelude::*, Description, InvoiceDescription, Sha256}; use bech32::ToBase32; @@ -16,7 +16,7 @@ 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::{Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop}; +use lightning::routing::router::{InFlightHtlcs, Route, RouteHint, RouteHintHop, RouteParameters, find_route, RouteHop, Router}; use lightning::routing::scoring::{ChannelUsage, LockableScore, Score}; use lightning::util::logger::Logger; use secp256k1::PublicKey; @@ -235,14 +235,14 @@ where /// /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for /// in excess of the current time. -pub fn create_invoice_from_channelmanager( - channelmanager: &ChannelManager, keys_manager: K, logger: L, +pub fn create_invoice_from_channelmanager( + channelmanager: &ChannelManager, keys_manager: K, logger: L, network: Currency, amt_msat: Option, description: String, invoice_expiry_delta_secs: u32 ) -> Result> where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -265,15 +265,15 @@ where /// /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for /// in excess of the current time. -pub fn create_invoice_from_channelmanager_with_description_hash( - channelmanager: &ChannelManager, keys_manager: K, logger: L, +pub fn create_invoice_from_channelmanager_with_description_hash( + channelmanager: &ChannelManager, keys_manager: K, logger: L, network: Currency, amt_msat: Option, description_hash: Sha256, invoice_expiry_delta_secs: u32 ) -> Result> where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -292,15 +292,15 @@ where /// See [`create_invoice_from_channelmanager_with_description_hash`] /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not /// available and the current time is supplied by the caller. -pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( - channelmanager: &ChannelManager, keys_manager: K, logger: L, +pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch( + channelmanager: &ChannelManager, keys_manager: K, logger: L, network: Currency, amt_msat: Option, description_hash: Sha256, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32 ) -> Result> where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -314,15 +314,15 @@ where /// See [`create_invoice_from_channelmanager`] /// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not /// available and the current time is supplied by the caller. -pub fn create_invoice_from_channelmanager_and_duration_since_epoch( - channelmanager: &ChannelManager, keys_manager: K, logger: L, +pub fn create_invoice_from_channelmanager_and_duration_since_epoch( + channelmanager: &ChannelManager, keys_manager: K, logger: L, network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32 ) -> Result> where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -335,15 +335,15 @@ where ) } -fn _create_invoice_from_channelmanager_and_duration_since_epoch( - channelmanager: &ChannelManager, keys_manager: K, logger: L, +fn _create_invoice_from_channelmanager_and_duration_since_epoch( + channelmanager: &ChannelManager, keys_manager: K, logger: L, network: Currency, amt_msat: Option, description: InvoiceDescription, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32 ) -> Result> where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -552,8 +552,8 @@ impl>, L: Deref, S: Deref> Router for DefaultR S::Target: for <'a> LockableScore<'a>, { fn find_route( - &self, payer: &PublicKey, params: &RouteParameters, _payment_hash: &PaymentHash, - first_hops: Option<&[&ChannelDetails]>, inflight_htlcs: InFlightHtlcs + &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(); @@ -567,7 +567,12 @@ impl>, L: Deref, S: Deref> Router for DefaultR &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); } @@ -585,11 +590,11 @@ impl>, L: Deref, S: Deref> Router for DefaultR } } -impl Payer for ChannelManager +impl Payer for ChannelManager where - M::Target: chain::Watch, + M::Target: chain::Watch<::Signer>, T::Target: BroadcasterInterface, - K::Target: KeysInterface, + K::Target: KeysInterface, F::Target: FeeEstimator, L::Target: Logger, { @@ -602,16 +607,16 @@ where } fn send_payment( - &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option - ) -> Result { - self.send_payment(route, payment_hash, payment_secret) + &self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option, + payment_id: PaymentId + ) -> Result<(), PaymentSendFailure> { + self.send_payment(route, payment_hash, payment_secret, payment_id) } fn send_spontaneous_payment( - &self, route: &Route, payment_preimage: PaymentPreimage, - ) -> Result { - self.send_spontaneous_payment(route, Some(payment_preimage)) - .map(|(_, payment_id)| payment_id) + &self, route: &Route, payment_preimage: PaymentPreimage, payment_id: PaymentId, + ) -> Result<(), PaymentSendFailure> { + self.send_spontaneous_payment(route, Some(payment_preimage), payment_id).map(|_| ()) } fn retry_payment( @@ -681,7 +686,7 @@ mod test { use bitcoin_hashes::sha256::Hash as Sha256; use lightning::chain::keysinterface::PhantomKeysManager; use lightning::ln::{PaymentPreimage, PaymentHash}; - use lightning::ln::channelmanager::{self, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY}; + use lightning::ln::channelmanager::{self, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY, PaymentId}; use lightning::ln::functional_test_utils::*; use lightning::ln::msgs::ChannelMessageHandler; use lightning::routing::router::{PaymentParameters, RouteParameters, find_route}; @@ -741,7 +746,7 @@ mod test { let payment_event = { let mut payment_hash = PaymentHash([0; 32]); payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); - nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap(); + nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap(); let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap(); assert_eq!(added_monitors.len(), 1); added_monitors.clear(); @@ -869,6 +874,8 @@ mod test { get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); nodes[0].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); + expect_channel_ready_event(&nodes[0], &nodes[2].node.get_our_node_id()); + expect_channel_ready_event(&nodes[2], &nodes[0].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the second // channel, the channel will never be assigned any `counterparty.forwarding_info`. @@ -1046,7 +1053,7 @@ mod test { let (payment_event, fwd_idx) = { let mut payment_hash = PaymentHash([0; 32]); payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); - nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone())).unwrap(); + nodes[0].node.send_payment(&route, payment_hash, &Some(invoice.payment_secret().clone()), PaymentId(payment_hash.0)).unwrap(); let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap(); assert_eq!(added_monitors.len(), 1); added_monitors.clear(); @@ -1257,6 +1264,8 @@ mod test { get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[3].node.get_our_node_id()); nodes[3].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready); get_event_msg!(nodes[3], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); + expect_channel_ready_event(&nodes[1], &nodes[3].node.get_our_node_id()); + expect_channel_ready_event(&nodes[3], &nodes[1].node.get_our_node_id()); // As `msgs::ChannelUpdate` was never handled for the participating node(s) of the third // channel, the channel will never be assigned any `counterparty.forwarding_info`.