@@ -64,20 +64,31 @@ impl OffersMessageHandler for TestOffersMessageHandler {
64
64
}
65
65
66
66
#[ derive( Clone ) ]
67
- struct TestCustomMessage { }
67
+ enum TestCustomMessage {
68
+ Request ,
69
+ Response ,
70
+ }
68
71
69
- const CUSTOM_MESSAGE_TYPE : u64 = 4242 ;
70
- const CUSTOM_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
72
+ const CUSTOM_REQUEST_MESSAGE_TYPE : u64 = 4242 ;
73
+ const CUSTOM_RESPONSE_MESSAGE_TYPE : u64 = 4343 ;
74
+ const CUSTOM_REQUEST_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 42 ; 32 ] ;
75
+ const CUSTOM_RESPONSE_MESSAGE_CONTENTS : [ u8 ; 32 ] = [ 43 ; 32 ] ;
71
76
72
77
impl CustomOnionMessageContents for TestCustomMessage {
73
78
fn tlv_type ( & self ) -> u64 {
74
- CUSTOM_MESSAGE_TYPE
79
+ match self {
80
+ TestCustomMessage :: Request => CUSTOM_REQUEST_MESSAGE_TYPE ,
81
+ TestCustomMessage :: Response => CUSTOM_RESPONSE_MESSAGE_TYPE ,
82
+ }
75
83
}
76
84
}
77
85
78
86
impl Writeable for TestCustomMessage {
79
87
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
80
- Ok ( CUSTOM_MESSAGE_CONTENTS . write ( w) ?)
88
+ match self {
89
+ TestCustomMessage :: Request => Ok ( CUSTOM_REQUEST_MESSAGE_CONTENTS . write ( w) ?) ,
90
+ TestCustomMessage :: Response => Ok ( CUSTOM_RESPONSE_MESSAGE_CONTENTS . write ( w) ?) ,
91
+ }
81
92
}
82
93
}
83
94
@@ -104,17 +115,27 @@ impl Drop for TestCustomMessageHandler {
104
115
105
116
impl CustomOnionMessageHandler for TestCustomMessageHandler {
106
117
type CustomMessage = TestCustomMessage ;
107
- fn handle_custom_message ( & self , _msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
118
+ fn handle_custom_message ( & self , msg : Self :: CustomMessage ) -> Option < Self :: CustomMessage > {
108
119
self . num_messages_expected . fetch_sub ( 1 , Ordering :: SeqCst ) ;
109
- None
120
+ match msg {
121
+ TestCustomMessage :: Request => Some ( TestCustomMessage :: Response ) ,
122
+ TestCustomMessage :: Response => None ,
123
+ }
110
124
}
111
125
fn read_custom_message < R : io:: Read > ( & self , message_type : u64 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , DecodeError > where Self : Sized {
112
- if message_type == CUSTOM_MESSAGE_TYPE {
113
- let buf = read_to_end ( buffer) ?;
114
- assert_eq ! ( buf, CUSTOM_MESSAGE_CONTENTS ) ;
115
- return Ok ( Some ( TestCustomMessage { } ) )
126
+ match message_type {
127
+ CUSTOM_REQUEST_MESSAGE_TYPE => {
128
+ let buf = read_to_end ( buffer) ?;
129
+ assert_eq ! ( buf, CUSTOM_REQUEST_MESSAGE_CONTENTS ) ;
130
+ Ok ( Some ( TestCustomMessage :: Request ) )
131
+ } ,
132
+ CUSTOM_RESPONSE_MESSAGE_TYPE => {
133
+ let buf = read_to_end ( buffer) ?;
134
+ assert_eq ! ( buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS ) ;
135
+ Ok ( Some ( TestCustomMessage :: Response ) )
136
+ } ,
137
+ _ => Ok ( None ) ,
116
138
}
117
- Ok ( None )
118
139
}
119
140
}
120
141
@@ -166,7 +187,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
166
187
#[ test]
167
188
fn one_hop ( ) {
168
189
let nodes = create_nodes ( 2 ) ;
169
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
190
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
170
191
171
192
let path = OnionMessagePath {
172
193
intermediate_nodes : vec ! [ ] ,
@@ -179,7 +200,7 @@ fn one_hop() {
179
200
#[ test]
180
201
fn two_unblinded_hops ( ) {
181
202
let nodes = create_nodes ( 3 ) ;
182
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
203
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
183
204
184
205
let path = OnionMessagePath {
185
206
intermediate_nodes : vec ! [ nodes[ 1 ] . get_node_pk( ) ] ,
@@ -192,7 +213,7 @@ fn two_unblinded_hops() {
192
213
#[ test]
193
214
fn two_unblinded_two_blinded ( ) {
194
215
let nodes = create_nodes ( 5 ) ;
195
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
216
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
196
217
197
218
let secp_ctx = Secp256k1 :: new ( ) ;
198
219
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 ( ) ;
@@ -225,7 +246,7 @@ fn three_blinded_hops() {
225
246
fn too_big_packet_error ( ) {
226
247
// Make sure we error as expected if a packet is too big to send.
227
248
let nodes = create_nodes ( 2 ) ;
228
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
249
+ let test_msg = OnionMessageContents :: Custom ( TestCustomMessage :: Response ) ;
229
250
230
251
let hop_node_id = nodes[ 1 ] . get_node_pk ( ) ;
231
252
let hops = vec ! [ hop_node_id; 400 ] ;
@@ -242,7 +263,7 @@ fn we_are_intro_node() {
242
263
// If we are sending straight to a blinded path and we are the introduction node, we need to
243
264
// advance the blinded path by 1 hop so the second hop is the new introduction node.
244
265
let mut nodes = create_nodes ( 3 ) ;
245
- let test_msg = TestCustomMessage { } ;
266
+ let test_msg = TestCustomMessage :: Response ;
246
267
247
268
let secp_ctx = Secp256k1 :: new ( ) ;
248
269
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 ( ) ;
@@ -269,7 +290,7 @@ fn we_are_intro_node() {
269
290
fn invalid_blinded_path_error ( ) {
270
291
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
271
292
let nodes = create_nodes ( 3 ) ;
272
- let test_msg = TestCustomMessage { } ;
293
+ let test_msg = TestCustomMessage :: Response ;
273
294
274
295
// 0 hops
275
296
let secp_ctx = Secp256k1 :: new ( ) ;
@@ -297,7 +318,7 @@ fn invalid_blinded_path_error() {
297
318
#[ test]
298
319
fn reply_path ( ) {
299
320
let nodes = create_nodes ( 4 ) ;
300
- let test_msg = TestCustomMessage { } ;
321
+ let test_msg = TestCustomMessage :: Response ;
301
322
let secp_ctx = Secp256k1 :: new ( ) ;
302
323
303
324
// Destination::Node
@@ -356,7 +377,7 @@ fn invalid_custom_message_type() {
356
377
#[ test]
357
378
fn peer_buffer_full ( ) {
358
379
let nodes = create_nodes ( 2 ) ;
359
- let test_msg = TestCustomMessage { } ;
380
+ let test_msg = TestCustomMessage :: Response ;
360
381
let path = OnionMessagePath {
361
382
intermediate_nodes : vec ! [ ] ,
362
383
destination : Destination :: Node ( nodes[ 1 ] . get_node_pk ( ) ) ,
@@ -374,7 +395,7 @@ fn many_hops() {
374
395
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
375
396
let num_nodes: usize = 25 ;
376
397
let nodes = create_nodes ( num_nodes as u8 ) ;
377
- let test_msg = OnionMessageContents :: Custom ( TestCustomMessage { } ) ;
398
+ let test_msg = TestCustomMessage :: Response ;
378
399
379
400
let mut intermediate_nodes = vec ! [ ] ;
380
401
for i in 1 ..( num_nodes-1 ) {
@@ -385,6 +406,6 @@ fn many_hops() {
385
406
intermediate_nodes,
386
407
destination : Destination :: Node ( nodes[ num_nodes-1 ] . get_node_pk ( ) ) ,
387
408
} ;
388
- nodes[ 0 ] . messenger . send_onion_message ( path, test_msg, None ) . unwrap ( ) ;
409
+ nodes[ 0 ] . messenger . send_onion_message ( path, OnionMessageContents :: Custom ( test_msg) , None ) . unwrap ( ) ;
389
410
pass_along_path ( & nodes) ;
390
411
}
0 commit comments