Skip to content

Commit f0d7633

Browse files
Seulgi Kimsgkim126
authored andcommitted
Add chain_getShardRoot RPC
1 parent 0f25945 commit f0d7633

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

core/src/client/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,14 @@ impl Shard for Client {
866866
};
867867
state.number_of_shards().ok()
868868
}
869+
870+
fn shard_root(&self, shard_id: u32, state: StateOrBlock) -> Option<H256> {
871+
let state = match state {
872+
StateOrBlock::State(s) => s,
873+
StateOrBlock::Block(id) => Box::new(self.state_at(id)?),
874+
};
875+
state.shard_root(shard_id).ok()?
876+
}
869877
}
870878

871879
impl ReopenBlock for Client {

core/src/client/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ pub trait RegularKey {
140140

141141
pub trait Shard {
142142
fn number_of_shards(&self, state: StateOrBlock) -> Option<u32>;
143+
144+
fn shard_root(&self, shard_id: u32, state: StateOrBlock) -> Option<H256>;
143145
}
144146

145147
/// Provides methods to access account info

rpc/src/v1/impls/chain.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ impl Chain for ChainClient {
113113
Ok(self.client.number_of_shards(block_id.into()))
114114
}
115115

116+
fn get_shard_root(&self, shard_id: u32, block_number: Option<u64>) -> Result<Option<H256>> {
117+
let block_id = block_number.map(BlockId::Number).unwrap_or(BlockId::Latest);
118+
Ok(self.client.shard_root(shard_id, block_id.into()))
119+
}
120+
116121
fn get_best_block_number(&self) -> Result<BlockNumber> {
117122
Ok(self.client.chain_info().best_block_number)
118123
}

rpc/src/v1/traits/chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ build_rpc_trait! {
6363
# [rpc(name = "chain_getNumberOfShards")]
6464
fn get_number_of_shards(&self, Option<u64>) -> Result<Option<u32>>;
6565

66+
/// Gets shard root
67+
# [rpc(name = "chain_getShardRoot")]
68+
fn get_shard_root(&self, u32, Option<u64>) -> Result<Option<H256>>;
69+
6670
/// Gets number of best block.
6771
# [rpc(name = "chain_getBestBlockNumber")]
6872
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
@@ -94,6 +94,7 @@ In the current version, it's only supported through HTTP.
9494
* [chain_getBalance](#chain_getbalance)
9595
* [chain_getRegularKey](#chain_getregularkey)
9696
* [chain_getNumberOfShards](#chain_getnumberofshards)
97+
* [chain_getShardRoot](#chain_getshardroot)
9798
* [chain_getPendingParcels](#chain_getpendingparcels)
9899
***
99100
* [miner_getWork](#miner_getwork)
@@ -487,6 +488,28 @@ Response Example
487488
{"jsonrpc":"2.0","result":3,"id":null}
488489
```
489490

491+
## chain_getShardRoot
492+
Gets the root of shard, at state of given blockNumber.
493+
494+
Param:
495+
1. shard id: `number`
496+
1. block number: `number` or `null`
497+
498+
Return Type: `null` or `string` - the root of shard
499+
500+
Request Example
501+
```
502+
curl \
503+
-H 'Content-Type: application/json' \
504+
-d '{"jsonrpc": "2.0", "method": "chain_getShardRoot", "params": [1, null], "id": null}' \
505+
localhost:8080
506+
```
507+
508+
Response Example
509+
```
510+
{"jsonrpc":"2.0","result":"0xf3841adc1615bfeabb801dda23585c1722b80d810df084a5f2198e92285d4bfd","id":null}
511+
```
512+
490513

491514
## chain_getPendingParcels
492515
Gets parcels in the current parcel queue.

0 commit comments

Comments
 (0)