@@ -18,7 +18,7 @@ use common::log::{debug, info, LogOnError};
18
18
use common:: { executor:: SpawnFuture , log:: error} ;
19
19
use connection_handler:: Handler ;
20
20
use error:: WalletConnectError ;
21
- use futures:: channel:: mpsc:: { unbounded, UnboundedReceiver , UnboundedSender } ;
21
+ use futures:: channel:: mpsc:: { unbounded, UnboundedReceiver } ;
22
22
use futures:: lock:: Mutex ;
23
23
use futures:: StreamExt ;
24
24
use inbound_message:: { process_inbound_request, process_inbound_response, SessionMessageType } ;
@@ -39,12 +39,12 @@ use serde::de::DeserializeOwned;
39
39
use session:: rpc:: delete:: send_session_delete_request;
40
40
use session:: Session ;
41
41
use session:: { key:: SymKeyPair , SessionManager } ;
42
- use std:: collections:: BTreeSet ;
42
+ use std:: collections:: { BTreeSet , HashMap } ;
43
43
use std:: ops:: Deref ;
44
44
use std:: { sync:: Arc , time:: Duration } ;
45
45
use storage:: SessionStorageDb ;
46
46
use storage:: WalletConnectStorageOps ;
47
- use tokio:: time :: timeout ;
47
+ use tokio:: sync :: oneshot ;
48
48
use wc_common:: { decode_and_decrypt_type0, encrypt_and_encode, EnvelopeType , SymKey } ;
49
49
50
50
const PUBLISH_TIMEOUT_SECS : f64 = 6. ;
@@ -79,7 +79,7 @@ pub struct WalletConnectCtxImpl {
79
79
pub ( crate ) key_pair : SymKeyPair ,
80
80
relay : Relay ,
81
81
metadata : Metadata ,
82
- message_rx : Mutex < UnboundedReceiver < SessionMessageType > > ,
82
+ pending_requests : Mutex < HashMap < MessageId , oneshot :: Sender < SessionMessageType > > > ,
83
83
abortable_system : AbortableQueue ,
84
84
}
85
85
@@ -103,7 +103,6 @@ impl WalletConnectCtx {
103
103
} ;
104
104
let ( inbound_message_tx, mut inbound_message_rx) = unbounded ( ) ;
105
105
let ( conn_live_sender, conn_live_receiver) = unbounded ( ) ;
106
- let ( message_tx, message_rx) = unbounded ( ) ;
107
106
let ( client, _) = Client :: new_with_callback (
108
107
Handler :: new ( "Komodefi" , inbound_message_tx, conn_live_sender) ,
109
108
|r, h| abortable_system. weak_spawner ( ) . spawn ( client_event_loop ( r, h) ) ,
@@ -116,7 +115,7 @@ impl WalletConnectCtx {
116
115
metadata : generate_metadata ( ) ,
117
116
key_pair : SymKeyPair :: new ( ) ,
118
117
session_manager : SessionManager :: new ( storage) ,
119
- message_rx : message_rx . into ( ) ,
118
+ pending_requests : Default :: default ( ) ,
120
119
abortable_system,
121
120
} ) ;
122
121
@@ -130,7 +129,7 @@ impl WalletConnectCtx {
130
129
let context = context. clone ( ) ;
131
130
async move {
132
131
while let Some ( msg) = inbound_message_rx. next ( ) . await {
133
- if let Err ( e) = context. handle_published_message ( msg, message_tx . clone ( ) ) . await {
132
+ if let Err ( e) = context. handle_published_message ( msg) . await {
134
133
error ! ( "Error processing message: {:?}" , e) ;
135
134
}
136
135
}
@@ -272,11 +271,7 @@ impl WalletConnectCtxImpl {
272
271
}
273
272
274
273
/// Handles an inbound published message by decrypting, decoding, and processing it.
275
- async fn handle_published_message (
276
- & self ,
277
- msg : PublishedMessage ,
278
- message_tx : UnboundedSender < SessionMessageType > ,
279
- ) -> MmResult < ( ) , WalletConnectError > {
274
+ async fn handle_published_message ( & self , msg : PublishedMessage ) -> MmResult < ( ) , WalletConnectError > {
280
275
let message = {
281
276
let key = self . sym_key ( & msg. topic ) ?;
282
277
decode_and_decrypt_type0 ( msg. message . as_bytes ( ) , & key) ?
@@ -286,7 +281,7 @@ impl WalletConnectCtxImpl {
286
281
287
282
match serde_json:: from_str ( & message) ? {
288
283
Payload :: Request ( request) => process_inbound_request ( self , request, & msg. topic ) . await ?,
289
- Payload :: Response ( response) => process_inbound_response ( self , response, & msg. topic , message_tx ) . await ,
284
+ Payload :: Response ( response) => process_inbound_response ( self , response, & msg. topic ) . await ,
290
285
}
291
286
292
287
info ! ( "[{}] Inbound message was handled successfully" , msg. topic) ;
@@ -333,6 +328,7 @@ impl WalletConnectCtxImpl {
333
328
. collect :: < Vec < _ > > ( ) ;
334
329
335
330
if !all_topics. is_empty ( ) {
331
+ println ! ( "SUBBING: {all_topics:?}" ) ;
336
332
self . client . batch_subscribe ( all_topics) . await ?;
337
333
}
338
334
@@ -344,13 +340,20 @@ impl WalletConnectCtxImpl {
344
340
& self ,
345
341
topic : & Topic ,
346
342
param : RequestParams ,
347
- message_id : MessageId ,
348
- ) -> MmResult < ( ) , WalletConnectError > {
343
+ ) -> MmResult < ( oneshot:: Receiver < SessionMessageType > , Duration ) , WalletConnectError > {
349
344
let irn_metadata = param. irn_metadata ( ) ;
345
+ let ttl = irn_metadata. ttl ;
346
+ let message_id = MessageIdGenerator :: new ( ) . next ( ) ;
350
347
let request = Request :: new ( message_id, param. into ( ) ) ;
351
348
352
349
self . publish_payload ( topic, irn_metadata, Payload :: Request ( request) )
353
- . await
350
+ . await ?;
351
+
352
+ let ( tx, rx) = oneshot:: channel ( ) ;
353
+ let mut pending_requests = self . pending_requests . lock ( ) . await ;
354
+ pending_requests. insert ( message_id, tx) ;
355
+
356
+ Ok ( ( rx, Duration :: from_secs ( ttl) ) )
354
357
}
355
358
356
359
/// Private function to publish a success request response.
@@ -581,41 +584,16 @@ impl WalletConnectCtxImpl {
581
584
} ,
582
585
} ;
583
586
let request = RequestParams :: SessionRequest ( request) ;
584
- let ttl = request. irn_metadata ( ) . ttl ;
585
- let message_id = MessageIdGenerator :: new ( ) . next ( ) ;
586
- self . publish_request ( & active_topic, request, message_id) . await ?;
587
-
588
- match timeout ( Duration :: from_secs ( ttl) , async {
589
- // Check if the message exists and matches the expected message ID and
590
- // wait till we get a message with expected id or timeout.
591
- loop {
592
- let next_message = {
593
- let mut lock = self . message_rx . lock ( ) . await ;
594
- lock. next ( ) . await
595
- } ;
596
-
597
- if let Some ( Ok ( message) ) = & next_message {
598
- if message. message_id == message_id {
599
- return next_message;
600
- }
601
- }
602
- }
603
- } )
604
- . await
605
- {
606
- Ok ( Some ( result) ) => {
607
- let result = result. mm_err ( WalletConnectError :: InternalError ) ;
608
- match result?. data {
609
- ResponseParamsSuccess :: Arbitrary ( data) => {
610
- let data = serde_json:: from_value :: < T > ( data)
611
- . map_err ( |e| WalletConnectError :: SerdeError ( e. to_string ( ) ) ) ?;
612
- callback ( data)
613
- } ,
614
- _ => MmError :: err ( WalletConnectError :: PayloadError ( "Unexpected response type" . to_string ( ) ) ) ,
615
- }
616
- } ,
617
- Ok ( None ) => MmError :: err ( WalletConnectError :: NoWalletFeedback ) ,
618
- Err ( _) => MmError :: err ( WalletConnectError :: TimeoutError ) ,
587
+ let ( rx, ttl) = self . publish_request ( & active_topic, request) . await ?;
588
+
589
+ let maybe_response = rx
590
+ . timeout ( ttl)
591
+ . await
592
+ . map_to_mm ( |_| WalletConnectError :: TimeoutError ) ?
593
+ . map_to_mm ( |err| WalletConnectError :: InternalError ( err. to_string ( ) ) ) ??;
594
+ match maybe_response. data {
595
+ ResponseParamsSuccess :: Arbitrary ( data) => callback ( serde_json:: from_value :: < T > ( data) ?) ,
596
+ _ => MmError :: err ( WalletConnectError :: PayloadError ( "Unexpected response type" . to_string ( ) ) ) ,
619
597
}
620
598
}
621
599
0 commit comments