Skip to content

Commit c060b67

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

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

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

Lines changed: 46 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;
@@ -96,19 +100,36 @@ pub struct BackupResult {
96100

97101
pub struct InfoRpcServerImpl<S> {
98102
actor: ActorRef<NetworkActorMessage>,
103+
104+
default_funding_lock_script: Script,
105+
106+
#[cfg(not(target_arch = "wasm32"))]
99107
store: S,
100-
ckb_key_path: PathBuf,
108+
#[cfg(not(target_arch = "wasm32"))]
101109
fiber_key_path: PathBuf,
102-
default_funding_lock_script: Script,
110+
#[cfg(not(target_arch = "wasm32"))]
111+
ckb_key_path: PathBuf,
112+
113+
#[cfg(target_arch = "wasm32")]
114+
_marker: std::marker::PhantomData<S>,
103115
}
104116

105-
impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
117+
#[cfg(not(target_arch = "wasm32"))]
118+
pub trait StoreInfo: RpcServerStore + KVStore + Clone + Send + Sync + 'static {}
119+
#[cfg(not(target_arch = "wasm32"))]
120+
impl<T> StoreInfo for T where T: RpcServerStore + KVStore + Clone + Send + Sync + 'static {}
121+
#[cfg(target_arch = "wasm32")]
122+
pub trait StoreInfo: Clone + Send + Sync + 'static {}
123+
#[cfg(target_arch = "wasm32")]
124+
impl<T> StoreInfo for T where T: Clone + Send + Sync + 'static {}
125+
126+
impl<S: StoreInfo> InfoRpcServerImpl<S> {
106127
#[allow(unused_variables)]
107128
pub fn new(
108129
actor: ActorRef<NetworkActorMessage>,
109130
store: S,
110131
ckb_config: CkbConfig,
111-
fiber_config: FiberConfig,
132+
fiber_config: Option<FiberConfig>,
112133
) -> Self {
113134
#[cfg(not(test))]
114135
let default_funding_lock_script = ckb_config
@@ -121,15 +142,21 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
121142
#[cfg(test)]
122143
let default_funding_lock_script = Default::default();
123144

124-
let ckb_key_path = ckb_config.base_dir().join("key");
125-
let fiber_key_path = fiber_config.base_dir().join("sk");
145+
#[cfg(not(target_arch = "wasm32"))]
146+
let fiber_config = fiber_config.expect("fiber config should be set");
126147

127148
InfoRpcServerImpl {
128149
actor,
129-
store,
130-
ckb_key_path,
131-
fiber_key_path,
132150
default_funding_lock_script,
151+
152+
#[cfg(not(target_arch = "wasm32"))]
153+
store,
154+
#[cfg(not(target_arch = "wasm32"))]
155+
ckb_key_path: ckb_config.base_dir().join("key"),
156+
#[cfg(not(target_arch = "wasm32"))]
157+
fiber_key_path: fiber_config.base_dir().join("sk"),
158+
#[cfg(target_arch = "wasm32")]
159+
_marker: std::marker::PhantomData,
133160
}
134161
}
135162
}
@@ -142,14 +169,14 @@ trait InfoRpc {
142169
#[method(name = "node_info")]
143170
async fn node_info(&self) -> Result<NodeInfoResult, ErrorObjectOwned>;
144171

145-
//Back the node information.
172+
/// Backup the node database and key files to a specified path.
146173
#[method(name = "backup_now")]
147174
async fn backup_now(&self, path: String) -> Result<BackupResult, ErrorObjectOwned>;
148175
}
149176

150177
#[async_trait::async_trait]
151178
#[cfg(not(target_arch = "wasm32"))]
152-
impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServer for InfoRpcServerImpl<S> {
179+
impl<S: StoreInfo> InfoRpcServer for InfoRpcServerImpl<S> {
153180
async fn node_info(&self) -> Result<NodeInfoResult, ErrorObjectOwned> {
154181
self.node_info().await
155182
}
@@ -158,7 +185,8 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServer for InfoRp
158185
self.backup_now(path).await
159186
}
160187
}
161-
impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
188+
189+
impl<S: StoreInfo> InfoRpcServerImpl<S> {
162190
pub async fn node_info(&self) -> Result<NodeInfoResult, ErrorObjectOwned> {
163191
let version = env!("CARGO_PKG_VERSION").to_string();
164192
let commit_hash = crate::get_git_commit_info();
@@ -188,6 +216,7 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
188216
})
189217
}
190218

219+
#[cfg(not(target_arch = "wasm32"))]
191220
async fn backup_now(&self, path: String) -> Result<BackupResult, ErrorObjectOwned> {
192221
let target_dir = PathBuf::from(&path);
193222

@@ -222,9 +251,8 @@ impl<S: RpcServerStore + Clone + Send + Sync + 'static> InfoRpcServerImpl<S> {
222251
timestamp: now,
223252
})
224253
}
225-
}
226254

227-
impl<S: RpcServerStore> InfoRpcServerImpl<S> {
255+
#[cfg(not(target_arch = "wasm32"))]
228256
fn perform_key_backup(&self, target_dir: &Path) -> Result<(), ErrorObjectOwned> {
229257
let keys_to_copy = [(&self.ckb_key_path, "key"), (&self.fiber_key_path, "sk")];
230258

@@ -246,7 +274,7 @@ impl<S: RpcServerStore> InfoRpcServerImpl<S> {
246274
}
247275
}
248276

249-
#[cfg(test)]
277+
#[cfg(all(test, not(target_arch = "wasm32")))]
250278
mod tests {
251279
use super::*;
252280
use crate::test_utils::{generate_store, get_fiber_config, NetworkNode, TempDir};
@@ -268,15 +296,15 @@ mod tests {
268296
let ckb_key_dir = ckb_config.base_dir.as_ref().unwrap();
269297
let fiber_key_dir = fiber_config.base_dir().to_path_buf();
270298

271-
fs::create_dir_all(&ckb_key_dir).unwrap();
299+
fs::create_dir_all(ckb_key_dir).unwrap();
272300
fs::create_dir_all(&fiber_key_dir).unwrap();
273301
fs::write(ckb_key_dir.join("key"), "mock_ckb_key").unwrap();
274302
fs::write(fiber_key_dir.join("sk"), "mock_fiber_key").unwrap();
275303

276304
let node = NetworkNode::new_with_node_name_opt(Some("backup_test".to_string()));
277305
let actor = node.await.get_actor();
278306

279-
let server = InfoRpcServerImpl::new(actor, store, ckb_config, fiber_config);
307+
let server = InfoRpcServerImpl::new(actor, store, ckb_config, Some(fiber_config));
280308

281309
(server, tempdir)
282310
}

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)