Skip to content

Commit d049993

Browse files
authored
Merge pull request #4170 from chenyukang/use-jsonrpc-utils
Use jsonrpc-utils to replace jsonrpc
2 parents e9a675f + 2d960b9 commit d049993

File tree

29 files changed

+1122
-1301
lines changed

29 files changed

+1122
-1301
lines changed

Cargo.lock

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

ckb-bin/src/subcommand/run.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub fn run(args: RunArgs, version: Version, async_handle: Handle) -> Result<(),
1212
deadlock_detection();
1313

1414
info!("ckb version: {}", version);
15-
1615
let mut launcher = Launcher::new(args, version, async_handle);
1716

1817
let block_assembler_config = launcher.sanitize_block_assembler_config()?;
@@ -45,7 +44,7 @@ pub fn run(args: RunArgs, version: Version, async_handle: Handle) -> Result<(),
4544

4645
launcher.start_block_filter(&shared);
4746

48-
let (network_controller, _rpc_server) = launcher.start_network_and_rpc(
47+
let network_controller = launcher.start_network_and_rpc(
4948
&shared,
5049
chain_controller.clone(),
5150
miner_enable,

rpc/Cargo.toml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ ckb-shared = { path = "../shared", version = "= 0.113.0-pre" }
1717
ckb-store = { path = "../store", version = "= 0.113.0-pre" }
1818
ckb-sync = { path = "../sync", version = "= 0.113.0-pre" }
1919
ckb-chain = { path = "../chain", version = "= 0.113.0-pre" }
20-
ckb-logger = { path = "../util/logger", version = "= 0.113.0-pre"}
21-
ckb-logger-service = { path = "../util/logger-service", version = "= 0.113.0-pre"}
20+
ckb-logger = { path = "../util/logger", version = "= 0.113.0-pre" }
21+
ckb-logger-service = { path = "../util/logger-service", version = "= 0.113.0-pre" }
2222
ckb-network-alert = { path = "../util/network-alert", version = "= 0.113.0-pre" }
2323
ckb-app-config = { path = "../util/app-config", version = "= 0.113.0-pre" }
2424
ckb-constant = { path = "../util/constant", version = "= 0.113.0-pre" }
2525
jsonrpc-core = "18.0"
26-
jsonrpc-derive = "18.0"
27-
jsonrpc-http-server = "18.0"
28-
jsonrpc-tcp-server = "18.0"
29-
jsonrpc-ws-server = "18.0"
30-
jsonrpc-server-utils = "18.0"
31-
jsonrpc-pubsub = "18.0"
3226
serde_json = "1.0"
27+
jsonrpc-utils = { version = "0.2.6", features = ["server", "macros", "axum"] }
3328
ckb-jsonrpc-types = { path = "../util/jsonrpc-types", version = "= 0.113.0-pre" }
3429
ckb-verification = { path = "../verification", version = "= 0.113.0-pre" }
3530
ckb-verification-traits = { path = "../verification/traits", version = "= 0.113.0-pre" }
@@ -43,8 +38,16 @@ ckb-tx-pool = { path = "../tx-pool", version = "= 0.113.0-pre" }
4338
ckb-memory-tracker = { path = "../util/memory-tracker", version = "= 0.113.0-pre" }
4439
ckb-pow = { path = "../pow", version = "= 0.113.0-pre" }
4540
ckb-indexer = { path = "../util/indexer", version = "= 0.113.0-pre" }
41+
ckb-stop-handler = { path = "../util/stop-handler", version = "= 0.113.0-pre" }
4642
itertools.workspace = true
4743
tokio = "1"
44+
async-trait = "0.1"
45+
axum = "0.6.1"
46+
tokio-util = { version = "0.7.3", features = ["codec"] }
47+
futures-util = { version = "0.3.21" }
48+
tower-http = { version = "0.3.5", features = ["timeout"] }
49+
async-stream = "0.3.3"
50+
ckb-async-runtime = { path = "../util/runtime", version = "= 0.113.0-pre" }
4851

4952
[dev-dependencies]
5053
reqwest = { version = "=0.11.20", features = ["blocking", "json"] }

rpc/README.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
9999
* [Method `get_deployments_info`](#method-get_deployments_info)
100100
* [Module Subscription](#module-subscription)
101101
* [Method `subscribe`](#method-subscribe)
102-
* [Method `unsubscribe`](#method-unsubscribe)
103102
* [RPC Errors](#rpc-errors)
104103
* [RPC Types](#rpc-types)
105104
* [Type `Alert`](#type-alert)
@@ -4956,7 +4955,7 @@ The type of the `params.result` in the push message is a two-elements array, whe
49564955

49574956
###### Examples
49584957

4959-
Request
4958+
Subscribe Request
49604959

49614960

49624961
```
@@ -4971,32 +4970,19 @@ Request
49714970
```
49724971

49734972

4974-
Response
4973+
Subscribe Response
49754974

49764975

49774976
```
49784977
{
49794978
"id": 42,
49804979
"jsonrpc": "2.0",
4981-
"result": "0x2a"
4980+
"result": "0xf3"
49824981
}
49834982
```
49844983

49854984

4986-
#### Method `unsubscribe`
4987-
* `unsubscribe(id)`
4988-
* `id`: `string`
4989-
* result: `boolean`
4990-
4991-
Unsubscribes from a subscribed topic.
4992-
4993-
###### Params
4994-
4995-
* `id` - Subscription ID
4996-
4997-
###### Examples
4998-
4999-
Request
4985+
Unsubscribe Request
50004986

50014987

50024988
```
@@ -5005,13 +4991,13 @@ Request
50054991
"jsonrpc": "2.0",
50064992
"method": "unsubscribe",
50074993
"params": [
5008-
"0x2a"
4994+
"0xf3"
50094995
]
50104996
}
50114997
```
50124998

50134999

5014-
Response
5000+
Unsubscribe Response
50155001

50165002

50175003
```

rpc/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ pub mod module;
1010
#[cfg(test)]
1111
mod tests;
1212

13+
use jsonrpc_core::MetaIoHandler;
14+
use jsonrpc_utils::pub_sub::Session;
15+
1316
pub use crate::error::RPCError;
1417
pub use crate::server::RpcServer;
1518
pub use crate::service_builder::ServiceBuilder;
1619

1720
#[doc(hidden)]
18-
pub type IoHandler = jsonrpc_pubsub::PubSubHandler<Option<crate::module::SubscriptionSession>>;
21+
pub type IoHandler = MetaIoHandler<Option<Session>>;

rpc/src/module/alert.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::error::RPCError;
2+
use async_trait::async_trait;
23
use ckb_jsonrpc_types::Alert;
34
use ckb_logger::error;
45
use ckb_network::{NetworkController, SupportProtocols};
56
use ckb_network_alert::{notifier::Notifier as AlertNotifier, verifier::Verifier as AlertVerifier};
67
use ckb_types::{packed, prelude::*};
78
use ckb_util::Mutex;
9+
810
use jsonrpc_core::Result;
9-
use jsonrpc_derive::rpc;
11+
use jsonrpc_utils::rpc;
1012
use std::sync::Arc;
1113

1214
/// RPC Module Alert for network alerts.
@@ -15,7 +17,8 @@ use std::sync::Arc;
1517
///
1618
/// The alerts must be signed by 2-of-4 signatures, where the public keys are hard-coded in the source code
1719
/// and belong to early CKB developers.
18-
#[rpc(server)]
20+
#[rpc]
21+
#[async_trait]
1922
pub trait AlertRpc {
2023
/// Sends an alert.
2124
///
@@ -70,6 +73,7 @@ pub trait AlertRpc {
7073
fn send_alert(&self, alert: Alert) -> Result<()>;
7174
}
7275

76+
#[derive(Clone)]
7377
pub(crate) struct AlertRpcImpl {
7478
network_controller: NetworkController,
7579
verifier: Arc<AlertVerifier>,
@@ -90,6 +94,7 @@ impl AlertRpcImpl {
9094
}
9195
}
9296

97+
#[async_trait]
9398
impl AlertRpc for AlertRpcImpl {
9499
fn send_alert(&self, alert: Alert) -> Result<()> {
95100
let alert: packed::Alert = alert.into();

rpc/src/module/chain.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::error::RPCError;
22
use crate::util::FeeRateCollector;
3+
use async_trait::async_trait;
34
use ckb_jsonrpc_types::{
45
BlockEconomicState, BlockFilter, BlockNumber, BlockResponse, BlockView, CellWithStatus,
56
Consensus, EpochNumber, EpochView, EstimateCycles, FeeRateStatistics, HeaderView, OutPoint,
@@ -26,7 +27,7 @@ use ckb_types::{
2627
use ckb_verification::ScriptVerifier;
2728
use ckb_verification::TxVerifyEnv;
2829
use jsonrpc_core::Result;
29-
use jsonrpc_derive::rpc;
30+
use jsonrpc_utils::rpc;
3031
use std::collections::HashSet;
3132
use std::sync::Arc;
3233

@@ -52,7 +53,8 @@ use std::sync::Arc;
5253
/// * it is found as an output in any transaction in the [canonical chain](#canonical-chain),
5354
/// and
5455
/// * it is not found as an input in any transaction in the canonical chain.
55-
#[rpc(server)]
56+
#[rpc]
57+
#[async_trait]
5658
pub trait ChainRpc {
5759
/// Returns the information about a block by hash.
5860
///
@@ -1606,6 +1608,7 @@ pub trait ChainRpc {
16061608
fn get_fee_rate_statistics(&self, target: Option<Uint64>) -> Result<Option<FeeRateStatistics>>;
16071609
}
16081610

1611+
#[derive(Clone)]
16091612
pub(crate) struct ChainRpcImpl {
16101613
pub shared: Shared,
16111614
}
@@ -1614,6 +1617,7 @@ const DEFAULT_BLOCK_VERBOSITY_LEVEL: u32 = 2;
16141617
const DEFAULT_HEADER_VERBOSITY_LEVEL: u32 = 1;
16151618
const DEFAULT_GET_TRANSACTION_VERBOSITY_LEVEL: u32 = 2;
16161619

1620+
#[async_trait]
16171621
impl ChainRpc for ChainRpcImpl {
16181622
fn get_block(
16191623
&self,

rpc/src/module/debug.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
use async_trait::async_trait;
12
use ckb_jsonrpc_types::{ExtraLoggerConfig, MainLoggerConfig};
23
use ckb_logger_service::Logger;
34
use jsonrpc_core::{Error, ErrorCode::InternalError, Result};
4-
use jsonrpc_derive::rpc;
5+
use jsonrpc_utils::rpc;
56
use std::time;
6-
77
/// RPC Module Debug for internal RPC methods.
88
///
99
/// **This module is for CKB developers and will not guarantee compatibility.** The methods here
1010
/// will be changed or removed without advanced notification.
11-
#[rpc(server)]
1211
#[doc(hidden)]
12+
#[rpc]
13+
#[async_trait]
1314
pub trait DebugRpc {
1415
/// Dumps jemalloc memory profiling information into a file.
1516
///
@@ -35,8 +36,10 @@ pub trait DebugRpc {
3536
fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> Result<()>;
3637
}
3738

39+
#[derive(Clone)]
3840
pub(crate) struct DebugRpcImpl {}
3941

42+
#[async_trait]
4043
impl DebugRpc for DebugRpcImpl {
4144
fn jemalloc_profiling_dump(&self) -> Result<String> {
4245
let timestamp = time::SystemTime::now()

rpc/src/module/experiment.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::error::RPCError;
22
use crate::module::chain::CyclesEstimator;
3+
use async_trait::async_trait;
34
use ckb_dao::DaoCalculator;
45
use ckb_jsonrpc_types::{
56
Capacity, DaoWithdrawingCalculationKind, EstimateCycles, OutPoint, Transaction,
@@ -8,14 +9,15 @@ use ckb_shared::{shared::Shared, Snapshot};
89
use ckb_store::ChainStore;
910
use ckb_types::{core, packed, prelude::*};
1011
use jsonrpc_core::Result;
11-
use jsonrpc_derive::rpc;
12+
use jsonrpc_utils::rpc;
1213

1314
/// RPC Module Experiment for experimenting methods.
1415
///
1516
/// **EXPERIMENTAL warning**
1617
///
1718
/// The methods here may be removed or changed in future releases without prior notifications.
18-
#[rpc(server)]
19+
#[rpc]
20+
#[async_trait]
1921
pub trait ExperimentRpc {
2022
/// Dry run a transaction and return the execution cycles.
2123
///
@@ -162,10 +164,12 @@ pub trait ExperimentRpc {
162164
) -> Result<Capacity>;
163165
}
164166

167+
#[derive(Clone)]
165168
pub(crate) struct ExperimentRpcImpl {
166169
pub shared: Shared,
167170
}
168171

172+
#[async_trait]
169173
impl ExperimentRpc for ExperimentRpcImpl {
170174
fn dry_run_transaction(&self, tx: Transaction) -> Result<EstimateCycles> {
171175
let tx: packed::Transaction = tx.into();

rpc/src/module/indexer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::error::RPCError;
2+
use async_trait::async_trait;
23
use ckb_indexer::IndexerHandle;
34
use ckb_jsonrpc_types::{
45
IndexerCell, IndexerCellsCapacity, IndexerOrder, IndexerPagination, IndexerSearchKey,
56
IndexerTip, IndexerTx, JsonBytes, Uint32,
67
};
78
use jsonrpc_core::Result;
8-
use jsonrpc_derive::rpc;
9+
use jsonrpc_utils::rpc;
910

1011
/// RPC Module Indexer.
11-
#[rpc(server)]
12+
#[rpc]
13+
#[async_trait]
1214
pub trait IndexerRpc {
1315
/// Returns the indexed tip
1416
///
@@ -873,6 +875,7 @@ pub trait IndexerRpc {
873875
) -> Result<Option<IndexerCellsCapacity>>;
874876
}
875877

878+
#[derive(Clone)]
876879
pub(crate) struct IndexerRpcImpl {
877880
pub(crate) handle: IndexerHandle,
878881
}
@@ -883,6 +886,7 @@ impl IndexerRpcImpl {
883886
}
884887
}
885888

889+
#[async_trait]
886890
impl IndexerRpc for IndexerRpcImpl {
887891
fn get_indexer_tip(&self) -> Result<Option<IndexerTip>> {
888892
self.handle

rpc/src/module/miner.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::error::RPCError;
2+
use async_trait::async_trait;
23
use ckb_chain::chain::ChainController;
34
use ckb_jsonrpc_types::{Block, BlockTemplate, Uint64, Version};
45
use ckb_logger::{debug, error, info, warn};
@@ -9,7 +10,7 @@ use ckb_types::{core, packed, prelude::*, H256};
910
use ckb_verification::HeaderVerifier;
1011
use ckb_verification_traits::Verifier;
1112
use jsonrpc_core::{Error, Result};
12-
use jsonrpc_derive::rpc;
13+
use jsonrpc_utils::rpc;
1314
use std::collections::HashSet;
1415
use std::fmt::Debug;
1516
use std::sync::Arc;
@@ -18,7 +19,8 @@ use std::sync::Arc;
1819
///
1920
/// A miner gets a template from CKB, optionally selects transactions, resolves the PoW puzzle, and
2021
/// submits the found new block.
21-
#[rpc(server)]
22+
#[rpc]
23+
#[async_trait]
2224
pub trait MinerRpc {
2325
/// Returns block template for miners.
2426
///
@@ -223,12 +225,14 @@ pub trait MinerRpc {
223225
fn submit_block(&self, work_id: String, block: Block) -> Result<H256>;
224226
}
225227

228+
#[derive(Clone)]
226229
pub(crate) struct MinerRpcImpl {
227230
pub network_controller: NetworkController,
228231
pub shared: Shared,
229232
pub chain: ChainController,
230233
}
231234

235+
#[async_trait]
232236
impl MinerRpc for MinerRpcImpl {
233237
fn get_block_template(
234238
&self,

rpc/src/module/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,17 @@ pub(crate) use self::miner::MinerRpcImpl;
131131
pub(crate) use self::net::NetRpcImpl;
132132
pub(crate) use self::pool::PoolRpcImpl;
133133
pub(crate) use self::stats::StatsRpcImpl;
134-
pub(crate) use self::subscription::{SubscriptionRpcImpl, SubscriptionSession};
134+
pub(crate) use self::subscription::SubscriptionRpcImpl;
135135
pub(crate) use self::test::IntegrationTestRpcImpl;
136136

137-
pub use self::alert::AlertRpc;
138-
pub use self::chain::ChainRpc;
139-
pub use self::debug::DebugRpc;
140-
pub use self::experiment::ExperimentRpc;
141-
pub use self::indexer::IndexerRpc;
142-
pub use self::miner::MinerRpc;
143-
pub use self::net::NetRpc;
144-
pub use self::pool::PoolRpc;
145-
pub use self::stats::StatsRpc;
146-
pub use self::subscription::SubscriptionRpc;
147-
pub use self::test::IntegrationTestRpc;
137+
pub use self::alert::{add_alert_rpc_methods, AlertRpc};
138+
pub use self::chain::{add_chain_rpc_methods, ChainRpc};
139+
pub use self::debug::{add_debug_rpc_methods, DebugRpc};
140+
pub use self::experiment::{add_experiment_rpc_methods, ExperimentRpc};
141+
pub use self::indexer::{add_indexer_rpc_methods, IndexerRpc};
142+
pub use self::miner::{add_miner_rpc_methods, MinerRpc};
143+
pub use self::net::{add_net_rpc_methods, NetRpc};
144+
pub use self::pool::{add_pool_rpc_methods, PoolRpc};
145+
pub use self::stats::{add_stats_rpc_methods, StatsRpc};
146+
pub use self::subscription::{add_subscription_rpc_methods, SubscriptionRpc};
147+
pub use self::test::{add_integration_test_rpc_methods, IntegrationTestRpc};

0 commit comments

Comments
 (0)