Use sensible gas price.

This commit is contained in:
Gav Wood 2016-03-28 18:53:33 +02:00
parent 3023d76a46
commit 712302391c
4 changed files with 25 additions and 14 deletions

View File

@ -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<SignedTransaction>;
/// Suggested gas price
fn sensible_gas_price(&self) -> U256 { x!(20000000000u64) }
}
/// Mining status

View File

@ -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()
}

View File

@ -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) {

View File

@ -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<C, S, A, M, EM = ExternalMiner>
where C: BlockChainClient,
@ -167,16 +162,18 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM>
}
}
fn sign_call(client: &Arc<C>, request: CallRequest) -> SignedTransaction {
fn sign_call(&self, request: CallRequest) -> Result<SignedTransaction, Error> {
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<u8>) -> Result<Value, Error> {
@ -284,7 +281,7 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
fn gas_price(&self, params: Params) -> Result<Value, Error> {
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<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
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<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
fn call(&self, params: Params) -> Result<Value, Error> {
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<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
fn estimate_gas(&self, params: Params) -> Result<Value, Error> {
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)
})