From 9ece109da5dc173775e92e1e267cbcae46183bab Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 28 Oct 2016 11:41:24 +0200 Subject: [PATCH] Replace bool by enum. --- libethereum/Block.cpp | 10 +++++----- libethereum/State.cpp | 14 +++++++------- libethereum/State.h | 12 +++++++++--- test/TestUtils.cpp | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/libethereum/Block.cpp b/libethereum/Block.cpp index 8ee69c7dabf..27769225759 100644 --- a/libethereum/Block.cpp +++ b/libethereum/Block.cpp @@ -625,9 +625,9 @@ u256 Block::enact(VerifiedBlockRef const& _block, BlockChain const& _bc) applyRewards(rewarded, _bc.chainParams().blockReward); // Commit all cached state changes to the state trie. - bool killEmptyAccounts = m_currentBlock.number() >= _bc.chainParams().u256Param("EIP158ForkBlock"); + bool removeEmptyAccounts = m_currentBlock.number() >= _bc.chainParams().u256Param("EIP158ForkBlock"); DEV_TIMED_ABOVE("commit", 500) - m_state.commit(killEmptyAccounts); + m_state.commit(removeEmptyAccounts ? State::CommitBehaviour::RemoveEmptyAccounts : State::CommitBehaviour::KeepEmptyAccounts); // Hash the state trie and check against the state_root hash in m_currentBlock. if (m_currentBlock.stateRoot() != m_previousBlock.stateRoot() && m_currentBlock.stateRoot() != rootHash()) @@ -689,7 +689,7 @@ void Block::performIrregularModifications() Addresses allDAOs = childDaos(); for (Address const& dao: allDAOs) m_state.transferBalance(dao, recipient, m_state.balance(dao)); - m_state.commit(false); + m_state.commit(State::CommitBehaviour::KeepEmptyAccounts); } } @@ -769,9 +769,9 @@ void Block::commitToSeal(BlockChain const& _bc, bytes const& _extraData) applyRewards(uncleBlockHeaders, _bc.chainParams().blockReward); // Commit any and all changes to the trie that are in the cache, then update the state root accordingly. - bool killEmptyAccounts = m_currentBlock.number() >= _bc.chainParams().u256Param("EIP158ForkBlock"); + bool removeEmptyAccounts = m_currentBlock.number() >= _bc.chainParams().u256Param("EIP158ForkBlock"); DEV_TIMED_ABOVE("commit", 500) - m_state.commit(killEmptyAccounts); + m_state.commit(removeEmptyAccounts ? State::CommitBehaviour::RemoveEmptyAccounts : State::CommitBehaviour::KeepEmptyAccounts); clog(StateDetail) << "Post-reward stateRoot:" << m_state.rootHash(); clog(StateDetail) << m_state; diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 33f68b9e402..9e5346aab57 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -117,7 +117,7 @@ OverlayDB State::openDB(std::string const& _basePath, h256 const& _genesisHash, void State::populateFrom(AccountMap const& _map) { eth::commit(_map, m_state); - commit(false); + commit(State::CommitBehaviour::KeepEmptyAccounts); } u256 const& State::requireAccountStartNonce() const @@ -135,7 +135,7 @@ void State::noteAccountStartNonce(u256 const& _actual) BOOST_THROW_EXCEPTION(IncorrectAccountStartNonceInState()); } -void State::killEmptyAccounts() +void State::removeEmptyAccounts() { for (auto& i: m_cache) if (i.second.isDirty() && i.second.isEmpty()) @@ -269,10 +269,10 @@ void State::clearCacheIfTooLarge() const } } -void State::commit(bool _killEmptyAccounts) +void State::commit(CommitBehaviour _commitBehaviour) { - if (_killEmptyAccounts) - killEmptyAccounts(); + if (_commitBehaviour == CommitBehaviour::RemoveEmptyAccounts) + removeEmptyAccounts(); m_touched += dev::eth::commit(m_cache, m_state); m_cache.clear(); } @@ -556,8 +556,8 @@ std::pair State::execute(EnvInfo const& _en m_cache.clear(); else { - bool killEmptyAccounts = _envInfo.number() >= _sealEngine->chainParams().u256Param("EIP158ForkBlock"); - commit(killEmptyAccounts); + bool removeEmptyAccounts = _envInfo.number() >= _sealEngine->chainParams().u256Param("EIP158ForkBlock"); + commit(removeEmptyAccounts ? State::CommitBehaviour::RemoveEmptyAccounts : State::CommitBehaviour::KeepEmptyAccounts); #if ETH_PARANOIA && !ETH_FATDB ctrace << "Executed; now" << rootHash(); diff --git a/libethereum/State.h b/libethereum/State.h index caa0b9eeda3..5f1800dee84 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -112,6 +112,12 @@ class State friend class BlockChain; public: + enum class CommitBehaviour + { + KeepEmptyAccounts, + RemoveEmptyAccounts + }; + /// Default constructor; creates with a blank database prepopulated with the genesis block. explicit State(u256 const& _accountStartNonce): State(_accountStartNonce, OverlayDB(), BaseState::Empty) {} @@ -243,8 +249,8 @@ class State StateDiff diff(State const& _c, bool _quick = false) const; /// Commit all changes waiting in the address cache to the DB. - /// @param _killEmptyAccounts if true, will remove all "touched" empty accounts from the state. - void commit(bool _killEmptyAccounts); + /// @param _commitBehaviour whether or not to remove empty accounts during commit. + void commit(CommitBehaviour _commitBehaviour); /// Resets any uncommitted changes to the cache. void setRoot(h256 const& _root); @@ -256,7 +262,7 @@ class State private: /// Turns all "touched" empty accounts into non-alive accounts. - void killEmptyAccounts(); + void removeEmptyAccounts(); /// @returns the account at the given address or a null pointer if it does not exist. /// The pointer is valid until the next access to the state or account. diff --git a/test/TestUtils.cpp b/test/TestUtils.cpp index 9f51f3d8bb1..9b59174660b 100644 --- a/test/TestUtils.cpp +++ b/test/TestUtils.cpp @@ -102,7 +102,7 @@ void ClientBaseFixture::enumerateClients(std::function void { cerr << "void ClientBaseFixture::enumerateClients. FixedClient now accepts block not sate!" << endl; - _state.commit(false); //unused variable. remove this line + _state.commit(State::CommitBehaviour::KeepEmptyAccounts); //unused variable. remove this line eth::Block b(Block::Null); b.noteChain(_bc); FixedClient client(_bc, b);