@@ -245,31 +245,28 @@ impl OnionMessageRecipient {
245
245
}
246
246
247
247
/// A enum to handle received [`OnionMessage`]
248
- pub enum ReceivedOnionMessage < R , T >
248
+ pub enum ReceivedOnionMessage < T >
249
249
where
250
- R : RespondFunction < T > ,
251
250
T : OnionMessageContents
252
251
{
253
252
WithReplyPath {
254
253
message : T ,
255
254
path_id : Option < [ u8 ; 32 ] > ,
256
- responder : Responder < R , T > ,
255
+ responder : Responder < T > ,
257
256
} ,
258
257
WithoutReplyPath {
259
258
message : T ,
260
259
path_id : Option < [ u8 ; 32 ] > ,
261
260
} ,
262
261
}
263
262
264
- impl < R , T > ReceivedOnionMessage < R , T >
263
+ impl < T > ReceivedOnionMessage < T >
265
264
where
266
- R : RespondFunction < T > ,
267
265
T : OnionMessageContents
268
266
{
269
- fn new ( messenger_function : R , message : T , reply_path_opt : Option < BlindedPath > , path_id : Option < [ u8 ; 32 ] > ) -> Self {
267
+ fn new ( message : T , reply_path_opt : Option < BlindedPath > , path_id : Option < [ u8 ; 32 ] > ) -> Self {
270
268
if let Some ( reply_path) = reply_path_opt {
271
269
let responder = Responder {
272
- messenger_function,
273
270
reply_path,
274
271
_phantom : PhantomData
275
272
} ;
@@ -287,43 +284,38 @@ where
287
284
}
288
285
}
289
286
290
- pub trait RespondFunction < T : OnionMessageContents > {
291
- fn respond ( self , response : T , reply_path : BlindedPath ) ;
292
- }
293
-
294
- impl < F , T > RespondFunction < T > for F
295
- where
296
- F : Fn ( T , BlindedPath ) ,
297
- T : OnionMessageContents
298
- {
299
- fn respond ( self , response : T , reply_path : BlindedPath ) {
300
- self ( response, reply_path)
301
- }
302
- }
303
-
304
287
/// A struct handling response to an [`OnionMessage`]
305
- pub struct Responder < R , T >
288
+ pub struct Responder < T >
306
289
where
307
- R : RespondFunction < T > ,
308
290
T : OnionMessageContents
309
291
{
310
- messenger_function : R ,
311
292
reply_path : BlindedPath ,
293
+
312
294
// This phantom Data is used to ensure that we use T in the struct definition
313
295
// This allow us to ensure at compile time that the received message type and response type will be same
314
296
_phantom : PhantomData < T >
315
297
}
316
298
317
- impl < R , T > Responder < R , T >
299
+ impl < T > Responder < T >
318
300
where
319
- R : RespondFunction < T > ,
320
301
T : OnionMessageContents
321
302
{
322
- pub fn respond ( self , response : T ) {
323
- self . messenger_function . respond ( response, self . reply_path )
303
+ pub fn respond ( self , response : T ) -> ResponseInstruction < T > {
304
+ ResponseInstruction :: HaveResponse {
305
+ response,
306
+ reply_path : self . reply_path ,
307
+ }
324
308
}
325
309
}
326
310
311
+ pub enum ResponseInstruction < T : OnionMessageContents > {
312
+ HaveResponse {
313
+ response : T ,
314
+ reply_path : BlindedPath ,
315
+ } ,
316
+ NoResponse
317
+ }
318
+
327
319
/// An [`OnionMessage`] for [`OnionMessenger`] to send.
328
320
///
329
321
/// These are obtained when released from [`OnionMessenger`]'s handlers after which they are
@@ -581,7 +573,7 @@ pub trait CustomOnionMessageHandler {
581
573
/// Called with the custom message that was received, returning a response to send, if any.
582
574
///
583
575
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
584
- fn handle_custom_message < R : RespondFunction < Self :: CustomMessage > > ( & self , message : ReceivedOnionMessage < R , Self :: CustomMessage > ) ;
576
+ fn handle_custom_message ( & self , message : ReceivedOnionMessage < Self :: CustomMessage > ) -> ResponseInstruction < Self :: CustomMessage > ;
585
577
586
578
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
587
579
/// message type is unknown.
@@ -910,11 +902,13 @@ where
910
902
}
911
903
912
904
fn handle_onion_message_response < T : OnionMessageContents > (
913
- & self , response : T , reply_path : BlindedPath , log_suffix : fmt:: Arguments
905
+ & self , response : ResponseInstruction < T > , log_suffix : fmt:: Arguments
914
906
) {
915
- let _ = self . find_path_and_enqueue_onion_message (
916
- response, Destination :: BlindedPath ( reply_path) , None , log_suffix
917
- ) ;
907
+ if let ResponseInstruction :: HaveResponse { response, reply_path } = response {
908
+ let _ = self . find_path_and_enqueue_onion_message (
909
+ response, Destination :: BlindedPath ( reply_path) , None , log_suffix
910
+ ) ;
911
+ }
918
912
}
919
913
920
914
#[ cfg( test) ]
@@ -995,30 +989,22 @@ where
995
989
let message_type = message. msg_type ( ) ;
996
990
match message {
997
991
ParsedOnionMessageContents :: Offers ( msg) => {
998
- let closure = |response, reply_path : BlindedPath | {
999
- self . handle_onion_message_response (
1000
- response, reply_path, format_args ! (
1001
- "when responding to {} onion message with path_id {:02x?}" ,
1002
- message_type,
1003
- path_id
1004
- )
1005
- ) ;
1006
- } ;
1007
- let message = ReceivedOnionMessage :: new ( closure, msg, reply_path, path_id) ;
1008
- self . offers_handler . handle_message ( message) ;
992
+ let received_message = ReceivedOnionMessage :: new ( msg, reply_path, path_id) ;
993
+ let response_instructions = self . offers_handler . handle_message ( received_message) ;
994
+ self . handle_onion_message_response ( response_instructions, format_args ! (
995
+ "when responding to {} onion message with path_id {:02x?}" ,
996
+ message_type,
997
+ path_id
998
+ ) )
1009
999
} ,
1010
1000
ParsedOnionMessageContents :: Custom ( msg) => {
1011
- let closure = |response, reply_path : BlindedPath | {
1012
- self . handle_onion_message_response (
1013
- response, reply_path, format_args ! (
1014
- "when responding to {} onion message with path_id {:02x?}" ,
1015
- message_type,
1016
- path_id
1017
- )
1018
- ) ;
1019
- } ;
1020
- let message = ReceivedOnionMessage :: new ( closure, msg, reply_path, path_id) ;
1021
- self . custom_handler . handle_custom_message ( message) ;
1001
+ let message = ReceivedOnionMessage :: new ( msg, reply_path, path_id) ;
1002
+ let response_instructions = self . custom_handler . handle_custom_message ( message) ;
1003
+ self . handle_onion_message_response ( response_instructions, format_args ! (
1004
+ "when responding to {} onion message with path_id {:02x?}" ,
1005
+ message_type,
1006
+ path_id
1007
+ ) )
1022
1008
} ,
1023
1009
}
1024
1010
} ,
0 commit comments