Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
199 changes: 179 additions & 20 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions resource/ckb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ modules = ["Net", "Pool", "Miner", "Chain", "Stats", "Experiment"] # {{
# integration => modules = ["Net", "Pool", "Miner", "Chain", "Experiment", "Stats", "Indexer", "IntegrationTest"]
# }}

# By default RPC only binds to HTTP service, you can bind it to TCP and WebSocket.
# tcp_listen_address = "127.0.0.1:18114"
# ws_listen_address = "127.0.0.1:28114"

[tx_pool]
max_mem_size = 20_000_000 # 20mb
max_cycles = 200_000_000_000
Expand Down
10 changes: 6 additions & 4 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ ckb-chain = { path = "../chain" }
ckb-logger = { path = "../util/logger"}
ckb-network-alert = { path = "../util/network-alert" }
ckb-fee-estimator = { path = "../util/fee-estimator" }
jsonrpc-core = "10.1"
jsonrpc-derive = "10.1"
jsonrpc-http-server = { git = "https://github.com/nervosnetwork/jsonrpc", rev = "7c101f83a8fe34369c1b7a0e9b6721fcb0f91ee0" }
jsonrpc-server-utils = "10.1"
jsonrpc-core = "14.0"
jsonrpc-derive = "14.0"
jsonrpc-http-server = "14.0"
jsonrpc-tcp-server = "14.0"
jsonrpc-ws-server = "14.0"
jsonrpc-server-utils = "14.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
num_cpus = "1.10"
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub enum Module {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub listen_address: String,
#[serde(default)]
pub tcp_listen_address: Option<String>,
#[serde(default)]
pub ws_listen_address: Option<String>,
pub max_request_body_size: usize,
pub threads: Option<usize>,
pub modules: Vec<Module>,
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::sync::Arc;

#[rpc]
#[rpc(server)]
pub trait AlertRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"send_alert","params": [{}]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "send_alert")]
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use jsonrpc_derive::rpc;

pub const PAGE_SIZE: u64 = 100;

#[rpc]
#[rpc(server)]
pub trait ChainRpc {
#[rpc(name = "get_block")]
fn get_block(&self, _hash: H256) -> Result<Option<BlockView>>;
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/experiment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use std::collections::HashSet;

#[rpc]
#[rpc(server)]
pub trait ExperimentRpc {
#[rpc(name = "_compute_transaction_hash")]
fn compute_transaction_hash(&self, tx: Transaction) -> Result<H256>;
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ckb_types::{prelude::*, H256};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

#[rpc]
#[rpc(server)]
pub trait IndexerRpc {
#[rpc(name = "get_live_cells_by_lock_hash")]
fn get_live_cells_by_lock_hash(
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::collections::HashSet;
use std::fmt::Debug;
use std::sync::Arc;

#[rpc]
#[rpc(server)]
pub trait MinerRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_block_template","params": ["0x1b1c832d02fdb4339f9868c8a8636c3d9dd10bd53ac7ce99595825bd6beeffb3", 1000, 1000]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "get_block_template")]
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::HashMap;
const MAX_ADDRS: usize = 50;
const DEFAULT_BAN_DURATION: u64 = 24 * 60 * 60 * 1000; // 1 day

#[rpc]
#[rpc(server)]
pub trait NetworkRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"local_node_info","params": []}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "local_node_info")]
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use std::sync::Arc;

#[rpc]
#[rpc(server)]
pub trait PoolRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"send_transaction","params": [{"version":2, "deps":[], "inputs":[], "outputs":[]}]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "send_transaction")]
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::sync::Arc;

