Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Revive parity_setMinGasPrice RPC call #10294

Merged
merged 13 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ fake-hardware-wallet = { path = "../accounts/fake-hardware-wallet" }
blooms-db = { path = "../util/blooms-db" }
criterion = "0.2"
env_logger = "0.5"
fetch = { path = "../util/fetch" }
kvdb-rocksdb = "0.1.3"
parity-runtime = { path = "../util/runtime" }
rlp_compress = { path = "../util/rlp-compress" }
tempdir = "0.3"
trie-standardmap = "0.1"
Expand Down
6 changes: 6 additions & 0 deletions ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ extern crate env_logger;
#[cfg(test)]
extern crate rlp_compress;

#[cfg(all(test, feature = "price-info"))]
extern crate fetch;

#[cfg(all(test, feature = "price-info"))]
extern crate parity_runtime;

pub mod account_provider;
pub mod block;
pub mod builtin;
Expand Down
82 changes: 82 additions & 0 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,32 @@ impl miner::MinerService for Miner {
self.params.read().gas_range_target.0 / 5
}

fn set_minimal_gas_price(&self, new_price: U256) -> Result<bool, &str> {
match *self.gas_pricer.lock() {
// Binding the gas pricer to `gp` here to prevent
// a deadlock when calling recalibrate()
ref mut gp @ GasPricer::Fixed(_) => {
trace!(target: "miner", "minimal_gas_price: recalibrating fixed...");
*gp = GasPricer::new_fixed(new_price);

let txq = self.transaction_queue.clone();
let mut options = self.options.pool_verification_options.clone();
gp.recalibrate(move |gas_price| {
debug!(target: "miner", "minimal_gas_price: Got gas price! {}", gas_price);
options.minimal_gas_price = gas_price;
txq.set_verifier_options(options);
});

Ok(true)
},
#[cfg(feature = "price-info")]
GasPricer::Calibrated(_) => {
let error_msg = "Can't update fixed gas price while automatic gas calibration is enabled.";
return Err(error_msg);
},
}
}

