Skip to content

Commit a4fdae2

Browse files
committed
Add pays_bolt12_invoice_asynchronously test
1 parent 7827e21 commit a4fdae2

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

lightning/src/ln/offers_tests.rs

+86-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ use core::time::Duration;
4646
use crate::blinded_path::{BlindedPath, IntroductionNode};
4747
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
4848
use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
49-
use crate::ln::channelmanager::{MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
49+
use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
5050
use crate::ln::functional_test_utils::*;
5151
use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
52+
use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
5253
use crate::offers::invoice::Bolt12Invoice;
5354
use crate::offers::invoice_error::InvoiceError;
5455
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
@@ -865,6 +866,90 @@ fn pays_for_refund_without_blinded_paths() {
865866
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
866867
}
867868

869+
/// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
870+
#[test]
871+
fn pays_bolt12_invoice_asynchronously() {
872+
let mut manually_pay_cfg = test_default_channel_config();
873+
manually_pay_cfg.manually_handle_bolt12_invoices = true;
874+
875+
let chanmon_cfgs = create_chanmon_cfgs(2);
876+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
877+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
878+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
879+
880+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
881+
882+
let alice = &nodes[0];
883+
let alice_id = alice.node.get_our_node_id();
884+
let bob = &nodes[1];
885+
let bob_id = bob.node.get_our_node_id();
886+
887+
let offer = alice.node
888+
.create_offer_builder(None).unwrap()
889+
.amount_msats(10_000_000)
890+
.build().unwrap();
891+
892+
let payment_id = PaymentId([1; 32]);
893+
bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
894+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
895+
896+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
897+
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
898+
899+
let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
900+
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
901+
offer_id: offer.id(),
902+
invoice_request: InvoiceRequestFields {
903+
payer_id: invoice_request.payer_id(),
904+
quantity: None,
905+
payer_note_truncated: None,
906+
},
907+
});
908+
909+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
910+
bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
911+
912+
let invoice = match get_event!(bob, Event::InvoiceReceived) {
913+
Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
914+
assert_eq!(actual_payment_id, payment_id);
915+
invoice
916+
},
917+
_ => panic!("No Event::InvoiceReceived"),
918+
};
919+
assert_eq!(invoice.amount_msats(), 10_000_000);
920+
assert_ne!(invoice.signing_pubkey(), alice_id);
921+
assert!(!invoice.payment_paths().is_empty());
922+
for (_, path) in invoice.payment_paths() {
923+
assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
924+
}
925+
926+
assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
927+
assert_eq!(
928+
bob.node.send_payment_for_bolt12_invoice(&invoice),
929+
Err(Bolt12PaymentError::DuplicateInvoice),
930+
);
931+
932+
route_bolt12_payment(bob, &[alice], &invoice);
933+
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
934+
935+
claim_bolt12_payment(bob, &[alice], payment_context);
936+
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
937+
938+
assert_eq!(
939+
bob.node.send_payment_for_bolt12_invoice(&invoice),
940+
Err(Bolt12PaymentError::DuplicateInvoice),
941+
);
942+
943+
for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
944+
bob.node.timer_tick_occurred();
945+
}
946+
947+
assert_eq!(
948+
bob.node.send_payment_for_bolt12_invoice(&invoice),
949+
Err(Bolt12PaymentError::UnexpectedInvoice),
950+
);
951+
}
952+
868953
/// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
869954
#[test]
870955
fn fails_creating_offer_without_blinded_paths() {

0 commit comments

Comments
 (0)