Skip to content

Commit 1a3c98c

Browse files
kakao-jun-ejoojis
authored andcommitted
Add getBestBlockId RPC method
1 parent fd07903 commit 1a3c98c

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

rpc/src/v1/impls/chain.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use jsonrpc_core::Result;
2828

2929
use super::super::errors;
3030
use super::super::traits::Chain;
31-
use super::super::types::{Block, Bytes, Parcel};
31+
use super::super::types::{Block, BlockNumberAndHash, Bytes, Parcel};
3232

3333
pub struct ChainClient {
3434
client: Arc<Client>,
@@ -122,6 +122,13 @@ impl Chain for ChainClient {
122122
Ok(self.client.chain_info().best_block_number)
123123
}
124124

125+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash> {
126+
Ok(BlockNumberAndHash {
127+
number: self.client.chain_info().best_block_number,
128+
hash: self.client.chain_info().best_block_hash,
129+
})
130+
}
131+
125132
fn get_block_hash(&self, block_number: u64) -> Result<Option<H256>> {
126133
Ok(self.client.block_hash(BlockId::Number(block_number)))
127134
}

rpc/src/v1/traits/chain.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ctypes::{H160, H256, Public, U256};
1919

2020
use jsonrpc_core::Result;
2121

22-
use super::super::types::{Block, Bytes, Parcel};
22+
use super::super::types::{Block, BlockNumberAndHash, Bytes, Parcel};
2323

2424
build_rpc_trait! {
2525
pub trait Chain {
@@ -71,6 +71,10 @@ build_rpc_trait! {
7171
# [rpc(name = "chain_getBestBlockNumber")]
7272
fn get_best_block_number(&self) -> Result<BlockNumber>;
7373

74+
/// Gets the number and the hash of the best block.
75+
# [rpc(name = "chain_getBestBlockId")]
76+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash>;
77+
7478
/// Gets the hash of the block with given number.
7579
# [rpc(name = "chain_getBlockHash")]
7680
fn get_block_hash(&self, u64) -> Result<Option<H256>>;

rpc/src/v1/types/block.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use ccore::Block as CoreBlock;
17+
use ccore::{Block as CoreBlock, BlockNumber};
1818
use ctypes::{H160, H256, U256};
1919

2020
use super::Parcel;
@@ -84,3 +84,10 @@ impl From<CoreBlock> for Block {
8484
}
8585
}
8686
}
87+
88+
#[derive(Debug, Serialize)]
89+
#[serde(rename_all = "camelCase")]
90+
pub struct BlockNumberAndHash {
91+
pub number: BlockNumber,
92+
pub hash: H256,
93+
}

rpc/src/v1/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod parcel;
2020
mod work;
2121

2222
pub use self::block::Block;
23+
pub use self::block::BlockNumberAndHash;
2324
pub use self::bytes::Bytes;
2425
pub use self::parcel::Parcel;
2526
pub use self::work::Work;

spec/JSON-RPC.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ In the current version, it's only supported through HTTP.
8282
* [version](#version)
8383
***
8484
* [chain_getBestBlockNumber](#chain_getbestblocknumber)
85-
* [chain_getBestBlockId](#chain_getbestblockid) (not implemented yet)
85+
* [chain_getBestBlockId](#chain_getbestblockid)
8686
* [chain_getBlockHash](#chain_getblockhash)
8787
* [chain_getBlockByHash](#chain_getblockbyhash)
8888
* [chain_sendSignedParcel](#chain_sendsignedparcel)
@@ -173,7 +173,7 @@ Response Example
173173
```
174174

175175
## chain_getBestBlockId
176-
(not implemented yet) Gets the number and the hash of the best block.
176+
Gets the number and the hash of the best block.
177177

178178
Params: No parameters
179179

@@ -189,7 +189,7 @@ Request Example
189189

190190
Response Example
191191
```
192-
{"jsonrpc":"2.0","result":{"number":1,"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077"},"id":null}
192+
{"jsonrpc":"2.0","result":{"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077","number":1},"id":null}
193193
```
194194

195195
## chain_getBlockHash

0 commit comments

Comments
 (0)