@@ -32,6 +32,7 @@ use crate::util::logger::Logger;
32
32
use crate :: util:: ser:: Writeable ;
33
33
34
34
use core:: fmt;
35
+ use core:: marker:: PhantomData ;
35
36
use core:: ops:: Deref ;
36
37
use crate :: io;
37
38
use crate :: sync:: Mutex ;
@@ -243,56 +244,79 @@ impl OnionMessageRecipient {
243
244
}
244
245
}
245
246
247
+ pub trait RespondFunction < T : OnionMessageContents > {
248
+ fn respond ( & self , response : T , reply_path : BlindedPath ) ;
249
+ }
250
+
251
+ impl < F , T > RespondFunction < T > for F
252
+ where
253
+ T : OnionMessageContents ,
254
+ F : Fn ( T , BlindedPath )
255
+ {
256
+ fn respond ( & self , response : T , reply_path : BlindedPath ) {
257
+ self ( response, reply_path)
258
+ }
259
+ }
260
+
246
261
/// A struct handling response to an [`OnionMessage`]
247
- pub struct Responder < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > {
248
- messenger : & ' a OMH ,
262
+ pub struct Responder < R , T >
263
+ where
264
+ R : RespondFunction < T > ,
265
+ T : OnionMessageContents
266
+ {
267
+ messenger_function : R ,
249
268
pub reply_path : BlindedPath ,
250
- pub path_id : Option < [ u8 ; 32 ] > ,
251
269
// This phantom Data is used to ensure that we use T in the struct definition
252
270
// This allow us to ensure at compile time that the received message type and response type will be same
253
- _phantom : core :: marker :: PhantomData < T >
271
+ _phantom : PhantomData < T >
254
272
}
255
273
256
- impl < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > Responder < ' a , OMH , T > {
274
+ impl < R , T > Responder < R , T >
275
+ where
276
+ R : RespondFunction < T > ,
277
+ T : OnionMessageContents
278
+ {
257
279
pub fn respond ( & self , response : T ) {
258
280
// Utilising the fact that we ensure at compile time that
259
281
// received message type, and response type will be same
260
- let message_type = T :: msg_type ( & response) ;
261
- self . messenger . handle_onion_message_response (
262
- response, self . reply_path . clone ( ) , format_args ! (
263
- "when responding to {} onion message with path_id {:02x?}" ,
264
- message_type,
265
- self . path_id
266
- )
267
- ) ;
268
- }
282
+ self . messenger_function . respond ( response, self . reply_path . clone ( ) )
283
+ }
269
284
}
270
285
271
286
/// A enum to handle received [`OnionMessage`]
272
- pub enum ReceivedOnionMessage < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > {
287
+ pub enum ReceivedOnionMessage < R , T >
288
+ where
289
+ R : RespondFunction < T > ,
290
+ T : OnionMessageContents
291
+ {
273
292
WithReplyPath {
293
+ responder : Responder < R , T > ,
274
294
message : T ,
275
- responder : Responder < ' a , OMH , T > ,
295
+ path_id : Option < [ u8 ; 32 ] >
276
296
} ,
277
297
WithoutReplyPath {
278
298
message : T ,
279
299
path_id : Option < [ u8 ; 32 ] > ,
280
300
} ,
281
301
}
282
302
283
- impl < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > ReceivedOnionMessage < ' a , OMH , T > {
284
- fn new ( messenger : & ' a OMH , message : T , reply_path_option : Option < BlindedPath > , path_id : Option < [ u8 ; 32 ] > ) -> Self {
303
+ impl < R , T > ReceivedOnionMessage < R , T >
304
+ where
305
+ R : RespondFunction < T > ,
306
+ T : OnionMessageContents
307
+ {
308
+ fn new ( messenger_function : R , message : T , reply_path_option : Option < BlindedPath > , path_id : Option < [ u8 ; 32 ] > ) -> Self {
285
309
match reply_path_option {
286
310
Some ( reply_path) => {
287
311
let responder = Responder {
288
- messenger ,
312
+ messenger_function ,
289
313
reply_path,
290
- path_id,
291
- _phantom : std:: marker:: PhantomData
314
+ _phantom : PhantomData
292
315
} ;
293
316
ReceivedOnionMessage :: WithReplyPath {
317
+ responder,
294
318
message,
295
- responder
319
+ path_id
296
320
}
297
321
}
298
322
None => ReceivedOnionMessage :: WithoutReplyPath {
@@ -560,7 +584,7 @@ pub trait CustomOnionMessageHandler {
560
584
/// Called with the custom message that was received, returning a response to send, if any.
561
585
///
562
586
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
563
- fn handle_custom_message < OMH : OnionMessageHandler > ( & self , message : & ReceivedOnionMessage < OMH , Self :: CustomMessage > ) ;
587
+ fn handle_custom_message < F : Fn ( Self :: CustomMessage , BlindedPath ) > ( & self , message : & ReceivedOnionMessage < F , Self :: CustomMessage > ) ;
564
588
565
589
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
566
590
/// message type is unknown.
@@ -965,11 +989,35 @@ where
965
989
966
990
match message {
967
991
ParsedOnionMessageContents :: Offers ( msg) => {
968
- let message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
992
+ // message_type is defined outside of closure to avoid burrowing
993
+ // issues when using msg for creating ReceivedOnionMessage::New()
994
+ let message_type = msg. msg_type ( ) ;
995
+ let closure = |response, reply_path : BlindedPath | {
996
+ self . handle_onion_message_response (
997
+ response, reply_path, format_args ! (
998
+ "when responding to {} onion message with path_id {:02x?}" ,
999
+ message_type,
1000
+ path_id
1001
+ )
1002
+ ) ;
1003
+ } ;
1004
+ let message = ReceivedOnionMessage :: new ( closure, msg, reply_path, path_id) ;
969
1005
self . offers_handler . handle_message ( & message) ;
970
1006
} ,
971
1007
ParsedOnionMessageContents :: Custom ( msg) => {
972
- let message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
1008
+ // message_type is defined outside of closure to avoid burrowing
1009
+ // issues when using msg for creating ReceivedOnionMessage::New()
1010
+ let message_type = msg. msg_type ( ) ;
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) ;
973
1021
self . custom_handler . handle_custom_message ( & message) ;
974
1022
} ,
975
1023
}
0 commit comments