Skip to content

Commit f15da3d

Browse files
committed
Add OnionMessagePath wrapper struct
To avoid confusion in the upcoming MessageRouter trait, introduce an OnionMessagePath struct that wraps the intermediate nodes and the destination. Use this in OnionMessenger::send_onion_message.
1 parent f521e4c commit f15da3d

File tree

3 files changed

+106
-28
lines changed

3 files changed

+106
-28
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::blinded_path::BlindedPath;
1313
use crate::sign::{NodeSigner, Recipient};
1414
use crate::ln::features::InitFeatures;
1515
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
16-
use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessenger, SendError};
16+
use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
1717
use crate::util::ser::{Writeable, Writer};
1818
use crate::util::test_utils;
1919

@@ -146,7 +146,11 @@ fn one_hop() {
146146
let nodes = create_nodes(2);
147147
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
148148

149-
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
149+
let path = OnionMessagePath {
150+
intermediate_nodes: vec![],
151+
destination: Destination::Node(nodes[1].get_node_pk()),
152+
};
153+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
150154
pass_along_path(&nodes);
151155
}
152156

@@ -155,7 +159,11 @@ fn two_unblinded_hops() {
155159
let nodes = create_nodes(3);
156160
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
157161

158-
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
162+
let path = OnionMessagePath {
163+
intermediate_nodes: vec![nodes[1].get_node_pk()],
164+
destination: Destination::Node(nodes[2].get_node_pk()),
165+
};
166+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
159167
pass_along_path(&nodes);
160168
}
161169

@@ -166,8 +174,12 @@ fn two_unblinded_two_blinded() {
166174

167175
let secp_ctx = Secp256k1::new();
168176
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
177+
let path = OnionMessagePath {
178+
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
179+
destination: Destination::BlindedPath(blinded_path),
180+
};
169181

170-
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
182+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
171183
pass_along_path(&nodes);
172184
}
173185

@@ -178,8 +190,12 @@ fn three_blinded_hops() {
178190

179191
let secp_ctx = Secp256k1::new();
180192
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();
193+
let path = OnionMessagePath {
194+
intermediate_nodes: vec![],
195+
destination: Destination::BlindedPath(blinded_path),
196+
};
181197

182-
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
198+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
183199
pass_along_path(&nodes);
184200
}
185201

@@ -190,8 +206,12 @@ fn too_big_packet_error() {
190206
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
191207

192208
let hop_node_id = nodes[1].get_node_pk();
193-
let hops = [hop_node_id; 400];
194-
let err = nodes[0].messenger.send_onion_message(&hops, Destination::Node(hop_node_id), test_msg, None).unwrap_err();
209+
let hops = vec![hop_node_id; 400];
210+
let path = OnionMessagePath {
211+
intermediate_nodes: hops,
212+
destination: Destination::Node(hop_node_id),
213+
};
214+
let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
195215
assert_eq!(err, SendError::TooBigPacket);
196216
}
197217

@@ -204,13 +224,21 @@ fn we_are_intro_node() {
204224

205225
let secp_ctx = Secp256k1::new();
206226
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
227+
let path = OnionMessagePath {
228+
intermediate_nodes: vec![],
229+
destination: Destination::BlindedPath(blinded_path),
230+
};
207231

208-
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
232+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
209233
pass_along_path(&nodes);
210234

211235
// Try with a two-hop blinded path where we are the introduction node.
212236
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
213-
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
237+
let path = OnionMessagePath {
238+
intermediate_nodes: vec![],
239+
destination: Destination::BlindedPath(blinded_path),
240+
};
241+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
214242
nodes.remove(2);
215243
pass_along_path(&nodes);
216244
}
@@ -225,14 +253,22 @@ fn invalid_blinded_path_error() {
225253
let secp_ctx = Secp256k1::new();
226254
let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
227255
blinded_path.blinded_hops.clear();
228-
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
256+
let path = OnionMessagePath {
257+
intermediate_nodes: vec![],
258+
destination: Destination::BlindedPath(blinded_path),
259+
};
260+
let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
229261
assert_eq!(err, SendError::TooFewBlindedHops);
230262

231263
// 1 hop
232264
let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
233265
blinded_path.blinded_hops.remove(0);
234266
assert_eq!(blinded_path.blinded_hops.len(), 1);
235-
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap_err();
267+
let path = OnionMessagePath {
268+
intermediate_nodes: vec![],
269+
destination: Destination::BlindedPath(blinded_path),
270+
};
271+
let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
236272
assert_eq!(err, SendError::TooFewBlindedHops);
237273
}
238274

