Skip to content

Commit

Permalink
Support GetFinalizedNumber (#16)
Browse files Browse the repository at this point in the history
* Add new interface

* Fix compile

* Remove TODO
  • Loading branch information
boundless-forest authored Aug 7, 2023
1 parent 4d55513 commit effcd2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/app/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub enum ChainCommand {
},
/// Get the finalized head hash
GetFinalizedHead,
/// Get the finalized head number
GetFinalizedNumber,
/// Get the header for a specific block
GetHeader {
#[arg(long)]
Expand Down
15 changes: 11 additions & 4 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use frame_metadata::RuntimeMetadata;
use frame_system::AccountInfo;
use pallet_balances::AccountData;
use prettytable::{row, Table};
use sp_runtime::traits::Header;
// this crate
use crate::{
app::{AccountInfoCommand, AppCommand, ChainCommand, PalletsCommand, RpcCommand, StateCommand},
Expand Down Expand Up @@ -35,10 +36,6 @@ pub async fn handle_commands<CI: ChainInfo>(
return Ok(ExecutionResult::SwitchNetworkTo(network));
},
AppCommand::Rpc(sub_commands) => match sub_commands {
// RpcCommand::RpcMethods => {
// let res = client.rpc_methods().await?;
// println!("{:?}", res);
// },
RpcCommand::SysName => {
let res = client.system_name().await?;
print_format_json(res);
Expand Down Expand Up @@ -84,6 +81,16 @@ pub async fn handle_commands<CI: ChainInfo>(
let res = client.get_finalized_head().await?;
print_format_json(res);
},
ChainCommand::GetFinalizedNumber => {
let finalized_hash = client.get_finalized_head().await?;
if let Some(hash) = finalized_hash {
let res = client.get_header(hash).await?;
print_format_json(res.number());
} else {
print_format_json(None::<String>);
}
},

ChainCommand::GetHeader { hash } => {
let hash = <CI as ChainInfo>::Hash::from_str(hash.as_str())
.map_err(|_| RpcError::InvalidCommandParams)?;
Expand Down
4 changes: 2 additions & 2 deletions src/networks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{fmt::Debug, marker::Sync, str::FromStr};
use clap::Subcommand;
use serde::{Deserialize, Serialize};
use sp_core::{Decode, Encode};
use sp_runtime::DeserializeOwned;
use sp_runtime::{traits::Header as HeaderT, DeserializeOwned};

/// The ChainInfo API
pub trait ChainInfo: Sync + Send {
Expand All @@ -32,7 +32,7 @@ pub trait ChainInfo: Sync + Send {
/// The hash type of the chain
type Hash: Serialize + DeserializeOwned + Send + FromStr;
/// The header type of the chain
type Header: Serialize + DeserializeOwned;
type Header: Serialize + DeserializeOwned + HeaderT;
/// The nonce type of the chain
type Nonce: Serialize + DeserializeOwned + Decode + Debug;
}
Expand Down

0 comments on commit effcd2e

Please sign in to comment.