@@ -68,20 +68,31 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
68
68
}
69
69
70
70
#[ derive( Clone ) ]
71
- struct TestCustomMessage { }
71
+ enum TestCustomMessage {
72
+ Request ,
73
+ Response ,
74
+ }
72
75
73
- const CUSTOM_MESSAGE_TYPE : u64 = 4242 ;
74
- const CUSTOM_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
76
+ const CUSTOM_REQUEST_MESSAGE_TYPE : u64 = 4242 ;
77
+ const CUSTOM_RESPONSE_MESSAGE_TYPE : u64 = 4343 ;
78
+ const CUSTOM_REQUEST_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
79
+ const CUSTOM_RESPONSE_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 43 ; 32 ] ;
75
80
76
81
impl CustomOnionMessageContents for TestCustomMessage {
77
82
fn tlv_type ( & self ) -> u64 {
78
- CUSTOM_MESSAGE_TYPE
83
+ match self {
84
+ TestCustomMessage :: Request => CUSTOM_REQUEST_MESSAGE_TYPE ,
85
+ TestCustomMessage :: Response => CUSTOM_RESPONSE_MESSAGE_TYPE ,
86
+ }
79
87
}
80
88
}
81
89
82
90
impl Writeable for TestCustomMessage {
83
91
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
84
- Ok ( CUSTOM_MESSAGE_CONTENTS . write ( w) ?)
92
+ match self {
93
+ TestCustomMessage :: Request => Ok ( CUSTOM_REQUEST_MESSAGE_CONTENTS . write ( w) ?) ,
94
+ TestCustomMessage :: Response => Ok ( CUSTOM_RESPONSE_MESSAGE_CONTENTS . write ( w) ?) ,
95
+ }
85
96
}
86
97
}
87
98
@@ -108,17 +119,27 @@ impl Drop for TestCustomMessageHandler {
108
119
109
120
impl CustomOnionMessageHandler for TestCustomMessageHandler {
110
121
type CustomMessage = TestCustomMessage ;
111
- fn handle_custom_message ( & self , _msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
122
+ fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
112
123
self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
113
- None
124
+ match msg {
125
+ TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
126
+ TestCustomMessage :: Response => None ,
127
+ }
114
128
}
115
129
fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , DecodeError > where Self : Sized {
116
- if message_type == CUSTOM_MESSAGE_TYPE {
117
- let buf = read_to_end ( buffer) ?;
118
- assert_eq ! ( buf, CUSTOM_MESSAGE_CONTENTS ) ;
119
- return Ok ( Some ( TestCustomMessage { } ) )
130
+ match message_type {
131
+ CUSTOM_REQUEST_MESSAGE_TYPE => {
132
+ let buf = read_to_end ( buffer) ?;
133
+ assert_eq ! ( buf, CUSTOM_REQUEST_MESSAGE_CONTENTS ) ;
134
+ Ok ( Some ( TestCustomMessage :: Request ) )
135
+ } ,
136
+ CUSTOM_RESPONSE_MESSAGE_TYPE => {
137
+ let buf = read_to_end ( buffer) ?;
138
+ assert_eq ! ( buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS ) ;
139
+ Ok ( Some ( TestCustomMessage :: Response ) )
140
+ } ,
141
+ _ => Ok ( None ) ,
120
142
}
121
- Ok ( None )
122
143
}
123
144
}
124
145
@@ -176,7 +197,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
176
197
#[ test]
177
198
fn one_hop ( ) {
178
199
let nodes = create_nodes ( 2 ) ;
179
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
200
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
180
201
181
202
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
182
203
pass_along_path ( & nodes) ;
@@ -185,7 +206,7 @@ fn one_hop() {
185
206
#[ test]
186
207
fn two_unblinded_hops ( ) {
187
208
let nodes = create_nodes ( 3 ) ;
188
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
209
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
189
210
190
211
nodes[ 0 ] . messenger . send_onion_message ( & [ nodes[ 1 ] . get_node_pk ( ) ] , Destination :: Node ( nodes[ 2 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
191
212
pass_along_path ( & nodes) ;
@@ -194,7 +215,7 @@ fn two_unblinded_hops() {
194
215
#[ test]
195
216
fn two_unblinded_two_blinded ( ) {
196
217
let nodes = create_nodes ( 5 ) ;
197
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
218
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
198
219
199
220
let secp_ctx = Secp256k1 :: new ( ) ;
200
221
let blinded_path = BlindedPath :: new_for_message ( & [ nodes[ 3 ] . get_node_pk ( ) , nodes[ 4 ] . get_node_pk ( ) ] , & * nodes[ 4 ] . keys_manager , & secp_ctx) . unwrap ( ) ;
@@ -206,7 +227,7 @@ fn two_unblinded_two_blinded() {
206
227
#[ test]
207
228
fn three_blinded_hops ( ) {
208
229
let nodes = create_nodes ( 4 ) ;
209
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
230
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
210
231
211
232
let secp_ctx = Secp256k1 :: new ( ) ;
212
233
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 ( ) ;
@@ -219,7 +240,7 @@ fn three_blinded_hops() {
219
240
fn too_big_packet_error ( ) {
220
241
// Make sure we error as expected if a packet is too big to send.
221
242
let nodes = create_nodes ( 2 ) ;
222
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
243
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
223
244
224
245
let hop_node_id = nodes[ 1 ] . get_node_pk ( ) ;
225
246
let hops = [ hop_node_id; 400 ] ;
@@ -232,7 +253,7 @@ fn we_are_intro_node() {
232
253
// If we are sending straight to a blinded path and we are the introduction node, we need to
233
254
// advance the blinded path by 1 hop so the second hop is the new introduction node.
234
255
let mut nodes = create_nodes ( 3 ) ;
235
- let test_msg = TestCustomMessage { } ;
256
+ let test_msg = TestCustomMessage :: Response ;
236
257
237
258
let secp_ctx = Secp256k1 :: new ( ) ;
238
259
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 ( ) ;
@@ -251,7 +272,7 @@ fn we_are_intro_node() {
251
272
fn invalid_blinded_path_error ( ) {
252
273
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
253
274
let nodes = create_nodes ( 3 ) ;
254
- let test_msg = TestCustomMessage { } ;
275
+ let test_msg = TestCustomMessage :: Response ;
255
276
256
277
// 0 hops
257
278
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -271,7 +292,7 @@ fn invalid_blinded_path_error() {
271
292
#[ test]
272
293
fn reply_path ( ) {
273
294
let nodes = create_nodes ( 4 ) ;
274
- let test_msg = TestCustomMessage { } ;
295
+ let test_msg = TestCustomMessage :: Response ;
275
296
let secp_ctx = Secp256k1 :: new ( ) ;
276
297
277
298
// Destination::Node
@@ -318,7 +339,7 @@ fn invalid_custom_message_type() {
318
339
#[ test]
319
340
fn peer_buffer_full ( ) {
320
341
let nodes = create_nodes ( 2 ) ;
321
- let test_msg = TestCustomMessage { } ;
342
+ let test_msg = TestCustomMessage :: Response ;
322
343
for _ in 0 ..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
323
344
nodes[ 0 ] . messenger . send_onion_message ( & [ ] , Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg. clone ( ) ) , None ) . unwrap ( ) ;
324
345
}
@@ -332,13 +353,13 @@ fn many_hops() {
332
353
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
333
354
let num_nodes: usize = 25 ;
334
355
let nodes = create_nodes ( num_nodes as u8 ) ;
335
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
356
+ let test_msg = TestCustomMessage :: Response ;
336
357
337
358
let mut intermediates = vec ! [ ] ;
338
359
for i in 1 ..( num_nodes-1 ) {
339
360
intermediates. push ( nodes[ i] . get_node_pk ( ) ) ;
340
361
}
341
362
342
- nodes[ 0 ] . messenger . send_onion_message ( & intermediates, Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) , test_msg, None ) . unwrap ( ) ;
363
+ nodes[ 0 ] . messenger . send_onion_message ( & intermediates, Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) , OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
343
364
pass_along_path ( & nodes) ;
344
365
}
0 commit comments