#[rpc]
#[rpc(server)]
pub trait StatsRpc {
#[rpc(name = "get_blockchain_info")]
fn get_blockchain_info(&self) -> Result<ChainInfo>;
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/module/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::sync::Arc;

#[rpc]
#[rpc(server)]
pub trait IntegrationTestRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"add_node","params": ["QmUsZHPbjjzU627UZFt4k8j6ycEcNvXRnVGxCPKqwbAfQS", "/ip4/192.168.2.100/tcp/30002"]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "add_node")]
Expand Down
46 changes: 40 additions & 6 deletions rpc/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::config::Config;
use jsonrpc_core::IoHandler;
use jsonrpc_http_server::{Server, ServerBuilder};
use jsonrpc_http_server;
use jsonrpc_server_utils::cors::AccessControlAllowOrigin;
use jsonrpc_server_utils::hosts::DomainsValidation;
use jsonrpc_tcp_server;
use jsonrpc_ws_server;
use std::net::ToSocketAddrs;

pub struct RpcServer {
pub(crate) server: Server,
pub(crate) http: jsonrpc_http_server::Server,
pub(crate) tcp: Option<jsonrpc_tcp_server::Server>,
pub(crate) ws: Option<jsonrpc_ws_server::Server>,
}

impl RpcServer {
pub fn new(config: Config, io_handler: IoHandler) -> RpcServer {
let server = ServerBuilder::new(io_handler)
let http = jsonrpc_http_server::ServerBuilder::new(io_handler.clone())
.cors(DomainsValidation::AllowOnly(vec![
AccessControlAllowOrigin::Null,
AccessControlAllowOrigin::Any,
Expand All @@ -27,12 +31,42 @@ impl RpcServer {
.next()
.expect("config listen_address parsed"),
)
.expect("Jsonrpc initialize");
.expect("Start Jsonrpc HTTP service");

RpcServer { server }
let tcp = config.tcp_listen_address.map(|tcp_listen_address| {
jsonrpc_tcp_server::ServerBuilder::new(io_handler.clone())
.start(
&tcp_listen_address
.to_socket_addrs()
.expect("config tcp_listen_address parsed")
.next()
.expect("config tcp_listen_address parsed"),
)
.expect("Start Jsonrpc TCP service")
});

let ws = config.ws_listen_address.map(|ws_listen_address| {
jsonrpc_ws_server::ServerBuilder::new(io_handler.clone())
.start(
&ws_listen_address
.to_socket_addrs()
.expect("config ws_listen_address parsed")
.next()
.expect("config ws_listen_address parsed"),
)
.expect("Start Jsonrpc WebSocket service")
});

RpcServer { http, tcp, ws }
}

pub fn close(self) {
self.server.close()
self.http.close();
if let Some(tcp) = self.tcp {
tcp.close();
}
if let Some(ws) = self.ws {
ws.close();
}
}
}
14 changes: 8 additions & 6 deletions rpc/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn setup_node(height: u64) -> (Shared, ChainController, RpcServer) {
}
.to_delegate(),
);
let server = ServerBuilder::new(io)
let http = ServerBuilder::new(io)
.cors(DomainsValidation::AllowOnly(vec![
AccessControlAllowOrigin::Null,
AccessControlAllowOrigin::Any,
Expand All @@ -300,7 +300,11 @@ fn setup_node(height: u64) -> (Shared, ChainController, RpcServer) {
.max_request_body_size(20_000_000)
.start_http(&"127.0.0.1:0".parse().unwrap())
.expect("JsonRpc initialize");
let rpc_server = RpcServer { server };
let rpc_server = RpcServer {
http,
tcp: None,
ws: None,
};

(shared, chain_controller, rpc_server)
}
Expand Down Expand Up @@ -492,8 +496,8 @@ fn test_rpc() {
let client = reqwest::Client::new();
let uri = format!(
"http://{}:{}/",
server.server.address().ip(),
server.server.address().port()
server.http.address().ip(),
server.http.address().port()
);

// Assert the params of jsonrpc requests
Expand Down Expand Up @@ -544,6 +548,4 @@ fn test_rpc() {
pretty_assert_eq!(actual, expected, "Assert results of jsonrpc",);
}
}

server.close();
Comment thread
doitian marked this conversation as resolved.
}