Modify gas price statistics (#2947)

* gas price distribution + median + tests

* put histogram in util

* use the util histogram

* remove the default gas price implementation

* histogram rpc

* fix empty corpus

* Add JS ethcore_gasPriceHistogram

* Fix typo (s/types/type/) & subsequent failing test

* Fix return type & formatting

* bucketBounds

* Add jsapi e2e test verification
This commit is contained in:
keorn
2016-10-31 11:57:48 +00:00
committed by Gav Wood
parent 8bf577e0fe
commit 7af20a5db0
17 changed files with 243 additions and 41 deletions

View File

@@ -92,8 +92,5 @@ fn prepare_transaction<C, M>(client: &C, miner: &M, request: TransactionRequest)
}
pub fn default_gas_price<C, M>(client: &C, miner: &M) -> U256 where C: MiningBlockChainClient, M: MinerService {
client
.gas_price_statistics(100, 8)
.map(|x| x[4])
.unwrap_or_else(|_| miner.sensible_gas_price())
client.gas_price_median(100).unwrap_or_else(|| miner.sensible_gas_price())
}

View File

@@ -32,6 +32,7 @@ mod codes {
pub const NO_WORK: i64 = -32001;
pub const NO_AUTHOR: i64 = -32002;
pub const NO_NEW_WORK: i64 = -32003;
pub const NOT_ENOUGH_DATA: i64 = -32006;
pub const UNKNOWN_ERROR: i64 = -32009;
pub const TRANSACTION_ERROR: i64 = -32010;
pub const EXECUTION_ERROR: i64 = -32015;
@@ -152,6 +153,14 @@ pub fn no_author() -> Error {
}
}
pub fn not_enough_data() -> Error {
Error {
code: ErrorCode::ServerError(codes::NOT_ENOUGH_DATA),
message: "The node does not have enough data to compute the given statistic.".into(),
data: None
}
}
pub fn token(e: String) -> Error {
Error {
code: ErrorCode::ServerError(codes::UNKNOWN_ERROR),

View File

@@ -33,7 +33,7 @@ use ethcore::ids::BlockID;
use jsonrpc_core::Error;
use v1::traits::Ethcore;
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings};
use v1::types::{Bytes, U256, H160, H256, H512, Peers, Transaction, RpcSettings, Histogram};
use v1::helpers::{errors, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::DEFAULT_MAC;
use v1::helpers::auto_args::Ready;
@@ -222,13 +222,9 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
Ok(Bytes::new(version_data()))
}
fn gas_price_statistics(&self) -> Result<Vec<U256>, Error> {
fn gas_price_histogram(&self) -> Result<Histogram, Error> {
try!(self.active());
match take_weak!(self.client).gas_price_statistics(100, 8) {
Ok(stats) => Ok(stats.into_iter().map(Into::into).collect()),
_ => Err(Error::internal_error()),
}
take_weak!(self.client).gas_price_histogram(100, 10).ok_or_else(errors::not_enough_data).map(Into::into)
}
fn unsigned_transactions_count(&self) -> Result<usize, Error> {

View File

@@ -253,4 +253,7 @@ impl MinerService for TestMinerService {
self.latest_closed_block.lock().as_ref().map_or(None, |b| b.block().fields().state.code(address).map(|c| (*c).clone()))
}
fn sensible_gas_price(&self) -> U256 {
20000000000u64.into()
}
}

View File

@@ -18,7 +18,7 @@
use jsonrpc_core::Error;
use v1::helpers::auto_args::{Wrap, WrapAsync, Ready};
use v1::types::{H160, H256, H512, U256, Bytes, Peers, Transaction, RpcSettings};
use v1::types::{H160, H256, H512, U256, Bytes, Peers, Transaction, RpcSettings, Histogram};
build_rpc_trait! {
/// Ethcore-specific rpc interface.
@@ -76,8 +76,8 @@ build_rpc_trait! {
fn default_extra_data(&self) -> Result<Bytes, Error>;
/// Returns distribution of gas price in latest blocks.
#[rpc(name = "ethcore_gasPriceStatistics")]
fn gas_price_statistics(&self) -> Result<Vec<U256>, Error>;
#[rpc(name = "ethcore_gasPriceHistogram")]
fn gas_price_histogram(&self) -> Result<Histogram, Error>;
/// Returns number of unsigned transactions waiting in the signer queue (if signer enabled)
/// Returns error when signer is disabled

View File

@@ -0,0 +1,39 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Gas prices histogram.
use v1::types::U256;
use util::stats;
/// Values of RPC settings.
#[derive(Serialize, Deserialize)]
pub struct Histogram {
/// Gas prices for bucket edges.
#[serde(rename="bucketBounds")]
pub bucket_bounds: Vec<U256>,
/// Transacion counts for each bucket.
pub counts: Vec<u64>,
}
impl From<stats::Histogram> for Histogram {
fn from(h: stats::Histogram) -> Self {
Histogram {
bucket_bounds: h.bucket_bounds.into_iter().map(Into::into).collect(),
counts: h.counts
}
}
}

View File

@@ -32,6 +32,7 @@ mod trace;
mod trace_filter;
mod uint;
mod work;
mod histogram;
pub use self::bytes::Bytes;
pub use self::block::{Block, BlockTransactions};
@@ -51,3 +52,4 @@ pub use self::trace::{LocalizedTrace, TraceResults};
pub use self::trace_filter::TraceFilter;
pub use self::uint::U256;
pub use self::work::Work;
pub use self::histogram::Histogram;