Skip to content

Commit

Permalink
feat: rewrite jsonrpc http server
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Feb 12, 2019
1 parent f9ab1b8 commit 6cca12d
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 156 deletions.
72 changes: 31 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ ckb-chain = { path = "../chain" }
ckb-miner = { path = "../miner" }
ckb-protocol = { path = "../protocol" }
ckb-pow = { path = "../pow"}
jsonrpc-core = { git = "https://github.com/nervosnetwork/jsonrpc.git", branch = "2018-edition" }
jsonrpc-macros = { git = "https://github.com/nervosnetwork/jsonrpc.git", branch = "2018-edition" }
jsonrpc-http-server = { git = "https://github.com/nervosnetwork/jsonrpc.git", branch = "2018-edition" }
jsonrpc-server-utils = { git = "https://github.com/nervosnetwork/jsonrpc.git", branch = "2018-edition" }
jsonrpc-core = "10.0"
jsonrpc-derive = "10.0"
jsonrpc-http-server = { git = "https://github.com/nervosnetwork/jsonrpc", branch = "http_remake" }
jsonrpc-server-utils = "10.0"
jsonrpc-pubsub = "10.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
Expand Down
45 changes: 22 additions & 23 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@ use ckb_shared::{
shared::{ChainProvider, Shared},
};
use jsonrpc_core::{Error, Result};
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use jsonrpc_types::{Block, CellOutputWithOutPoint, CellWithStatus, Header, OutPoint, Transaction};
use numext_fixed_hash::H256;

build_rpc_trait! {
pub trait ChainRpc {
#[rpc(name = "get_block")]
fn get_block(&self, _hash: H256) -> Result<Option<Block>>;
#[rpc]
pub trait ChainRpc {
#[rpc(name = "get_block")]
fn get_block(&self, _hash: H256) -> Result<Option<Block>>;

#[rpc(name = "get_transaction")]
fn get_transaction(&self, _hash: H256) -> Result<Option<Transaction>>;
#[rpc(name = "get_transaction")]
fn get_transaction(&self, _hash: H256) -> Result<Option<Transaction>>;

#[rpc(name = "get_block_hash")]
fn get_block_hash(&self, _number: u64) -> Result<Option<H256>>;
#[rpc(name = "get_block_hash")]
fn get_block_hash(&self, _number: u64) -> Result<Option<H256>>;

#[rpc(name = "get_tip_header")]
fn get_tip_header(&self) -> Result<Header>;
#[rpc(name = "get_tip_header")]
fn get_tip_header(&self) -> Result<Header>;

#[rpc(name = "get_cells_by_type_hash")]
fn get_cells_by_type_hash(
&self,
_type_hash: H256,
_from: BlockNumber,
_to: BlockNumber
) -> Result<Vec<CellOutputWithOutPoint>>;
#[rpc(name = "get_cells_by_type_hash")]
fn get_cells_by_type_hash(
&self,
_type_hash: H256,
_from: BlockNumber,
_to: BlockNumber,
) -> Result<Vec<CellOutputWithOutPoint>>;

#[rpc(name = "get_live_cell")]
fn get_live_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;
#[rpc(name = "get_live_cell")]
fn get_live_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;

#[rpc(name = "get_tip_block_number")]
fn get_tip_block_number(&self) -> Result<BlockNumber>;
}
#[rpc(name = "get_tip_block_number")]
fn get_tip_block_number(&self) -> Result<BlockNumber>;
}

pub(crate) struct ChainRpcImpl<CI> {
Expand Down
24 changes: 14 additions & 10 deletions rpc/src/module/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ use ckb_shared::{index::ChainIndex, shared::Shared};
use ckb_sync::RELAY_PROTOCOL_ID;
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::{Error, Result};
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use jsonrpc_types::{Block, BlockTemplate};
use log::debug;
use numext_fixed_hash::H256;
use std::collections::HashSet;
use std::sync::Arc;

build_rpc_trait! {
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")]
fn get_block_template(&self, cycles_limit: Option<u64>, bytes_limit: Option<u64>, max_version: Option<u32>) -> Result<BlockTemplate>;
#[rpc]
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")]
fn get_block_template(
&self,
cycles_limit: Option<u64>,
bytes_limit: Option<u64>,
max_version: Option<u32>,
) -> Result<BlockTemplate>;

// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"submit_block","params": [{"header":{}, "uncles":[], "commit_transactions":[], "proposal_transactions":[]}]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "submit_block")]
fn submit_block(&self, _work_id: String, _data: Block) -> Result<H256>;
}
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"submit_block","params": [{"header":{}, "uncles":[], "commit_transactions":[], "proposal_transactions":[]}]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "submit_block")]
fn submit_block(&self, _work_id: String, _data: Block) -> Result<H256>;
}

