@@ -243,50 +243,62 @@ impl OnionMessageRecipient {
243
243
}
244
244
}
245
245
246
+ /// A struct handling response to an [`OnionMessage`]
246
247
pub struct Responder < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > {
247
248
messenger : & ' a OMH ,
248
- pub message : T ,
249
249
pub reply_path : BlindedPath ,
250
- pub path_id : Option < [ u8 ; 32 ] >
250
+ pub path_id : Option < [ u8 ; 32 ] > ,
251
+ // This phantom Data is used to ensure that we use T in the struct definition
252
+ // This allow us to ensure at compile time that the received message type and response type will be same
253
+ _phantom : std:: marker:: PhantomData < T >
251
254
}
252
255
253
256
impl < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > Responder < ' a , OMH , T > {
254
257
pub fn respond ( & self , response : T ) {
258
+ // Utilising the fact that we ensure at compile time that
259
+ // received message type, and response type will be same
260
+ let message_type = T :: msg_type ( & response) ;
255
261
self . messenger . handle_onion_message_response (
256
262
response, self . reply_path . clone ( ) , format_args ! (
257
263
"when responding to {} onion message with path_id {:02x?}" ,
258
- self . message . msg_type ( ) ,
264
+ message_type ,
259
265
self . path_id. clone( )
260
266
)
261
267
) ;
262
268
}
263
269
}
264
270
265
- pub struct MessageWithId < T : OnionMessageContents > {
266
- pub message : T ,
267
- pub path_id : Option < [ u8 ; 32 ] > ,
268
- }
269
-
271
+ /// A enum to handle received [`OnionMessage`]
270
272
pub enum ReceivedOnionMessage < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > {
271
- WithReplyPath ( Responder < ' a , OMH , T > ) ,
272
- WithoutReplyPath ( MessageWithId < T > ) ,
273
+ WithReplyPath {
274
+ message : T ,
275
+ responder : Responder < ' a , OMH , T > ,
276
+ } ,
277
+ WithoutReplyPath {
278
+ message : T ,
279
+ path_id : Option < [ u8 ; 32 ] > ,
280
+ } ,
273
281
}
274
282
275
283
impl < ' a , OMH : OnionMessageHandler , T : OnionMessageContents > ReceivedOnionMessage < ' a , OMH , T > {
276
284
fn new ( messenger : & ' a OMH , message : T , reply_path_option : Option < BlindedPath > , path_id : Option < [ u8 ; 32 ] > ) -> Self {
277
285
match reply_path_option {
278
286
Some ( reply_path) => {
279
- ReceivedOnionMessage :: WithReplyPath ( Responder {
287
+ let responder = Responder {
280
288
messenger,
281
- message,
282
289
reply_path,
283
- path_id
284
- } )
290
+ path_id,
291
+ _phantom : std:: marker:: PhantomData
292
+ } ;
293
+ ReceivedOnionMessage :: WithReplyPath {
294
+ message,
295
+ responder
296
+ }
285
297
}
286
- None => ReceivedOnionMessage :: WithoutReplyPath ( MessageWithId {
298
+ None => ReceivedOnionMessage :: WithoutReplyPath {
287
299
message,
288
300
path_id
289
- } )
301
+ }
290
302
}
291
303
}
292
304
}
@@ -548,7 +560,7 @@ pub trait CustomOnionMessageHandler {
548
560
/// Called with the custom message that was received, returning a response to send, if any.
549
561
///
550
562
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
551
- fn handle_custom_message < OMH : OnionMessageHandler > ( & self , received_onion_message : & ReceivedOnionMessage < OMH , Self :: CustomMessage > ) ;
563
+ fn handle_custom_message < OMH : OnionMessageHandler > ( & self , message : & ReceivedOnionMessage < OMH , Self :: CustomMessage > ) ;
552
564
553
565
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
554
566
/// message type is unknown.
@@ -953,12 +965,12 @@ where
953
965
954
966
match message {
955
967
ParsedOnionMessageContents :: Offers ( msg) => {
956
- let received_onion_message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
957
- self . offers_handler . handle_message ( & received_onion_message ) ;
968
+ let message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
969
+ self . offers_handler . handle_message ( & message ) ;
958
970
} ,
959
971
ParsedOnionMessageContents :: Custom ( msg) => {
960
- let received_onion_message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
961
- self . custom_handler . handle_custom_message ( & received_onion_message ) ;
972
+ let message = ReceivedOnionMessage :: new ( self , msg, reply_path, path_id) ;
973
+ self . custom_handler . handle_custom_message ( & message ) ;
962
974
} ,
963
975
}
964
976
} ,
0 commit comments