@@ -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
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
184
205
pass_along_path ( & nodes) ;
@@ -187,7 +208,7 @@ fn one_hop() {
187
208
#[ test]
188
209
fn two_unblinded_hops ( ) {
189
210
let nodes = create_nodes ( 3 ) ;
190
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
211
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
191
212
192
213
nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
193
214
pass_along_path ( & nodes) ;
@@ -196,7 +217,7 @@ fn two_unblinded_hops() {
196
217
#[ test]
197
218
fn two_unblinded_two_blinded ( ) {
198
219
let nodes = create_nodes ( 5 ) ;
199
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
220
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
200
221
201
222
let secp_ctx = Secp256k1 :: new ( ) ;
202
223
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 3 ] . get_node_pk ( ) , nodes[ 4 ] . get_node_pk ( ) ] , & * nodes[ 4 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
@@ -208,7 +229,7 @@ fn two_unblinded_two_blinded() {
208
229
#[ test]
209
230
fn three_blinded_hops ( ) {
210
231
let nodes = create_nodes ( 4 ) ;
211
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
232
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
212
233
213
234
let secp_ctx = Secp256k1 :: new ( ) ;
214
235
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 ( ) ;
@@ -221,7 +242,7 @@ fn three_blinded_hops() {
221
242
fn too_big_packet_error ( ) {
222
243
// Make sure we error as expected if a packet is too big to send.
223
244
let nodes = create_nodes ( 2 ) ;
224
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
245
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
225
246
226
247
let hop_node_id = nodes[ 1 ] . get_node_pk ( ) ;
227
248
let hops = [ hop_node_id; 400 ] ;
@@ -234,7 +255,7 @@ fn we_are_intro_node() {
234
255
// If we are sending straight to a blinded path and we are the introduction node, we need to
235
256
// advance the blinded path by 1 hop so the second hop is the new introduction node.
236
257
let mut nodes = create_nodes ( 3 ) ;
237
- let test_msg = TestCustomMessage { } ;
258
+ let test_msg = TestCustomMessage :: Response ;
238
259
239
260
let secp_ctx = Secp256k1 :: new ( ) ;
240
261
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 ( ) ;
@@ -253,7 +274,7 @@ fn we_are_intro_node() {
253
274
fn invalid_blinded_path_error ( ) {
254
275
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
255
276
let nodes = create_nodes ( 3 ) ;
256
- let test_msg = TestCustomMessage { } ;
277
+ let test_msg = TestCustomMessage :: Response ;
257
278
258
279
// 0 hops
259
280
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -273,7 +294,7 @@ fn invalid_blinded_path_error() {
273
294
#[ test]
274
295
fn reply_path ( ) {
275
296
let nodes = create_nodes ( 4 ) ;
276
- let test_msg = TestCustomMessage { } ;
297
+ let test_msg = TestCustomMessage :: Response ;
277
298
let secp_ctx = Secp256k1 :: new ( ) ;
278
299
279
300
// Destination::Node
@@ -320,7 +341,7 @@ fn invalid_custom_message_type() {
320
341
#[ test]
321
342
fn peer_buffer_full ( ) {
322
343
let nodes = create_nodes ( 2 ) ;
323
- let test_msg = TestCustomMessage { } ;
344
+ let test_msg = TestCustomMessage :: Response ;
324
345
for _ in 0 ..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
325
346
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
326
347
}
@@ -334,13 +355,13 @@ fn many_hops() {
334
355
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
335
356
let num_nodes: usize = 25 ;
336
357
let nodes = create_nodes ( num_nodes as u8 ) ;
337
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
358
+ let test_msg = TestCustomMessage :: Response ;
338
359
339
360
let mut intermediates = vec ! [ ] ;
340
361
for i in 1 ..( num_nodes-1 ) {
341
362
intermediates. push ( nodes[ i] . get_node_pk ( ) ) ;
342
363
}
343
364
344
- nodes[ 0 ] . messenger . send_onion_message ( & intermediates, Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
365
+ nodes[ 0 ] . messenger . send_onion_message ( & intermediates, Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
345
366
pass_along_path ( & nodes) ;
346
367
}
0 commit comments