Skip to content

Commit f20f841

Browse files
committed
Add getBestBlockId RPC method
1 parent 752aa7b commit f20f841

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

rpc/src/v1/impls/chain.rs

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

2828
use super::super::errors;
2929
use super::super::traits::Chain;
30-
use super::super::types::{Block, Bytes, Parcel};
30+
use super::super::types::{Block, BlockNumberAndHash, Bytes, Parcel};
3131

3232
pub struct ChainClient {
3333
client: Arc<Client>,
@@ -110,6 +110,13 @@ impl Chain for ChainClient {
110110
Ok(self.client.chain_info().best_block_number)
111111
}
112112

113+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash> {
114+
Ok(BlockNumberAndHash {
115+
number: self.client.chain_info().best_block_number,
116+
hash: self.client.chain_info().best_block_hash,
117+
})
118+
}
119+
113120
fn get_block_hash(&self, block_number: u64) -> Result<Option<H256>> {
114121
Ok(self.client.block_hash(BlockId::Number(block_number)))
115122
}

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 {
@@ -63,6 +63,10 @@ build_rpc_trait! {
6363
# [rpc(name = "chain_getBestBlockNumber")]
6464
fn get_best_block_number(&self) -> Result<BlockNumber>;
6565

66+
/// Gets the number and the hash of the best block.
67+
# [rpc(name = "chain_getBestBlockId")]
68+
fn get_best_block_id(&self) -> Result<BlockNumberAndHash>;
69+
6670
/// Gets the hash of the block with given number.
6771
# [rpc(name = "chain_getBlockHash")]
6872
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ In the current version, it's only supported through HTTP.
7171
* [ping](#ping)
7272
***
7373
* [chain_getBestBlockNumber](#chain_getbestblocknumber)
74-
* [chain_getBestBlockId](#chain_getbestblockid) (not implemented yet)
74+
* [chain_getBestBlockId](#chain_getbestblockid)
7575
* [chain_getBlockHash](#chain_getblockhash)
7676
* [chain_getBlockByHash](#chain_getblockbyhash)
7777
* [chain_sendSignedParcel](#chain_sendsignedparcel)
@@ -139,7 +139,7 @@ Response Example
139139
```
140140

141141
## chain_getBestBlockId
142-
(not implemented yet) Gets the number and the hash of the best block.
142+
Gets the number and the hash of the best block.
143143

144144
Params: No parameters
145145

@@ -155,7 +155,7 @@ Request Example
155155

156156
Response Example
157157
```
158-
{"jsonrpc":"2.0","result":{"number":1,"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077"},"id":null}
158+
{"jsonrpc":"2.0","result":{"hash":"0x56642f04d519ae3262c7ba6facf1c5b11450ebaeb7955337cfbc45420d573077","number":1},"id":null}
159159
```
160160

161161
## chain_getBlockHash
@@ -646,4 +646,4 @@ Request Example
646646
Response Example
647647
```
648648
{"jsonrpc":"2.0","result":["0x20d560025f3a1c6675cb32384355ae05b224a3473ae17d3d15b6aa164af7d717","0xf84541a053000000000000002ab33f741ba153ff1ffdf1107845828637c864d5360e4932a00000000000000000000000000000000000000000000000000000000000000000c06f"],"id":null}
649-
```
649+
```

0 commit comments

Comments
 (0)