@@ -70,20 +70,31 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
70
70
}
71
71
72
72
#[ derive( Clone ) ]
73
- struct TestCustomMessage { }
73
+ enum TestCustomMessage {
74
+ Request ,
75
+ Response ,
76
+ }
74
77
75
- const CUSTOM_MESSAGE_TYPE : u64 = 4242 ;
76
- const CUSTOM_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
78
+ const CUSTOM_REQUEST_MESSAGE_TYPE : u64 = 4242 ;
79
+ const CUSTOM_RESPONSE_MESSAGE_TYPE : u64 = 4343 ;
80
+ const CUSTOM_REQUEST_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
81
+ const CUSTOM_RESPONSE_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 43 ; 32 ] ;
77
82
78
83
impl CustomOnionMessageContents for TestCustomMessage {
79
84
fn tlv_type ( & self ) -> u64 {
80
- CUSTOM_MESSAGE_TYPE
85
+ match self {
86
+ TestCustomMessage :: Request => CUSTOM_REQUEST_MESSAGE_TYPE ,
87
+ TestCustomMessage :: Response => CUSTOM_RESPONSE_MESSAGE_TYPE ,
88
+ }
81
89
}
82
90
}
83
91
84
92
impl Writeable for TestCustomMessage {
85
93
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
86
- Ok ( CUSTOM_MESSAGE_CONTENTS . write ( w) ?)
94
+ match self {
95
+ TestCustomMessage :: Request => Ok ( CUSTOM_REQUEST_MESSAGE_CONTENTS . write ( w) ?) ,
96
+ TestCustomMessage :: Response => Ok ( CUSTOM_RESPONSE_MESSAGE_CONTENTS . write ( w) ?) ,
97
+ }
87
98
}
88
99
}
89
100
@@ -110,17 +121,27 @@ impl Drop for TestCustomMessageHandler {
110
121
111
122
impl CustomOnionMessageHandler for TestCustomMessageHandler {
112
123
type CustomMessage = TestCustomMessage ;
113
- fn handle_custom_message ( & self , _msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
124
+ fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
114
125
self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
115
- None
126
+ match msg {
127
+ TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
128
+ TestCustomMessage :: Response => None ,
129
+ }
116
130
}
117
131
fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , DecodeError > where Self : Sized {
118
- if message_type == CUSTOM_MESSAGE_TYPE {
119
- let buf = read_to_end ( buffer) ?;
120
- assert_eq ! ( buf, CUSTOM_MESSAGE_CONTENTS ) ;
121
- return Ok ( Some ( TestCustomMessage { } ) )
132
+ match message_type {
133
+ CUSTOM_REQUEST_MESSAGE_TYPE => {
134
+ let buf = read_to_end ( buffer) ?;
135
+ assert_eq ! ( buf, CUSTOM_REQUEST_MESSAGE_CONTENTS ) ;
136
+ Ok ( Some ( TestCustomMessage :: Request ) )
137
+ } ,
138
+ CUSTOM_RESPONSE_MESSAGE_TYPE => {
139
+ let buf = read_to_end ( buffer) ?;
140
+ assert_eq ! ( buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS ) ;
141
+ Ok ( Some ( TestCustomMessage :: Response ) )
142
+ } ,
143
+ _ => Ok ( None ) ,
122
144
}
123
- Ok ( None )
124
145
}
125
146
}
126
147
@@ -178,7 +199,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
178
199
#[ test]
179
200
fn one_hop ( ) {
180
201
let nodes = create_nodes ( 2 ) ;
181
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
202
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
182
203
183
204
let path = OnionMessagePath {
184
205
intermediate_nodes : vec ! [ ] ,
@@ -191,7 +212,7 @@ fn one_hop() {
191
212
#[ test]
192
213
fn two_unblinded_hops ( ) {
193
214
let nodes = create_nodes ( 3 ) ;
194
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
215
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
195
216
196
217
let path = OnionMessagePath {
197
218
intermediate_nodes : vec ! [ nodes[ 1 ] . get_node_pk( ) ] ,
@@ -204,7 +225,7 @@ fn two_unblinded_hops() {
204
225
#[ test]
205
226
fn two_unblinded_two_blinded ( ) {
206
227
let nodes = create_nodes ( 5 ) ;
207
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
228
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
208
229
209
230
let secp_ctx = Secp256k1 :: new ( ) ;
210
231
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 3 ] . get_node_pk ( ) , nodes[ 4 ] . get_node_pk ( ) ] , & * nodes[ 4 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
@@ -220,7 +241,7 @@ fn two_unblinded_two_blinded() {
220
241
#[ test]
221
242
fn three_blinded_hops ( ) {
222
243
let nodes = create_nodes ( 4 ) ;
223
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
244
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
224
245
225
246
let secp_ctx = Secp256k1 :: new ( ) ;
226
247
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 ( ) ;
@@ -237,7 +258,7 @@ fn three_blinded_hops() {
237
258
fn too_big_packet_error ( ) {
238
259
// Make sure we error as expected if a packet is too big to send.
239
260
let nodes = create_nodes ( 2 ) ;
240
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
261
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
241
262
242
263
let hop_node_id = nodes[ 1 ] . get_node_pk ( ) ;
243
264
let hops = vec ! [ hop_node_id; 400 ] ;
@@ -254,7 +275,7 @@ fn we_are_intro_node() {
254
275
// If we are sending straight to a blinded path and we are the introduction node, we need to
255
276
// advance the blinded path by 1 hop so the second hop is the new introduction node.
256
277
let mut nodes = create_nodes ( 3 ) ;
257
- let test_msg = TestCustomMessage { } ;
278
+ let test_msg = TestCustomMessage :: Response ;
258
279
259
280
let secp_ctx = Secp256k1 :: new ( ) ;
260
281
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 ( ) ;
@@ -281,7 +302,7 @@ fn we_are_intro_node() {
281
302
fn invalid_blinded_path_error ( ) {
282
303
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
283
304
let nodes = create_nodes ( 3 ) ;
284
- let test_msg = TestCustomMessage { } ;
305
+ let test_msg = TestCustomMessage :: Response ;
285
306
286
307
// 0 hops
287
308
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -309,7 +330,7 @@ fn invalid_blinded_path_error() {
309
330
#[ test]
310
331
fn reply_path ( ) {
311
332
let nodes = create_nodes ( 4 ) ;
312
- let test_msg = TestCustomMessage { } ;
333
+ let test_msg = TestCustomMessage :: Response ;
313
334
let secp_ctx = Secp256k1 :: new ( ) ;
314
335
315
336
// Destination::Node
@@ -368,7 +389,7 @@ fn invalid_custom_message_type() {
368
389
#[ test]
369
390
fn peer_buffer_full ( ) {
370
391
let nodes = create_nodes ( 2 ) ;
371
- let test_msg = TestCustomMessage { } ;
392
+ let test_msg = TestCustomMessage :: Response ;
372
393
let path = OnionMessagePath {
373
394
intermediate_nodes : vec ! [ ] ,
374
395
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
@@ -386,7 +407,7 @@ fn many_hops() {
386
407
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
387
408
let num_nodes: usize = 25 ;
388
409
let nodes = create_nodes ( num_nodes as u8 ) ;
389
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
410
+ let test_msg = TestCustomMessage :: Response ;
390
411
391
412
let mut intermediate_nodes = vec ! [ ] ;
392
413
for i in 1 ..( num_nodes-1 ) {
@@ -397,6 +418,6 @@ fn many_hops() {
397
418
intermediate_nodes,
398
419
destination : Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) ,
399
420
} ;
400
- nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
421
+ nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
401
422
pass_along_path ( & nodes) ;
402
423
}
0 commit comments