1+ #[ cfg( not( target_arch = "wasm32" ) ) ]
12use std:: path:: { Path , PathBuf } ;
23
34use super :: graph:: UdtCfgInfos ;
@@ -8,8 +9,10 @@ use crate::fiber::{
89 types:: { Hash256 , Pubkey } ,
910 FiberConfig , NetworkActorCommand , NetworkActorMessage ,
1011} ;
12+ #[ cfg( not( target_arch = "wasm32" ) ) ]
1113use crate :: now_timestamp_as_millis_u64;
12- use crate :: rpc:: server:: RpcServerStore ;
14+ #[ cfg( not( target_arch = "wasm32" ) ) ]
15+ use crate :: rpc:: server:: { KVStore , RpcServerStore } ;
1316use crate :: { handle_actor_call, log_and_error} ;
1417use ckb_jsonrpc_types:: Script ;
1518#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -18,6 +21,7 @@ use jsonrpsee::types::error::CALL_EXECUTION_FAILED_CODE;
1821use jsonrpsee:: types:: ErrorObjectOwned ;
1922
2023use ractor:: { call, ActorRef } ;
24+ #[ cfg( not( target_arch = "wasm32" ) ) ]
2125use rocksdb:: checkpoint:: Checkpoint ;
2226use serde:: { Deserialize , Serialize } ;
2327use serde_with:: serde_as;
@@ -86,29 +90,48 @@ pub struct NodeInfoResult {
8690 pub udt_cfg_infos : UdtCfgInfos ,
8791}
8892
93+ #[ serde_as]
8994#[ derive( Clone , Serialize , Deserialize , Debug ) ]
9095pub struct BackupResult {
9196 // The path of backup file
9297 path : String ,
9398 // The timestamp of backup
99+ #[ serde_as( as = "U64Hex" ) ]
94100 timestamp : u64 ,
95101}
96102
97103pub struct InfoRpcServerImpl < S > {
98104 actor : ActorRef < NetworkActorMessage > ,
105+
106+ default_funding_lock_script : Script ,
107+
108+ #[ cfg( not( target_arch = "wasm32" ) ) ]
99109 store : S ,
100- ckb_key_path : PathBuf ,
110+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
101111 fiber_key_path : PathBuf ,
102- default_funding_lock_script : Script ,
112+ #[ cfg( not( target_arch = "wasm32" ) ) ]
113+ ckb_key_path : PathBuf ,
114+
115+ #[ cfg( target_arch = "wasm32" ) ]
116+ _marker : std:: marker:: PhantomData < S > ,
103117}
104118
105- impl < S : RpcServerStore + Clone + Send + Sync + ' static > InfoRpcServerImpl < S > {
119+ #[ cfg( not( target_arch = "wasm32" ) ) ]
120+ pub trait StoreInfo : RpcServerStore + KVStore + Clone + Send + Sync + ' static { }
121+ #[ cfg( not( target_arch = "wasm32" ) ) ]
122+ impl < T > StoreInfo for T where T : RpcServerStore + KVStore + Clone + Send + Sync + ' static { }
123+ #[ cfg( target_arch = "wasm32" ) ]
124+ pub trait StoreInfo : Clone + Send + Sync + ' static { }
125+ #[ cfg( target_arch = "wasm32" ) ]
126+ impl < T > StoreInfo for T where T : Clone + Send + Sync + ' static { }
127+
128+ impl < S : StoreInfo > InfoRpcServerImpl < S > {
106129 #[ allow( unused_variables) ]
107130 pub fn new (
108131 actor : ActorRef < NetworkActorMessage > ,
109132 store : S ,
110133 ckb_config : CkbConfig ,
111- fiber_config : FiberConfig ,
134+ fiber_config : Option < FiberConfig > ,
112135 ) -> Self {
113136 #[ cfg( not( test) ) ]
114137 let default_funding_lock_script = ckb_config
@@ -121,15 +144,21 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
121144 #[ cfg( test) ]
122145 let default_funding_lock_script = Default :: default ( ) ;
123146
124- let ckb_key_path = ckb_config . base_dir ( ) . join ( "key" ) ;
125- let fiber_key_path = fiber_config. base_dir ( ) . join ( "sk ") ;
147+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
148+ let fiber_config = fiber_config. expect ( "fiber config should be set ") ;
126149
127150 InfoRpcServerImpl {
128151 actor,
129- store,
130- ckb_key_path,
131- fiber_key_path,
132152 default_funding_lock_script,
153+
154+ #[ cfg( not( target_arch = "wasm32" ) ) ]
155+ store,
156+ #[ cfg( not( target_arch = "wasm32" ) ) ]
157+ ckb_key_path : ckb_config. base_dir ( ) . join ( "key" ) ,
158+ #[ cfg( not( target_arch = "wasm32" ) ) ]
159+ fiber_key_path : fiber_config. base_dir ( ) . join ( "sk" ) ,
160+ #[ cfg( target_arch = "wasm32" ) ]
161+ _marker : std:: marker:: PhantomData ,
133162 }
134163 }
135164}
@@ -142,14 +171,14 @@ trait InfoRpc {
142171 #[ method( name = "node_info" ) ]
143172 async fn node_info ( & self ) -> Result < NodeInfoResult , ErrorObjectOwned > ;
144173
145- //Back the node information .
174+ /// Backup the node database and key files to a specified path .
146175 #[ method( name = "backup_now" ) ]
147176 async fn backup_now ( & self , path : String ) -> Result < BackupResult , ErrorObjectOwned > ;
148177}
149178
150179#[ async_trait:: async_trait]
151180#[ cfg( not( target_arch = "wasm32" ) ) ]
152- impl < S : RpcServerStore + Clone + Send + Sync + ' static > InfoRpcServer for InfoRpcServerImpl < S > {
181+ impl < S : StoreInfo > InfoRpcServer for InfoRpcServerImpl < S > {
153182 async fn node_info ( & self ) -> Result < NodeInfoResult , ErrorObjectOwned > {
154183 self . node_info ( ) . await
155184 }
@@ -158,7 +187,8 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServer for InfoRp
158187 self . backup_now ( path) . await
159188 }
160189}
161- impl < S : RpcServerStore + Clone + Send + Sync + ' static > InfoRpcServerImpl < S > {
190+
191+ impl < S : StoreInfo > InfoRpcServerImpl < S > {
162192 pub async fn node_info ( & self ) -> Result < NodeInfoResult , ErrorObjectOwned > {
163193 let version = env ! ( "CARGO_PKG_VERSION" ) . to_string ( ) ;
164194 let commit_hash = crate :: get_git_commit_info ( ) ;
@@ -188,6 +218,7 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
188218 } )
189219 }
190220
221+ #[ cfg( not( target_arch = "wasm32" ) ) ]
191222 async fn backup_now ( & self , path : String ) -> Result < BackupResult , ErrorObjectOwned > {
192223 let target_dir = PathBuf :: from ( & path) ;
193224
@@ -222,9 +253,8 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
222253 timestamp : now,
223254 } )
224255 }
225- }
226256
227- impl < S : RpcServerStore > InfoRpcServerImpl < S > {
257+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
228258 fn perform_key_backup ( & self , target_dir : & Path ) -> Result < ( ) , ErrorObjectOwned > {
229259 let keys_to_copy = [ ( & self . ckb_key_path , "key" ) , ( & self . fiber_key_path , "sk" ) ] ;
230260
@@ -246,7 +276,7 @@ impl<S: RpcServerStore> InfoRpcServerImpl<S> {
246276 }
247277}
248278
249- #[ cfg( test) ]
279+ #[ cfg( all ( test, not ( target_arch = "wasm32" ) ) ) ]
250280mod tests {
251281 use super :: * ;
252282 use crate :: test_utils:: { generate_store, get_fiber_config, NetworkNode , TempDir } ;
@@ -268,15 +298,15 @@ mod tests {
268298 let ckb_key_dir = ckb_config. base_dir . as_ref ( ) . unwrap ( ) ;
269299 let fiber_key_dir = fiber_config. base_dir ( ) . to_path_buf ( ) ;
270300
271- fs:: create_dir_all ( & ckb_key_dir) . unwrap ( ) ;
301+ fs:: create_dir_all ( ckb_key_dir) . unwrap ( ) ;
272302 fs:: create_dir_all ( & fiber_key_dir) . unwrap ( ) ;
273303 fs:: write ( ckb_key_dir. join ( "key" ) , "mock_ckb_key" ) . unwrap ( ) ;
274304 fs:: write ( fiber_key_dir. join ( "sk" ) , "mock_fiber_key" ) . unwrap ( ) ;
275305
276306 let node = NetworkNode :: new_with_node_name_opt ( Some ( "backup_test" . to_string ( ) ) ) ;
277307 let actor = node. await . get_actor ( ) ;
278308
279- let server = InfoRpcServerImpl :: new ( actor, store, ckb_config, fiber_config) ;
309+ let server = InfoRpcServerImpl :: new ( actor, store, ckb_config, Some ( fiber_config) ) ;
280310
281311 ( server, tempdir)
282312 }
0 commit comments