@@ -243,8 +279,12 @@ fn reply_path() {
243279
let secp_ctx = Secp256k1::new();
244280

245281
// Destination::Node
282+
let path = OnionMessagePath {
283+
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
284+
destination: Destination::Node(nodes[3].get_node_pk()),
285+
};
246286
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();
247-
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
287+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
248288
pass_along_path(&nodes);
249289
// Make sure the last node successfully decoded the reply path.
250290
nodes[3].logger.assert_log_contains(
@@ -253,9 +293,13 @@ fn reply_path() {
253293

254294
// Destination::BlindedPath
255295
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();
296+
let path = OnionMessagePath {
297+
intermediate_nodes: vec![],
298+
destination: Destination::BlindedPath(blinded_path),
299+
};
256300
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();
257301

258-
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
302+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
259303
pass_along_path(&nodes);
260304
nodes[3].logger.assert_log_contains(
261305
"lightning::onion_message::messenger",
@@ -279,18 +323,26 @@ fn invalid_custom_message_type() {
279323
}
280324

281325
let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
282-
let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap_err();
326+
let path = OnionMessagePath {
327+
intermediate_nodes: vec![],
328+
destination: Destination::Node(nodes[1].get_node_pk()),
329+
};
330+
let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
283331
assert_eq!(err, SendError::InvalidMessage);
284332
}
285333

286334
#[test]
287335
fn peer_buffer_full() {
288336
let nodes = create_nodes(2);
289337
let test_msg = TestCustomMessage {};
338+
let path = OnionMessagePath {
339+
intermediate_nodes: vec![],
340+
destination: Destination::Node(nodes[1].get_node_pk()),
341+
};
290342
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
291-
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
343+
nodes[0].messenger.send_onion_message(path.clone(), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
292344
}
293-
let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap_err();
345+
let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
294346
assert_eq!(err, SendError::BufferFull);
295347
}
296348

@@ -302,11 +354,15 @@ fn many_hops() {
302354
let nodes = create_nodes(num_nodes as u8);
303355
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
304356

305-
let mut intermediates = vec![];
357+
let mut intermediate_nodes = vec![];
306358
for i in 1..(num_nodes-1) {
307-
intermediates.push(nodes[i].get_node_pk());
359+
intermediate_nodes.push(nodes[i].get_node_pk());
308360
}
309361

310-
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), test_msg, None).unwrap();
362+
let path = OnionMessagePath {
363+
intermediate_nodes,
364+
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
365+
};
366+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
311367
pass_along_path(&nodes);
312368
}

lightning/src/onion_message/messenger.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::prelude::*;
4646
/// # use lightning::blinded_path::BlindedPath;
4747
/// # use lightning::sign::KeysManager;
4848
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
49-
/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
49+
/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessagePath, OnionMessenger};
5050
/// # use lightning::util::logger::{Logger, Record};
5151
/// # use lightning::util::ser::{Writeable, Writer};
5252
/// # use lightning::io;
@@ -84,23 +84,29 @@ use crate::prelude::*;
8484
/// }
8585
/// }
8686
/// // Send a custom onion message to a node id.
87-
/// let intermediate_hops = [hop_node_id1, hop_node_id2];
87+
/// let path = OnionMessagePath {
88+
/// intermediate_nodes: vec![hop_node_id1, hop_node_id2],
89+
/// destination: Destination::Node(destination_node_id),
90+
/// };
8891
/// let reply_path = None;
8992
/// # let your_custom_message = YourCustomMessage {};
9093
/// let message = OnionMessageContents::Custom(your_custom_message);
91-
/// onion_messenger.send_onion_message(&intermediate_hops, Destination::Node(destination_node_id), message, reply_path);
94+
/// onion_messenger.send_onion_message(path, message, reply_path);
9295
///
9396
/// // Create a blinded path to yourself, for someone to send an onion message to.
9497
/// # let your_node_id = hop_node_id1;
9598
/// let hops = [hop_node_id3, hop_node_id4, your_node_id];
9699
/// let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
97100
///
98101
/// // Send a custom onion message to a blinded path.
99-
/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
102+
/// let path = OnionMessagePath {
103+
/// intermediate_nodes: vec![hop_node_id1, hop_node_id2],
104+
/// destination: Destination::BlindedPath(blinded_path),
105+
/// };
100106
/// let reply_path = None;
101107
/// # let your_custom_message = YourCustomMessage {};
102108
/// let message = OnionMessageContents::Custom(your_custom_message);
103-
/// onion_messenger.send_onion_message(&intermediate_hops, Destination::BlindedPath(blinded_path), message, reply_path);
109+
/// onion_messenger.send_onion_message(path, message, reply_path);
104110
/// ```
105111
///
106112
/// [offers]: <https://github.com/lightning/bolts/pull/798>
@@ -122,7 +128,18 @@ where
122128
custom_handler: CMH,
123129
}
124130

