Merge pull request #2746 from TheBlueMatt/2023-11-bitcoin-0.30-followups
[rust-lightning] / lightning-invoice / src / utils.rs
index 25a7cf77d5e981bfc68b662a9cce862432fbcaa8..9930b545662ae4598115f548b983c24860c3337e 100644 (file)
@@ -158,7 +158,7 @@ where
 
        let invoice = match description {
                Bolt11InvoiceDescription::Direct(description) => {
-                       InvoiceBuilder::new(network).description(description.0.clone())
+                       InvoiceBuilder::new(network).description(description.0.0.clone())
                }
                Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
        };
@@ -191,7 +191,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)
@@ -534,11 +534,11 @@ 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 {
                Bolt11InvoiceDescription::Direct(description) => {
-                       InvoiceBuilder::new(network).description(description.0.clone())
+                       InvoiceBuilder::new(network).description(description.0.0.clone())
                }
                Bolt11InvoiceDescription::Hash(hash) => InvoiceBuilder::new(network).description_hash(hash.0),
        };
@@ -627,7 +627,7 @@ where
        log_trace!(logger, "Considering {} channels for invoice route hints", channels.len());
        for channel in channels.into_iter().filter(|chan| chan.is_channel_ready) {
                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 +641,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);
                        }
                }
@@ -681,18 +681,18 @@ 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);
                                }
                        }
@@ -731,14 +731,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
@@ -808,6 +808,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() {
@@ -852,7 +853,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(), Bolt11InvoiceDescription::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
@@ -869,13 +870,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();
@@ -965,7 +963,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(), Bolt11InvoiceDescription::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());
        }
 
@@ -1143,7 +1141,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());
@@ -1296,7 +1294,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
                };
@@ -1309,7 +1307,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 {
@@ -1317,7 +1315,7 @@ mod test {
                };
 
                assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
-               assert_eq!(invoice.description(), Bolt11InvoiceDescription::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());
@@ -1326,13 +1324,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();
@@ -1373,22 +1368,7 @@ mod test {
                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]
@@ -1472,7 +1452,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);
@@ -1566,7 +1546,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());