Skip to content

Commit c7a3bf5

Browse files
committed
Update handle_message to accept recipient_data as an input field
1. The recipient data, if present, will be used by the handler to abandon outbound payments that have failed for some reason. 2. Also update handle_custom_message to accept optional recipient data for consistency. This will remain unused in this PR for now.
1 parent 8769dde commit c7a3bf5

File tree

8 files changed

+37
-17
lines changed

8 files changed

+37
-17
lines changed

fuzz/src/onion_message.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bitcoin::secp256k1::ecdsa::RecoverableSignature;
77
use bitcoin::secp256k1::schnorr;
88

99
use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
10+
use lightning::blinded_path::message::RecipientData;
1011
use lightning::ln::features::InitFeatures;
1112
use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
1213
use lightning::ln::script::ShutdownScript;
@@ -97,7 +98,7 @@ impl MessageRouter for TestMessageRouter {
9798
struct TestOffersMessageHandler {}
9899

99100
impl OffersMessageHandler for TestOffersMessageHandler {
100-
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
101+
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<OffersMessage> {
101102
ResponseInstruction::NoResponse
102103
}
103104
}
@@ -127,7 +128,7 @@ struct TestCustomMessageHandler {}
127128

128129
impl CustomOnionMessageHandler for TestCustomMessageHandler {
129130
type CustomMessage = TestCustomMessage;
130-
fn handle_custom_message(&self, message: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
131+
fn handle_custom_message(&self, message: Self::CustomMessage, responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<Self::CustomMessage> {
131132
match responder {
132133
Some(responder) => responder.respond(message),
133134
None => ResponseInstruction::NoResponse

lightning/src/blinded_path/message.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Data structures and methods for constructing [`BlindedPath`]s to send a message over.
2+
//!
3+
//! [`BlindedPath`]: crate::blinded_path::BlindedPath
4+
15
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
26

37
use crate::ln::channelmanager::PaymentId;

lightning/src/blinded_path/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! Creating blinded paths and related utilities live here.
1111
1212
pub mod payment;
13-
pub(crate) mod message;
13+
pub mod message;
1414
pub(crate) mod utils;
1515

1616
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};

lightning/src/ln/channelmanager.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3131
use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::{secp256k1, Sequence};
3333

34+
use crate::blinded_path::message::RecipientData;
3435
use crate::blinded_path::{BlindedPath, NodeIdLookUp};
3536
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentConstraints, PaymentContext, ReceiveTlvs};
3637
use crate::chain;
@@ -10348,10 +10349,16 @@ where
1034810349
R::Target: Router,
1034910350
L::Target: Logger,
1035010351
{
10351-
fn handle_message(&self, message: OffersMessage, responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
10352+
fn handle_message(&self, message: OffersMessage, responder: Option<Responder>, recipient_data: RecipientData) -> ResponseInstruction<OffersMessage> {
1035210353
let secp_ctx = &self.secp_ctx;
1035310354
let expanded_key = &self.inbound_payment_key;
1035410355

10356+
let abandon_if_payment = |payment_id| {
10357+
if let Some(payment_id) = payment_id {
10358+
self.abandon_payment(payment_id);
10359+
}
10360+
};
10361+
1035510362
match message {
1035610363
OffersMessage::InvoiceRequest(invoice_request) => {
1035710364
let responder = match responder {
@@ -10460,8 +10467,12 @@ where
1046010467
});
1046110468

1046210469
match (responder, response) {
10463-
(Some(responder), Err(e)) => responder.respond(OffersMessage::InvoiceError(e)),
10470+
(Some(responder), Err(e)) => {
10471+
abandon_if_payment(recipient_data.payment_id);
10472+
responder.respond(OffersMessage::InvoiceError(e))
10473+
},
1046410474
(None, Err(_)) => {
10475+
abandon_if_payment(recipient_data.payment_id);
1046510476
log_trace!(
1046610477
self.logger,
1046710478
"A response was generated, but there is no reply_path specified for sending the response."
@@ -10472,6 +10483,7 @@ where
1047210483
}
1047310484
},
1047410485
OffersMessage::InvoiceError(invoice_error) => {
10486+
abandon_if_payment(recipient_data.payment_id);
1047510487
log_trace!(self.logger, "Received invoice_error: {}", invoice_error);
1047610488
return ResponseInstruction::NoResponse;
1047710489
},

lightning/src/ln/peer_handler.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use bitcoin::blockdata::constants::ChainHash;
1919
use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey};
2020

21+
use crate::blinded_path::message::RecipientData;
2122
use crate::sign::{NodeSigner, Recipient};
2223
use crate::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
2324
use crate::ln::types::ChannelId;
@@ -137,13 +138,13 @@ impl OnionMessageHandler for IgnoringMessageHandler {
137138
}
138139

139140
impl OffersMessageHandler for IgnoringMessageHandler {
140-
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
141+
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<OffersMessage> {
141142
ResponseInstruction::NoResponse
142143
}
143144
}
144145
impl CustomOnionMessageHandler for IgnoringMessageHandler {
145146
type CustomMessage = Infallible;
146-
fn handle_custom_message(&self, _message: Self::CustomMessage, _responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
147+
fn handle_custom_message(&self, _message: Self::CustomMessage, _responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<Self::CustomMessage> {
147148
// Since we always return `None` in the read the handle method should never be called.
148149
unreachable!();
149150
}

lightning/src/onion_message/functional_tests.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! Onion message testing and test utilities live here.
1111
12+
use crate::blinded_path::message::RecipientData;
1213
use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
1314
use crate::events::{Event, EventsProvider};
1415
use crate::ln::features::{ChannelFeatures, InitFeatures};
@@ -73,7 +74,7 @@ impl Drop for MessengerNode {
7374
struct TestOffersMessageHandler {}
7475

7576
impl OffersMessageHandler for TestOffersMessageHandler {
76-
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>) -> ResponseInstruction<OffersMessage> {
77+
fn handle_message(&self, _message: OffersMessage, _responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<OffersMessage> {
7778
ResponseInstruction::NoResponse
7879
}
7980
}
@@ -160,7 +161,7 @@ impl Drop for TestCustomMessageHandler {
160161

161162
impl CustomOnionMessageHandler for TestCustomMessageHandler {
162163
type CustomMessage = TestCustomMessage;
163-
fn handle_custom_message(&self, msg: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage> {
164+
fn handle_custom_message(&self, msg: Self::CustomMessage, responder: Option<Responder>, _recipient_data: RecipientData) -> ResponseInstruction<Self::CustomMessage> {
164165
let expectation = self.get_next_expectation();
165166
assert_eq!(msg, expectation.expect);
166167

@@ -418,7 +419,7 @@ fn async_response_over_one_blinded_hop() {
418419

419420
// 5. Expect Alice to receive the message and create a response instruction for it.
420421
alice.custom_message_handler.expect_message(message.clone());
421-
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder);
422+
let response_instruction = nodes[0].custom_message_handler.handle_custom_message(message, responder, RecipientData::new());
422423

423424
// 6. Simulate Alice asynchronously responding back to Bob with a response.
424425
assert_eq!(
@@ -452,7 +453,7 @@ fn async_response_with_reply_path_succeeds() {
452453
// Alice asynchronously responds to Bob, expecting a response back from him.
453454
let responder = Responder::new(reply_path, path_id);
454455
alice.custom_message_handler.expect_message_and_response(message.clone());
455-
let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
456+
let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder), RecipientData::new());
456457

457458
assert_eq!(
458459
alice.messenger.handle_onion_message_response(response_instruction),
@@ -490,7 +491,7 @@ fn async_response_with_reply_path_fails() {
490491
// Therefore, the reply_path cannot be used for the response.
491492
let responder = Responder::new(reply_path, path_id);
492493
alice.custom_message_handler.expect_message_and_response(message.clone());
493-
let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder));
494+
let response_instruction = alice.custom_message_handler.handle_custom_message(message, Some(responder), RecipientData::new());
494495

495496
assert_eq!(
496497
alice.messenger.handle_onion_message_response(response_instruction),

lightning/src/onion_message/messenger.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub trait CustomOnionMessageHandler {
613613
/// Called with the custom message that was received, returning a response to send, if any.
614614
///
615615
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
616-
fn handle_custom_message(&self, message: Self::CustomMessage, responder: Option<Responder>) -> ResponseInstruction<Self::CustomMessage>;
616+
fn handle_custom_message(&self, message: Self::CustomMessage, responder: Option<Responder>, recipient_data: RecipientData) -> ResponseInstruction<Self::CustomMessage>;
617617

618618
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
619619
/// message type is unknown.
@@ -1209,7 +1209,7 @@ where
12091209
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
12101210
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
12111211
match self.peel_onion_message(msg) {
1212-
Ok(PeeledOnion::Receive(message, path_id, _, reply_path)) => {
1212+
Ok(PeeledOnion::Receive(message, path_id, recipient_data, reply_path)) => {
12131213
log_trace!(
12141214
logger,
12151215
"Received an onion message with path_id {:02x?} and {} reply_path: {:?}",
@@ -1220,14 +1220,14 @@ where
12201220
let responder = reply_path.map(
12211221
|reply_path| Responder::new(reply_path, path_id)
12221222
);
1223-
let response_instructions = self.offers_handler.handle_message(msg, responder);
1223+
let response_instructions = self.offers_handler.handle_message(msg, responder, recipient_data);
12241224
let _ = self.handle_onion_message_response(response_instructions);
12251225
},
12261226
ParsedOnionMessageContents::Custom(msg) => {
12271227
let responder = reply_path.map(
12281228
|reply_path| Responder::new(reply_path, path_id)
12291229
);
1230-
let response_instructions = self.custom_handler.handle_custom_message(msg, responder);
1230+
let response_instructions = self.custom_handler.handle_custom_message(msg, responder, recipient_data);
12311231
let _ = self.handle_onion_message_response(response_instructions);
12321232
},
12331233
}

lightning/src/onion_message/offers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! Message handling for BOLT 12 Offers.
1111
1212
use core::fmt;
13+
use crate::blinded_path::message::RecipientData;
1314
use crate::io::{self, Read};
1415
use crate::ln::msgs::DecodeError;
1516
use crate::offers::invoice_error::InvoiceError;
@@ -40,7 +41,7 @@ pub trait OffersMessageHandler {
4041
/// The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
4142
///
4243
/// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
43-
fn handle_message(&self, message: OffersMessage, responder: Option<Responder>) -> ResponseInstruction<OffersMessage>;
44+
fn handle_message(&self, message: OffersMessage, responder: Option<Responder>, recipient_data: RecipientData) -> ResponseInstruction<OffersMessage>;
4445

4546
/// Releases any [`OffersMessage`]s that need to be sent.
4647
///

0 commit comments

Comments
 (0)