diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 27081471e..2d4d20c12 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -346,6 +346,16 @@ impl Client where V: Verifier { imported } + /// Attempt to get a copy of a specific block's state. + /// + /// This can fail (but may not) if the DB prunes state. + pub fn state_at_id(&self, id: BlockID) -> Option { + self.block_header(id).map(|header| { + let db = self.state_db.lock().unwrap().boxed_clone(); + State::from_existing(db, HeaderView::new(&header).state_root(), self.engine.account_start_nonce()) + }) + } + /// Get a copy of the best block's state. pub fn state(&self) -> State { State::from_existing(self.state_db.lock().unwrap().boxed_clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce()) @@ -557,6 +567,13 @@ impl BlockChainClient for Client where V: Verifier { self.state().balance(address) } + fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { + match id { + BlockID::Latest => Some(self.balance(address)), + id => self.state_at_id(id).map(|s| s.balance(address)), + } + } + fn storage_at(&self, address: &Address, position: &H256) -> H256 { self.state().storage_at(address, position) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 4a3d2c5f4..0ac255c7d 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -74,9 +74,21 @@ pub trait BlockChainClient : Sync + Send { /// Get address code. fn code(&self, address: &Address) -> Option; - /// Get address balance. + /// Get address balance at latest state. fn balance(&self, address: &Address) -> U256; + /// Account balance at a specific block ID. + /// + /// Will fail if the block is not valid or the block's root hash has been + /// pruned from the DB. + fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.balance(address)) + } else { + None + } + } + /// Get value of the storage at given position. fn storage_at(&self, address: &Address, position: &H256) -> H256; diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 211c61b1f..e9ac7aba2 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -63,8 +63,8 @@ pub use external::{ExternalMiner, ExternalMinerService}; use std::collections::BTreeMap; use util::{H256, U256, Address, Bytes}; use ethcore::client::{BlockChainClient, Executed}; -use ethcore::block::{ClosedBlock}; -use ethcore::receipt::{Receipt}; +use ethcore::block::ClosedBlock; +use ethcore::receipt::Receipt; use ethcore::error::{Error, ExecutionError}; use ethcore::transaction::SignedTransaction; @@ -154,7 +154,7 @@ pub trait MinerService : Send + Sync { /// Suggested gas limit. fn sensible_gas_limit(&self) -> U256 { x!(21000) } - /// Account balance + /// Latest account balance in pending state. fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256; /// Call into contract code using pending state. diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index a13512357..5b4fc6c89 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -26,9 +26,9 @@ use ethminer::{MinerService, AccountDetails, ExternalMinerService}; use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; -use util::bytes::{ToPretty}; +use util::bytes::ToPretty; use util::rlp::{encode, decode, UntrustedRlp, View}; -use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; +use ethcore::client::{self, BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; use ethcore::views::*; use ethcore::ethereum::Ethash; @@ -257,6 +257,17 @@ fn pending_logs(miner: &M, filter: &EthcoreFilter) -> Vec where M: Miner result } +// must be in range [-32099, -32000] +const UNSUPPORTED_REQUEST_CODE: i64 = -32000; + +fn make_unsupported_err() -> Error { + Error { + code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), + message: "Unsupported request.".into(), + data: None + } +} + impl Eth for EthClient where C: BlockChainClient + 'static, S: SyncProvider + 'static, @@ -343,7 +354,7 @@ impl Eth for EthClient where .and_then(|(address, block_number,)| match block_number { BlockNumber::Latest => to_value(&take_weak!(self.client).balance(&address)), BlockNumber::Pending => to_value(&take_weak!(self.miner).balance(take_weak!(self.client).deref(), &address)), - _ => Err(Error::invalid_params()), + id => to_value(&try!(take_weak!(self.client).balance_at_id(&address, id.into()).ok_or_else(make_unsupported_err))), }) }