From 712302391c785161b2fe9518a5632cec7a2c1993 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 74c8e1a1a..92811b82e 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -65,7 +65,7 @@ pub use transaction_queue::{TransactionQueue, AccountDetails}; pub use miner::{Miner}; use std::sync::Mutex; -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}; @@ -108,6 +108,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 bd4757c21..04e98cecb 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -146,6 +146,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 b1b4f9e6d..bfbbfaecf 100644 --- a/miner/src/transaction_queue.rs +++ b/miner/src/transaction_queue.rs @@ -311,6 +311,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 565b255d9..786abac8c 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -28,7 +28,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 v1::traits::{Eth, EthFilter}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; @@ -40,10 +39,6 @@ fn default_gas() -> U256 { U256::from(21_000) } -fn default_gas_price() -> U256 { - shannon() * U256::from(50) -} - /// Eth rpc implementation. pub struct EthClient where C: BlockChainClient, @@ -167,16 +162,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 { @@ -284,7 +281,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()) } } @@ -484,11 +481,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) @@ -514,8 +512,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) }) @@ -523,8 +521,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) })