Skip to content

Commit 2cc29aa

Browse files
committed
fix: resolve WASM compatibility and clippy warnings
1 parent 971b3b0 commit 2cc29aa

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

crates/fiber-lib/src/rpc/info.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(not(target_arch = "wasm32"))]
12
use std::path::{Path, PathBuf};
23

34
use 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"))]
1113
use 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};
1316
use crate::{handle_actor_call, log_and_error};
1417
use ckb_jsonrpc_types::Script;
1518
#[cfg(not(target_arch = "wasm32"))]
@@ -18,6 +21,7 @@ use jsonrpsee::types::error::CALL_EXECUTION_FAILED_CODE;
1821
use jsonrpsee::types::ErrorObjectOwned;
1922

2023
use ractor::{call, ActorRef};
24+
#[cfg(not(target_arch = "wasm32"))]
2125
use rocksdb::checkpoint::Checkpoint;
2226
use serde::{Deserialize, Serialize};
2327
use 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)]
9095
pub 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

97103
pub 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")))]
250280
mod 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
}

crates/fiber-lib/src/rpc/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod server {
7676

7777
use super::biscuit::BiscuitAuth;
7878

79+
#[cfg(not(target_arch = "wasm32"))]
7980
pub trait KVStore {
8081
fn inner_db(&self) -> &std::sync::Arc<rocksdb::DB>;
8182
}
@@ -88,7 +89,6 @@ pub mod server {
8889
+ GossipMessageStore
8990
+ WatchtowerStore
9091
+ PreimageStore
91-
+ KVStore
9292
{
9393
}
9494
#[cfg(feature = "watchtower")]
@@ -99,7 +99,6 @@ pub mod server {
9999
+ GossipMessageStore
100100
+ WatchtowerStore
101101
+ PreimageStore
102-
+ KVStore
103102
{
104103
}
105104
#[cfg(not(feature = "watchtower"))]
@@ -224,7 +223,7 @@ pub mod server {
224223

225224
#[allow(clippy::type_complexity)]
226225
#[allow(clippy::too_many_arguments)]
227-
pub async fn start_rpc<S: RpcServerStore + Clone + Send + Sync + 'static>(
226+
pub async fn start_rpc<S: RpcServerStore + KVStore + Clone + Send + Sync + 'static>(
228227
config: RpcConfig,
229228
ckb_config: Option<CkbConfig>,
230229
fiber_config: Option<FiberConfig>,
@@ -271,13 +270,14 @@ pub mod server {
271270
}
272271
if let Some(network_actor) = network_actor {
273272
if config.is_module_enabled("info") {
273+
#[cfg(not(target_arch = "wasm32"))]
274274
modules
275275
.merge(
276276
InfoRpcServerImpl::new(
277277
network_actor.clone(),
278278
store.clone(),
279279
ckb_config.clone().expect("ckb config should be set"),
280-
fiber_config.clone().expect("fiber config should be set"),
280+
fiber_config.clone(),
281281
)
282282
.into_rpc(),
283283
)

crates/fiber-wasm/src/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) struct FiberWasm<
3131
> {
3232
pub(crate) channel: ChannelRpcServerImpl<ChannelStoreType>,
3333
pub(crate) graph: GraphRpcServerImpl<GraphStoreType>,
34-
pub(crate) info: InfoRpcServerImpl,
34+
pub(crate) info: InfoRpcServerImpl<()>,
3535
pub(crate) invoice: InvoiceRpcServerImpl<InvoiceStoreType>,
3636
pub(crate) payment: PaymentRpcServerImpl<PaymentStoreType>,
3737
pub(crate) peer: PeerRpcServerImpl,

crates/fiber-wasm/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ pub async fn fiber(
282282
.set(WrappedFiberWasm {
283283
channel: ChannelRpcServerImpl::new(network_actor.clone(), store.clone()),
284284
graph: GraphRpcServerImpl::new(network_graph.clone(), store.clone()),
285-
info: InfoRpcServerImpl::new(network_actor.clone(), config.ckb.unwrap_or_default()),
285+
info: InfoRpcServerImpl::new(
286+
network_actor.clone(),
287+
(),
288+
config.ckb.unwrap_or_default(),
289+
None,
290+
),
286291
invoice: InvoiceRpcServerImpl::new(
287292
store.clone(),
288293
Some(network_actor.clone()),

0 commit comments

Comments
 (0)