added nonce, nonce_latest

This commit is contained in:
Robert Habermeier 2016-05-26 12:40:29 +02:00
parent 86eab79d9d
commit 3c7e4b8c6c
3 changed files with 19 additions and 7 deletions

View File

@ -348,7 +348,8 @@ impl<V> Client<V> where V: Verifier {
/// Attempt to get a copy of a specific block's state.
///
/// This can fail (but may not) if the DB prunes state.
/// This will not fail if given BlockID::Latest.
/// Otherwise, this can fail (but may not) if the DB prunes state.
pub fn state_at(&self, id: BlockID) -> Option<State> {
// fast path for latest state.
if let BlockID::Latest = id.clone() {
@ -556,10 +557,11 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
Self::block_hash(&self.chain, id).and_then(|hash| self.chain.block_details(&hash)).map(|d| d.total_difficulty)
}
fn nonce(&self, address: &Address) -> U256 {
self.state().nonce(address)
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
self.state_at(id).map(|s| s.nonce(address))
}
fn block_hash(&self, id: BlockID) -> Option<H256> {
Self::block_hash(&self.chain, id)
}

View File

@ -65,8 +65,15 @@ pub trait BlockChainClient : Sync + Send {
/// Get block total difficulty.
fn block_total_difficulty(&self, id: BlockID) -> Option<U256>;
/// Get address nonce.
fn nonce(&self, address: &Address) -> U256;
/// Attempt to get address nonce at given block.
/// May not fail on BlockID::Latest.
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256>;
fn nonce_latest(&self, address: &Address) -> U256 {
self.nonce(address, BlockID::Latest)
.expect("nonce will return Some when given BlockID::Latest. nonce was given BlockID::Latest. \
Therefore nonce has returned Some; qed")
}
/// Get block hash.
fn block_hash(&self, id: BlockID) -> Option<H256>;

View File

@ -245,8 +245,11 @@ impl BlockChainClient for TestBlockChainClient {
Self::block_hash(self, id)
}
fn nonce(&self, address: &Address) -> U256 {
self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
match id {
BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)),
_ => None,
}
}
fn code(&self, address: &Address) -> Option<Bytes> {