131+
/// A path for sending an [`msgs::OnionMessage`].
132+
#[derive(Clone)]
133+
pub struct OnionMessagePath {
134+
/// Nodes on the path between the sender and the destination.
135+
pub intermediate_nodes: Vec<PublicKey>,
136+
137+
/// The recipient of the message.
138+
pub destination: Destination,
139+
}
140+
125141
/// The destination of an onion message.
142+
#[derive(Clone)]
126143
pub enum Destination {
127144
/// We're sending this onion message to a node.
128145
Node(PublicKey),
@@ -216,9 +233,14 @@ where
216233
}
217234
}
218235

219-
/// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
236+
/// Send an onion message with contents `message` to the destination of `path`.
237+
///
220238
/// See [`OnionMessenger`] for example usage.
221-
pub fn send_onion_message<T: CustomOnionMessageContents>(&self, intermediate_nodes: &[PublicKey], mut destination: Destination, message: OnionMessageContents<T>, reply_path: Option<BlindedPath>) -> Result<(), SendError> {
239+
pub fn send_onion_message<T: CustomOnionMessageContents>(
240+
&self, path: OnionMessagePath, message: OnionMessageContents<T>,
241+
reply_path: Option<BlindedPath>
242+
) -> Result<(), SendError> {
243+
let OnionMessagePath { intermediate_nodes, mut destination } = path;
222244
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
223245
if blinded_hops.len() < 2 {
224246
return Err(SendError::TooFewBlindedHops);
@@ -252,7 +274,7 @@ where
252274
}
253275
};
254276
let (packet_payloads, packet_keys) = packet_payloads_and_keys(
255-
&self.secp_ctx, intermediate_nodes, destination, message, reply_path, &blinding_secret)
277+
&self.secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
256278
.map_err(|e| SendError::Secp256k1(e))?;
257279

258280
let prng_seed = self.entropy_source.get_secure_random_bytes();

lightning/src/onion_message/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ mod packet;
2727
mod functional_tests;
2828

2929
// Re-export structs so they can be imported with just the `onion_message::` module prefix.
30-
pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
30+
pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
3131
pub use self::offers::{OffersMessage, OffersMessageHandler};
3232
pub(crate) use self::packet::{ControlTlvs, Packet};

0 commit comments

Comments
 (0)