add state_at_id and balance_at_id, integrate with RPC

This commit is contained in:
Robert Habermeier 2016-05-25 17:35:15 +02:00
parent ca008fb541
commit 2b7fae8fa6
4 changed files with 47 additions and 7 deletions

View File

@ -346,6 +346,16 @@ impl<V> Client<V> where V: Verifier {
imported 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<State> {
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. /// Get a copy of the best block's state.
pub fn state(&self) -> 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()) 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<V> BlockChainClient for Client<V> where V: Verifier {
self.state().balance(address) self.state().balance(address)
} }
fn balance_at_id(&self, address: &Address, id: BlockID) -> Option<U256> {
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 { fn storage_at(&self, address: &Address, position: &H256) -> H256 {
self.state().storage_at(address, position) self.state().storage_at(address, position)
} }

View File

@ -74,9 +74,21 @@ pub trait BlockChainClient : Sync + Send {
/// Get address code. /// Get address code.
fn code(&self, address: &Address) -> Option<Bytes>; fn code(&self, address: &Address) -> Option<Bytes>;
/// Get address balance. /// Get address balance at latest state.
fn balance(&self, address: &Address) -> U256; 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<U256> {
if let BlockID::Latest = id {
Some(self.balance(address))
} else {
None
}
}
/// Get value of the storage at given position. /// Get value of the storage at given position.
fn storage_at(&self, address: &Address, position: &H256) -> H256; fn storage_at(&self, address: &Address, position: &H256) -> H256;

View File

@ -63,8 +63,8 @@ pub use external::{ExternalMiner, ExternalMinerService};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use util::{H256, U256, Address, Bytes}; use util::{H256, U256, Address, Bytes};
use ethcore::client::{BlockChainClient, Executed}; use ethcore::client::{BlockChainClient, Executed};
use ethcore::block::{ClosedBlock}; use ethcore::block::ClosedBlock;
use ethcore::receipt::{Receipt}; use ethcore::receipt::Receipt;
use ethcore::error::{Error, ExecutionError}; use ethcore::error::{Error, ExecutionError};
use ethcore::transaction::SignedTransaction; use ethcore::transaction::SignedTransaction;
@ -154,7 +154,7 @@ pub trait MinerService : Send + Sync {
/// Suggested gas limit. /// Suggested gas limit.
fn sensible_gas_limit(&self) -> U256 { x!(21000) } fn sensible_gas_limit(&self) -> U256 { x!(21000) }
/// Account balance /// Latest account balance in pending state.
fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256; fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256;
/// Call into contract code using pending state. /// Call into contract code using pending state.

View File

@ -26,9 +26,9 @@ use ethminer::{MinerService, AccountDetails, ExternalMinerService};
use jsonrpc_core::*; use jsonrpc_core::*;
use util::numbers::*; use util::numbers::*;
use util::sha3::*; use util::sha3::*;
use util::bytes::{ToPretty}; use util::bytes::ToPretty;
use util::rlp::{encode, decode, UntrustedRlp, View}; 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::block::IsBlock;
use ethcore::views::*; use ethcore::views::*;
use ethcore::ethereum::Ethash; use ethcore::ethereum::Ethash;
@ -257,6 +257,17 @@ fn pending_logs<M>(miner: &M, filter: &EthcoreFilter) -> Vec<Log> where M: Miner
result 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<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
C: BlockChainClient + 'static, C: BlockChainClient + 'static,
S: SyncProvider + 'static, S: SyncProvider + 'static,
@ -343,7 +354,7 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
.and_then(|(address, block_number,)| match block_number { .and_then(|(address, block_number,)| match block_number {
BlockNumber::Latest => to_value(&take_weak!(self.client).balance(&address)), 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)), 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))),
}) })
} }