Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use jsonrpc-utils to replace jsonrpc #4170

Merged
merged 31 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
26e6839
move alert and chain to jsonrpc_utils
chenyukang May 9, 2023
c3eaec2
make the ping router works
chenyukang Sep 19, 2023
ab2ed6c
fix rpc api router issues
chenyukang Sep 20, 2023
5a372b4
migrate more rpc apis to jsonrpc_utils
chenyukang Sep 20, 2023
1b517df
fix deprecated issues
chenyukang Sep 20, 2023
483f623
fix config of RpcServer
chenyukang Sep 20, 2023
ebba92c
fix rpc test
chenyukang Sep 21, 2023
d8cdb7d
test tcp for rpc
chenyukang Sep 22, 2023
1d3665d
working on pubsub
chenyukang Sep 22, 2023
84cc344
refactor subscription rpc
chenyukang Sep 24, 2023
c87b77b
fix tcp stream closing issues
chenyukang Sep 24, 2023
5917afb
fix clippy and test
chenyukang Sep 24, 2023
4734fc7
fix dep checks
chenyukang Sep 25, 2023
641207b
code refactor on rpc server
chenyukang Sep 25, 2023
92a0ec7
update jsonrpc-utils for CI
chenyukang Sep 25, 2023
7c3801d
refactor rpc test setup and fix clippy
chenyukang Sep 25, 2023
aad0a60
fix rpc doc
chenyukang Sep 25, 2023
42abf0a
fix SubscriptionRpc document issue
chenyukang Sep 26, 2023
306b0d1
update jsonrpc-utils and remove missing_docs
chenyukang Sep 29, 2023
3199475
fix conflicts
chenyukang Oct 13, 2023
1d39048
fix conflict error
chenyukang Nov 21, 2023
c6b5f63
fix borrow lints
chenyukang Nov 21, 2023
bd79f04
remove keep alive for tcp
chenyukang Nov 27, 2023
1eeb02c
fix websocket disconnect issue
chenyukang Nov 27, 2023
b08ac61
use with_shutdown for tcp connection
chenyukang Nov 30, 2023
b0cb0fc
update jsonrpc-utils for better parameter error messages
chenyukang Nov 30, 2023
1aa9a5a
remove unnecessary clone
chenyukang Dec 5, 2023
f9c0092
comments feedback
chenyukang Dec 5, 2023
6868b40
fix SubscriptionRpc
chenyukang Dec 5, 2023
2d960b9
update jsonrpc-utils
chenyukang Dec 11, 2023
1b16d6e
update axum
chenyukang Dec 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
732 changes: 306 additions & 426 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions ckb-bin/src/subcommand/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub fn run(args: RunArgs, version: Version, async_handle: Handle) -> Result<(),
deadlock_detection();

info!("ckb version: {}", version);

let mut launcher = Launcher::new(args, version, async_handle);

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

launcher.start_block_filter(&shared);

