Skip to content

Commit c8df14e

Browse files
committed
Generalize CustomOnionMessageContents trait
Rename CustomOnionMessageContents to OnionMessageContents and use it as a trait bound on messages passed to OnionMessenger methods. This allows using the trait in an upcoming commit as a bound on the contents of PendingOnionMessage. Also, make ParsedOnionMessageContent have pub(super) visibility as it no longer needs to be exposed. It now implements OnionMessageContents so that Payload can be bounded by OnionMessageContents directly, but used when either reading a ParsedOnionMessageContent or writing a specific type of OnionMessageContents (e.g., OffersMessage).
1 parent ec0e39f commit c8df14e

File tree

7 files changed

+97
-91
lines changed

7 files changed

+97
-91
lines changed

fuzz/src/onion_message.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use lightning::offers::invoice_request::UnsignedInvoiceRequest;
1414
use lightning::util::test_channel_signer::TestChannelSigner;
1515
use lightning::util::logger::Logger;
1616
use lightning::util::ser::{Readable, Writeable, Writer};
17-
use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessagePath, OnionMessenger};
17+
use lightning::onion_message::{CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger};
1818

1919
use crate::utils::test_logger;
2020

@@ -84,7 +84,7 @@ struct TestCustomMessage {}
8484
const CUSTOM_MESSAGE_TYPE: u64 = 4242;
8585
const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
8686

87-
impl CustomOnionMessageContents for TestCustomMessage {
87+
impl OnionMessageContents for TestCustomMessage {
8888
fn tlv_type(&self) -> u64 {
8989
CUSTOM_MESSAGE_TYPE
9090
}
@@ -216,9 +216,9 @@ mod tests {
216216
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
217217
"Received an onion message with path_id None and a reply_path".to_string())), Some(&1));
218218
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
219-
"Sending onion message when responding to onion message with path_id None".to_string())), Some(&1));
219+
"Sending onion message when responding to Custom onion message with path_id None".to_string())), Some(&1));
220220
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
221-
"Failed sending onion message when responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
221+
"Failed sending onion message when responding to Custom onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
222222
}
223223

224224
let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::util::ser::{VecWriter, Writeable, Writer};
2929
use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
3030
use crate::ln::wire;
3131
use crate::ln::wire::{Encode, Type};
32-
use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, OffersMessage, OffersMessageHandler, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
32+
use crate::onion_message::{CustomOnionMessageHandler, OffersMessage, OffersMessageHandler, OnionMessageContents, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
3333
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, NodeAlias};
3434
use crate::util::atomic_counter::AtomicCounter;
3535
use crate::util::logger::Logger;
@@ -131,7 +131,7 @@ impl CustomOnionMessageHandler for IgnoringMessageHandler {
131131
}
132132
}
133133

