Skip to content

Commit ab92369

Browse files
committed
Introduce OfferMessageFlowEvent test with manual offer response
1 parent 9af2f65 commit ab92369

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,9 @@ pub struct ChannelManager<
26662666
fee_estimator: LowerBoundedFeeEstimator<F>,
26672667
chain_monitor: M,
26682668
tx_broadcaster: T,
2669+
#[cfg(test)]
2670+
pub(super) router: R,
2671+
#[cfg(not(test))]
26692672
router: R,
26702673

26712674
#[cfg(test)]
@@ -2892,6 +2895,9 @@ pub struct ChannelManager<
28922895
pub(super) entropy_source: ES,
28932896
#[cfg(not(test))]
28942897
entropy_source: ES,
2898+
#[cfg(test)]
2899+
pub(super) node_signer: NS,
2900+
#[cfg(not(test))]
28952901
node_signer: NS,
28962902
#[cfg(test)]
28972903
pub(super) signer_provider: SP,
@@ -13407,7 +13413,7 @@ where
1340713413
now
1340813414
}
1340913415

13410-
fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
13416+
pub(crate) fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> {
1341113417
let per_peer_state = self.per_peer_state.read().unwrap();
1341213418
per_peer_state
1341313419
.iter()

lightning/src/ln/offers_tests.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, Paym
5151
use crate::blinded_path::message::OffersContext;
5252
use crate::events::{ClosureReason, Event, HTLCHandlingFailureType, PaidBolt12Invoice, PaymentFailureReason, PaymentPurpose};
5353
use crate::ln::channelmanager::{Bolt12PaymentError, PaymentId, RecentPaymentDetails, RecipientOnionFields, Retry, self};
54+
use crate::offers::flow::OfferMessageFlowEvent;
5455
use crate::types::features::Bolt12InvoiceFeatures;
5556
use crate::ln::functional_test_utils::*;
5657
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
@@ -866,6 +867,119 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
866867
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
867868
}
868869

870+
/// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
871+
/// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
872+
/// introduction node of the blinded path.
873+
#[test]
874+
fn creates_and_manually_respond_to_ir_then_pays_for_offer_using_one_hop_blinded_path() {
875+
let chanmon_cfgs = create_chanmon_cfgs(2);
876+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
877+
let mut node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
878+
node_chanmgrs[0].flow.enable_events();
879+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
880+
881+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
882+
883+
let alice = &nodes[0];
884+
let alice_id = alice.node.get_our_node_id();
885+
let bob = &nodes[1];
886+
let bob_id = bob.node.get_our_node_id();
887+
888+
let offer = alice.node
889+
.create_offer_builder().unwrap()
890+
.amount_msats(10_000_000)
891+
.build().unwrap();
892+
assert_ne!(offer.issuer_signing_pubkey(), Some(alice_id));
893+
assert!(!offer.paths().is_empty());
894+
for path in offer.paths() {
895+
assert!(check_compact_path_introduction_node(&path, bob, alice_id));
896+
}
897+
898+
let payment_id = PaymentId([1; 32]);
899+
bob.node.pay_for_offer(&offer, None, payment_id, Default::default()).unwrap();
900+
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
901+
902+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
903+
alice.onion_messenger.handle_onion_message(bob_id, &onion_message);
904+
905+
let flow_events = alice.node.flow.release_pending_flow_events();
906+
assert_eq!(flow_events.len(), 1, "expected exactly one flow event");
907+
908+
let (invoice_request, reply_path) = match flow_events.into_iter().next().unwrap() {
909+
OfferMessageFlowEvent::InvoiceRequestReceived {
910+
invoice_request: InvoiceRequestVerifiedFromOffer::DerivedKeys(req),
911+
reply_path
912+
} => (req, reply_path),
913+
_ => panic!("Unexpected flow event"),
914+
};
915+
916+
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
917+
offer_id: offer.id(),
918+
invoice_request: InvoiceRequestFields {
919+
payer_signing_pubkey: invoice_request.payer_signing_pubkey(),
920+
quantity: None,
921+
payer_note_truncated: None,
922+
human_readable_name: None,
923+
},
924+
});
925+
assert_eq!(invoice_request.amount_msats(), Some(10_000_000));
926+
assert_ne!(invoice_request.payer_signing_pubkey(), bob_id);
927+
assert!(check_compact_path_introduction_node(&reply_path, alice, bob_id));
928+
929+
// Create response for invoice request manually.
930+
let get_payment_info = |amount_msats, relative_expiry| {
931+
alice
932+
.node
933+
.create_inbound_payment(Some(amount_msats), relative_expiry, None)
934+
.map_err(|_| Bolt12SemanticError::InvalidAmount)
935+
};
936+
937+
let router = &alice.node.router;
938+
let (builder, _) = alice
939+
.node
940+
.flow
941+
.create_invoice_builder_from_invoice_request_with_keys(
942+
router,
943+
&DefaultCurrencyConversion {},
944+
&invoice_request,
945+
alice.node.list_usable_channels(),
946+
get_payment_info,
947+
)
948+
.expect("failed to create builder with derived keys");
949+
950+
let invoice = builder
951+
.build_and_sign(&alice.node.secp_ctx)
952+
.expect("failed to build and sign invoice");
953+
954+
alice
955+
.node
956+
.flow
957+
.enqueue_invoice_using_reply_paths(
958+
invoice,
959+
&[reply_path],
960+
alice.node.get_peers_for_blinded_path(),
961+
)
962+
.expect("failed to enqueue invoice");
963+
964+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
965+
bob.onion_messenger.handle_onion_message(alice_id, &onion_message);
966+
967+
let (invoice, reply_path) = extract_invoice(bob, &onion_message);
968+
assert_eq!(invoice.amount_msats(), 10_000_000);
969+
assert_ne!(invoice.signing_pubkey(), alice_id);
970+
assert!(!invoice.payment_paths().is_empty());
971+
for path in invoice.payment_paths() {
972+
assert_eq!(path.introduction_node(), &IntroductionNode::NodeId(alice_id));
973+
}
974+
assert!(check_compact_path_introduction_node(&reply_path, bob, alice_id));
975+
976+
route_bolt12_payment(bob, &[alice], &invoice);
977+
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
978+
979+
claim_bolt12_payment(bob, &[alice], payment_context, &invoice);
980+
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
981+
}
982+
869983
/// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
870984
/// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
871985
/// introduction node of the blinded path.

0 commit comments

Comments
 (0)