Merge pull request #787 from ethcore/eth_estimateGas

eth_estimateGas
This commit is contained in:
Marek Kotewicz
2016-03-20 15:13:22 +01:00
10 changed files with 193 additions and 14 deletions

View File

@@ -37,6 +37,8 @@ use log_entry::LocalizedLogEntry;
use block_queue::{BlockQueue, BlockQueueInfo};
use blockchain::{BlockChain, BlockProvider, TreeRoute, ImportRoute};
use client::{BlockId, TransactionId, ClientConfig, BlockChainClient};
use env_info::EnvInfo;
use executive::{Executive, Executed};
pub use blockchain::CacheSize as BlockChainCacheSize;
/// General block status
@@ -385,6 +387,29 @@ impl<V> Client<V> where V: Verifier {
}
impl<V> BlockChainClient for Client<V> where V: Verifier {
fn call(&self, t: &SignedTransaction) -> Result<Executed, Error> {
let header = self.block_header(BlockId::Latest).unwrap();
let view = HeaderView::new(&header);
let last_hashes = self.build_last_hashes(view.hash());
let env_info = EnvInfo {
number: view.number(),
author: view.author(),
timestamp: view.timestamp(),
difficulty: view.difficulty(),
last_hashes: last_hashes,
gas_used: U256::zero(),
gas_limit: U256::max_value(),
};
// that's just a copy of the state.
let mut state = self.state();
let sender = try!(t.sender());
let balance = state.balance(&sender);
// give the sender max balance
state.sub_balance(&sender, &balance);
state.add_balance(&sender, &U256::max_value());
Executive::new(&mut state, &env_info, self.engine.deref().deref()).transact(t, false)
}
// TODO [todr] Should be moved to miner crate eventually.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock> {
block.try_seal(self.engine.deref().deref(), seal)

View File

@@ -25,6 +25,7 @@ pub use self::client::*;
pub use self::config::{ClientConfig, BlockQueueConfig, BlockChainConfig};
pub use self::ids::{BlockId, TransactionId};
pub use self::test_client::{TestBlockChainClient, EachBlockWith};
pub use executive::Executed;
use std::collections::HashSet;
use util::bytes::Bytes;
@@ -37,7 +38,7 @@ use header::BlockNumber;
use transaction::{LocalizedTransaction, SignedTransaction};
use log_entry::LocalizedLogEntry;
use filter::Filter;
use error::{ImportResult};
use error::{ImportResult, Error};
/// Blockchain database client. Owns and manages a blockchain and a block queue.
pub trait BlockChainClient : Sync + Send {
@@ -118,5 +119,7 @@ pub trait BlockChainClient : Sync + Send {
/// Attempts to seal given block. Returns `SealedBlock` on success and the same block in case of error.
fn try_seal(&self, block: ClosedBlock, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock>;
/// Makes a non-persistent transaction call.
fn call(&self, t: &SignedTransaction) -> Result<Executed, Error>;
}

View File

@@ -29,6 +29,8 @@ use error::{ImportResult};
use block_queue::BlockQueueInfo;
use block::{SealedBlock, ClosedBlock};
use executive::Executed;
use error::Error;
/// Test client.
pub struct TestBlockChainClient {
@@ -48,6 +50,8 @@ pub struct TestBlockChainClient {
pub storage: RwLock<HashMap<(Address, H256), H256>>,
/// Code.
pub code: RwLock<HashMap<Address, Bytes>>,
/// Execution result.
pub execution_result: RwLock<Option<Executed>>,
}
#[derive(Clone)]
@@ -82,12 +86,18 @@ impl TestBlockChainClient {
balances: RwLock::new(HashMap::new()),
storage: RwLock::new(HashMap::new()),
code: RwLock::new(HashMap::new()),
execution_result: RwLock::new(None),
};
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
client.genesis_hash = client.last_hash.read().unwrap().clone();
client
}
/// Set the execution result.
pub fn set_execution_result(&self, result: Executed) {
*self.execution_result.write().unwrap() = Some(result);
}
/// Set the balance of account `address` to `balance`.
pub fn set_balance(&self, address: Address, balance: U256) {
self.balances.write().unwrap().insert(address, balance);
@@ -182,6 +192,10 @@ impl TestBlockChainClient {
}
impl BlockChainClient for TestBlockChainClient {
fn call(&self, _t: &SignedTransaction) -> Result<Executed, Error> {
Ok(self.execution_result.read().unwrap().clone().unwrap())
}
fn block_total_difficulty(&self, _id: BlockId) -> Option<U256> {
Some(U256::zero())
}

View File

@@ -37,7 +37,7 @@ pub fn contract_address(address: &Address, nonce: &U256) -> Address {
}
/// Transaction execution receipt.
#[derive(Debug)]
#[derive(Debug, PartialEq, Clone)]
pub struct Executed {
/// Gas paid up front for execution of transaction.
pub gas: U256,

View File

@@ -256,6 +256,9 @@ impl<'a> HeaderView<'a> {
}
}
/// Returns header hash.
pub fn hash(&self) -> H256 { self.sha3() }
/// Returns raw rlp.
pub fn rlp(&self) -> &Rlp<'a> { &self.rlp }