Skip to content

Commit 0f25945

Browse files
Seulgi Kimsgkim126
authored andcommitted
Add chain_getNumberOfShards RPC
1 parent e22ef56 commit 0f25945

File tree

9 files changed

+61
-3
lines changed

9 files changed

+61
-3
lines changed

core/src/client/client.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use super::{
5454
AccountData, Balance, BlockChain as BlockChainTrait, BlockChainClient, BlockChainInfo, BlockInfo, BlockProducer,
5555
ChainInfo, ChainNotify, ClientConfig, EngineClient, Error as ClientError, ImportBlock, ImportResult,
5656
ImportSealedBlock, Invoice, MiningBlockChainClient, Nonce, ParcelInfo, PrepareOpenBlock, RegularKey, ReopenBlock,
57-
StateOrBlock,
57+
Shard, StateOrBlock,
5858
};
5959

6060
const MAX_MEM_POOL_SIZE: usize = 4096;
@@ -858,6 +858,16 @@ impl RegularKey for Client {
858858
}
859859
}
860860

861+
impl Shard for Client {
862+
fn number_of_shards(&self, state: StateOrBlock) -> Option<u32> {
863+
let state = match state {
864+
StateOrBlock::State(s) => s,
865+
StateOrBlock::Block(id) => Box::new(self.state_at(id)?),
866+
};
867+
state.number_of_shards().ok()
868+
}
869+
}
870+
861871
impl ReopenBlock for Client {
862872
fn reopen_block(&self, block: ClosedBlock) -> OpenBlock {
863873
let engine = &*self.engine;

core/src/client/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ pub trait RegularKey {
138138
fn regular_key(&self, address: &Address, state: StateOrBlock) -> Option<Public>;
139139
}
140140

141+
pub trait Shard {
142+
fn number_of_shards(&self, state: StateOrBlock) -> Option<u32>;
143+
}
144+
141145
/// Provides methods to access account info
142146
pub trait AccountData: Nonce + Balance {}
143147

core/src/client/test_client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ impl TopStateInfo for () {
445445
unimplemented!()
446446
}
447447

448+
fn number_of_shards(&self) -> trie::Result<u32> {
449+
unimplemented!()
450+
}
451+
448452
fn shard_root(&self, _shard_id: u32) -> trie::Result<Option<H256>> {
449453
unimplemented!()
450454
}

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub use account_provider::AccountProvider;
9090
pub use block::Block;
9191
pub use blockchain::ParcelInvoice;
9292
pub use client::{
93-
Balance, BlockChainClient, BlockInfo, ChainInfo, ChainNotify, Client, ImportBlock, Nonce, RegularKey,
93+
Balance, BlockChainClient, BlockInfo, ChainInfo, ChainNotify, Client, ImportBlock, Nonce, RegularKey, Shard,
9494
TestBlockChainClient,
9595
};
9696
pub use db::COL_STATE;

core/src/state/info.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub trait TopStateInfo {
2929
/// Get the regular key of account `a`.
3030
fn regular_key(&self, a: &Address) -> TrieResult<Option<Public>>;
3131

32+
fn number_of_shards(&self) -> TrieResult<u32>;
33+
3234
fn shard_root(&self, shard_id: u32) -> TrieResult<Option<H256>>;
3335

3436
/// Get the asset scheme.

core/src/state/top_level.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl<B: Backend + TopBackend + ShardBackend + Clone> TopStateInfo for TopLevelSt
136136
self.ensure_account_cached(a, |a| a.as_ref().map_or(None, |account| account.regular_key()))
137137
}
138138

139+
fn number_of_shards(&self) -> TrieResult<u32> {
140+
let metadata = self.require_metadata()?;
141+
Ok(*metadata.number_of_shards())
142+
}
139143

140144
fn shard_root(&self, shard_id: u32) -> TrieResult<Option<H256>> {
141145
let shard_address = ShardAddress::new(shard_id);

rpc/src/v1/impls/chain.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use std::sync::Arc;
1818

1919
use ccore::{
2020
Asset, AssetAddress, AssetScheme, AssetSchemeAddress, Balance, BlockChainClient, BlockId, BlockInfo, BlockNumber,
21-
ChainInfo, Client, Invoice, Miner, MinerService, Nonce, ParcelInvoice, RegularKey, SignedParcel, TopStateInfo,
21+
ChainInfo, Client, Invoice, Miner, MinerService, Nonce, ParcelInvoice, RegularKey, Shard, SignedParcel,
22+
TopStateInfo,
2223
};
2324
use ctypes::{H160, H256, Public, U256};
2425
use rlp::UntrustedRlp;
@@ -106,6 +107,12 @@ impl Chain for ChainClient {
106107
Ok(self.client.regular_key(&address.into(), block_id.into()))
107108
}
108109

110+
111+
fn get_number_of_shards(&self, block_number: Option<u64>) -> Result<Option<u32>> {
112+
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
113+
Ok(self.client.number_of_shards(block_id.into()))
114+
}
115+
109116
fn get_best_block_number(&self) -> Result<BlockNumber> {
110117
Ok(self.client.chain_info().best_block_number)
111118
}

rpc/src/v1/traits/chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ build_rpc_trait! {
5959
# [rpc(name = "chain_getRegularKey")]
6060
fn get_regular_key(&self, H160, Option<u64>) -> Result<Option<Public>>;
6161

62+
/// Gets the number of shards
63+
# [rpc(name = "chain_getNumberOfShards")]
64+
fn get_number_of_shards(&self, Option<u64>) -> Result<Option<u32>>;
65+
6266
/// Gets number of best block.
6367
# [rpc(name = "chain_getBestBlockNumber")]
6468
fn get_best_block_number(&self) -> Result<BlockNumber>;

spec/JSON-RPC.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ In the current version, it's only supported through HTTP.
9393
* [chain_getNonce](#chain_getnonce)
9494
* [chain_getBalance](#chain_getbalance)
9595
* [chain_getRegularKey](#chain_getregularkey)
96+
* [chain_getNumberOfShards](#chain_getnumberofshards)
9697
* [chain_getPendingParcels](#chain_getpendingparcels)
9798
***
9899
* [miner_getWork](#miner_getwork)
@@ -465,6 +466,28 @@ Response Example
465466
{"jsonrpc":"2.0","result":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","id":null}
466467
```
467468

469+
## chain_getNumberOfShards
470+
Gets the number of shards, at state of given blockNumber.
471+
472+
Param:
473+
1. block number: `number` or `null`
474+
475+
Return Type: `number` - the number of shards
476+
477+
Request Example
478+
```
479+
curl \
480+
-H 'Content-Type: application/json' \
481+
-d '{"jsonrpc": "2.0", "method": "chain_getNumberOfShards", "params": [null], "id": null}' \
482+
localhost:8080
483+
```
484+
485+
Response Example
486+
```
487+
{"jsonrpc":"2.0","result":3,"id":null}
488+
```
489+
490+
468491
## chain_getPendingParcels
469492
Gets parcels in the current parcel queue.
470493

0 commit comments

Comments
 (0)