Skip to content

Commit

Permalink
feat: add get_block_median_time rpc method
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWaWaR committed Feb 22, 2021
1 parent 46679ec commit 2c94645
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ckb-sdk/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ckb_jsonrpc_types::{
Transaction, TransactionProof, TransactionWithStatus, TxPoolInfo, Uint64, Version,
};

use super::primitive;
use super::types;
use ckb_types::{packed, H256};

Expand Down Expand Up @@ -86,6 +87,7 @@ jsonrpc!(pub struct RawHttpRpcClient {
pub fn verify_transaction_proof(&mut self, tx_proof: TransactionProof) -> Vec<H256>;
pub fn get_fork_block(&mut self, block_hash: H256) -> Option<BlockView>;
pub fn get_consensus(&mut self) -> Consensus;
pub fn get_block_median_time(&mut self, block_hash: H256) -> Option<Timestamp>;

// Net
pub fn get_banned_addresses(&mut self) -> Vec<BannedAddr>;
Expand Down Expand Up @@ -267,6 +269,15 @@ impl HttpRpcClient {
.map(Into::into)
.map_err(|err| err.to_string())
}
pub fn get_block_median_time(
&mut self,
hash: H256,
) -> Result<Option<primitive::Timestamp>, String> {
self.client
.get_block_median_time(hash)
.map(|opt| opt.map(Into::into))
.map_err(|err| err.to_string())
}

// Net
pub fn get_banned_addresses(&mut self) -> Result<Vec<types::BannedAddr>, String> {
Expand Down
30 changes: 29 additions & 1 deletion src/subcommands/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ckb_jsonrpc_types::{
use ckb_sdk::{
rpc::{
BannedAddr, BlockReward, BlockView, EpochView, HeaderView, RawHttpRpcClient, RemoteNode,
TransactionProof, TransactionWithStatus,
Timestamp, TransactionProof, TransactionWithStatus,
},
HttpRpcClient,
};
Expand Down Expand Up @@ -148,6 +148,9 @@ impl<'a> RpcSubCommand<'a> {
.arg(arg_hash.clone().about("The fork block hash")),
App::new("get_consensus")
.about("Return various consensus parameters"),
App::new("get_block_median_time")
.about("Returns the past median time by block hash")
.arg(arg_hash.clone().about("A median time is calculated for a consecutive block sequence. `block_hash` indicates the highest block of the sequence")),
// [Net]
App::new("get_banned_addresses").about("Get all banned IPs/Subnets"),
App::new("get_peers").about("Get connected peers"),
Expand Down Expand Up @@ -530,6 +533,25 @@ impl<'a> CliSubCommand for RpcSubCommand<'a> {
Ok(Output::new_output(resp))
}
}
("get_block_median_time", Some(m)) => {
let is_raw_data = is_raw_data || m.is_present("raw-data");
let hash: H256 = FixedHashParser::<H256>::default().from_matches(m, "hash")?;

if is_raw_data {
let resp = self
.raw_rpc_client
.get_block_median_time(hash)
.map(RawOptionTimestamp)
.map_err(|err| err.to_string())?;
Ok(Output::new_output(resp))
} else {
let resp = self
.rpc_client
.get_block_median_time(hash)
.map(OptionTimestamp)?;
Ok(Output::new_output(resp))
}
}
// [Net]
("get_banned_addresses", Some(m)) => {
let is_raw_data = is_raw_data || m.is_present("raw-data");
Expand Down Expand Up @@ -712,6 +734,9 @@ pub struct RemoteNodes(pub Vec<RemoteNode>);
#[derive(Serialize, Deserialize)]
pub struct OptionTransactionWithStatus(pub Option<TransactionWithStatus>);

#[derive(Serialize, Deserialize)]
pub struct OptionTimestamp(pub Option<Timestamp>);

#[derive(Serialize, Deserialize)]
pub struct OptionBlockView(pub Option<BlockView>);

Expand Down Expand Up @@ -745,6 +770,9 @@ pub struct RawOptionBlockView(pub Option<rpc_types::BlockView>);
#[derive(Serialize, Deserialize)]
pub struct RawOptionHeaderView(pub Option<rpc_types::HeaderView>);

#[derive(Serialize, Deserialize)]
pub struct RawOptionTimestamp(pub Option<rpc_types::Timestamp>);

#[derive(Serialize, Deserialize)]
pub struct RawOptionEpochView(pub Option<rpc_types::EpochView>);

Expand Down

0 comments on commit 2c94645

Please sign in to comment.