From 5d626c7dd3bc3710f1c7869f844e5252ed1b8ffd Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 28 Mar 2016 18:53:33 +0200 Subject: [PATCH] Use sensible gas price. --- miner/src/lib.rs | 5 ++++- miner/src/miner.rs | 5 +++++ miner/src/transaction_queue.rs | 5 +++++ rpc/src/v1/impls/eth.rs | 24 +++++++++++------------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/miner/src/lib.rs b/miner/src/lib.rs index c7a02bdc4..0daac48e6 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -64,7 +64,7 @@ mod transaction_queue; pub use transaction_queue::{TransactionQueue, AccountDetails}; pub use miner::{Miner}; -use util::{H256, Address, FixedHash, Bytes}; +use util::{H256, U256, Address, FixedHash, Bytes}; use ethcore::client::{BlockChainClient}; use ethcore::block::{ClosedBlock}; use ethcore::error::{Error}; @@ -107,6 +107,9 @@ pub trait MinerService : Send + Sync { /// Query pending transactions for hash fn transaction(&self, hash: &H256) -> Option; + + /// Suggested gas price + fn sensible_gas_price(&self) -> U256 { x!(20000000000u64) } } /// Mining status diff --git a/miner/src/miner.rs b/miner/src/miner.rs index bf38d6a42..70bf1711a 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -198,6 +198,11 @@ impl MinerService for Miner { } } + fn sensible_gas_price(&self) -> U256 { + // 10% above our minimum. + self.transaction_queue.lock().unwrap().minimal_gas_price().clone() * x!(110) / x!(100) + } + fn author(&self) -> Address { *self.author.read().unwrap() } diff --git a/miner/src/transaction_queue.rs b/miner/src/transaction_queue.rs index 8f2855836..b6dd9c829 100644 --- a/miner/src/transaction_queue.rs +++ b/miner/src/transaction_queue.rs @@ -312,6 +312,11 @@ impl TransactionQueue { } } + /// Get the minimal gas price. + pub fn minimal_gas_price(&self) -> &U256 { + &self.minimal_gas_price + } + /// Sets new gas price threshold for incoming transactions. /// Any transaction already imported to the queue is not affected. pub fn set_minimal_gas_price(&mut self, min_gas_price: U256) { diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 0fd3c95b4..dd241c9ec 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -31,7 +31,6 @@ use ethcore::client::*; use ethcore::block::IsBlock; use ethcore::views::*; use ethcore::ethereum::Ethash; -use ethcore::ethereum::denominations::shannon; use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action}; use self::ethash::SeedHashCompute; use v1::traits::{Eth, EthFilter}; @@ -44,10 +43,6 @@ fn default_gas() -> U256 { U256::from(21_000) } -fn default_gas_price() -> U256 { - shannon() * U256::from(20) -} - /// Eth rpc implementation. pub struct EthClient where C: BlockChainClient, @@ -173,16 +168,18 @@ impl EthClient } } - fn sign_call(client: &Arc, request: CallRequest) -> SignedTransaction { + fn sign_call(&self, request: CallRequest) -> Result { + let client = take_weak!(self.client); + let miner = take_weak!(self.miner); let from = request.from.unwrap_or(Address::zero()); - EthTransaction { + Ok(EthTransaction { nonce: request.nonce.unwrap_or_else(|| client.nonce(&from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or_else(default_gas), - gas_price: request.gas_price.unwrap_or_else(default_gas_price), + gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), value: request.value.unwrap_or_else(U256::zero), data: request.data.map_or_else(Vec::new, |d| d.to_vec()) - }.fake_sign(from) + }.fake_sign(from)) } fn dispatch_transaction(&self, signed_transaction: SignedTransaction, raw_transaction: Vec) -> Result { @@ -290,7 +287,7 @@ impl Eth for EthClient fn gas_price(&self, params: Params) -> Result { match params { - Params::None => to_value(&default_gas_price()), + Params::None => to_value(&take_weak!(self.miner).sensible_gas_price()), _ => Err(Error::invalid_params()) } } @@ -485,11 +482,12 @@ impl Eth for EthClient Ok(secret) => { let signed_transaction = { let client = take_weak!(self.client); + let miner = take_weak!(self.miner); EthTransaction { nonce: request.nonce.unwrap_or_else(|| client.nonce(&request.from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or_else(default_gas), - gas_price: request.gas_price.unwrap_or_else(default_gas_price), + gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), value: request.value.unwrap_or_else(U256::zero), data: request.data.map_or_else(Vec::new, |d| d.to_vec()), }.sign(&secret) @@ -515,8 +513,8 @@ impl Eth for EthClient fn call(&self, params: Params) -> Result { from_params_discard_second(params).and_then(|(request, )| { + let signed = try!(self.sign_call(request)); let client = take_weak!(self.client); - let signed = Self::sign_call(&client, request); let output = client.call(&signed).map(|e| Bytes(e.output)).unwrap_or(Bytes::new(vec![])); to_value(&output) }) @@ -524,8 +522,8 @@ impl Eth for EthClient fn estimate_gas(&self, params: Params) -> Result { from_params_discard_second(params).and_then(|(request, )| { + let signed = try!(self.sign_call(request)); let client = take_weak!(self.client); - let signed = Self::sign_call(&client, request); let used = client.call(&signed).map(|res| res.gas_used + res.refunded).unwrap_or(From::from(0)); to_value(&used) })