Skip to content

Commit d68a1e0

Browse files
committed
Test Bolt12Invoice with unknown required features
1 parent 36570af commit d68a1e0

File tree

1 file changed

+122
-3
lines changed

1 file changed

+122
-3
lines changed

lightning/src/ln/offers_tests.rs

+122-3
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@
4141
//! blinded paths are used.
4242
4343
use bitcoin::network::Network;
44-
use bitcoin::secp256k1::PublicKey;
44+
use bitcoin::secp256k1::{PublicKey, Secp256k1};
4545
use core::time::Duration;
4646
use crate::blinded_path::{BlindedPath, IntroductionNode};
4747
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
48-
use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
48+
use crate::blinded_path::message::{MessageContext, OffersContext};
49+
use crate::events::{Event, MessageSendEventsProvider, PaymentFailureReason, PaymentPurpose};
4950
use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
51+
use crate::ln::features::Bolt12InvoiceFeatures;
5052
use crate::ln::functional_test_utils::*;
53+
use crate::ln::inbound_payment::ExpandedKey;
5154
use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
5255
use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
5356
use crate::offers::invoice::Bolt12Invoice;
5457
use crate::offers::invoice_error::InvoiceError;
5558
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
59+
use crate::offers::nonce::Nonce;
5660
use crate::offers::parse::Bolt12SemanticError;
57-
use crate::onion_message::messenger::{Destination, PeeledOnion};
61+
use crate::onion_message::messenger::{Destination, PeeledOnion, new_pending_onion_message};
5862
use crate::onion_message::offers::OffersMessage;
5963
use crate::onion_message::packet::ParsedOnionMessageContents;
6064
use crate::routing::gossip::{NodeAlias, NodeId};
@@ -184,6 +188,15 @@ fn claim_bolt12_payment<'a, 'b, 'c>(
184188
claim_payment(node, path, payment_preimage);
185189
}
186190

191+
fn extract_offer_nonce<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Nonce {
192+
match node.onion_messenger.peel_onion_message(message) {
193+
Ok(PeeledOnion::Receive(_, Some(MessageContext::Offers(OffersContext::InvoiceRequest { nonce })), _)) => nonce,
194+
Ok(PeeledOnion::Receive(_, context, _)) => panic!("Unexpected onion message context: {:?}", context),
195+
Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
196+
Err(e) => panic!("Failed to process onion message {:?}", e),
197+
}
198+
}
199+
187200
fn extract_invoice_request<'a, 'b, 'c>(
188201
node: &Node<'a, 'b, 'c>, message: &OnionMessage
189202
) -> (InvoiceRequest, BlindedPath) {
@@ -2059,3 +2072,109 @@ fn fails_paying_invoice_more_than_once() {
20592072
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
20602073
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
20612074
}
2075+
2076+
#[test]
2077+
fn fails_paying_invoice_with_unknown_required_features() {
2078+
let mut accept_forward_cfg = test_default_channel_config();
2079+
accept_forward_cfg.accept_forwards_to_priv_channels = true;
2080+
2081+
// Clearing route_blinding prevents forming any payment paths since the node is unannounced.
2082+
let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
2083+
features.set_onion_messages_optional();
2084+
features.set_route_blinding_optional();
2085+
2086+
let chanmon_cfgs = create_chanmon_cfgs(6);
2087+
let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
2088+
2089+
*node_cfgs[1].override_init_features.borrow_mut() = Some(features);
2090+
2091+
let node_chanmgrs = create_node_chanmgrs(
2092+
6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
2093+
);
2094+
let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
2095+
2096+
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
2097+
create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
2098+
create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
2099+
create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
2100+
create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
2101+
create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
2102+
create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
2103+
2104+
let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
2105+
let alice_id = alice.node.get_our_node_id();
2106+
let bob_id = bob.node.get_our_node_id();
2107+
let charlie_id = charlie.node.get_our_node_id();
2108+
let david_id = david.node.get_our_node_id();
2109+
2110+
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
2111+
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
2112+
2113+
let offer = alice.node
2114+
.create_offer_builder(None).unwrap()
2115+
.amount_msats(10_000_000)
2116+
.build().unwrap();
2117+
2118+
let payment_id = PaymentId([1; 32]);
2119+
david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
2120+
.unwrap();
2121+
2122+
connect_peers(david, bob);
2123+
2124+
let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
2125+
bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
2126+
2127+
connect_peers(alice, charlie);
2128+
2129+
let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
2130+
alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
2131+
2132+
let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
2133+
let nonce = extract_offer_nonce(alice, &onion_message);
2134+
2135+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
2136+
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
2137+
2138+
// Drop the invoice in favor for one with unknown required features.
2139+
let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
2140+
let (invoice, _) = extract_invoice(david, &onion_message);
2141+
2142+
let payment_paths = invoice.payment_paths().to_vec();
2143+
let payment_hash = invoice.payment_hash();
2144+
2145+
let expanded_key = ExpandedKey::new(&alice.keys_manager.get_inbound_payment_key_material());
2146+
let secp_ctx = Secp256k1::new();
2147+
2148+
let created_at = alice.node.duration_since_epoch();
2149+
let invoice = invoice_request
2150+
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap()
2151+
.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at).unwrap()
2152+
.features_unchecked(Bolt12InvoiceFeatures::unknown())
2153+
.build_and_sign(&secp_ctx).unwrap();
2154+
2155+
// Enqueue an onion message containing the new invoice.
2156+
let pending_message = new_pending_onion_message(
2157+
OffersMessage::Invoice(invoice), Destination::BlindedPath(reply_path), None
2158+
);
2159+
alice.node.pending_offers_messages.lock().unwrap().push(pending_message);
2160+
2161+
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
2162+
charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
2163+
2164+
let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
2165+
david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
2166+
2167+
// Confirm that david drops this failed payment from his pending outbound payments.
2168+
match get_event!(david, Event::PaymentFailed) {
2169+
Event::PaymentFailed {
2170+
payment_id: event_payment_id,
2171+
payment_hash: event_payment_hash,
2172+
reason: Some(event_reason),
2173+
} => {
2174+
assert_eq!(event_payment_id, payment_id);
2175+
assert_eq!(event_payment_hash, payment_hash);
2176+
assert_eq!(event_reason, PaymentFailureReason::UnknownRequiredFeatures);
2177+
},
2178+
_ => panic!("Expected Event::PaymentFailed with reason"),
2179+
}
2180+
}

0 commit comments

Comments
 (0)