134-
impl CustomOnionMessageContents for Infallible {
134+
impl OnionMessageContents for Infallible {
135135
fn tlv_type(&self) -> u64 { unreachable!(); }
136136
}
137137

lightning/src/onion_message/functional_tests.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
1515
use crate::sign::{NodeSigner, Recipient};
1616
use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
1717
use crate::util::test_utils;
18-
use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
18+
use super::{CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
1919

2020
use bitcoin::network::constants::Network;
2121
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
@@ -77,7 +77,7 @@ const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
7777
const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
7878
const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
7979

80-
impl CustomOnionMessageContents for TestCustomMessage {
80+
impl OnionMessageContents for TestCustomMessage {
8181
fn tlv_type(&self) -> u64 {
8282
match self {
8383
TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
@@ -196,7 +196,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
196196
#[test]
197197
fn one_hop() {
198198
let nodes = create_nodes(2);
199-
let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
199+
let test_msg = TestCustomMessage::Response;
200200

201201
let path = OnionMessagePath {
202202
intermediate_nodes: vec![],
@@ -210,7 +210,7 @@ fn one_hop() {
210210
#[test]
211211
fn two_unblinded_hops() {
212212
let nodes = create_nodes(3);
213-
let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
213+
let test_msg = TestCustomMessage::Response;
214214

215215
let path = OnionMessagePath {
216216
intermediate_nodes: vec![nodes[1].get_node_pk()],
@@ -224,7 +224,7 @@ fn two_unblinded_hops() {
224224
#[test]
225225
fn two_unblinded_two_blinded() {
226226
let nodes = create_nodes(5);
227-
let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
227+
let test_msg = TestCustomMessage::Response;
228228

229229
let secp_ctx = Secp256k1::new();
230230
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -241,7 +241,7 @@ fn two_unblinded_two_blinded() {
241241
#[test]
242242
fn three_blinded_hops() {
243243
let nodes = create_nodes(4);
244-
let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
244+
let test_msg = TestCustomMessage::Response;
245245

246246
let secp_ctx = Secp256k1::new();
247247
let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -259,7 +259,7 @@ fn three_blinded_hops() {
259259
fn too_big_packet_error() {
260260
// Make sure we error as expected if a packet is too big to send.
261261
let nodes = create_nodes(2);
262-
let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
262+
let test_msg = TestCustomMessage::Response;
263263

264264
let hop_node_id = nodes[1].get_node_pk();
265265
let hops = vec![hop_node_id; 400];
@@ -285,7 +285,7 @@ fn we_are_intro_node() {
285285
destination: Destination::BlindedPath(blinded_path),
286286
};
287287

288-
nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
288+
nodes[0].messenger.send_onion_message(path, test_msg.clone(), None).unwrap();
289289
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
290290
pass_along_path(&nodes);
291291

@@ -295,7 +295,7 @@ fn we_are_intro_node() {
295295
intermediate_nodes: vec![],
296296
destination: Destination::BlindedPath(blinded_path),
297297
};
298-
nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
298+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
299299
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
300300
nodes.remove(2);
301301
pass_along_path(&nodes);
@@ -315,7 +315,7 @@ fn invalid_blinded_path_error() {
315315
intermediate_nodes: vec![],
316316
destination: Destination::BlindedPath(blinded_path),
317317
};
318-
let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
318+
let err = nodes[0].messenger.send_onion_message(path, test_msg.clone(), None).unwrap_err();
319319
assert_eq!(err, SendError::TooFewBlindedHops);
320320

321321
// 1 hop
@@ -326,7 +326,7 @@ fn invalid_blinded_path_error() {
326326
intermediate_nodes: vec![],
327327
destination: Destination::BlindedPath(blinded_path),
328328
};
329-
let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
329+
let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
330330
assert_eq!(err, SendError::TooFewBlindedHops);
331331
}
332332

@@ -342,7 +342,7 @@ fn reply_path() {
342342
destination: Destination::Node(nodes[3].get_node_pk()),
343343
};
344344
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
345-
nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
345+
nodes[0].messenger.send_onion_message(path, test_msg.clone(), Some(reply_path)).unwrap();
346346
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
347347
pass_along_path(&nodes);
348348
// Make sure the last node successfully decoded the reply path.
@@ -358,7 +358,7 @@ fn reply_path() {
358358
};
359359
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
360360

361-
nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
361+
nodes[0].messenger.send_onion_message(path, test_msg, Some(reply_path)).unwrap();
362362
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
363363
pass_along_path(&nodes);
364364

@@ -373,7 +373,7 @@ fn invalid_custom_message_type() {
373373
let nodes = create_nodes(2);
374374

375375
struct InvalidCustomMessage{}
376-
impl CustomOnionMessageContents for InvalidCustomMessage {
376+
impl OnionMessageContents for InvalidCustomMessage {
377377
fn tlv_type(&self) -> u64 {
378378
// Onion message contents must have a TLV >= 64.
379379
63
@@ -384,7 +384,7 @@ fn invalid_custom_message_type() {
384384
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
385385
}
386386

387-
let test_msg = ParsedOnionMessageContents::Custom(InvalidCustomMessage {});
387+
let test_msg = InvalidCustomMessage {};
388388
let path = OnionMessagePath {
389389
intermediate_nodes: vec![],
390390
destination: Destination::Node(nodes[1].get_node_pk()),
@@ -402,9 +402,9 @@ fn peer_buffer_full() {
402402
destination: Destination::Node(nodes[1].get_node_pk()),
403403
};
404404
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
405-
nodes[0].messenger.send_onion_message(path.clone(), ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
405+
nodes[0].messenger.send_onion_message(path.clone(), test_msg.clone(), None).unwrap();
406406
}
407-
let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
407+
let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
408408
assert_eq!(err, SendError::BufferFull);
409409
}
410410

@@ -425,7 +425,7 @@ fn many_hops() {
425425
intermediate_nodes,
426426
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
427427
};
428-
nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
428+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
429429
nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
430430
pass_along_path(&nodes);
431431
}

0 commit comments

Comments
 (0)