Skip to content

Commit 549bed1

Browse files
committed
add set concurrency level and get concurrency level, and
get height blocks
1 parent 2436a7a commit 549bed1

File tree

8 files changed

+89
-0
lines changed

8 files changed

+89
-0
lines changed

chain/api/src/chain.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub trait ChainReader {
4343
reverse: bool,
4444
count: u64,
4545
) -> Result<Vec<Block>>;
46+
fn get_num_blocks(
47+
&self,
48+
number: BlockNumber
49+
) -> Result<Vec<Block>>;
4650
fn get_block(&self, hash: HashValue) -> Result<Option<Block>>;
4751
/// Get block hash by block number, if not exist, return None
4852
fn get_hash_by_number(&self, number: BlockNumber) -> Result<Option<HashValue>>;

chain/src/chain.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ impl ChainReader for BlockChain {
605605
Ok(blocks)
606606
}
607607

608+
fn get_num_blocks(&self, number: BlockNumber) -> Result<Vec<Block>> {
609+
// XXX FIXME YSG
610+
todo!()
611+
}
612+
608613
fn get_block(&self, hash: HashValue) -> Result<Option<Block>> {
609614
self.storage
610615
.get_block_by_hash(hash)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) The Starcoin Core Contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::cli_state::CliState;
5+
use crate::StarcoinOpt;
6+
use anyhow::Result;
7+
use clap::Parser;
8+
use scmd::{CommandAction, ExecContext};
9+
use starcoin_crypto::HashValue;
10+
use starcoin_rpc_api::chain::GetBlockOption;
11+
use starcoin_rpc_api::types::BlockView;
12+
use std::str::FromStr;
13+
use starcoin_types::block::BlockNumber;
14+
15+
/// Get block by hash or number.
16+
#[derive(Debug, Parser)]
17+
#[clap(name = "get-block", alias = "get_block")]
18+
pub struct GetNumBlocksOpt {
19+
#[clap(name = "number")]
20+
number: BlockNumber,
21+
22+
#[clap(name = "contains-raw-block")]
23+
raw: bool,
24+
}
25+
26+
pub struct GetNumBlocksCommand;
27+
28+
impl CommandAction for GetNumBlocksCommand {
29+
type State = CliState;
30+
type GlobalOpt = StarcoinOpt;
31+
type Opt = GetNumBlocksOpt;
32+
type ReturnItem = Vec<BlockView>;
33+
34+
fn run(
35+
&self,
36+
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
37+
) -> Result<Self::ReturnItem> {
38+
let client = ctx.state().client();
39+
let opt = ctx.opt();
40+
let blocks = client
41+
.chain_get_num_blocks(
42+
number,
43+
Some(GetBlockOption {
44+
decode: true,
45+
raw: opt.raw,
46+
}),
47+
)?
48+
.ok_or_else(|| anyhow::format_err!("block of height {} not found", number))?;
49+
Ok(blocks)
50+
}
51+
}

cmd/starcoin/src/chain/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
mod epoch_info;
55
mod get_block_cmd;
6+
mod get_num_blocks_cmd;
67
mod get_block_info_cmd;
78
mod get_events_cmd;
89
mod get_txn_cmd;

rpc/api/src/chain/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ pub trait ChainApi {
4747
count: u64,
4848
option: Option<GetBlocksOption>,
4949
) -> FutureResult<Vec<BlockView>>;
50+
51+
/// Get height `number` blocks.
52+
#[rpc(name = "chain.get_num_blocks")]
53+
fn get_num_blocks(
54+
&self,
55+
number: BlockNumber,
56+
option: Option<GetBlocksOption>,
57+
) -> FutureResult<Vec<BlockView>>;
5058
#[rpc(name = "chain.get_block_info_by_number")]
5159
fn get_block_info_by_number(&self, number: BlockNumber) -> FutureResult<Option<BlockInfoView>>;
5260
/// Get chain transactions

rpc/client/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,14 @@ impl RpcClient {
799799
.map_err(map_err)
800800
}
801801

802+
pub fn chain_get_num_blocks(&self,
803+
number: BlockNumber,
804+
option: Option<GetBlocksOption>,) -> anyhow::Result<Vec<BlockView>> {
805+
self.call_rpc_blocking(|inner| {
806+
inner.chain_client.get_num_blocks(number, option)
807+
}).map_err(map_err)
808+
}
809+
802810
pub fn chain_get_transaction(
803811
&self,
804812
txn_id: HashValue,

rpc/server/src/module/chain_rpc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ where
171171
Box::pin(fut.boxed())
172172
}
173173

174+
fn get_num_blocks(&self, number: BlockNumber, option: Option<GetBlocksOption>) -> FutureResult<Vec<BlockView>> {
175+
// XXX FIXME YSG
176+
todo!()
177+
}
178+
174179
fn get_block_info_by_number(&self, number: u64) -> FutureResult<Option<BlockInfoView>> {
175180
let service = self.service.clone();
176181

vm/starcoin-transactional-test-harness/src/fork_chain.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ impl ChainApi for MockChainApi {
307307
Box::pin(fut.boxed().map_err(map_err))
308308
}
309309

310+
fn get_num_blocks(&self, number: BlockNumber, option: Option<GetBlocksOption>) -> FutureResult<Vec<BlockView>> {
311+
let fut = async move {
312+
bail!("not implemented.");
313+
};
314+
Box::pin(fut.boxed().map_err(map_err))
315+
}
316+
310317
fn get_block_info_by_number(&self, number: BlockNumber) -> FutureResult<Option<BlockInfoView>> {
311318
let chain = self.chain.lock().unwrap();
312319
let client = chain.remote_chain_client();

0 commit comments

Comments
 (0)