X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-invoice%2Fsrc%2Futils.rs;h=1d6b9210afd967f58c79e7453f53a3eda856a9ee;hb=ae4c35c3e05d918607104dce74e4a54060712023;hp=c052706975968105c95b7dbf08c4ec7132b6c6c5;hpb=63c77a960b3e35a3c28a874b8d321e38831543a0;p=rust-lightning diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index c0527069..1d6b9210 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -1,10 +1,10 @@ //! Convenient utilities to create an invoice. -use crate::{CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError}; +use crate::{Bolt11Invoice, CreationError, Currency, InvoiceBuilder, SignOrCreationError}; -use crate::{prelude::*, Description, InvoiceDescription, Sha256}; +use crate::{prelude::*, Description, Bolt11InvoiceDescription, Sha256}; use bech32::ToBase32; -use bitcoin_hashes::Hash; +use bitcoin::hashes::Hash; use lightning::chain; use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use lightning::sign::{Recipient, NodeSigner, SignerProvider, EntropySource}; @@ -14,10 +14,12 @@ use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA}; use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey}; use lightning::routing::gossip::RoutingFees; use lightning::routing::router::{RouteHint, RouteHintHop, Router}; -use lightning::util::logger::Logger; +use lightning::util::logger::{Logger, Record}; use secp256k1::PublicKey; +use alloc::collections::{btree_map, BTreeMap}; use core::ops::Deref; use core::time::Duration; +#[cfg(not(feature = "std"))] use core::iter::Iterator; /// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice." @@ -64,14 +66,14 @@ pub fn create_phantom_invoice( amt_msat: Option, payment_hash: Option, description: String, invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, entropy_source: ES, node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option, duration_since_epoch: Duration, -) -> Result> +) -> Result> where ES::Target: EntropySource, NS::Target: NodeSigner, L::Target: Logger, { let description = Description::new(description).map_err(SignOrCreationError::CreationError)?; - let description = InvoiceDescription::Direct(&description,); + let description = Bolt11InvoiceDescription::Direct(&description,); _create_phantom_invoice::( amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints, entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta, duration_since_epoch, @@ -120,14 +122,14 @@ pub fn create_phantom_invoice_with_description_hash, payment_hash: Option, invoice_expiry_delta_secs: u32, description_hash: Sha256, phantom_route_hints: Vec, entropy_source: ES, node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option, duration_since_epoch: Duration, -) -> Result> +) -> Result> where ES::Target: EntropySource, NS::Target: NodeSigner, L::Target: Logger, { _create_phantom_invoice::( - amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash), + amt_msat, payment_hash, Bolt11InvoiceDescription::Hash(&description_hash), invoice_expiry_delta_secs, phantom_route_hints, entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta, duration_since_epoch, ) @@ -136,10 +138,10 @@ where const MAX_CHANNEL_HINTS: usize = 3; fn _create_phantom_invoice( - amt_msat: Option, payment_hash: Option, description: InvoiceDescription, + amt_msat: Option, payment_hash: Option, description: Bolt11InvoiceDescription, invoice_expiry_delta_secs: u32, phantom_route_hints: Vec, entropy_source: ES, node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option, duration_since_epoch: Duration, -) -> Result> +) -> Result> where ES::Target: EntropySource, NS::Target: NodeSigner, @@ -157,10 +159,10 @@ where } let invoice = match description { - InvoiceDescription::Direct(description) => { - InvoiceBuilder::new(network).description(description.0.clone()) + Bolt11InvoiceDescription::Direct(description) => { + InvoiceBuilder::new(network).description(description.0.0.clone()) } - InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), + Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), }; // If we ever see performance here being too slow then we should probably take this ExpandedKey as a parameter instead. @@ -191,7 +193,7 @@ where }; log_trace!(logger, "Creating phantom invoice from {} participating nodes with payment hash {}", - phantom_route_hints.len(), log_bytes!(payment_hash.0)); + phantom_route_hints.len(), &payment_hash); let mut invoice = invoice .duration_since_epoch(duration_since_epoch) @@ -219,7 +221,7 @@ where let data_without_signature = raw_invoice.data.to_base32(); let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode)); match signed_raw_invoice { - Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()), + Ok(inv) => Ok(Bolt11Invoice::from_signed(inv).unwrap()), Err(e) => Err(SignOrCreationError::SignError(e)) } } @@ -333,9 +335,9 @@ pub fn create_invoice_from_channelmanager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description: String, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -374,9 +376,9 @@ pub fn create_invoice_from_channelmanager_with_description_hash, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description_hash: Sha256, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -404,9 +406,9 @@ pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_sin channelmanager: &ChannelManager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description_hash: Sha256, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -417,7 +419,7 @@ pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_sin { _create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager, node_signer, logger, network, amt_msat, - InvoiceDescription::Hash(&description_hash), + Bolt11InvoiceDescription::Hash(&description_hash), duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta, ) } @@ -429,9 +431,9 @@ pub fn create_invoice_from_channelmanager_and_duration_since_epoch, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -442,7 +444,7 @@ pub fn create_invoice_from_channelmanager_and_duration_since_epoch( channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description: InvoiceDescription, + network: Currency, amt_msat: Option, description: Bolt11InvoiceDescription, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -486,9 +488,9 @@ pub fn create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_ channelmanager: &ChannelManager, node_signer: NS, logger: L, network: Currency, amt_msat: Option, description: String, duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option, -) -> Result> +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -503,7 +505,7 @@ pub fn create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_ .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?; _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( channelmanager, node_signer, logger, network, amt_msat, - InvoiceDescription::Direct( + Bolt11InvoiceDescription::Direct( &Description::new(description).map_err(SignOrCreationError::CreationError)?, ), duration_since_epoch, invoice_expiry_delta_secs, payment_hash, payment_secret, @@ -513,12 +515,12 @@ pub fn create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash( channelmanager: &ChannelManager, node_signer: NS, logger: L, - network: Currency, amt_msat: Option, description: InvoiceDescription, duration_since_epoch: Duration, - invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, payment_secret: PaymentSecret, - min_final_cltv_expiry_delta: Option, -) -> Result> + network: Currency, amt_msat: Option, description: Bolt11InvoiceDescription, + duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, + payment_secret: PaymentSecret, min_final_cltv_expiry_delta: Option, +) -> Result> where - M::Target: chain::Watch<::Signer>, + M::Target: chain::Watch<::EcdsaSigner>, T::Target: BroadcasterInterface, ES::Target: EntropySource, NS::Target: NodeSigner, @@ -534,13 +536,13 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has return Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort)); } - log_trace!(logger, "Creating invoice with payment hash {}", log_bytes!(payment_hash.0)); + log_trace!(logger, "Creating invoice with payment hash {}", &payment_hash); let invoice = match description { - InvoiceDescription::Direct(description) => { - InvoiceBuilder::new(network).description(description.0.clone()) + Bolt11InvoiceDescription::Direct(description) => { + InvoiceBuilder::new(network).description(description.0.0.clone()) } - InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), + Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0), }; let mut invoice = invoice @@ -571,7 +573,7 @@ fn _create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_has let data_without_signature = raw_invoice.data.to_base32(); let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::Node)); match signed_raw_invoice { - Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()), + Ok(inv) => Ok(Bolt11Invoice::from_signed(inv).unwrap()), Err(e) => Err(SignOrCreationError::SignError(e)) } } @@ -603,7 +605,7 @@ fn sort_and_filter_channels( where L::Target: Logger, { - let mut filtered_channels: HashMap = HashMap::new(); + let mut filtered_channels: BTreeMap = BTreeMap::new(); let min_inbound_capacity = min_inbound_capacity_msat.unwrap_or(0); let mut min_capacity_channel_exists = false; let mut online_channel_exists = false; @@ -626,8 +628,9 @@ where log_trace!(logger, "Considering {} channels for invoice route hints", channels.len()); for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) { + let logger = WithChannelDetails::from(logger, &channel); if channel.get_inbound_payment_scid().is_none() || channel.counterparty.forwarding_info.is_none() { - log_trace!(logger, "Ignoring channel {} for invoice route hints", log_bytes!(channel.channel_id)); + log_trace!(logger, "Ignoring channel {} for invoice route hints", &channel.channel_id); continue; } @@ -641,7 +644,7 @@ where // If any public channel exists, return no hints and let the sender // look at the public channels instead. log_trace!(logger, "Not including channels in invoice route hints on account of public channel {}", - log_bytes!(channel.channel_id)); + &channel.channel_id); return vec![].into_iter().take(MAX_CHANNEL_HINTS).map(route_hint_from_channel); } } @@ -663,7 +666,7 @@ where } match filtered_channels.entry(channel.counterparty.node_id) { - hash_map::Entry::Occupied(mut entry) => { + btree_map::Entry::Occupied(mut entry) => { let current_max_capacity = entry.get().inbound_capacity_msat; // If this channel is public and the previous channel is not, ensure we replace the // previous channel to avoid announcing non-public channels. @@ -681,22 +684,22 @@ where log_trace!(logger, "Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints", log_pubkey!(channel.counterparty.node_id), - log_bytes!(channel.channel_id), channel.short_channel_id, + &channel.channel_id, channel.short_channel_id, channel.inbound_capacity_msat, - log_bytes!(entry.get().channel_id), entry.get().short_channel_id, + &entry.get().channel_id, entry.get().short_channel_id, current_max_capacity); entry.insert(channel); } else { log_trace!(logger, "Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints", log_pubkey!(channel.counterparty.node_id), - log_bytes!(entry.get().channel_id), entry.get().short_channel_id, + &entry.get().channel_id, entry.get().short_channel_id, current_max_capacity, - log_bytes!(channel.channel_id), channel.short_channel_id, + &channel.channel_id, channel.short_channel_id, channel.inbound_capacity_msat); } } - hash_map::Entry::Vacant(entry) => { + btree_map::Entry::Vacant(entry) => { entry.insert(channel); } } @@ -710,6 +713,7 @@ where .into_iter() .map(|(_, channel)| channel) .filter(|channel| { + let logger = WithChannelDetails::from(logger, &channel); let has_enough_capacity = channel.inbound_capacity_msat >= min_inbound_capacity; let include_channel = if has_pub_unconf_chan { // If we have a public channel, but it doesn't have enough confirmations to (yet) @@ -731,14 +735,14 @@ where if include_channel { log_trace!(logger, "Including channel {} in invoice route hints", - log_bytes!(channel.channel_id)); + &channel.channel_id); } else if !has_enough_capacity { log_trace!(logger, "Ignoring channel {} without enough capacity for invoice route hints", - log_bytes!(channel.channel_id)); + &channel.channel_id); } else { debug_assert!(!channel.is_usable || (has_pub_unconf_chan && !channel.is_public)); log_trace!(logger, "Ignoring channel {} with disconnected peer", - log_bytes!(channel.channel_id)); + &channel.channel_id); } include_channel @@ -790,15 +794,39 @@ fn prefer_current_channel(min_inbound_capacity_msat: Option, current_channe current_channel > candidate_channel } +/// Adds relevant context to a [`Record`] before passing it to the wrapped [`Logger`]. +struct WithChannelDetails<'a, 'b, L: Deref> where L::Target: Logger { + /// The logger to delegate to after adding context to the record. + logger: &'a L, + /// The [`ChannelDetails`] for adding relevant context to the logged record. + details: &'b ChannelDetails +} + +impl<'a, 'b, L: Deref> Logger for WithChannelDetails<'a, 'b, L> where L::Target: Logger { + fn log(&self, mut record: Record) { + record.peer_id = Some(self.details.counterparty.node_id); + record.channel_id = Some(self.details.channel_id); + self.logger.log(record) + } +} + +impl<'a, 'b, L: Deref> WithChannelDetails<'a, 'b, L> where L::Target: Logger { + fn from(logger: &'a L, details: &'b ChannelDetails) -> Self { + Self { logger, details } + } +} + #[cfg(test)] mod test { use core::time::Duration; - use crate::{Currency, Description, InvoiceDescription, SignOrCreationError, CreationError}; - use bitcoin_hashes::{Hash, sha256}; - use bitcoin_hashes::sha256::Hash as Sha256; + use crate::{Currency, Description, Bolt11InvoiceDescription, SignOrCreationError, CreationError}; + use bitcoin::hashes::{Hash, sha256}; + use bitcoin::hashes::sha256::Hash as Sha256; use lightning::sign::PhantomKeysManager; - use lightning::events::{MessageSendEvent, MessageSendEventsProvider, Event}; - use lightning::ln::{PaymentPreimage, PaymentHash}; + use lightning::events::{MessageSendEvent, MessageSendEventsProvider}; + use lightning::ln::PaymentHash; + #[cfg(feature = "std")] + use lightning::ln::PaymentPreimage; use lightning::ln::channelmanager::{PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry}; use lightning::ln::functional_test_utils::*; use lightning::ln::msgs::ChannelMessageHandler; @@ -807,6 +835,7 @@ mod test { use lightning::util::config::UserConfig; use crate::utils::{create_invoice_from_channelmanager_and_duration_since_epoch, rotate_through_iterators}; use std::collections::HashSet; + use lightning::util::string::UntrustedString; #[test] fn test_prefer_current_channel() { @@ -851,7 +880,7 @@ mod test { assert_eq!(invoice.amount_pico_btc(), Some(100_000)); // If no `min_final_cltv_expiry_delta` is specified, then it should be `MIN_FINAL_CLTV_EXPIRY_DELTA`. assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string()))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); // Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is @@ -868,13 +897,10 @@ mod test { invoice.min_final_cltv_expiry_delta() as u32) .with_bolt11_features(invoice.features().unwrap().clone()).unwrap() .with_route_hints(invoice.route_hints()).unwrap(); - let route_params = RouteParameters { - payment_params, - final_value_msat: invoice.amount_milli_satoshis().unwrap(), - }; + let route_params = RouteParameters::from_payment_params_and_value( + payment_params, invoice.amount_milli_satoshis().unwrap()); let payment_event = { - let mut payment_hash = PaymentHash([0; 32]); - payment_hash.0.copy_from_slice(&invoice.payment_hash().as_ref()[0..32]); + let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array()); nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(*invoice.payment_secret()), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap(); @@ -947,7 +973,7 @@ mod test { ).unwrap(); assert_eq!(invoice.amount_pico_btc(), Some(100_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Testing description_hash".as_bytes())))); } #[test] @@ -964,7 +990,7 @@ mod test { ).unwrap(); assert_eq!(invoice.amount_pico_btc(), Some(100_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string()))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&payment_hash.0[..]).unwrap()); } @@ -1142,7 +1168,7 @@ mod test { // is never handled, the `channel.counterparty.forwarding_info` is never assigned. let mut private_chan_cfg = UserConfig::default(); private_chan_cfg.channel_handshake_config.announced_channel = false; - let temporary_channel_id = nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap(); + let temporary_channel_id = nodes[2].node.create_channel(nodes[0].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None, Some(private_chan_cfg)).unwrap(); let open_channel = get_event_msg!(nodes[2], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id()); nodes[0].node.handle_open_channel(&nodes[2].node.get_our_node_id(), &open_channel); let accept_channel = get_event_msg!(nodes[0], MessageSendEvent::SendAcceptChannel, nodes[2].node.get_our_node_id()); @@ -1271,6 +1297,9 @@ mod test { #[cfg(feature = "std")] fn do_test_multi_node_receive(user_generated_pmt_hash: bool) { + use lightning::events::{Event, EventsProvider}; + use core::cell::RefCell; + let mut chanmon_cfgs = create_chanmon_cfgs(3); let seed_1 = [42u8; 32]; let seed_2 = [43u8; 32]; @@ -1295,7 +1324,7 @@ mod test { let user_payment_preimage = PaymentPreimage([1; 32]); let payment_hash = if user_generated_pmt_hash { - Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner())) + Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).to_byte_array())) } else { None }; @@ -1308,7 +1337,7 @@ mod test { route_hints, nodes[1].keys_manager, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet, None, Duration::from_secs(genesis_timestamp) ).unwrap(); - let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret()); + let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().to_byte_array()), *invoice.payment_secret()); let payment_preimage = if user_generated_pmt_hash { user_payment_preimage } else { @@ -1316,7 +1345,7 @@ mod test { }; assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); - assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string()))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Direct(&Description(UntrustedString("test".to_string())))); assert_eq!(invoice.route_hints().len(), 2); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); assert!(!invoice.features().unwrap().supports_basic_mpp()); @@ -1325,13 +1354,10 @@ mod test { invoice.min_final_cltv_expiry_delta() as u32) .with_bolt11_features(invoice.features().unwrap().clone()).unwrap() .with_route_hints(invoice.route_hints()).unwrap(); - let params = RouteParameters { - payment_params, - final_value_msat: invoice.amount_milli_satoshis().unwrap(), - }; + let params = RouteParameters::from_payment_params_and_value( + payment_params, invoice.amount_milli_satoshis().unwrap()); 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]); + let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array()); nodes[0].node.send_payment(payment_hash, RecipientOnionFields::secret_only(*invoice.payment_secret()), PaymentId(payment_hash.0), params, Retry::Attempts(0)).unwrap(); @@ -1357,30 +1383,22 @@ mod test { // Note that we have to "forward pending HTLCs" twice before we see the PaymentClaimable as // this "emulates" the payment taking two hops, providing some privacy to make phantom node // payments "look real" by taking more time. - expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]); - nodes[fwd_idx].node.process_pending_htlc_forwards(); - expect_pending_htlcs_forwardable_ignore!(nodes[fwd_idx]); - nodes[fwd_idx].node.process_pending_htlc_forwards(); + let other_events = RefCell::new(Vec::new()); + let forward_event_handler = |event: Event| { + if let Event::PendingHTLCsForwardable { .. } = event { + nodes[fwd_idx].node.process_pending_htlc_forwards(); + } else { + other_events.borrow_mut().push(event); + } + }; + nodes[fwd_idx].node.process_pending_events(&forward_event_handler); + nodes[fwd_idx].node.process_pending_events(&forward_event_handler); let payment_preimage_opt = if user_generated_pmt_hash { None } else { Some(payment_preimage) }; - expect_payment_claimable!(&nodes[fwd_idx], payment_hash, payment_secret, payment_amt, payment_preimage_opt, invoice.recover_payee_pub_key()); + assert_eq!(other_events.borrow().len(), 1); + check_payment_claimable(&other_events.borrow()[0], payment_hash, payment_secret, payment_amt, payment_preimage_opt, invoice.recover_payee_pub_key()); do_claim_payment_along_route(&nodes[0], &[&vec!(&nodes[fwd_idx])[..]], false, payment_preimage); - let events = nodes[0].node.get_and_clear_pending_events(); - assert_eq!(events.len(), 2); - match events[0] { - Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => { - assert_eq!(payment_preimage, *ev_preimage); - assert_eq!(payment_hash, *ev_hash); - assert_eq!(fee_paid_msat, &Some(0)); - }, - _ => panic!("Unexpected event") - } - match events[1] { - Event::PaymentPathSuccessful { payment_hash: hash, .. } => { - assert_eq!(hash, Some(payment_hash)); - }, - _ => panic!("Unexpected event") - } + expect_payment_sent(&nodes[0], payment_preimage, None, true, true); } #[test] @@ -1447,7 +1465,7 @@ mod test { assert_eq!(invoice.amount_pico_btc(), Some(200_000)); assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64); assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into())); - assert_eq!(invoice.description(), InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes())))); + assert_eq!(invoice.description(), Bolt11InvoiceDescription::Hash(&crate::Sha256(Sha256::hash("Description hash phantom invoice".as_bytes())))); } #[test] @@ -1464,7 +1482,7 @@ mod test { nodes[2].node.get_phantom_route_hints(), ]; let user_payment_preimage = PaymentPreimage([1; 32]); - let payment_hash = Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner())); + let payment_hash = Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).to_byte_array())); let non_default_invoice_expiry_secs = 4200; let min_final_cltv_expiry_delta = Some(100); let duration_since_epoch = Duration::from_secs(1234567); @@ -1558,7 +1576,7 @@ mod test { // is never handled, the `channel.counterparty.forwarding_info` is never assigned. let mut private_chan_cfg = UserConfig::default(); private_chan_cfg.channel_handshake_config.announced_channel = false; - let temporary_channel_id = nodes[1].node.create_channel(nodes[3].node.get_our_node_id(), 1_000_000, 500_000_000, 42, Some(private_chan_cfg)).unwrap(); + let temporary_channel_id = nodes[1].node.create_channel(nodes[3].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None, Some(private_chan_cfg)).unwrap(); let open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[3].node.get_our_node_id()); nodes[3].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_channel); let accept_channel = get_event_msg!(nodes[3], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());