@@ -27,12 +27,22 @@ impl StorageHubRpcClient {
2727 self . connection . is_connected ( ) . await
2828 }
2929
30+ /// Attempts to reconnect if the connection isn't connected
31+ async fn ensure_connected ( & self ) {
32+ if !self . is_connected ( ) . await {
33+ // TODO: More robust reconnection mechanism, like we do for the original connection
34+ _ = self . connection . reconnect ( ) . await ;
35+ }
36+ }
37+
3038 /// Call a JSON-RPC method on the connected node
3139 pub async fn call < P , R > ( & self , method : & str , params : P ) -> RpcResult < R >
3240 where
3341 P : ToRpcParams + Send ,
3442 R : DeserializeOwned ,
3543 {
44+ self . ensure_connected ( ) . await ;
45+
3646 self . connection . call ( method, params) . await
3747 }
3848
@@ -41,6 +51,8 @@ impl StorageHubRpcClient {
4151 where
4252 R : DeserializeOwned ,
4353 {
54+ self . ensure_connected ( ) . await ;
55+
4456 self . connection . call_no_params ( method) . await
4557 }
4658
@@ -54,15 +66,14 @@ impl StorageHubRpcClient {
5466 pub async fn get_current_price_per_giga_unit_per_tick ( & self ) -> RpcResult < u128 > {
5567 debug ! ( target: "rpc::client::get_current_price_per_giga_unit_per_tick" , "RPC call: get_current_price_per_giga_unit_per_tick" ) ;
5668
57- self . connection . call_no_params ( methods:: CURRENT_PRICE ) . await
69+ self . call_no_params ( methods:: CURRENT_PRICE ) . await
5870 }
5971
6072 /// Returns whether the given `file_key` is expected to be received by the MSP node
6173 pub async fn is_file_key_expected ( & self , file_key : & str ) -> RpcResult < bool > {
6274 debug ! ( target: "rpc::client::is_file_key_expected" , file_key = %file_key, "RPC call: is_file_key_expected" ) ;
6375
64- self . connection
65- . call ( methods:: FILE_KEY_EXPECTED , jsonrpsee:: rpc_params![ file_key] )
76+ self . call ( methods:: FILE_KEY_EXPECTED , jsonrpsee:: rpc_params![ file_key] )
6677 . await
6778 }
6879
@@ -77,12 +88,11 @@ impl StorageHubRpcClient {
7788 & self ,
7889 file_key : & str ,
7990 ) -> RpcResult < GetFileFromFileStorageResult > {
80- self . connection
81- . call (
82- methods:: IS_FILE_IN_FILE_STORAGE ,
83- jsonrpsee:: rpc_params![ file_key] ,
84- )
85- . await
91+ self . call (
92+ methods:: IS_FILE_IN_FILE_STORAGE ,
93+ jsonrpsee:: rpc_params![ file_key] ,
94+ )
95+ . await
8696 }
8797
8898 /// Request the MSP node to export the given `file_key` to the given URL
@@ -94,12 +104,11 @@ impl StorageHubRpcClient {
94104 "RPC call: save_file_to_disk"
95105 ) ;
96106
97- self . connection
98- . call (
99- methods:: SAVE_FILE_TO_DISK ,
100- jsonrpsee:: rpc_params![ file_key, url] ,
101- )
102- . await
107+ self . call (
108+ methods:: SAVE_FILE_TO_DISK ,
109+ jsonrpsee:: rpc_params![ file_key, url] ,
110+ )
111+ . await
103112 }
104113
105114 /// Request the MSP to accept a FileKeyProof (`proof`) for the given `file_key`
@@ -111,33 +120,32 @@ impl StorageHubRpcClient {
111120 "RPC call: receive_file_chunks"
112121 ) ;
113122
114- self . connection
115- . call (
116- methods:: RECEIVE_FILE_CHUNKS ,
117- jsonrpsee:: rpc_params![ file_key, proof] ,
118- )
119- . await
123+ self . call (
124+ methods:: RECEIVE_FILE_CHUNKS ,
125+ jsonrpsee:: rpc_params![ file_key, proof] ,
126+ )
127+ . await
120128 }
121129
122130 /// Retrieve the Onchain Provider ID of the MSP Node (therefore the MSP ID)
123131 pub async fn get_provider_id ( & self ) -> RpcResult < RpcProviderId > {
124132 debug ! ( target: "rpc::client::get_provider_id" , "RPC call: get_provider_id" ) ;
125133
126- self . connection . call_no_params ( methods:: PROVIDER_ID ) . await
134+ self . call_no_params ( methods:: PROVIDER_ID ) . await
127135 }
128136
129137 /// Retrieve the list of value propositions of the MSP Node
130138 pub async fn get_value_props ( & self ) -> RpcResult < GetValuePropositionsResult > {
131139 debug ! ( target: "rpc::client::get_value_props" , "RPC call: get_value_props" ) ;
132140
133- self . connection . call_no_params ( methods:: VALUE_PROPS ) . await
141+ self . call_no_params ( methods:: VALUE_PROPS ) . await
134142 }
135143
136144 /// Retrieve the list of multiaddresses associated with the MSP Node
137145 pub async fn get_multiaddresses ( & self ) -> RpcResult < Vec < String > > {
138146 debug ! ( target: "rpc::client::get_multiaddresses" , "RPC call: get_multiaddresses" ) ;
139147
140- self . connection . call_no_params ( methods:: PEER_IDS ) . await
148+ self . call_no_params ( methods:: PEER_IDS ) . await
141149 }
142150}
143151
@@ -175,6 +183,27 @@ mod tests {
175183 StorageHubRpcClient :: new ( connection)
176184 }
177185
186+ #[ tokio:: test]
187+ async fn reconnect_automatically ( ) {
188+ let conn = MockConnection :: new ( ) ;
189+ conn. disconnect ( ) . await ;
190+ let conn = Arc :: new ( AnyRpcConnection :: Mock ( conn) ) ;
191+ let client = StorageHubRpcClient :: new ( conn) ;
192+
193+ assert ! (
194+ !client. is_connected( ) . await ,
195+ "Should not be connected initially"
196+ ) ;
197+
198+ let result = client. get_provider_id ( ) . await ;
199+ assert ! (
200+ result. is_ok( ) ,
201+ "Should reconnect and be able to retrieve provider id"
202+ ) ;
203+
204+ assert ! ( client. is_connected( ) . await , "Should be connected now" ) ;
205+ }
206+
178207 #[ tokio:: test]
179208 async fn get_current_price_per_unit_per_tick ( ) {
180209 let client = mock_rpc ( ) ;
0 commit comments