Skip to content

Commit 3aaf055

Browse files
Tests: DRY static invoice creation
1 parent b67267c commit 3aaf055

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed

lightning/src/ln/async_payments_tests.rs

+37-57
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use crate::ln::offers_tests;
2020
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
2121
use crate::ln::outbound_payment::Retry;
2222
use crate::offers::nonce::Nonce;
23+
use crate::offers::offer::Offer;
24+
use crate::offers::static_invoice::StaticInvoice;
2325
use crate::onion_message::async_payments::{
2426
AsyncPaymentsMessage, AsyncPaymentsMessageHandler, ReleaseHeldHtlc,
2527
};
@@ -32,11 +34,39 @@ use crate::sign::NodeSigner;
3234
use crate::types::features::Bolt12InvoiceFeatures;
3335
use crate::types::payment::{PaymentPreimage, PaymentSecret};
3436
use crate::util::config::UserConfig;
37+
use bitcoin::secp256k1;
3538
use bitcoin::secp256k1::Secp256k1;
3639

3740
use core::convert::Infallible;
3841
use core::time::Duration;
3942

43+
fn create_static_invoice<T: secp256k1::Signing + secp256k1::Verification>(
44+
always_online_counterparty: &Node, recipient: &Node, relative_expiry: Option<Duration>,
45+
secp_ctx: &Secp256k1<T>,
46+
) -> (Offer, StaticInvoice) {
47+
let blinded_paths_to_always_online_node = always_online_counterparty
48+
.message_router
49+
.create_blinded_paths(
50+
always_online_counterparty.node.get_our_node_id(),
51+
MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
52+
Vec::new(),
53+
&secp_ctx,
54+
)
55+
.unwrap();
56+
let (offer_builder, offer_nonce) = recipient
57+
.node
58+
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
59+
.unwrap();
60+
let offer = offer_builder.build().unwrap();
61+
let static_invoice = recipient
62+
.node
63+
.create_static_invoice_builder(&offer, offer_nonce, relative_expiry)
64+
.unwrap()
65+
.build_and_sign(&secp_ctx)
66+
.unwrap();
67+
(offer, static_invoice)
68+
}
69+
4070
#[test]
4171
fn blinded_keysend() {
4272
let chanmon_cfgs = create_chanmon_cfgs(3);
@@ -362,20 +392,8 @@ fn ignore_unexpected_static_invoice() {
362392
create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
363393

364394
// Initiate payment to the sender's intended offer.
365-
let blinded_paths_to_always_online_node = nodes[1]
366-
.message_router
367-
.create_blinded_paths(
368-
nodes[1].node.get_our_node_id(),
369-
MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
370-
Vec::new(),
371-
&secp_ctx,
372-
)
373-
.unwrap();
374-
let (offer_builder, offer_nonce) = nodes[2]
375-
.node
376-
.create_async_receive_offer_builder(blinded_paths_to_always_online_node.clone())
377-
.unwrap();
378-
let offer = offer_builder.build().unwrap();
395+
let (offer, valid_static_invoice) =
396+
create_static_invoice(&nodes[1], &nodes[2], None, &secp_ctx);
379397
let amt_msat = 5000;
380398
let payment_id = PaymentId([1; 32]);
381399
nodes[0]
@@ -393,20 +411,7 @@ fn ignore_unexpected_static_invoice() {
393411

394412
// Create a static invoice to be sent over the reply path containing the original payment_id, but
395413
// the static invoice corresponds to a different offer than was originally paid.
396-
let unexpected_static_invoice = {
397-
let (offer_builder, nonce) = nodes[2]
398-
.node
399-
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
400-
.unwrap();
401-
let sender_unintended_offer = offer_builder.build().unwrap();
402-
403-
nodes[2]
404-
.node
405-
.create_static_invoice_builder(&sender_unintended_offer, nonce, None)
406-
.unwrap()
407-
.build_and_sign(&secp_ctx)
408-
.unwrap()
409-
};
414+
let unexpected_static_invoice = create_static_invoice(&nodes[1], &nodes[2], None, &secp_ctx).1;
410415

411416
// Check that we'll ignore the unexpected static invoice.
412417
nodes[1]
@@ -433,13 +438,6 @@ fn ignore_unexpected_static_invoice() {
433438

434439
// A valid static invoice corresponding to the correct offer will succeed and cause us to send a
435440
// held_htlc_available onion message.
436-
let valid_static_invoice = nodes[2]
437-
.node
438-
.create_static_invoice_builder(&offer, offer_nonce, None)
439-
.unwrap()
440-
.build_and_sign(&secp_ctx)
441-
.unwrap();
442-
443441
nodes[1]
444442
.onion_messenger
445443
.send_onion_message(
@@ -499,32 +497,14 @@ fn pays_static_invoice() {
499497
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
500498
create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
501499

502-
let blinded_paths_to_always_online_node = nodes[1]
503-
.message_router
504-
.create_blinded_paths(
505-
nodes[1].node.get_our_node_id(),
506-
MessageContext::Offers(OffersContext::InvoiceRequest { nonce: Nonce([42; 16]) }),
507-
Vec::new(),
508-
&secp_ctx,
509-
)
510-
.unwrap();
511-
let (offer_builder, offer_nonce) = nodes[2]
512-
.node
513-
.create_async_receive_offer_builder(blinded_paths_to_always_online_node)
514-
.unwrap();
515-
let offer = offer_builder.build().unwrap();
516-
let amt_msat = 5000;
517-
let payment_id = PaymentId([1; 32]);
518500
let relative_expiry = Duration::from_secs(1000);
519-
let static_invoice = nodes[2]
520-
.node
521-
.create_static_invoice_builder(&offer, offer_nonce, Some(relative_expiry))
522-
.unwrap()
523-
.build_and_sign(&secp_ctx)
524-
.unwrap();
501+
let (offer, static_invoice) =
502+
create_static_invoice(&nodes[1], &nodes[2], Some(relative_expiry), &secp_ctx);
525503
assert!(static_invoice.invoice_features().supports_basic_mpp());
526504
assert_eq!(static_invoice.relative_expiry(), relative_expiry);
527505

506+
let amt_msat = 5000;
507+
let payment_id = PaymentId([1; 32]);
528508
nodes[0]
529509
.node
530510
.pay_for_offer(&offer, None, Some(amt_msat), None, payment_id, Retry::Attempts(0), None)

0 commit comments

Comments
 (0)