X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=b6442934da78898d56594a2f90951f47c9d35b50;hb=087c0bdd8716d8fedde84cd793bcbd805a0f6c0e;hp=81b584aa2461092eeaf852eeeaeb22f42e9fff0c;hpb=d15b7cb86efb4d7e5cf0337b7b2fd1d68081a8d1;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 81b584aa..b6442934 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,14 +1,14 @@ //! 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; use bitcoin_hashes::{Hash, sha256}; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; -use lightning::chain::keysinterface::{Recipient, KeysInterface, Sign}; +use lightning::chain::keysinterface::{Recipient, KeysInterface}; use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, PaymentId, PaymentSendFailure, MIN_FINAL_CLTV_EXPIRY}; #[cfg(feature = "std")] @@ -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; @@ -54,7 +54,7 @@ use crate::sync::Mutex; /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels -pub fn create_phantom_invoice( +pub fn create_phantom_invoice( amt_msat: Option, payment_hash: Option, description: String, invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, keys_manager: K, logger: L, network: Currency, @@ -65,7 +65,7 @@ where { let description = Description::new(description).map_err(SignOrCreationError::CreationError)?; let description = InvoiceDescription::Direct(&description,); - _create_phantom_invoice::( + _create_phantom_invoice::( amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints, keys_manager, logger, network, ) @@ -103,7 +103,7 @@ where /// [`ChannelManager::create_inbound_payment`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment /// [`ChannelManager::create_inbound_payment_for_hash`]: lightning::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash /// [`PhantomRouteHints::channels`]: lightning::ln::channelmanager::PhantomRouteHints::channels -pub fn create_phantom_invoice_with_description_hash( +pub fn create_phantom_invoice_with_description_hash( amt_msat: Option, payment_hash: Option, invoice_expiry_delta_secs: u32, description_hash: Sha256, phantom_route_hints: Vec, keys_manager: K, logger: L, network: Currency @@ -112,14 +112,14 @@ where K::Target: KeysInterface, L::Target: Logger, { - _create_phantom_invoice::( + _create_phantom_invoice::( amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash), invoice_expiry_delta_secs, phantom_route_hints, keys_manager, logger, network, ) } #[cfg(feature = "std")] -fn _create_phantom_invoice( +fn _create_phantom_invoice( amt_msat: Option, payment_hash: Option, description: InvoiceDescription, invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, keys_manager: K, logger: L, network: Currency, @@ -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); } @@ -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,11 +686,10 @@ 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}; - use lightning::util::enforcing_trait_impls::EnforcingSigner; use lightning::util::events::{MessageSendEvent, MessageSendEventsProvider, Event}; use lightning::util::test_utils; use lightning::util::config::UserConfig; @@ -741,7 +745,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(); @@ -1011,7 +1015,7 @@ mod test { let non_default_invoice_expiry_secs = 4200; let invoice = - crate::utils::create_phantom_invoice::( + crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>( Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet ).unwrap(); @@ -1048,7 +1052,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(); @@ -1120,7 +1124,7 @@ mod test { nodes[2].node.get_phantom_route_hints(), ]; - let invoice = crate::utils::create_phantom_invoice::(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet).unwrap(); + let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), Some(payment_hash), "test".to_string(), 3600, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet).unwrap(); let chan_0_1 = &nodes[1].node.list_usable_channels()[0]; assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat); @@ -1148,7 +1152,7 @@ mod test { let description_hash = crate::Sha256(Hash::hash("Description hash phantom invoice".as_bytes())); let non_default_invoice_expiry_secs = 4200; let invoice = crate::utils::create_phantom_invoice_with_description_hash::< - EnforcingSigner, &test_utils::TestKeysInterface, &test_utils::TestLogger, + &test_utils::TestKeysInterface, &test_utils::TestLogger, >( Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash, route_hints, &nodes[1].keys_manager, &nodes[1].logger, Currency::BitcoinTestnet @@ -1465,7 +1469,7 @@ mod test { .map(|route_hint| route_hint.phantom_scid) .collect::>(); - let invoice = crate::utils::create_phantom_invoice::(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, &invoice_node.logger, Currency::BitcoinTestnet).unwrap(); + let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestLogger>(invoice_amt, None, "test".to_string(), 3600, phantom_route_hints, &invoice_node.keys_manager, &invoice_node.logger, Currency::BitcoinTestnet).unwrap(); let invoice_hints = invoice.private_routes();