Skip to content

Commit 96f6719

Browse files
committed
Introduce custom data in handle_custom_message
1 parent b14b154 commit 96f6719

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

fuzz/src/onion_message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
161161
type CustomMessage = TestCustomMessage;
162162
fn handle_custom_message(
163163
&self, message: Self::CustomMessage, _context: Option<Vec<u8>>,
164-
responder: Option<Responder>,
164+
_custom_data: Option<Vec<u8>>, responder: Option<Responder>,
165165
) -> Option<(Self::CustomMessage, ResponseInstruction)> {
166166
match responder {
167167
Some(responder) => Some((message, responder.respond())),

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12296,7 +12296,7 @@ where
1229612296
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
1229712297
let hmac = payment_hash.hmac_for_offer_payment(nonce, expanded_key);
1229812298
let context = MessageContext::Offers(OffersContext::InboundPayment { payment_hash, nonce, hmac });
12299-
Some((OffersMessage::Invoice(invoice), responder.respond_with_reply_path(context)))
12299+
Some((OffersMessage::Invoice(invoice), responder.respond_with_reply_path(context, None)))
1230012300
},
1230112301
Err(error) => Some((OffersMessage::InvoiceError(error.into()), responder.respond())),
1230212302
}

lightning/src/ln/peer_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl DNSResolverMessageHandler for IgnoringMessageHandler {
169169
}
170170
impl CustomOnionMessageHandler for IgnoringMessageHandler {
171171
type CustomMessage = Infallible;
172-
fn handle_custom_message(&self, _message: Infallible, _context: Option<Vec<u8>>, _responder: Option<Responder>) -> Option<(Infallible, ResponseInstruction)> {
172+
fn handle_custom_message(&self, _message: Infallible, _context: Option<Vec<u8>>, _custom_data: Option<Vec<u8>>, _responder: Option<Responder>) -> Option<(Infallible, ResponseInstruction)> {
173173
// Since we always return `None` in the read the handle method should never be called.
174174
unreachable!();
175175
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ impl Drop for TestCustomMessageHandler {
191191
impl CustomOnionMessageHandler for TestCustomMessageHandler {
192192
type CustomMessage = TestCustomMessage;
193193
fn handle_custom_message(
194-
&self, msg: Self::CustomMessage, context: Option<Vec<u8>>, responder: Option<Responder>,
194+
&self, msg: Self::CustomMessage, context: Option<Vec<u8>>, custom_data: Option<Vec<u8>>,
195+
responder: Option<Responder>,
195196
) -> Option<(Self::CustomMessage, ResponseInstruction)> {
196197
let expectation = self.get_next_expectation();
197198
assert_eq!(msg, expectation.expect);
@@ -209,7 +210,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
209210
match responder {
210211
Some(responder) if expectation.include_reply_path => {
211212
let context = MessageContext::Custom(context.unwrap_or_else(Vec::new));
212-
let reply = responder.respond_with_reply_path(context);
213+
let reply = responder.respond_with_reply_path(context, custom_data);
213214
Some((response, reply))
214215
},
215216
Some(responder) => Some((response, responder.respond())),
@@ -507,7 +508,7 @@ fn async_response_over_one_blinded_hop() {
507508
// 5. Expect Alice to receive the message and create a response instruction for it.
508509
alice.custom_message_handler.expect_message(message.clone());
509510
let response_instruction =
510-
nodes[0].custom_message_handler.handle_custom_message(message, None, responder);
511+
nodes[0].custom_message_handler.handle_custom_message(message, None, None, responder);
511512

512513
// 6. Simulate Alice asynchronously responding back to Bob with a response.
513514
let (msg, instructions) = response_instruction.unwrap();
@@ -546,7 +547,7 @@ fn async_response_with_reply_path_succeeds() {
546547
let responder = Responder::new(reply_path);
547548
alice.custom_message_handler.expect_message_and_response(message.clone());
548549
let response_instruction =
549-
alice.custom_message_handler.handle_custom_message(message, None, Some(responder));
550+
alice.custom_message_handler.handle_custom_message(message, None, None, Some(responder));
550551

551552
let (msg, instructions) = response_instruction.unwrap();
552553
assert_eq!(
@@ -590,7 +591,7 @@ fn async_response_with_reply_path_fails() {
590591
let responder = Responder::new(reply_path);
591592
alice.custom_message_handler.expect_message_and_response(message.clone());
592593
let response_instruction =
593-
alice.custom_message_handler.handle_custom_message(message, None, Some(responder));
594+
alice.custom_message_handler.handle_custom_message(message, None, None, Some(responder));
594595

595596
let (msg, instructions) = response_instruction.unwrap();
596597
assert_eq!(

lightning/src/onion_message/messenger.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,19 @@ impl Responder {
418418
pub fn respond(self) -> ResponseInstruction {
419419
ResponseInstruction {
420420
destination: Destination::BlindedPath(self.reply_path),
421-
context: None,
421+
reply_data: (None, None),
422422
}
423423
}
424424

425425
/// Creates a [`ResponseInstruction`] for responding including a reply path.
426426
///
427427
/// Use when the recipient needs to send back a reply to us.
428-
pub fn respond_with_reply_path(self, context: MessageContext) -> ResponseInstruction {
428+
pub fn respond_with_reply_path(
429+
self, context: MessageContext, custom_data: Option<Vec<u8>>,
430+
) -> ResponseInstruction {
429431
ResponseInstruction {
430432
destination: Destination::BlindedPath(self.reply_path),
431-
context: Some(context),
433+
reply_data: (Some(context), custom_data),
432434
}
433435
}
434436
}
@@ -440,7 +442,7 @@ pub struct ResponseInstruction {
440442
/// [`Destination`] rather than an explicit [`BlindedMessagePath`] simplifies the logic in
441443
/// [`OnionMessenger::send_onion_message_internal`] somewhat.
442444
destination: Destination,
443-
context: Option<MessageContext>,
445+
reply_data: (Option<MessageContext>, Option<Vec<u8>>),
444446
}
445447

446448
impl ResponseInstruction {
@@ -469,7 +471,7 @@ pub enum MessageSendInstructions {
469471
destination: Destination,
470472
/// The context to include in the reply path we'll give the recipient so they can respond
471473
/// to us.
472-
context: MessageContext,
474+
reply_data: (MessageContext, Option<Vec<u8>>),
473475
},
474476
/// Indicates that a message should be sent without including a reply path, preventing the
475477
/// recipient from responding.
@@ -886,7 +888,8 @@ pub trait CustomOnionMessageHandler {
886888
///
887889
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
888890
fn handle_custom_message(
889-
&self, message: Self::CustomMessage, context: Option<Vec<u8>>, responder: Option<Responder>,
891+
&self, message: Self::CustomMessage, context: Option<Vec<u8>>,
892+
custom_data: Option<Vec<u8>>, responder: Option<Responder>,
890893
) -> Option<(Self::CustomMessage, ResponseInstruction)>;
891894

892895
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
@@ -1332,10 +1335,14 @@ where
13321335
MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path } => {
13331336
(destination, Some(reply_path))
13341337
},
1335-
MessageSendInstructions::WithReplyPath { destination, context }
1338+
MessageSendInstructions::WithReplyPath {
1339+
destination,
1340+
reply_data: (context, custom_data),
1341+
}
13361342
| MessageSendInstructions::ForReply {
1337-
instructions: ResponseInstruction { destination, context: Some(context) },
1338-
} => match self.create_blinded_path(context) {
1343+
instructions:
1344+
ResponseInstruction { destination, reply_data: (Some(context), custom_data) },
1345+
} => match self.create_blinded_path(context, custom_data) {
13391346
Ok(reply_path) => (destination, Some(reply_path)),
13401347
Err(err) => {
13411348
log_trace!(
@@ -1349,7 +1356,7 @@ where
13491356
},
13501357
MessageSendInstructions::WithoutReplyPath { destination }
13511358
| MessageSendInstructions::ForReply {
1352-
instructions: ResponseInstruction { destination, context: None },
1359+
instructions: ResponseInstruction { destination, reply_data: (None, _) },
13531360
} => (destination, None),
13541361
};
13551362

@@ -1407,7 +1414,7 @@ where
14071414
}
14081415

14091416
fn create_blinded_path(
1410-
&self, context: MessageContext,
1417+
&self, context: MessageContext, custom_data: Option<Vec<u8>>,
14111418
) -> Result<BlindedMessagePath, SendError> {
14121419
let recipient = self
14131420
.node_signer
@@ -1424,7 +1431,7 @@ where
14241431
.collect::<Vec<_>>()
14251432
};
14261433

1427-
let recipient_tlvs = ReceiveTlvs { context: Some(context), custom_data: None };
1434+
let recipient_tlvs = ReceiveTlvs { context: Some(context), custom_data };
14281435

14291436
self.message_router
14301437
.create_blinded_paths(recipient, recipient_tlvs, peers, secp_ctx)
@@ -1932,8 +1939,12 @@ where
19321939
return;
19331940
},
19341941
};
1935-
let response_instructions =
1936-
self.custom_handler.handle_custom_message(msg, context, responder);
1942+
let response_instructions = self.custom_handler.handle_custom_message(
1943+
msg,
1944+
context,
1945+
custom_data,
1946+
responder,
1947+
);
19371948
if let Some((msg, instructions)) = response_instructions {
19381949
let _ = self.handle_onion_message_response(msg, instructions);
19391950
}

0 commit comments

Comments
 (0)