Skip to content

Commit b1cd887

Browse files
committed
Introduce handle_message_received test
- Add a test to verify the functionality of the handle_message_received function. - Ensure the test covers scenarios where InvoiceRequest messages are retried for PendingOutboundPayments after a simulated connection loss.
1 parent 5cccee5 commit b1cd887

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

lightning/src/ln/offers_tests.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,78 @@ fn send_invoice_for_refund_with_distinct_reply_path() {
10701070
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(nodes[6].node.get_our_node_id()));
10711071
}
10721072

1073+
/// Verifies that the invoice request message can be retried if it fails to reach the
1074+
/// payee on the first attempt.
1075+
#[test]
1076+
fn creates_and_pays_for_offer_with_retry() {
1077+
let chanmon_cfgs = create_chanmon_cfgs(2);
1078+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1079+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1080+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1081+
1082+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1083+
1084+
let alice = &nodes[0];
1085+
let alice_id = alice.node.get_our_node_id();
1086+
let bob = &nodes[1];
1087+
let bob_id = bob.node.get_our_node_id();
1088+
1089+
let offer = alice.node
1090+
.create_offer_builder(None).unwrap()
1091+
.amount_msats(10_000_000)
1092+
.build().unwrap();
1093+
assert_ne!(offer.signing_pubkey(), Some(alice_id));
1094+
assert!(!offer.paths().is_empty());
1095+
for path in offer.paths() {
1096+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
1097+
}
1098+
let payment_id = PaymentId([1; 32]);
1099+
bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
1100+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
1101+
1102+
let _lost_onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1103+
1104+
// Simulate a scenario where the original onion_message is lost before reaching Alice.
1105+
// Use handle_message_received to regenerate the message.
1106+
bob.node.message_received();
1107+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1108+
1109+
alice.onion_messenger.handle_onion_message(bob_id, &onion_message);
1110+
1111+
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
1112+
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
1113+
offer_id: offer.id(),
1114+
invoice_request: InvoiceRequestFields {
1115+
payer_id: invoice_request.payer_id(),
1116+
quantity: None,
1117+
payer_note_truncated: None,
1118+
},
1119+
});
1120+
assert_eq!(invoice_request.amount_msats(), None);
1121+
assert_ne!(invoice_request.payer_id(), bob_id);
1122+
assert_eq!(reply_path.introduction_node(), &IntroductionNode::NodeId(bob_id));
1123+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1124+
bob.onion_messenger.handle_onion_message(alice_id, &onion_message);
1125+
1126+
// Expect no more OffersMessage to be enqueued by this point, even after calling
1127+
// handle_message_received.
1128+
bob.node.message_received();
1129+
1130+
assert!(bob.onion_messenger.next_onion_message_for_peer(alice_id).is_none());
1131+
1132+
let (invoice, _) = extract_invoice(bob, &onion_message);
1133+
assert_eq!(invoice.amount_msats(), 10_000_000);
1134+
assert_ne!(invoice.signing_pubkey(), alice_id);
1135+
assert!(!invoice.payment_paths().is_empty());
1136+
for path in invoice.payment_paths() {
1137+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
1138+
}
1139+
route_bolt12_payment(bob, &[alice], &invoice);
1140+
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
1141+
claim_bolt12_payment(bob, &[alice], payment_context);
1142+
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
1143+
}
1144+
10731145
/// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
10741146
#[test]
10751147
fn pays_bolt12_invoice_asynchronously() {

0 commit comments

Comments
 (0)