Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Style polishing & comments improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 2, 2017
1 parent d01152a commit aa1dce9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
3 changes: 2 additions & 1 deletion libethereum/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ class Block
/// Get the bloom filter of a particular transaction that happened in the block.
LogBloom const& logBloom(unsigned _i) const { return receipt(_i).bloom(); }

/// Get the State root hash immediately after the given number of pending transactions have been applied.
/// Get the State root hash immediately after all previous transactions before transaction @a _i have been applied.
/// If (_i == 0) returns the initial state of the block.
/// If (_i == pending().size()) returns the final state of the block, prior to rewards.
/// Returns zero hash if intermediate state root is not available in the receipt (the case after EIP98)
h256 stateRootBeforeTx(unsigned _i) const;

// State-change operations
Expand Down
12 changes: 2 additions & 10 deletions libethereum/Executive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,12 @@ Executive::Executive(Block& _s, LastHashes const& _lh, unsigned _level):
{
}

Executive::Executive(State& _s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level):
m_s(_s = _block.state()),
Executive::Executive(State& o_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level):
m_s(createIntermediateState(o_s, _block, _txIndex, _bc)),
m_envInfo(_block.info(), _bc.lastHashes(_block.info().parentHash()), _txIndex ? _block.receipt(_txIndex - 1).gasUsed() : 0),
m_depth(_level),
m_sealEngine(*_bc.sealEngine())
{
u256 const rootHash = _block.stateRootBeforeTx(_txIndex);
if (rootHash)
m_s.setRoot(rootHash);
else
{
m_s.setRoot(_block.stateRootBeforeTx(0));
m_s.executeBlockTransactions(_block, _txIndex, _bc.lastHashes(_block.info().parentHash()), *_bc.sealEngine());
}
}

u256 Executive::gasUsed() const
Expand Down
2 changes: 1 addition & 1 deletion libethereum/Executive.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Executive
* populating environment info from the given Block and the LastHashes portion from the BlockChain.
* State is assigned the resultant value, but otherwise unused.
*/
Executive(State& _s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);
Executive(State& o_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);

Executive(Executive const&) = delete;
void operator=(Executive) = delete;
Expand Down
37 changes: 26 additions & 11 deletions libethereum/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "Defaults.h"
#include "ExtVM.h"
#include "Executive.h"
#include "BlockChain.h"
#include "TransactionQueue.h"

using namespace std;
Expand Down Expand Up @@ -524,13 +523,9 @@ std::pair<ExecutionResult, TransactionReceipt> State::execute(EnvInfo const& _en
Executive e(*this, _envInfo, _sealEngine);
ExecutionResult res;
e.setResultRecipient(res);
e.initialize(_t);

// OK - transaction looks valid - execute.
u256 startGasUsed = _envInfo.gasUsed();
if (!e.execute())
e.go(onOp);
e.finalize();
u256 const startGasUsed = _envInfo.gasUsed();
executeTransaction(e, _t, onOp);

if (_p == Permanence::Reverted)
m_cache.clear();
Expand All @@ -546,6 +541,15 @@ std::pair<ExecutionResult, TransactionReceipt> State::execute(EnvInfo const& _en
return make_pair(res, receipt);
}

void State::executeTransaction(Executive& _e, Transaction const& _t, OnOpFunc const& _onOp)
{
_e.initialize(_t);

if (!_e.execute())
_e.go(_onOp);
_e.finalize();
}

void State::executeBlockTransactions(Block const& _block, unsigned _txCount, LastHashes const& _lastHashes, SealEngineFace const& _sealEngine)
{
u256 gasUsed = 0;
Expand All @@ -554,10 +558,7 @@ void State::executeBlockTransactions(Block const& _block, unsigned _txCount, Las
EnvInfo envInfo(_block.info(), _lastHashes, gasUsed);

Executive e(*this, envInfo, _sealEngine);
e.initialize(_block.pending()[i]);
if (!e.execute())
e.go();
e.finalize();
executeTransaction(e, _block.pending()[i], OnOpFunc());

gasUsed += e.gasUsed();
}
Expand Down Expand Up @@ -638,3 +639,17 @@ std::ostream& dev::eth::operator<<(std::ostream& _out, State const& _s)
}
return _out;
}

State& dev::eth::createIntermediateState(State& o_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc)
{
o_s = _block.state();
u256 const rootHash = _block.stateRootBeforeTx(_txIndex);
if (rootHash)
o_s.setRoot(rootHash);
else
{
o_s.setRoot(_block.stateRootBeforeTx(0));
o_s.executeBlockTransactions(_block, _txIndex, _bc.lastHashes(_block.info().parentHash()), *_bc.sealEngine());
}
return o_s;
}
8 changes: 7 additions & 1 deletion libethereum/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ DEV_SIMPLE_EXCEPTION(InvalidAccountStartNonceInState);
DEV_SIMPLE_EXCEPTION(IncorrectAccountStartNonceInState);

class SealEngineFace;

class Executive;

namespace detail
{
Expand Down Expand Up @@ -207,6 +207,8 @@ class State
/// This will change the state accordingly.
std::pair<ExecutionResult, TransactionReceipt> execute(EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Transaction const& _t, Permanence _p = Permanence::Committed, OnOpFunc const& _onOp = OnOpFunc());

/// Execute @a _txCount transactions of a given block.
/// This will change the state accordingly.
void executeBlockTransactions(Block const& _block, unsigned _txCount, LastHashes const& _lastHashes, SealEngineFace const& _sealEngine);

/// Check if the address is in use.
Expand Down Expand Up @@ -324,6 +326,8 @@ class State

void createAccount(Address const& _address, Account const&& _account);

void executeTransaction(Executive& _e, Transaction const& _t, OnOpFunc const& _onOp);

OverlayDB m_db; ///< Our overlay for the state tree.
SecureTrieDB<Address, OverlayDB> m_state; ///< Our state tree, as an OverlayDB DB.
mutable std::unordered_map<Address, Account> m_cache; ///< Our address cache. This stores the states of each address that has (or at least might have) been changed.
Expand All @@ -339,6 +343,8 @@ class State

std::ostream& operator<<(std::ostream& _out, State const& _s);

State& createIntermediateState(State& o_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc);

template <class DB>
AddressHash commit(AccountMap const& _cache, SecureTrieDB<Address, DB>& _state)
{
Expand Down
3 changes: 3 additions & 0 deletions libethereum/TransactionReceipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace dev
namespace eth
{

/// Transaction receipt, constructed either from RLP representation or from individual values.
/// State Root is optional, m_stateRoot is h256() when it is empty (for transactions after Metropolis)
/// Empty state root is not included into RLP-encoding.
class TransactionReceipt
{
public:
Expand Down

0 comments on commit aa1dce9

Please sign in to comment.