let (network_controller, _rpc_server) = launcher.start_network_and_rpc(
let network_controller = launcher.start_network_and_rpc(
&shared,
chain_controller.clone(),
miner_enable,
Expand Down
19 changes: 11 additions & 8 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ ckb-shared = { path = "../shared", version = "= 0.113.0-pre" }
ckb-store = { path = "../store", version = "= 0.113.0-pre" }
ckb-sync = { path = "../sync", version = "= 0.113.0-pre" }
ckb-chain = { path = "../chain", version = "= 0.113.0-pre" }
ckb-logger = { path = "../util/logger", version = "= 0.113.0-pre"}
ckb-logger-service = { path = "../util/logger-service", version = "= 0.113.0-pre"}
ckb-logger = { path = "../util/logger", version = "= 0.113.0-pre" }
ckb-logger-service = { path = "../util/logger-service", version = "= 0.113.0-pre" }
ckb-network-alert = { path = "../util/network-alert", version = "= 0.113.0-pre" }
ckb-app-config = { path = "../util/app-config", version = "= 0.113.0-pre" }
ckb-constant = { path = "../util/constant", version = "= 0.113.0-pre" }
jsonrpc-core = "18.0"
jsonrpc-derive = "18.0"
jsonrpc-http-server = "18.0"
jsonrpc-tcp-server = "18.0"
jsonrpc-ws-server = "18.0"
jsonrpc-server-utils = "18.0"
jsonrpc-pubsub = "18.0"
serde_json = "1.0"
jsonrpc-utils = { version = "0.2.6", features = ["server", "macros", "axum"] }
ckb-jsonrpc-types = { path = "../util/jsonrpc-types", version = "= 0.113.0-pre" }
ckb-verification = { path = "../verification", version = "= 0.113.0-pre" }
ckb-verification-traits = { path = "../verification/traits", version = "= 0.113.0-pre" }
Expand All @@ -43,8 +38,16 @@ ckb-tx-pool = { path = "../tx-pool", version = "= 0.113.0-pre" }
ckb-memory-tracker = { path = "../util/memory-tracker", version = "= 0.113.0-pre" }
ckb-pow = { path = "../pow", version = "= 0.113.0-pre" }
ckb-indexer = { path = "../util/indexer", version = "= 0.113.0-pre" }
ckb-stop-handler = { path = "../util/stop-handler", version = "= 0.113.0-pre" }
itertools.workspace = true
tokio = "1"
async-trait = "0.1"
axum = "0.6.20"
tokio-util = { version = "0.7.3", features = ["codec"] }
futures-util = { version = "0.3.21" }
tower-http = { version = "0.3.5", features = ["timeout"] }
async-stream = "0.3.3"
ckb-async-runtime = { path = "../util/runtime", version = "= 0.113.0-pre" }

[dev-dependencies]
reqwest = { version = "=0.11.20", features = ["blocking", "json"] }
Expand Down
26 changes: 6 additions & 20 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
* [Method `get_deployments_info`](#method-get_deployments_info)
* [Module Subscription](#module-subscription)
* [Method `subscribe`](#method-subscribe)
* [Method `unsubscribe`](#method-unsubscribe)
chenyukang marked this conversation as resolved.
Show resolved Hide resolved
* [RPC Errors](#rpc-errors)
* [RPC Types](#rpc-types)
* [Type `Alert`](#type-alert)
Expand Down Expand Up @@ -4956,7 +4955,7 @@ The type of the `params.result` in the push message is a two-elements array, whe

###### Examples

Request
Subscribe Request


```
Expand All @@ -4971,32 +4970,19 @@ Request
```


Response
Subscribe Response


```
{
"id": 42,
"jsonrpc": "2.0",
"result": "0x2a"
"result": "0xf3"
}
```


#### Method `unsubscribe`
* `unsubscribe(id)`
* `id`: `string`
* result: `boolean`

Unsubscribes from a subscribed topic.

###### Params

* `id` - Subscription ID

###### Examples

Request
Unsubscribe Request


```
Expand All @@ -5005,13 +4991,13 @@ Request
"jsonrpc": "2.0",
"method": "unsubscribe",
"params": [
"0x2a"
"0xf3"
]
}
```


Response
Unsubscribe Response


```
Expand Down
5 changes: 4 additions & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ pub mod module;
#[cfg(test)]
mod tests;

use jsonrpc_core::MetaIoHandler;
use jsonrpc_utils::pub_sub::Session;

pub use crate::error::RPCError;
pub use crate::server::RpcServer;
pub use crate::service_builder::ServiceBuilder;

#[doc(hidden)]
pub type IoHandler = jsonrpc_pubsub::PubSubHandler<Option<crate::module::SubscriptionSession>>;
pub type IoHandler = MetaIoHandler<Option<Session>>;
9 changes: 7 additions & 2 deletions rpc/src/module/alert.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::error::RPCError;
use async_trait::async_trait;
use ckb_jsonrpc_types::Alert;
use ckb_logger::error;
use ckb_network::{NetworkController, SupportProtocols};
use ckb_network_alert::{notifier::Notifier as AlertNotifier, verifier::Verifier as AlertVerifier};
use ckb_types::{packed, prelude::*};
use ckb_util::Mutex;

use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;
use std::sync::Arc;

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

#[derive(Clone)]
pub(crate) struct AlertRpcImpl {
network_controller: NetworkController,
verifier: Arc<AlertVerifier>,
Expand All @@ -90,6 +94,7 @@ impl AlertRpcImpl {
}
}

#[async_trait]
impl AlertRpc for AlertRpcImpl {
fn send_alert(&self, alert: Alert) -> Result<()> {
let alert: packed::Alert = alert.into();
Expand Down
8 changes: 6 additions & 2 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::RPCError;
use crate::util::FeeRateCollector;
use async_trait::async_trait;
use ckb_jsonrpc_types::{
BlockEconomicState, BlockFilter, BlockNumber, BlockResponse, BlockView, CellWithStatus,
Consensus, EpochNumber, EpochView, EstimateCycles, FeeRateStatistics, HeaderView, OutPoint,
Expand All @@ -26,7 +27,7 @@ use ckb_types::{
use ckb_verification::ScriptVerifier;
use ckb_verification::TxVerifyEnv;
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;
use std::collections::HashSet;
use std::sync::Arc;

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

#[derive(Clone)]
pub(crate) struct ChainRpcImpl {
pub shared: Shared,
}
Expand All @@ -1614,6 +1617,7 @@ const DEFAULT_BLOCK_VERBOSITY_LEVEL: u32 = 2;
const DEFAULT_HEADER_VERBOSITY_LEVEL: u32 = 1;
const DEFAULT_GET_TRANSACTION_VERBOSITY_LEVEL: u32 = 2;

#[async_trait]
impl ChainRpc for ChainRpcImpl {
fn get_block(
&self,
Expand Down
9 changes: 6 additions & 3 deletions rpc/src/module/debug.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use async_trait::async_trait;
use ckb_jsonrpc_types::{ExtraLoggerConfig, MainLoggerConfig};
use ckb_logger_service::Logger;
use jsonrpc_core::{Error, ErrorCode::InternalError, Result};
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;
use std::time;

/// RPC Module Debug for internal RPC methods.
///
/// **This module is for CKB developers and will not guarantee compatibility.** The methods here
/// will be changed or removed without advanced notification.
#[rpc(server)]
#[doc(hidden)]
#[rpc]
#[async_trait]
pub trait DebugRpc {
/// Dumps jemalloc memory profiling information into a file.
///
Expand All @@ -35,8 +36,10 @@ pub trait DebugRpc {
fn set_extra_logger(&self, name: String, config_opt: Option<ExtraLoggerConfig>) -> Result<()>;
}

#[derive(Clone)]
pub(crate) struct DebugRpcImpl {}

#[async_trait]
impl DebugRpc for DebugRpcImpl {
fn jemalloc_profiling_dump(&self) -> Result<String> {
let timestamp = time::SystemTime::now()
Expand Down
8 changes: 6 additions & 2 deletions rpc/src/module/experiment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::RPCError;
use crate::module::chain::CyclesEstimator;
use async_trait::async_trait;
use ckb_dao::DaoCalculator;
use ckb_jsonrpc_types::{
Capacity, DaoWithdrawingCalculationKind, EstimateCycles, OutPoint, Transaction,
Expand All @@ -8,14 +9,15 @@ use ckb_shared::{shared::Shared, Snapshot};
use ckb_store::ChainStore;
use ckb_types::{core, packed, prelude::*};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;

/// RPC Module Experiment for experimenting methods.
///
/// **EXPERIMENTAL warning**
///
/// The methods here may be removed or changed in future releases without prior notifications.
#[rpc(server)]
#[rpc]
#[async_trait]
pub trait ExperimentRpc {
/// Dry run a transaction and return the execution cycles.
///
Expand Down Expand Up @@ -162,10 +164,12 @@ pub trait ExperimentRpc {
) -> Result<Capacity>;
}

#[derive(Clone)]
pub(crate) struct ExperimentRpcImpl {
pub shared: Shared,
}

#[async_trait]
impl ExperimentRpc for ExperimentRpcImpl {
fn dry_run_transaction(&self, tx: Transaction) -> Result<EstimateCycles> {
let tx: packed::Transaction = tx.into();
Expand Down
8 changes: 6 additions & 2 deletions rpc/src/module/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::error::RPCError;
use async_trait::async_trait;
use ckb_indexer::IndexerHandle;
use ckb_jsonrpc_types::{
IndexerCell, IndexerCellsCapacity, IndexerOrder, IndexerPagination, IndexerSearchKey,
IndexerTip, IndexerTx, JsonBytes, Uint32,
};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;

/// RPC Module Indexer.
#[rpc(server)]
#[rpc]
#[async_trait]
pub trait IndexerRpc {
/// Returns the indexed tip
///
Expand Down Expand Up @@ -873,6 +875,7 @@ pub trait IndexerRpc {
) -> Result<Option<IndexerCellsCapacity>>;
}

#[derive(Clone)]
pub(crate) struct IndexerRpcImpl {
pub(crate) handle: IndexerHandle,
}
Expand All @@ -883,6 +886,7 @@ impl IndexerRpcImpl {
}
}

#[async_trait]
impl IndexerRpc for IndexerRpcImpl {
fn get_indexer_tip(&self) -> Result<Option<IndexerTip>> {
self.handle
Expand Down
8 changes: 6 additions & 2 deletions rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::RPCError;
use async_trait::async_trait;
use ckb_chain::chain::ChainController;
use ckb_jsonrpc_types::{Block, BlockTemplate, Uint64, Version};
use ckb_logger::{debug, error, info, warn};
Expand All @@ -9,7 +10,7 @@ use ckb_types::{core, packed, prelude::*, H256};
use ckb_verification::HeaderVerifier;
use ckb_verification_traits::Verifier;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use jsonrpc_utils::rpc;
use std::collections::HashSet;
use std::fmt::Debug;
use std::sync::Arc;
Expand All @@ -18,7 +19,8 @@ use std::sync::Arc;
///
/// A miner gets a template from CKB, optionally selects transactions, resolves the PoW puzzle, and
/// submits the found new block.
#[rpc(server)]
#[rpc]
#[async_trait]
pub trait MinerRpc {
/// Returns block template for miners.
///
Expand Down Expand Up @@ -223,12 +225,14 @@ pub trait MinerRpc {
fn submit_block(&self, work_id: String, block: Block) -> Result<H256>;
}

#[derive(Clone)]
pub(crate) struct MinerRpcImpl {
pub network_controller: NetworkController,
pub shared: Shared,
pub chain: ChainController,
}

#[async_trait]
impl MinerRpc for MinerRpcImpl {
fn get_block_template(
&self,
Expand Down
24 changes: 12 additions & 12 deletions rpc/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,17 @@ pub(crate) use self::miner::MinerRpcImpl;
pub(crate) use self::net::NetRpcImpl;
pub(crate) use self::pool::PoolRpcImpl;
pub(crate) use self::stats::StatsRpcImpl;
pub(crate) use self::subscription::{SubscriptionRpcImpl, SubscriptionSession};
pub(crate) use self::subscription::SubscriptionRpcImpl;
pub(crate) use self::test::IntegrationTestRpcImpl;

pub use self::alert::AlertRpc;
pub use self::chain::ChainRpc;
pub use self::debug::DebugRpc;
pub use self::experiment::ExperimentRpc;
pub use self::indexer::IndexerRpc;
pub use self::miner::MinerRpc;
pub use self::net::NetRpc;
pub use self::pool::PoolRpc;
pub use self::stats::StatsRpc;
pub use self::subscription::SubscriptionRpc;
pub use self::test::IntegrationTestRpc;
pub use self::alert::{add_alert_rpc_methods, AlertRpc};
pub use self::chain::{add_chain_rpc_methods, ChainRpc};
pub use self::debug::{add_debug_rpc_methods, DebugRpc};
pub use self::experiment::{add_experiment_rpc_methods, ExperimentRpc};
pub use self::indexer::{add_indexer_rpc_methods, IndexerRpc};
pub use self::miner::{add_miner_rpc_methods, MinerRpc};
pub use self::net::{add_net_rpc_methods, NetRpc};
pub use self::pool::{add_pool_rpc_methods, PoolRpc};
pub use self::stats::{add_stats_rpc_methods, StatsRpc};
pub use self::subscription::{add_subscription_rpc_methods, SubscriptionRpc};
pub use self::test::{add_integration_test_rpc_methods, IntegrationTestRpc};
Loading
Loading