Skip to content

Commit 1f58dcf

Browse files
committed
Introduce pay_for_bolt11_invoice tests
1 parent f5970ac commit 1f58dcf

File tree

2 files changed

+100
-21
lines changed

2 files changed

+100
-21
lines changed

lightning/src/ln/bolt11_payment.rs

+94-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ fn params_from_invoice(
8787
#[cfg(test)]
8888
mod tests {
8989
use super::*;
90-
use crate::routing::router::Payee;
90+
use crate::events::Event;
91+
use crate::ln::channelmanager::{PaymentId, Retry};
92+
use crate::ln::functional_test_utils::*;
93+
use crate::ln::msgs::ChannelMessageHandler;
94+
use crate::ln::outbound_payment::Bolt11PaymentError;
95+
use crate::routing::router::{Payee, RouteParametersConfig};
9196
use crate::sign::{NodeSigner, Recipient};
9297
use crate::types::payment::PaymentSecret;
9398
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -160,12 +165,7 @@ mod tests {
160165
}
161166

162167
#[test]
163-
fn payment_metadata_end_to_end() {
164-
use crate::events::Event;
165-
use crate::ln::channelmanager::{PaymentId, Retry};
166-
use crate::ln::functional_test_utils::*;
167-
use crate::ln::msgs::ChannelMessageHandler;
168-
168+
fn payment_metadata_end_to_end_for_invoice_with_amount() {
169169
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
170170
// the way out through the `PaymentClaimable` event.
171171
let chanmon_cfgs = create_chanmon_cfgs(2);
@@ -194,11 +194,96 @@ mod tests {
194194
let invoice = invoice.sign::<_, ()>(|_| Ok(sig)).unwrap();
195195
let invoice = Bolt11Invoice::from_signed(invoice).unwrap();
196196

197-
let (hash, onion, params) = payment_parameters_from_invoice(&invoice).unwrap();
197+
match nodes[0].node.pay_for_bolt11_invoice(
198+
&invoice,
199+
PaymentId(payment_hash.0),
200+
Some(100),
201+
RouteParametersConfig::default(),
202+
Retry::Attempts(0),
203+
) {
204+
Err(Bolt11PaymentError::InvalidAmount) => (),
205+
_ => panic!("Unexpected result"),
206+
};
207+
208+
nodes[0]
209+
.node
210+
.pay_for_bolt11_invoice(
211+
&invoice,
212+
PaymentId(payment_hash.0),
213+
None,
214+
RouteParametersConfig::default(),
215+
Retry::Attempts(0),
216+
)
217+
.unwrap();
218+
219+
check_added_monitors(&nodes[0], 1);
220+
let send_event = SendEvent::from_node(&nodes[0]);
221+
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]);
222+
commitment_signed_dance!(nodes[1], nodes[0], &send_event.commitment_msg, false);
223+
224+
expect_pending_htlcs_forwardable!(nodes[1]);
225+
226+
let mut events = nodes[1].node.get_and_clear_pending_events();
227+
assert_eq!(events.len(), 1);
228+
match events.pop().unwrap() {
229+
Event::PaymentClaimable { onion_fields, .. } => {
230+
assert_eq!(Some(payment_metadata), onion_fields.unwrap().payment_metadata);
231+
},
232+
_ => panic!("Unexpected event"),
233+
}
234+
}
235+
236+
#[test]
237+
fn payment_metadata_end_to_end_for_invoice_with_no_amount() {
238+
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all
239+
// the way out through the `PaymentClaimable` event.
240+
let chanmon_cfgs = create_chanmon_cfgs(2);
241+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
242+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
243+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
244+
create_announced_chan_between_nodes(&nodes, 0, 1);
245+
246+
let payment_metadata = vec![42, 43, 44, 45, 46, 47, 48, 49, 42];
247+
248+
let (payment_hash, payment_secret) =
249+
nodes[1].node.create_inbound_payment(None, 7200, None).unwrap();
250+
251+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
252+
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
253+
.description("test".into())
254+
.payment_hash(Sha256::from_slice(&payment_hash.0).unwrap())
255+
.payment_secret(payment_secret)
256+
.duration_since_epoch(timestamp)
257+
.min_final_cltv_expiry_delta(144)
258+
.payment_metadata(payment_metadata.clone())
259+
.build_raw()
260+
.unwrap();
261+
let sig = nodes[1].keys_manager.backing.sign_invoice(&invoice, Recipient::Node).unwrap();
262+
let invoice = invoice.sign::<_, ()>(|_| Ok(sig)).unwrap();
263+
let invoice = Bolt11Invoice::from_signed(invoice).unwrap();
264+
265+
match nodes[0].node.pay_for_bolt11_invoice(
266+
&invoice,
267+
PaymentId(payment_hash.0),
268+
None,
269+
RouteParametersConfig::default(),
270+
Retry::Attempts(0),
271+
) {
272+
Err(Bolt11PaymentError::InvalidAmount) => (),
273+
_ => panic!("Unexpected result"),
274+
};
275+
198276
nodes[0]
199277
.node
200-
.send_payment(hash, onion, PaymentId(hash.0), params, Retry::Attempts(0))
278+
.pay_for_bolt11_invoice(
279+
&invoice,
280+
PaymentId(payment_hash.0),
281+
Some(50_000),
282+
RouteParametersConfig::default(),
283+
Retry::Attempts(0),
284+
)
201285
.unwrap();
286+
202287
check_added_monitors(&nodes[0], 1);
203288
let send_event = SendEvent::from_node(&nodes[0]);
204289
nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &send_event.msgs[0]);

lightning/src/ln/invoice_utils.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ mod test {
715715
use crate::ln::channelmanager::{Bolt11InvoiceParameters, PhantomRouteHints, MIN_FINAL_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields, Retry};
716716
use crate::ln::functional_test_utils::*;
717717
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, MessageSendEvent};
718-
use crate::routing::router::{PaymentParameters, RouteParameters};
718+
use crate::routing::router::{PaymentParameters, RouteParameters, RouteParametersConfig};
719719
use crate::util::test_utils;
720720
use crate::util::config::UserConfig;
721721
use std::collections::HashSet;
@@ -750,7 +750,7 @@ mod test {
750750

751751

752752
#[test]
753-
fn test_from_channelmanager() {
753+
fn create_and_pay_for_bolt11_invoice() {
754754
let chanmon_cfgs = create_chanmon_cfgs(2);
755755
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
756756
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -784,17 +784,11 @@ mod test {
784784
assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan.inbound_htlc_minimum_msat);
785785
assert_eq!(invoice.route_hints()[0].0[0].htlc_maximum_msat, chan.inbound_htlc_maximum_msat);
786786

787-
let payment_params = PaymentParameters::from_node_id(invoice.recover_payee_pub_key(),
788-
invoice.min_final_cltv_expiry_delta() as u32)
789-
.with_bolt11_features(invoice.features().unwrap().clone()).unwrap()
790-
.with_route_hints(invoice.route_hints()).unwrap();
791-
let route_params = RouteParameters::from_payment_params_and_value(
792-
payment_params, invoice.amount_milli_satoshis().unwrap());
793787
let payment_event = {
794-
let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
795-
nodes[0].node.send_payment(payment_hash,
796-
RecipientOnionFields::secret_only(*invoice.payment_secret()),
797-
PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
788+
nodes[0].node.pay_for_bolt11_invoice(
789+
&invoice, PaymentId([42; 32]), None, RouteParametersConfig::default(),
790+
Retry::Attempts(0)
791+
).unwrap();
798792
check_added_monitors(&nodes[0], 1);
799793

800794
let mut events = nodes[0].node.get_and_clear_pending_msg_events();

0 commit comments

Comments
 (0)