pub(crate) struct MinerRpcImpl<CI> {
Expand Down
13 changes: 6 additions & 7 deletions rpc/src/module/net.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use build_info::{get_version, Version};
use ckb_network::NetworkService;
use jsonrpc_core::Result;
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use jsonrpc_types::{LocalNode, NodeAddress};
use std::sync::Arc;

const MAX_ADDRS: usize = 50;

build_rpc_trait! {
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")]
fn local_node_info(&self) -> Result<LocalNode>;
}
#[rpc]
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")]
fn local_node_info(&self) -> Result<LocalNode>;
}

pub(crate) struct NetworkRpcImpl {
Expand Down
13 changes: 6 additions & 7 deletions rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ use ckb_protocol::RelayMessage;
use ckb_sync::RELAY_PROTOCOL_ID;
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::Result;
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use jsonrpc_types::Transaction;
use log::debug;
use numext_fixed_hash::H256;
use std::sync::Arc;

build_rpc_trait! {
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")]
fn send_transaction(&self, _tx: Transaction) -> Result<H256>;
}
#[rpc]
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")]
fn send_transaction(&self, _tx: Transaction) -> Result<H256>;
}

pub(crate) struct PoolRpcImpl {
Expand Down
17 changes: 8 additions & 9 deletions rpc/src/module/test.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use ckb_network::NetworkService;
use ckb_pow::Clicker;
use jsonrpc_core::Result;
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use std::sync::Arc;

build_rpc_trait! {
pub trait IntegrationTestRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"submit_solution","params": [1]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "submit_pow_solution")]
fn submit_pow_solution(&self, _nonce: u64) -> Result<()>;
#[rpc]
pub trait IntegrationTestRpc {
// curl -d '{"id": 2, "jsonrpc": "2.0", "method":"submit_solution","params": [1]}' -H 'content-type:application/json' 'http://localhost:8114'
#[rpc(name = "submit_pow_solution")]
fn submit_pow_solution(&self, _nonce: u64) -> Result<()>;

#[rpc(name = "add_node")]
fn add_node(&self, _node_id: String) -> Result<()>;
}
#[rpc(name = "add_node")]
fn add_node(&self, _node_id: String) -> Result<()>;
}

pub(crate) struct IntegrationTestRpcImpl {
Expand Down
15 changes: 7 additions & 8 deletions rpc/src/module/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ use ckb_protocol::RelayMessage;
use ckb_sync::RELAY_PROTOCOL_ID;
use flatbuffers::FlatBufferBuilder;
use jsonrpc_core::Result;
use jsonrpc_macros::build_rpc_trait;
use jsonrpc_derive::rpc;
use jsonrpc_types::Transaction;
use log::debug;
use numext_fixed_hash::H256;
use std::sync::Arc;

build_rpc_trait! {
pub trait TraceRpc {
#[rpc(name = "trace_transaction")]
fn trace_transaction(&self, _tx: Transaction) -> Result<H256>;
#[rpc]
pub trait TraceRpc {
#[rpc(name = "trace_transaction")]
fn trace_transaction(&self, _tx: Transaction) -> Result<H256>;

#[rpc(name = "get_transaction_trace")]
fn get_transaction_trace(&self, _hash: H256) -> Result<Option<Vec<TxTrace>>> ;
}
#[rpc(name = "get_transaction_trace")]
fn get_transaction_trace(&self, _hash: H256) -> Result<Option<Vec<TxTrace>>>;
}

pub(crate) struct TraceRpcImpl {
Expand Down
Loading

0 comments on commit 6cca12d

Please sign in to comment.