fn import_external_transactions<C: miner::BlockChainClient>(
&self,
chain: &C,
Expand Down Expand Up @@ -1646,4 +1672,60 @@ mod tests {

assert!(miner.is_currently_sealing());
}

#[test]
fn should_set_new_minimum_gas_price() {
// Creates a new GasPricer::Fixed behind the scenes
let miner = Miner::new_for_tests(&Spec::new_test(), None);

let expected_minimum_gas_price: U256 = 0x1337.into();
miner.set_minimal_gas_price(expected_minimum_gas_price).unwrap();

let txq_options = miner.transaction_queue.status().options;
let current_minimum_gas_price = txq_options.minimal_gas_price;

assert!(current_minimum_gas_price == expected_minimum_gas_price);
}

#[cfg(feature = "price-info")]
fn dynamic_gas_pricer() -> GasPricer {
use std::time::Duration;
use parity_runtime::Executor;
use fetch::Client as FetchClient;
use ethcore_miner::gas_price_calibrator::{GasPriceCalibrator, GasPriceCalibratorOptions};

// Don't really care about any of these settings since
// the gas pricer is never actually going to be used
let fetch = FetchClient::new(1).unwrap();
let p = Executor::new_sync();

GasPricer::new_calibrated(
GasPriceCalibrator::new(
GasPriceCalibratorOptions {
usd_per_tx: 0.0,
recalibration_period: Duration::from_secs(0),
},
fetch,
p,
)
)
}

#[test]
#[cfg(feature = "price-info")]
fn should_fail_to_set_new_minimum_gas_price() {
// We get a fixed gas pricer by default, need to change that
let miner = Miner::new_for_tests(&Spec::new_test(), None);
let calibrated_gas_pricer = dynamic_gas_pricer();
*miner.gas_pricer.lock() = calibrated_gas_pricer;

let expected_minimum_gas_price: U256 = 0x1337.into();
let result = miner.set_minimal_gas_price(expected_minimum_gas_price);
assert!(result.is_err());

let received_error_msg = result.unwrap_err();
let expected_error_msg = "Can't update fixed gas price while automatic gas calibration is enabled.";

assert!(received_error_msg == expected_error_msg);
}
}
4 changes: 4 additions & 0 deletions ethcore/src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,8 @@ pub trait MinerService : Send + Sync {

/// Suggested gas limit.
fn sensible_gas_limit(&self) -> U256;

/// Set a new minimum gas limit.
/// Will not work if dynamic gas calibration is set.
fn set_minimal_gas_price(&self, gas_price: U256) -> Result<bool, &str>;
}
2 changes: 1 addition & 1 deletion miner/src/gas_pricer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl GasPricer {
/// Recalibrate current gas price.
pub fn recalibrate<F: FnOnce(U256) + Sync + Send + 'static>(&mut self, set_price: F) {
match *self {
GasPricer::Fixed(ref max) => set_price(max.clone()),
GasPricer::Fixed(ref curr) => set_price(curr.clone()),
#[cfg(feature = "price-info")]
GasPricer::Calibrated(ref mut cal) => cal.recalibrate(set_price),
}
Expand Down
8 changes: 5 additions & 3 deletions rpc/src/v1/impls/parity_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ impl<C, M, U, F> ParitySet for ParitySetClient<C, M, U, F> where
F: Fetch + 'static,
{

fn set_min_gas_price(&self, _gas_price: U256) -> Result<bool> {
warn!("setMinGasPrice is deprecated. Ignoring request.");
Ok(false)
fn set_min_gas_price(&self, gas_price: U256) -> Result<bool> {
match self.miner.set_minimal_gas_price(gas_price.into()) {
Ok(success) => Ok(success),
Err(e) => Err(errors::unsupported(e, None)),
}
}

fn set_transactions_limit(&self, _limit: usize) -> Result<bool> {
Expand Down
17 changes: 17 additions & 0 deletions rpc/src/v1/tests/helpers/miner_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct TestMinerService {
pub next_nonces: RwLock<HashMap<Address, U256>>,
/// Password held by Engine.
pub password: RwLock<Password>,
/// Minimum gas price
pub min_gas_price: RwLock<Option<U256>>,

authoring_params: RwLock<AuthoringParams>,
}
Expand All @@ -66,6 +68,7 @@ impl Default for TestMinerService {
pending_receipts: Default::default(),
next_nonces: Default::default(),
password: RwLock::new("".into()),
min_gas_price: RwLock::new(Some(0.into())),
authoring_params: RwLock::new(AuthoringParams {
author: Address::zero(),
gas_range_target: (12345.into(), 54321.into()),
Expand Down Expand Up @@ -282,4 +285,18 @@ impl MinerService for TestMinerService {
fn sensible_gas_limit(&self) -> U256 {
0x5208.into()
}

fn set_minimal_gas_price(&self, gas_price: U256) -> Result<bool, &str> {
let mut new_price = self.min_gas_price.write();
match *new_price {
Some(ref mut v) => {
*v = gas_price;
Ok(true)
},
None => {
let error_msg = "Can't update fixed gas price while automatic gas calibration is enabled.";
Err(error_msg)
},
}
}
}
20 changes: 19 additions & 1 deletion rpc/src/v1/tests/mocked/parity_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,25 @@ fn rpc_parity_set_min_gas_price() {
io.extend_with(parity_set_client(&client, &miner, &updater, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "parity_setMinGasPrice", "params":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":false,"id":1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;

assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}

#[test]
fn rpc_parity_set_min_gas_price_with_automated_calibration_enabled() {
let miner = miner_service();
*miner.min_gas_price.write() = None;

let client = client_service();
let network = network_service();
let updater = updater_service();

let mut io = IoHandler::new();
io.extend_with(parity_set_client(&client, &miner, &updater, &network).to_delegate());

let request = r#"{"jsonrpc": "2.0", "method": "parity_setMinGasPrice", "params":["0xdeadbeef"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","error":{"code":-32000,"message":"Can't update fixed gas price while automatic gas calibration is enabled."},"id":1}"#;

assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
Expand Down