From f20f841514e612c3c6c4ad3fa69131fcf193e90d Mon Sep 17 00:00:00 2001 From: LEE Dongjun Date: Wed, 4 Jul 2018 16:54:32 +0900 Subject: [PATCH] Add getBestBlockId RPC method --- rpc/src/v1/impls/chain.rs | 9 ++++++++- rpc/src/v1/traits/chain.rs | 6 +++++- rpc/src/v1/types/block.rs | 9 ++++++++- rpc/src/v1/types/mod.rs | 1 + spec/JSON-RPC.md | 8 ++++---- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/rpc/src/v1/impls/chain.rs b/rpc/src/v1/impls/chain.rs index 3a3bd97f44..14582b9407 100644 --- a/rpc/src/v1/impls/chain.rs +++ b/rpc/src/v1/impls/chain.rs @@ -27,7 +27,7 @@ use jsonrpc_core::Result; use super::super::errors; use super::super::traits::Chain; -use super::super::types::{Block, Bytes, Parcel}; +use super::super::types::{Block, BlockNumberAndHash, Bytes, Parcel}; pub struct ChainClient { client: Arc, @@ -110,6 +110,13 @@ impl Chain for ChainClient { Ok(self.client.chain_info().best_block_number) } + fn get_best_block_id(&self) -> Result { + Ok(BlockNumberAndHash { + number: self.client.chain_info().best_block_number, + hash: self.client.chain_info().best_block_hash, + }) + } + fn get_block_hash(&self, block_number: u64) -> Result> { Ok(self.client.block_hash(BlockId::Number(block_number))) } diff --git a/rpc/src/v1/traits/chain.rs b/rpc/src/v1/traits/chain.rs index c56af26c06..2cc8a59c3d 100644 --- a/rpc/src/v1/traits/chain.rs +++ b/rpc/src/v1/traits/chain.rs @@ -19,7 +19,7 @@ use ctypes::{H160, H256, Public, U256}; use jsonrpc_core::Result; -use super::super::types::{Block, Bytes, Parcel}; +use super::super::types::{Block, BlockNumberAndHash, Bytes, Parcel}; build_rpc_trait! { pub trait Chain { @@ -63,6 +63,10 @@ build_rpc_trait! { # [rpc(name = "chain_getBestBlockNumber")] fn get_best_block_number(&self) -> Result; + /// Gets the number and the hash of the best block. + # [rpc(name = "chain_getBestBlockId")] + fn get_best_block_id(&self) -> Result; + /// Gets the hash of the block with given number. # [rpc(name = "chain_getBlockHash")] fn get_block_hash(&self, u64) -> Result>; diff --git a/rpc/src/v1/types/block.rs b/rpc/src/v1/types/block.rs index 37f7dca1f4..cd2dd12c02 100644 --- a/rpc/src/v1/types/block.rs +++ b/rpc/src/v1/types/block.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use ccore::Block as CoreBlock; +use ccore::{Block as CoreBlock, BlockNumber}; use ctypes::{H160, H256, U256}; use super::Parcel; @@ -84,3 +84,10 @@ impl From for Block { } } } + +#[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct BlockNumberAndHash { + pub number: BlockNumber, + pub hash: H256, +} diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index 4a505a053b..222e2f7c7b 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -20,6 +20,7 @@ mod parcel; mod work; pub use self::block::Block; +pub use self::block::BlockNumberAndHash; pub use self::bytes::Bytes; pub use self::parcel::Parcel; pub use self::work::Work; diff --git a/spec/JSON-RPC.md b/spec/JSON-RPC.md index 7a5a5d78ed..4098a7863f 100644 --- a/spec/JSON-RPC.md +++ b/spec/JSON-RPC.md @@ -71,7 +71,7 @@ In the current version, it's only supported through HTTP. * [ping](#ping) *** * [chain_getBestBlockNumber](#chain_getbestblocknumber) - * [chain_getBestBlockId](#chain_getbestblockid) (not implemented yet) + * [chain_getBestBlockId](#chain_getbestblockid) * [chain_getBlockHash](#chain_getblockhash) * [chain_getBlockByHash](#chain_getblockbyhash) * [chain_sendSignedParcel](#chain_sendsignedparcel) @@ -139,7 +139,7 @@ Response Example ``` ## chain_getBestBlockId -(not implemented yet) Gets the number and the hash of the best block. +Gets the number and the hash of the best block. Params: No parameters @@ -155,7 +155,7 @@ Request Example Response Example ``` -{"jsonrpc":"2.0","result":{"number":1,"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077"},"id":null} +{"jsonrpc":"2.0","result":{"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077","number":1},"id":null} ``` ## chain_getBlockHash @@ -646,4 +646,4 @@ Request Example Response Example ``` {"jsonrpc":"2.0","result":["0x20d560025f3a1c6675cb32384355ae05b224a3473ae17d3d15b6aa164af7d717","0xf84541a053000000000000002ab33f741ba153ff1ffdf1107845828637c864d5360e4932a00000000000000000000000000000000000000000000000000000000000000000c06f"],"id":null} -``` \ No newline at end of file +```