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
35 changes: 35 additions & 0 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,26 @@ impl miner::MinerService for Miner {
self.params.read().gas_range_target.0 / 5
}

fn set_minimal_gas_price(&self, new_price: U256) -> Result<bool, ()> {
if cfg!(feature = "price-info") {
warn!("Can't update fixed gas price while automatic gas calibration is enabled.");
HCastano marked this conversation as resolved.
Show resolved Hide resolved
return Err(());
}

trace!(target: "miner", "minimal_gas_price: recalibrating fixed...");
*self.gas_pricer.lock() = GasPricer::new_fixed(new_price);

let txq = self.transaction_queue.clone();
let mut options = self.options.pool_verification_options.clone();
self.gas_pricer.lock().recalibrate(move |gas_price| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth keeping it DRY? and re-using the same method as for per-block update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only call recalibrate() twice, so for now I'm going to say no unless somebody else has a strong opinion about it.

debug!(target: "miner", "minimal_gas_price: Got gas price! {}", gas_price);
options.minimal_gas_price = gas_price;
txq.set_verifier_options(options);
});

Ok(true)
}

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

assert!(miner.is_currently_sealing());
}

#[test]
#[cfg(not(feature = "price-info"))]
HCastano marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
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, ()>;
}
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(_) => Err(errors::request_rejected()),
HCastano marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn set_transactions_limit(&self, _limit: usize) -> Result<bool> {
Expand Down
9 changes: 9 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<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(0.into()),
authoring_params: RwLock::new(AuthoringParams {
author: Address::zero(),
gas_range_target: (12345.into(), 54321.into()),
Expand Down Expand Up @@ -282,4 +285,10 @@ impl MinerService for TestMinerService {
fn sensible_gas_limit(&self) -> U256 {
0x5208.into()
}

fn set_minimal_gas_price(&self, gas_price: U256) -> Result<bool, ()> {
let mut min_gas_price = self.min_gas_price.write();
*min_gas_price = gas_price;
Ok(true)
}
}
19 changes: 18 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,24 @@ 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]
#[cfg(feature = "price-info")]
HCastano marked this conversation as resolved.
Show resolved Hide resolved
fn rpc_parity_set_min_gas_price_with_automated_calibration_enabled() {
let miner = miner_service();
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":["0xcd1722f3947def4cf144679da39c4c32bdc35681"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","error":{"code":-32040,"message":"Request has been rejected."},"id":1}"#;

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