From 2b7fae8fa64073112361cc9703baac2db0947556 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 25 May 2016 17:35:15 +0200 Subject: [PATCH 01/10] add state_at_id and balance_at_id, integrate with RPC --- ethcore/src/client/client.rs | 17 +++++++++++++++++ ethcore/src/client/mod.rs | 14 +++++++++++++- miner/src/lib.rs | 6 +++--- rpc/src/v1/impls/eth.rs | 17 ++++++++++++++--- 4 files changed, 47 insertions(+), 7 deletions(-) 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))), }) } From 3405f3eab13894e17e60c4b7135978bb27e191f6 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 25 May 2016 17:54:20 +0200 Subject: [PATCH 02/10] implement storage_at_id --- ethcore/src/client/client.rs | 7 +++++++ ethcore/src/client/mod.rs | 12 +++++++++++- rpc/src/v1/impls/eth.rs | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 2d4d20c12..c28493d7a 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -578,6 +578,13 @@ impl BlockChainClient for Client where V: Verifier { self.state().storage_at(address, position) } + fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { + match id { + BlockID::Latest => Some(self.storage_at(address, position)), + id => self.state_at_id(id).map(|s| s.storage_at(address, position)), + } + } + fn transaction(&self, id: TransactionID) -> Option { self.transaction_address(id).and_then(|address| self.chain.transaction(&address)) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 0ac255c7d..75517190f 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -79,6 +79,7 @@ pub trait BlockChainClient : Sync + Send { /// Account balance at a specific block ID. /// + /// Must not fail if `id` is `BlockID::Latest`. /// 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 { @@ -89,9 +90,18 @@ pub trait BlockChainClient : Sync + Send { } } - /// Get value of the storage at given position. + /// Get value of the storage at given position from the latest state. fn storage_at(&self, address: &Address, position: &H256) -> H256; + /// Get value of the storage at given position form the latest state. + fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.storage_at(address, position)) + } else { + None + } + } + /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 5b4fc6c89..2751444e2 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -361,9 +361,9 @@ impl Eth for EthClient where fn storage_at(&self, params: Params) -> Result { from_params_default_third::(params) .and_then(|(address, position, block_number,)| match block_number { - BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(take_weak!(self.client).deref(), &address, &H256::from(position)))), + BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), BlockNumber::Latest => to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))), - _ => Err(Error::invalid_params()), + id => to_value(&try!(take_weak!(self.client).storage_at_id(&address, &H256::from(position), id.into()).ok_or_else(make_unsupported_err))), }) } From 86eab79d9de8946d67922a0bea2360384e54e413 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 11:46:45 +0200 Subject: [PATCH 03/10] consolidate [balance/storage]_at and _at_id functionality --- ethcore/src/client/client.rs | 29 ++++++++++------------------ ethcore/src/client/mod.rs | 32 +++++++------------------------ ethcore/src/client/test_client.rs | 16 ++++++++++++---- miner/src/miner.rs | 14 ++++++++++---- rpc/src/v1/impls/eth.rs | 13 +++++++------ sync/src/chain.rs | 2 +- 6 files changed, 47 insertions(+), 59 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index c28493d7a..1b31a7b3b 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -349,7 +349,12 @@ 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. - pub fn state_at_id(&self, id: BlockID) -> Option { + pub fn state_at(&self, id: BlockID) -> Option { + // fast path for latest state. + if let BlockID::Latest = id.clone() { + return Some(self.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()) @@ -563,26 +568,12 @@ impl BlockChainClient for Client where V: Verifier { self.state().code(address) } - fn balance(&self, address: &Address) -> U256 { - self.state().balance(address) + fn balance(&self, address: &Address, id: BlockID) -> Option { + self.state_at(id).map(|s| s.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) - } - - fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { - match id { - BlockID::Latest => Some(self.storage_at(address, position)), - id => self.state_at_id(id).map(|s| s.storage_at(address, position)), - } + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option { + self.state_at(id).map(|s| s.storage_at(address, position)) } fn transaction(&self, id: TransactionID) -> Option { diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 75517190f..988dac023 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -74,33 +74,15 @@ pub trait BlockChainClient : Sync + Send { /// Get address code. fn code(&self, address: &Address) -> Option; - /// Get address balance at latest state. - fn balance(&self, address: &Address) -> U256; - - /// Account balance at a specific block ID. + /// Get address balance at the given block's state. /// - /// Must not fail if `id` is `BlockID::Latest`. - /// 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 - } - } + /// Returns None if and only if the block's root hash has been pruned from the DB. + fn balance(&self, address: &Address, id: BlockID) -> Option; - /// Get value of the storage at given position from the latest state. - fn storage_at(&self, address: &Address, position: &H256) -> H256; - - /// Get value of the storage at given position form the latest state. - fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { - if let BlockID::Latest = id { - Some(self.storage_at(address, position)) - } else { - None - } - } + /// Get value of the storage at given position at the given block. + /// + /// Returns None if and only if the block's root hash has been pruned from the DB. + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index a60218a31..3e7608395 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -253,12 +253,20 @@ impl BlockChainClient for TestBlockChainClient { self.code.read().unwrap().get(address).cloned() } - fn balance(&self, address: &Address) -> U256 { - self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) + fn balance(&self, address: &Address, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)) + } else { + None + } } - fn storage_at(&self, address: &Address, position: &H256) -> H256 { - self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new) + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new)) + } else { + None + } } fn transaction(&self, _id: TransactionID) -> Option { diff --git a/miner/src/miner.rs b/miner/src/miner.rs index f159cac1c..efb2f2c77 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -168,7 +168,7 @@ impl Miner { let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -290,12 +290,18 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.balance(address), |b| b.block().fields().state.balance(address)) + sealing_work.peek_last_ref().map_or_else( + || chain.balance(address, BlockID::Latest).unwrap(), + |b| b.block().fields().state.balance(address) + ) } fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.storage_at(address, position), |b| b.block().fields().state.storage_at(address, position)) + sealing_work.peek_last_ref().map_or_else( + || chain.storage_at(address, position, BlockID::Latest).unwrap(), + |b| b.block().fields().state.storage_at(address, position) + ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { @@ -546,7 +552,7 @@ impl MinerService for Miner { } let _ = self.import_transactions(txs, |a| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }); }); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 2751444e2..2aa5ff3f2 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -28,7 +28,7 @@ use util::numbers::*; use util::sha3::*; use util::bytes::ToPretty; use util::rlp::{encode, decode, UntrustedRlp, View}; -use ethcore::client::{self, BlockChainClient, BlockID, TransactionID, UncleID}; +use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; use ethcore::views::*; use ethcore::ethereum::Ethash; @@ -200,7 +200,7 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { nonce: client.nonce(&a), - balance: client.balance(&a), + balance: client.balance(&a, BlockID::Latest).unwrap(), } }) }; @@ -352,9 +352,8 @@ impl Eth for EthClient where fn balance(&self, params: Params) -> Result { from_params_default_second(params) .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)), - id => to_value(&try!(take_weak!(self.client).balance_at_id(&address, id.into()).ok_or_else(make_unsupported_err))), + id => to_value(&try!(take_weak!(self.client).balance(&address, id.into()).ok_or_else(make_unsupported_err))), }) } @@ -362,8 +361,10 @@ impl Eth for EthClient where from_params_default_third::(params) .and_then(|(address, position, block_number,)| match block_number { BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), - BlockNumber::Latest => to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))), - id => to_value(&try!(take_weak!(self.client).storage_at_id(&address, &H256::from(position), id.into()).ok_or_else(make_unsupported_err))), + id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) { + Some(s) => to_value(&U256::from(s)), + None => Err(make_unsupported_err()), // None is only returned on unsupported requests. + } }) } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 2a59d36df..5f9cf6371 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -901,7 +901,7 @@ impl ChainSync { let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From 3c7e4b8c6cebb9d108fbb4f370d36a806657206a Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:40:29 +0200 Subject: [PATCH 04/10] 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 { From 5afa4621f9369b007dd306474b2dafe86ebb6cb7 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:45:43 +0200 Subject: [PATCH 05/10] added balance_latest, storage_at_latest utilities with modus ponens panickers --- ethcore/src/client/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 3cb4101f7..39f007281 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -83,14 +83,30 @@ pub trait BlockChainClient : Sync + Send { /// Get address balance at the given block's state. /// + /// May not return None if given BlockID::Latest. /// Returns None if and only if the block's root hash has been pruned from the DB. fn balance(&self, address: &Address, id: BlockID) -> Option; - /// Get value of the storage at given position at the given block. + /// Get address balance at the latest block's state. + fn balance_latest(&self, address: &Address) -> U256 { + self.balance(address, BlockID::Latest) + .expect("balance will return Some if given BlockID::Latest. balance was given BlockID::Latest \ + Therefore balance has returned Some; qed") + } + + /// Get value of the storage at given position at the given block's state. /// + /// May not return None if given BlockID::Latest. /// Returns None if and only if the block's root hash has been pruned from the DB. fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; + /// Get value of the storage at given position at the latest block's state. + fn storage_at_latest(&self, address: &Address, position: &H256) -> H256 { + self.storage_at(address, position, BlockID::Latest) + .expect("storage_at will return Some if given BlockID::Latest. storage_at was given BlockID::Latest. \ + Therefore storage_at has returned Some; qed") + } + /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; From a3b1cdb175b1722bda1f09b87e1cfd133876f814 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:52:33 +0200 Subject: [PATCH 06/10] add docs for nonce_latest --- ethcore/src/client/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 39f007281..7ffa0c5d8 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -69,6 +69,7 @@ pub trait BlockChainClient : Sync + Send { /// May not fail on BlockID::Latest. fn nonce(&self, address: &Address, id: BlockID) -> Option; + /// Get address nonce at the latest block's state. 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. \ From c2a4ed6fc48bd1e8bab19d89bc1fcfa714f24d3f Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:54:18 +0200 Subject: [PATCH 07/10] change nonce, balance, storage_at to *_latest counterparts --- miner/src/miner.rs | 16 ++++++++-------- sync/src/chain.rs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/miner/src/miner.rs b/miner/src/miner.rs index efb2f2c77..9fb771bb7 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -167,8 +167,8 @@ impl Miner { }; let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -291,7 +291,7 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.balance(address, BlockID::Latest).unwrap(), + || chain.balance_latest(address), |b| b.block().fields().state.balance(address) ) } @@ -299,14 +299,14 @@ impl MinerService for Miner { fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.storage_at(address, position, BlockID::Latest).unwrap(), + || chain.storage_at_latest(address, position), |b| b.block().fields().state.storage_at(address, position) ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.nonce(address), |b| b.block().fields().state.nonce(address)) + sealing_work.peek_last_ref().map_or_else(|| chain.nonce_latest(address), |b| b.block().fields().state.nonce(address)) } fn code(&self, chain: &BlockChainClient, address: &Address) -> Option { @@ -551,8 +551,8 @@ impl MinerService for Miner { let _sender = tx.sender(); } let _ = self.import_transactions(txs, |a| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }); }); } @@ -572,7 +572,7 @@ impl MinerService for Miner { }) .collect::>(); for sender in to_remove.into_iter() { - transaction_queue.remove_all(sender, chain.nonce(&sender)); + transaction_queue.remove_all(sender, chain.nonce_latest(&sender)); } }); } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 5f9cf6371..7f8e21e58 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -900,8 +900,8 @@ impl ChainSync { } let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From 30eee767676a6819410b48aeeeac97defb5aefa3 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:57:15 +0200 Subject: [PATCH 08/10] use new nonce function in eth_TransactionCount --- rpc/src/v1/impls/eth.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 2aa5ff3f2..0d9f7211d 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -164,7 +164,7 @@ impl EthClient where .or_else(|| miner .last_nonce(&request.from) .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.nonce(&request.from)), + .unwrap_or_else(|| client.nonce_latest(&request.from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or_else(|| miner.sensible_gas_limit()), gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), @@ -181,7 +181,7 @@ impl EthClient where let miner = take_weak!(self.miner); let from = request.from.unwrap_or(Address::zero()); Ok(EthTransaction { - nonce: request.nonce.unwrap_or_else(|| client.nonce(&from)), + nonce: request.nonce.unwrap_or_else(|| client.nonce_latest(&from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or(U256::from(50_000_000)), gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), @@ -199,8 +199,8 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { - nonce: client.nonce(&a), - balance: client.balance(&a, BlockID::Latest).unwrap(), + nonce: client.nonce_latest(&a), + balance: client.balance_latest(&a), } }) }; @@ -372,8 +372,7 @@ impl Eth for EthClient where from_params_default_second(params) .and_then(|(address, block_number,)| match block_number { BlockNumber::Pending => to_value(&take_weak!(self.miner).nonce(take_weak!(self.client).deref(), &address)), - BlockNumber::Latest => to_value(&take_weak!(self.client).nonce(&address)), - _ => Err(Error::invalid_params()), + id => to_value(&take_weak!(self.client).nonce(&address, id.into())), }) } From 3f8936263079da2cccb540f609ea0db352a596de Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 22:17:55 +0200 Subject: [PATCH 09/10] rename x_latest to latest_x in BlockChainClient --- ethcore/src/client/mod.rs | 6 +++--- miner/src/miner.rs | 16 ++++++++-------- rpc/src/v1/impls/eth.rs | 8 ++++---- sync/src/chain.rs | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 7ffa0c5d8..8e0a7b2dd 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -70,7 +70,7 @@ pub trait BlockChainClient : Sync + Send { fn nonce(&self, address: &Address, id: BlockID) -> Option; /// Get address nonce at the latest block's state. - fn nonce_latest(&self, address: &Address) -> U256 { + fn latest_nonce(&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") @@ -89,7 +89,7 @@ pub trait BlockChainClient : Sync + Send { fn balance(&self, address: &Address, id: BlockID) -> Option; /// Get address balance at the latest block's state. - fn balance_latest(&self, address: &Address) -> U256 { + fn latest_balance(&self, address: &Address) -> U256 { self.balance(address, BlockID::Latest) .expect("balance will return Some if given BlockID::Latest. balance was given BlockID::Latest \ Therefore balance has returned Some; qed") @@ -102,7 +102,7 @@ pub trait BlockChainClient : Sync + Send { fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; /// Get value of the storage at given position at the latest block's state. - fn storage_at_latest(&self, address: &Address, position: &H256) -> H256 { + fn latest_storage_at(&self, address: &Address, position: &H256) -> H256 { self.storage_at(address, position, BlockID::Latest) .expect("storage_at will return Some if given BlockID::Latest. storage_at was given BlockID::Latest. \ Therefore storage_at has returned Some; qed") diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 9fb771bb7..3860a79e6 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -167,8 +167,8 @@ impl Miner { }; let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -291,7 +291,7 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.balance_latest(address), + || chain.latest_balance(address), |b| b.block().fields().state.balance(address) ) } @@ -299,14 +299,14 @@ impl MinerService for Miner { fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.storage_at_latest(address, position), + || chain.latest_storage_at(address, position), |b| b.block().fields().state.storage_at(address, position) ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.nonce_latest(address), |b| b.block().fields().state.nonce(address)) + sealing_work.peek_last_ref().map_or_else(|| chain.latest_nonce(address), |b| b.block().fields().state.nonce(address)) } fn code(&self, chain: &BlockChainClient, address: &Address) -> Option { @@ -551,8 +551,8 @@ impl MinerService for Miner { let _sender = tx.sender(); } let _ = self.import_transactions(txs, |a| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }); }); } @@ -572,7 +572,7 @@ impl MinerService for Miner { }) .collect::>(); for sender in to_remove.into_iter() { - transaction_queue.remove_all(sender, chain.nonce_latest(&sender)); + transaction_queue.remove_all(sender, chain.latest_nonce(&sender)); } }); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 0d9f7211d..7b7daeab9 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -164,7 +164,7 @@ impl EthClient where .or_else(|| miner .last_nonce(&request.from) .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.nonce_latest(&request.from)), + .unwrap_or_else(|| client.latest_nonce(&request.from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or_else(|| miner.sensible_gas_limit()), gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), @@ -181,7 +181,7 @@ impl EthClient where let miner = take_weak!(self.miner); let from = request.from.unwrap_or(Address::zero()); Ok(EthTransaction { - nonce: request.nonce.unwrap_or_else(|| client.nonce_latest(&from)), + nonce: request.nonce.unwrap_or_else(|| client.latest_nonce(&from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or(U256::from(50_000_000)), gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), @@ -199,8 +199,8 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { - nonce: client.nonce_latest(&a), - balance: client.balance_latest(&a), + nonce: client.latest_nonce(&a), + balance: client.latest_balance(&a), } }) }; diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 7f8e21e58..e66fce0d5 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -900,8 +900,8 @@ impl ChainSync { } let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From a272f8570c9aba7de1f5e889756665386dc832ce Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 27 May 2016 16:25:06 +0200 Subject: [PATCH 10/10] correct indentation --- rpc/src/v1/impls/eth.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 7b7daeab9..2225cbc61 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -262,9 +262,9 @@ const UNSUPPORTED_REQUEST_CODE: i64 = -32000; fn make_unsupported_err() -> Error { Error { - code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), - message: "Unsupported request.".into(), - data: None + code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), + message: "Unsupported request.".into(), + data: None } } @@ -362,8 +362,8 @@ impl Eth for EthClient where .and_then(|(address, position, block_number,)| match block_number { BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) { - Some(s) => to_value(&U256::from(s)), - None => Err(make_unsupported_err()), // None is only returned on unsupported requests. + Some(s) => to_value(&U256::from(s)), + None => Err(make_unsupported_err()), // None is only returned on unsupported requests. } }) }