From 3c7e4b8c6cebb9d108fbb4f370d36a806657206a Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:40:29 +0200 Subject: [PATCH] added nonce, nonce_latest --- ethcore/src/client/client.rs | 8 +++++--- ethcore/src/client/mod.rs | 11 +++++++++-- ethcore/src/client/test_client.rs | 7 +++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 1b31a7b3b..13d678c14 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -348,7 +348,8 @@ impl Client 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 { // fast path for latest state. if let BlockID::Latest = id.clone() { @@ -556,10 +557,11 @@ impl BlockChainClient for Client 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 { + self.state_at(id).map(|s| s.nonce(address)) } + fn block_hash(&self, id: BlockID) -> Option { Self::block_hash(&self.chain, id) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 988dac023..3cb4101f7 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -65,8 +65,15 @@ pub trait BlockChainClient : Sync + Send { /// Get block total difficulty. fn block_total_difficulty(&self, id: BlockID) -> Option; - /// 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; + + 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; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 3e7608395..de2973029 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -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 { + match id { + BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)), + _ => None, + } } fn code(&self, address: &Address) -> Option {