Skip to content

Commit

Permalink
feat: 🎸 Support rpc changes in v0.106.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Liu Chuankai committed Jan 3, 2023
1 parent a13e73a commit 60856d4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/subcommands/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use std::time::Duration;

use super::{CliSubCommand, Output};
use crate::utils::arg_parser::{
ArgParser, DurationParser, FilePathParser, FixedHashParser, FromStrParser, HexParser,
ArgParser, DurationParser, FeeRateStaticsTargetParser, FilePathParser, FixedHashParser,
FromStrParser, HexParser,
};
use crate::utils::rpc::{
BannedAddr, BlockEconomicState, BlockView, EpochView, HeaderView, HttpRpcClient,
Expand Down Expand Up @@ -148,6 +149,25 @@ impl<'a> RpcSubCommand<'a> {
App::new("get_block_economic_state")
.about("Returns increased issuance, miner reward, and the total transaction fee of a block")
.arg(arg_hash.clone().about("Specifies the block hash which rewards should be analyzed")),
App::new("estimate_cycles")
.arg(
Arg::with_name("json-path")
.long("json-path")
.takes_value(true)
.required(true)
.validator(|input| FilePathParser::new(true).validate(input))
.about("Transaction content (json format, see rpc estimate_cycles)")
)
.about("estimate_cycles run a transaction and return the execution consumed cycles."),
App::new("get_fee_rate_statics")
.arg(
Arg::with_name("target")
.long("target")
.takes_value(true)
.validator(|input| FromStrParser::<u64>::default().validate(input))
.about("Specify the number (1 - 101) of confirmed blocks to be counted. If the number is even, automatically add one. Default is 21.")
)
.about("estimate_cycles run a transaction and return the execution consumed cycles."),
// [Net]
App::new("get_banned_addresses").about("Get all banned IPs/Subnets"),
App::new("get_peers").about("Get connected peers"),
Expand Down Expand Up @@ -573,6 +593,39 @@ impl<'a> CliSubCommand for RpcSubCommand<'a> {
Ok(Output::new_output(resp))
}
}
("estimate_cycles", Some(m)) => {
let is_raw_data = is_raw_data || m.is_present("raw-data");
let json_path: PathBuf = FilePathParser::new(true).from_matches(m, "json-path")?;
let content = fs::read_to_string(json_path).map_err(|err| err.to_string())?;
let tx: Transaction =
serde_json::from_str(&content).map_err(|err| err.to_string())?;
if is_raw_data {
let resp = self
.raw_rpc_client
.estimate_cycles(tx)
.map_err(|err| err.to_string())?;
Ok(Output::new_output(resp))
} else {
let resp = self.rpc_client.estimate_cycles(tx.into())?;
Ok(Output::new_output(resp))
}
}
("get_fee_rate_statics", Some(m)) => {
let is_raw_data = is_raw_data || m.is_present("raw-data");
let target: Option<u64> =
FeeRateStaticsTargetParser {}.from_matches_opt(m, "target")?;

if is_raw_data {
let resp = self
.raw_rpc_client
.get_fee_rate_statics(target.map(|v| v.into()))
.map_err(|err| err.to_string())?;
Ok(Output::new_output(resp))
} else {
let resp = self.rpc_client.get_fee_rate_statics(target)?;
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
12 changes: 12 additions & 0 deletions src/utils/arg_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ impl ArgParser<Vec<u8>> for HexParser {
}
}

pub struct FeeRateStaticsTargetParser;

impl ArgParser<u64> for FeeRateStaticsTargetParser {
fn parse(&self, input: &str) -> Result<u64, String> {
let target = FromStrParser::<u64>::default().parse(input)?;
if target == 0 || target > 101 {
return Err(format!("target ({}) is out of range[1 ~ 101]", target));
}
Ok(target)
}
}

#[derive(Default)]
pub struct FixedHashParser<T> {
_h: PhantomData<T>,
Expand Down
18 changes: 18 additions & 0 deletions src/utils/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ impl HttpRpcClient {
.map_err(|err| err.to_string())
}

pub fn estimate_cycles(
&mut self,
tx: packed::Transaction,
) -> Result<types::EstimateCycles, String> {
self.client
.estimate_cycles(tx.into())
.map(Into::into)
.map_err(|err| err.to_string())
}
pub fn get_fee_rate_statics(
&mut self,
target: Option<u64>,
) -> Result<types::FeeRateStatics, String> {
self.client
.get_fee_rate_statics(target.map(Into::into))
.map(Into::into)
.map_err(|err| err.to_string())
}
// Net
pub fn get_banned_addresses(&mut self) -> Result<Vec<types::BannedAddr>, String> {
self.client
Expand Down
36 changes: 36 additions & 0 deletions src/utils/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ pub struct ResponseFormat<V> {
pub struct TransactionWithStatus {
/// The transaction.
pub transaction: Option<TransactionView>,
/// The transaction consumed cycles.
pub cycles: Option<Cycle>,
/// The Transaction status.
pub tx_status: TxStatus,
}
Expand All @@ -383,6 +385,7 @@ impl TryFrom<rpc_types::TransactionWithStatusResponse> for TransactionWithStatus
}
})
.transpose()?,
cycles: json.cycles.map(|c| c.into()),
tx_status: json.tx_status,
})
}
Expand Down Expand Up @@ -1266,3 +1269,36 @@ impl From<rpc_types::RawTxPool> for RawTxPool {
}
}
}

/// Response result of the RPC method `estimate_cycles`.
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
pub struct EstimateCycles {
/// The count of cycles that the VM has consumed to verify this transaction.
pub cycles: Cycle,
}

impl From<rpc_types::EstimateCycles> for EstimateCycles {
fn from(json: rpc_types::EstimateCycles) -> EstimateCycles {
EstimateCycles {
cycles: json.cycles.into(),
}
}
}

/// The fee_rate statistics information, includes mean and median, unit: shannons per kilo-weight
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct FeeRateStatics {
/// mean
pub mean: Uint64,
/// median
pub median: Uint64,
}

impl From<rpc_types::FeeRateStatics> for FeeRateStatics {
fn from(json: rpc_types::FeeRateStatics) -> FeeRateStatics {
FeeRateStatics {
mean: json.mean.into(),
median: json.median.into(),
}
}
}

0 comments on commit 60856d4

Please sign in to comment.