Skip to content

Commit 2db81e4

Browse files
authored
Merge pull request #892 from quake/quake/ckb-rpc-client-timeout
chore: timeout ckb rpc call
2 parents bd3f3e9 + 6a1dc74 commit 2db81e4

File tree

9 files changed

+187
-34
lines changed

9 files changed

+187
-34
lines changed

Cargo.lock

Lines changed: 64 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fiber-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bitmask-enum = "2.2.5"
2020
bs58 = "0.5.1"
2121
ckb-gen-types = "0.202.0"
2222
ckb-jsonrpc-types = "0.202.0"
23-
ckb-sdk = "4.1.0"
23+
ckb-sdk = "4.4"
2424
ckb-testtool = {version = "0.16.0", optional = true}
2525
ckb-types = "0.202.0"
2626
clap = {version = "4.5.2", features = ["derive", "env", "string"]}

crates/fiber-lib/src/ckb/actor.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ impl Actor for CkbChainActor {
254254
}
255255
}
256256
CkbChainMessage::SendTx(tx, reply_port) => {
257-
let rpc_url = state.config.rpc_url.clone();
258-
let ckb_client = CkbRpcAsyncClient::new(&rpc_url);
257+
let ckb_client = state.config.ckb_rpc_client();
259258
let result = match ckb_client.send_transaction(tx.data().into(), None).await {
260259
Ok(_) => Ok(()),
261260
Err(err) => {
@@ -289,8 +288,7 @@ impl Actor for CkbChainActor {
289288
}
290289
}
291290
CkbChainMessage::GetTx(tx_hash, reply_port) => {
292-
let rpc_url = state.config.rpc_url.clone();
293-
let ckb_client = CkbRpcAsyncClient::new(&rpc_url);
291+
let ckb_client = state.config.ckb_rpc_client();
294292
let result = ckb_client.get_transaction(tx_hash.into()).await;
295293
if !reply_port.is_closed() {
296294
// ignore error
@@ -317,8 +315,7 @@ impl Actor for CkbChainActor {
317315
GetBlockTimestampRequest { block_hash },
318316
reply_port,
319317
) => {
320-
let rpc_url = state.config.rpc_url.clone();
321-
let ckb_client = CkbRpcAsyncClient::new(&rpc_url);
318+
let ckb_client = state.config.ckb_rpc_client();
322319
let _ = reply_port.send(
323320
ckb_client
324321
.get_header(block_hash.into())
@@ -327,8 +324,7 @@ impl Actor for CkbChainActor {
327324
);
328325
}
329326
CkbChainMessage::GetShutdownTx(request, reply_port) => {
330-
let rpc_url = state.config.rpc_url.clone();
331-
let client = CkbRpcAsyncClient::new(&rpc_url);
327+
let client = state.config.ckb_rpc_client();
332328
let response = get_shutdown_tx(&client, request).await;
333329
let _ = reply_port.send(response);
334330
}

crates/fiber-lib/src/ckb/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::contracts::{get_script_by_contract, Contract};
33
use crate::utils::encrypt_decrypt_file::{decrypt_from_file, encrypt_to_file};
44
use crate::Result;
55
use ckb_jsonrpc_types::{OutPoint as OutPointWrapper, Script as ScriptWrapper};
6+
use ckb_sdk::{traits::DefaultCellCollector, CkbRpcAsyncClient};
67
use ckb_types::core::ScriptHashType;
78
use ckb_types::prelude::Builder;
89
use ckb_types::H256;
@@ -109,6 +110,11 @@ impl CkbConfig {
109110
Ok(())
110111
}
111112
}
113+
114+
pub fn ckb_rpc_client(&self) -> CkbRpcAsyncClient {
115+
new_ckb_rpc_async_client(&self.rpc_url)
116+
}
117+
112118
#[cfg(target_arch = "wasm32")]
113119
pub fn read_secret_key(&self) -> Result<SecretKey> {
114120
Ok(self.wasm_secret_key.expect("SecretKey not found on wasm"))
@@ -280,3 +286,21 @@ impl From<&UdtCellDep> for CellDep {
280286
.build()
281287
}
282288
}
289+
290+
pub const CKB_RPC_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
291+
292+
pub fn new_ckb_rpc_async_client(rpc_url: &str) -> CkbRpcAsyncClient {
293+
#[cfg(not(target_arch = "wasm32"))]
294+
return CkbRpcAsyncClient::with_builder(rpc_url, |builder| builder.timeout(CKB_RPC_TIMEOUT))
295+
.expect("create ckb rpc client should not fail");
296+
#[cfg(target_arch = "wasm32")]
297+
return CkbRpcAsyncClient::new(rpc_url);
298+
}
299+
300+
pub fn new_default_cell_collector(rpc_url: &str) -> DefaultCellCollector {
301+
#[cfg(not(target_arch = "wasm32"))]
302+
return DefaultCellCollector::new_with_timeout(rpc_url, CKB_RPC_TIMEOUT)
303+
.expect("create default cell collector should not fail");
304+
#[cfg(target_arch = "wasm32")]
305+
return DefaultCellCollector::new(rpc_url);
306+
}

crates/fiber-lib/src/ckb/contracts.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use ckb_sdk::{
2-
rpc::ckb_indexer::{Order, ScriptType, SearchKey, SearchMode},
3-
CkbRpcAsyncClient,
4-
};
1+
use ckb_sdk::rpc::ckb_indexer::{Order, ScriptType, SearchKey, SearchMode};
52
use ckb_types::{
63
core::{BlockView, DepType, ScriptHashType},
74
packed::{CellDep, CellDepVec, CellDepVecBuilder, CellOutput, OutPoint, Script},
@@ -14,9 +11,12 @@ use std::{collections::HashMap, vec};
1411
use thiserror::Error;
1512
use tracing::info;
1613

17-
use crate::fiber::{
18-
config::FiberScript,
19-
gen::fiber::{UdtDep, UdtDepUnion},
14+
use crate::{
15+
ckb::config::new_ckb_rpc_async_client,
16+
fiber::{
17+
config::FiberScript,
18+
gen::fiber::{UdtDep, UdtDepUnion},
19+
},
2020
};
2121

2222
use super::config::{UdtArgInfo, UdtCfgInfos};
@@ -82,7 +82,7 @@ impl TypeIDResolver {
8282
}
8383

8484
pub async fn resolve(&self, type_id: Script) -> Option<CellDep> {
85-
let ckb_client = CkbRpcAsyncClient::new(&self.ckb_url);
85+
let ckb_client = new_ckb_rpc_async_client(&self.ckb_url);
8686
let search_key = SearchKey {
8787
script: type_id.into(),
8888
script_type: ScriptType::Type,

crates/fiber-lib/src/ckb/funding/funding_tx.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
use super::super::FundingError;
2-
use crate::{ckb::contracts::get_udt_cell_deps, fiber::serde_utils::EntityHex};
2+
use crate::{
3+
ckb::{
4+
config::{new_ckb_rpc_async_client, new_default_cell_collector},
5+
contracts::get_udt_cell_deps,
6+
},
7+
fiber::serde_utils::EntityHex,
8+
};
39
use anyhow::anyhow;
410
use ckb_sdk::{
511
constants::SIGHASH_TYPE_HASH,
612
rpc::ckb_indexer::SearchMode,
713
traits::{
8-
CellCollector, CellDepResolver, CellQueryOptions, DefaultCellCollector,
9-
DefaultCellDepResolver, DefaultHeaderDepResolver, DefaultTransactionDependencyProvider,
10-
HeaderDepResolver, SecpCkbRawKeySigner, TransactionDependencyProvider, ValueRangeOption,
14+
CellCollector, CellDepResolver, CellQueryOptions, DefaultCellDepResolver,
15+
DefaultHeaderDepResolver, DefaultTransactionDependencyProvider, HeaderDepResolver,
16+
SecpCkbRawKeySigner, TransactionDependencyProvider, ValueRangeOption,
1117
},
1218
tx_builder::{unlock_tx_async, CapacityBalancer, TxBuilder, TxBuilderError},
1319
unlock::{ScriptUnlocker, SecpSighashUnlocker},
14-
CkbRpcAsyncClient, ScriptId,
20+
ScriptId,
1521
};
1622
use ckb_types::{
1723
core::{BlockView, Capacity, TransactionView},
@@ -392,7 +398,7 @@ impl FundingTxBuilder {
392398
self.request.funding_fee_rate,
393399
);
394400

395-
let ckb_client = CkbRpcAsyncClient::new(&self.context.rpc_url);
401+
let ckb_client = new_ckb_rpc_async_client(&self.context.rpc_url);
396402
let cell_dep_resolver = {
397403
match ckb_client.get_block_by_number(0.into()).await? {
398404
Some(genesis_block) => {
@@ -419,7 +425,7 @@ impl FundingTxBuilder {
419425
};
420426

421427
let header_dep_resolver = DefaultHeaderDepResolver::new(&self.context.rpc_url);
422-
let mut cell_collector = DefaultCellCollector::new(&self.context.rpc_url);
428+
let mut cell_collector = new_default_cell_collector(&self.context.rpc_url);
423429
let tx_dep_provider = DefaultTransactionDependencyProvider::new(&self.context.rpc_url, 10);
424430

425431
let tip_block_number: u64 = ckb_client.get_tip_block_number().await?.into();
@@ -621,7 +627,7 @@ impl FundingTx {
621627
return Err(FundingError::InvalidPeerFundingTx);
622628
}
623629
// Peer SHOULD NOT add inputs locked by our lock scripts
624-
let ckb_client = CkbRpcAsyncClient::new(&context.rpc_url);
630+
let ckb_client = new_ckb_rpc_async_client(&context.rpc_url);
625631
for input in remote_tx.input_pts_iter().skip(local_tx.inputs().len()) {
626632
match ckb_client.get_live_cell(input.into(), false).await?.cell {
627633
Some(cell) => {

crates/fiber-lib/src/ckb/tx_tracing_actor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::collections::HashMap;
22

33
use bitmask_enum::bitmask;
4-
use ckb_sdk::CkbRpcAsyncClient;
54
use ckb_types::core::tx_pool::TxStatus;
65
use ractor::{concurrency::Duration, Actor, ActorProcessingErr, ActorRef, RpcReplyPort};
76

8-
use crate::fiber::types::Hash256;
7+
use crate::{ckb::config::new_ckb_rpc_async_client, fiber::types::Hash256};
98

109
use super::jsonrpc_types_convert::tx_status_from_json;
1110

@@ -266,7 +265,7 @@ impl TracingTask {
266265
}
267266

268267
async fn run_inner(self) -> Result<(), Box<dyn std::error::Error>> {
269-
let ckb_client = CkbRpcAsyncClient::new(&self.rpc_url);
268+
let ckb_client = new_ckb_rpc_async_client(&self.rpc_url);
270269
let tip_block_number: u64 = ckb_client.get_tip_block_number().await?.into();
271270

272271
for tx_hash in self.tx_hashes {

crates/fiber-lib/src/watchtower/actor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use tracing::{debug, error, info, warn};
2121

2222
use crate::{
2323
ckb::{
24+
config::{new_default_cell_collector, CKB_RPC_TIMEOUT},
2425
contracts::{get_cell_deps_sync, get_script_by_contract, Contract},
2526
CkbConfig,
2627
},
@@ -147,10 +148,13 @@ where
147148
let secret_key = state.secret_key;
148149
let rpc_url = state.config.rpc_url.clone();
149150
tokio::task::block_in_place(move || {
150-
let mut cell_collector = DefaultCellCollector::new(&rpc_url);
151+
let mut cell_collector = new_default_cell_collector(&rpc_url);
151152

152153
for channel_data in self.store.get_watch_channels() {
153-
let ckb_client = CkbRpcClient::new(&rpc_url);
154+
let ckb_client = CkbRpcClient::with_builder(&rpc_url, |builder| {
155+
builder.timeout(CKB_RPC_TIMEOUT)
156+
})
157+
.expect("create ckb rpc client should not fail");
154158
let search_key = SearchKey {
155159
script: channel_data.funding_tx_lock.clone().into(),
156160
script_type: ScriptType::Lock,

0 commit comments

Comments
 (0)