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. /// 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> { pub fn state_at(&self, id: BlockID) -> Option<State> {
// fast path for latest state. // fast path for latest state.
if let BlockID::Latest = id.clone() { 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) 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 { fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
self.state().nonce(address) self.state_at(id).map(|s| s.nonce(address))
} }
fn block_hash(&self, id: BlockID) -> Option<H256> { fn block_hash(&self, id: BlockID) -> Option<H256> {
Self::block_hash(&self.chain, id) Self::block_hash(&self.chain, id)
} }

View File

@ -65,8 +65,15 @@ pub trait BlockChainClient : Sync + Send {
/// Get block total difficulty. /// Get block total difficulty.
fn block_total_difficulty(&self, id: BlockID) -> Option<U256>; fn block_total_difficulty(&self, id: BlockID) -> Option<U256>;
/// Get address nonce. /// Attempt to get address nonce at given block.
fn nonce(&self, address: &Address) -> U256; /// 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. /// Get block hash.
fn block_hash(&self, id: BlockID) -> Option<H256>; fn block_hash(&self, id: BlockID) -> Option<H256>;

View File

@ -245,8 +245,11 @@ impl BlockChainClient for TestBlockChainClient {
Self::block_hash(self, id) Self::block_hash(self, id)
} }
fn nonce(&self, address: &Address) -> U256 { fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) 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> { fn code(&self, address: &Address) -> Option<Bytes> {