|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Tests for verifying the correct end-to-end handling of BOLT11 payments, including metadata propagation. |
| 11 | +
|
| 12 | +use crate::events::Event; |
| 13 | +use crate::ln::channelmanager::{PaymentId, Retry}; |
| 14 | +use crate::ln::functional_test_utils::*; |
| 15 | +use crate::ln::msgs::ChannelMessageHandler; |
| 16 | +use crate::ln::outbound_payment::Bolt11PaymentError; |
| 17 | +use crate::routing::router::RouteParametersConfig; |
| 18 | +use crate::sign::{NodeSigner, Recipient}; |
| 19 | +use bitcoin::hashes::sha256::Hash as Sha256; |
| 20 | +use bitcoin::hashes::Hash; |
| 21 | +use lightning_invoice::{Bolt11Invoice, Currency, InvoiceBuilder}; |
| 22 | +use std::time::SystemTime; |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn payment_metadata_end_to_end_for_invoice_with_amount() { |
| 26 | + // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all |
| 27 | + // the way out through the `PaymentClaimable` event. |
| 28 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 29 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 30 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); |
| 31 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 32 | + create_announced_chan_between_nodes(&nodes, 0, 1); |
| 33 | + |
| 34 | + let payment_metadata = vec![42, 43, 44, 45, 46, 47, 48, 49, 42]; |
| 35 | + |
| 36 | + let (payment_hash, payment_secret) = |
| 37 | + nodes[1].node.create_inbound_payment(None, 7200, None).unwrap(); |
| 38 | + |
| 39 | + let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); |
| 40 | + let invoice = InvoiceBuilder::new(Currency::Bitcoin) |
| 41 | + .description("test".into()) |
| 42 | + .payment_hash(Sha256::from_slice(&payment_hash.0).unwrap()) |
| 43 | + .payment_secret(payment_secret) |
| 44 | + .duration_since_epoch(timestamp) |
| 45 | + .min_final_cltv_expiry_delta(144) |
| 46 | + .amount_milli_satoshis(50_000) |
| 47 | + .payment_metadata(payment_metadata.clone()) |
| 48 | + .build_raw() |
| 49 | + .unwrap(); |
| 50 | + let sig = nodes[1].keys_manager.backing.sign_invoice(&invoice, Recipient::Node).unwrap(); |
| 51 | + let invoice = invoice.sign::<_, ()>(|_| Ok(sig)).unwrap(); |
| 52 | + let invoice = Bolt11Invoice::from_signed(invoice).unwrap(); |
| 53 | + |
| 54 | + match nodes[0].node.pay_for_bolt11_invoice( |
| 55 | + &invoice, |
| 56 | + PaymentId(payment_hash.0), |
| 57 | + Some(100), |
| 58 | + RouteParametersConfig::default(), |
| 59 | + Retry::Attempts(0), |
| 60 | + ) { |
| 61 | + Err(Bolt11PaymentError::InvalidAmount) => (), |
| 62 | + _ => panic!("Unexpected result"), |
| 63 | + }; |
| 64 | + |
| 65 | + nodes[0] |
| 66 | + .node |
| 67 | + .pay_for_bolt11_invoice( |
| 68 | + &invoice, |
| 69 | + PaymentId(payment_hash.0), |
| 70 | + None, |
| 71 | + RouteParametersConfig::default(), |
| 72 | + Retry::Attempts(0), |
| 73 | + ) |
| 74 | + .unwrap(); |
| 75 | + |
| 76 | + check_added_monitors(&nodes[0], 1); |
| 77 | + let send_event = SendEvent::from_node(&nodes[0]); |
| 78 | + nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]); |
| 79 | + commitment_signed_dance!(nodes[1], nodes[0], &send_event.commitment_msg, false); |
| 80 | + |
| 81 | + expect_pending_htlcs_forwardable!(nodes[1]); |
| 82 | + |
| 83 | + let mut events = nodes[1].node.get_and_clear_pending_events(); |
| 84 | + assert_eq!(events.len(), 1); |
| 85 | + match events.pop().unwrap() { |
| 86 | + Event::PaymentClaimable { onion_fields, .. } => { |
| 87 | + assert_eq!(Some(payment_metadata), onion_fields.unwrap().payment_metadata); |
| 88 | + }, |
| 89 | + _ => panic!("Unexpected event"), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn payment_metadata_end_to_end_for_invoice_with_no_amount() { |
| 95 | + // Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all |
| 96 | + // the way out through the `PaymentClaimable` event. |
| 97 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 98 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 99 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); |
| 100 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 101 | + create_announced_chan_between_nodes(&nodes, 0, 1); |
| 102 | + |
| 103 | + let payment_metadata = vec![42, 43, 44, 45, 46, 47, 48, 49, 42]; |
| 104 | + |
| 105 | + let (payment_hash, payment_secret) = |
| 106 | + nodes[1].node.create_inbound_payment(None, 7200, None).unwrap(); |
| 107 | + |
| 108 | + let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); |
| 109 | + let invoice = InvoiceBuilder::new(Currency::Bitcoin) |
| 110 | + .description("test".into()) |
| 111 | + .payment_hash(Sha256::from_slice(&payment_hash.0).unwrap()) |
| 112 | + .payment_secret(payment_secret) |
| 113 | + .duration_since_epoch(timestamp) |
| 114 | + .min_final_cltv_expiry_delta(144) |
| 115 | + .payment_metadata(payment_metadata.clone()) |
| 116 | + .build_raw() |
| 117 | + .unwrap(); |
| 118 | + let sig = nodes[1].keys_manager.backing.sign_invoice(&invoice, Recipient::Node).unwrap(); |
| 119 | + let invoice = invoice.sign::<_, ()>(|_| Ok(sig)).unwrap(); |
| 120 | + let invoice = Bolt11Invoice::from_signed(invoice).unwrap(); |
| 121 | + |
| 122 | + match nodes[0].node.pay_for_bolt11_invoice( |
| 123 | + &invoice, |
| 124 | + PaymentId(payment_hash.0), |
| 125 | + None, |
| 126 | + RouteParametersConfig::default(), |
| 127 | + Retry::Attempts(0), |
| 128 | + ) { |
| 129 | + Err(Bolt11PaymentError::InvalidAmount) => (), |
| 130 | + _ => panic!("Unexpected result"), |
| 131 | + }; |
| 132 | + |
| 133 | + nodes[0] |
| 134 | + .node |
| 135 | + .pay_for_bolt11_invoice( |
| 136 | + &invoice, |
| 137 | + PaymentId(payment_hash.0), |
| 138 | + Some(50_000), |
| 139 | + RouteParametersConfig::default(), |
| 140 | + Retry::Attempts(0), |
| 141 | + ) |
| 142 | + .unwrap(); |
| 143 | + |
| 144 | + check_added_monitors(&nodes[0], 1); |
| 145 | + let send_event = SendEvent::from_node(&nodes[0]); |
| 146 | + nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]); |
| 147 | + commitment_signed_dance!(nodes[1], nodes[0], &send_event.commitment_msg, false); |
| 148 | + |
| 149 | + expect_pending_htlcs_forwardable!(nodes[1]); |
| 150 | + |
| 151 | + let mut events = nodes[1].node.get_and_clear_pending_events(); |
| 152 | + assert_eq!(events.len(), 1); |
| 153 | + match events.pop().unwrap() { |
| 154 | + Event::PaymentClaimable { onion_fields, .. } => { |
| 155 | + assert_eq!(Some(payment_metadata), onion_fields.unwrap().payment_metadata); |
| 156 | + }, |
| 157 | + _ => panic!("Unexpected event"), |
| 158 | + } |
| 159 | +} |
0 commit comments