Browse Source

Fix for Trie roots.

AddressState -> Account.
cl-refactor
Gav Wood 10 years ago
parent
commit
9987248907
  1. 4
      libethereum/Account.cpp
  2. 18
      libethereum/Account.h
  3. 2
      libethereum/All.h
  4. 6
      libethereum/BlockChain.cpp
  5. 4
      libethereum/BlockChain.h
  6. 2
      libethereum/Executive.cpp
  7. 2
      libethereum/ExtVM.h
  8. 22
      libethereum/State.cpp
  9. 8
      libethereum/State.h
  10. 2
      test/vm.cpp

4
libethereum/AddressState.cpp → libethereum/Account.cpp

@ -14,12 +14,12 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file AddressState.cpp
/** @file Account.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "AddressState.h"
#include "Account.h"
#include <libethcore/CommonEth.h>
using namespace std;
using namespace dev;

18
libethereum/AddressState.h → libethereum/Account.h

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file AddressState.h
/** @file Account.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
@ -33,20 +33,20 @@ namespace eth
// TODO: Document fully.
class AddressState
class Account
{
public:
enum NewAccountType { NormalCreation, ContractConception };
/// Construct a dead AddressState.
AddressState() {}
/// Construct an alive AddressState, with given endowment, for either a normal (non-contract) account or for a contract account in the
/// Construct a dead Account.
Account() {}
/// Construct an alive Account, with given endowment, for either a normal (non-contract) account or for a contract account in the
/// conception phase, where the code is not yet known.
AddressState(u256 _balance, NewAccountType _t): m_isAlive(true), m_balance(_balance), m_codeHash(_t == NormalCreation ? h256() : EmptySHA3) {}
Account(u256 _balance, NewAccountType _t): m_isAlive(true), m_balance(_balance), m_codeHash(_t == NormalCreation ? h256() : EmptySHA3) {}
/// Explicit constructor for wierd cases of construction of a normal account.
AddressState(u256 _nonce, u256 _balance): m_isAlive(true), m_nonce(_nonce), m_balance(_balance) {}
Account(u256 _nonce, u256 _balance): m_isAlive(true), m_nonce(_nonce), m_balance(_balance) {}
/// Explicit constructor for wierd cases of construction or a contract account.
AddressState(u256 _nonce, u256 _balance, h256 _contractRoot, h256 _codeHash): m_isAlive(true), m_nonce(_nonce), m_balance(_balance), m_storageRoot(_contractRoot), m_codeHash(_codeHash) {}
Account(u256 _nonce, u256 _balance, h256 _contractRoot, h256 _codeHash): m_isAlive(true), m_nonce(_nonce), m_balance(_balance), m_storageRoot(_contractRoot), m_codeHash(_codeHash) { assert(_contractRoot); }
void kill() { m_isAlive = false; m_storageOverlay.clear(); m_codeHash = EmptySHA3; m_storageRoot = EmptyTrie; m_balance = 0; m_nonce = 0; }
bool isAlive() const { return m_isAlive; }
@ -59,7 +59,7 @@ public:
u256 const& nonce() const { return m_nonce; }
void incNonce() { m_nonce++; }
h256 baseRoot() const { return m_storageRoot; }
h256 baseRoot() const { assert(m_storageRoot); return m_storageRoot; }
std::map<u256, u256> const& storage() const { return m_storageOverlay; }
void setStorage(u256 _p, u256 _v) { m_storageOverlay[_p] = _v; }

2
libethereum/All.h

@ -1,6 +1,6 @@
#pragma once
#include "AddressState.h"
#include "Account.h"
#include "BlockChain.h"
#include "Client.h"
#include "Defaults.h"

6
libethereum/BlockChain.cpp

@ -52,9 +52,9 @@ std::ostream& dev::eth::operator<<(std::ostream& _out, BlockChain const& _bc)
return _out;
}
std::map<Address, AddressState> const& dev::eth::genesisState()
std::map<Address, Account> const& dev::eth::genesisState()
{
static std::map<Address, AddressState> s_ret;
static std::map<Address, Account> s_ret;
if (s_ret.empty())
// Initialise.
for (auto i: vector<string>({
@ -67,7 +67,7 @@ std::map<Address, AddressState> const& dev::eth::genesisState()
"6c386a4b26f73c802f34673f7248bb118f97424a",
"e4157b34ea9615cfbde6b4fda419828124b70c78"
}))
s_ret[Address(fromHex(i))] = AddressState(u256(1) << 200, AddressState::NormalCreation);
s_ret[Address(fromHex(i))] = Account(u256(1) << 200, Account::NormalCreation);
return s_ret;
}

4
libethereum/BlockChain.h

@ -33,7 +33,7 @@
#include <libethcore/BlockInfo.h>
#include <libdevcore/Guards.h>
#include "BlockDetails.h"
#include "AddressState.h"
#include "Account.h"
#include "BlockQueue.h"
namespace ldb = leveldb;
@ -57,7 +57,7 @@ struct BlockChainChat: public LogChannel { static const char* name() { return "-
struct BlockChainNote: public LogChannel { static const char* name() { return "=B="; } static const int verbosity = 4; };
// TODO: Move all this Genesis stuff into Genesis.h/.cpp
std::map<Address, AddressState> const& genesisState();
std::map<Address, Account> const& genesisState();
ldb::Slice toSlice(h256 _h, unsigned _sub = 0);

2
libethereum/Executive.cpp

@ -138,7 +138,7 @@ bool Executive::create(Address _sender, u256 _endowment, u256 _gasPrice, u256 _g
m_newAddress = right160(sha3(rlpList(_sender, m_s.transactionsFrom(_sender) - 1)));
// Set up new account...
m_s.m_cache[m_newAddress] = AddressState(0, m_s.balance(m_newAddress) + _endowment, h256(), h256());
m_s.m_cache[m_newAddress] = Account(m_s.balance(m_newAddress) + _endowment, Account::ContractConception);
// Execute _init.
m_vm = new VM(_gas);

2
libethereum/ExtVM.h

@ -102,7 +102,7 @@ public:
private:
State& m_s; ///< A reference to the base state.
std::map<Address, AddressState> m_origCache; ///< The cache of the address states (i.e. the externalities) as-was prior to the execution.
std::map<Address, Account> m_origCache; ///< The cache of the address states (i.e. the externalities) as-was prior to the execution.
Manifest* m_ms;
};

22
libethereum/State.cpp

@ -224,7 +224,7 @@ Address State::nextActiveAddress(Address _a) const
// TODO: repot
struct CachedAddressState
{
CachedAddressState(std::string const& _rlp, AddressState const* _s, OverlayDB const* _o): rS(_rlp), r(rS), s(_s), o(_o) {}
CachedAddressState(std::string const& _rlp, Account const* _s, OverlayDB const* _o): rS(_rlp), r(rS), s(_s), o(_o) {}
bool exists() const
{
@ -300,7 +300,7 @@ struct CachedAddressState
std::string rS;
RLP r;
AddressState const* s;
Account const* s;
OverlayDB const* o;
};
@ -346,7 +346,7 @@ void State::ensureCached(Address _a, bool _requireCode, bool _forceCreate) const
ensureCached(m_cache, _a, _requireCode, _forceCreate);
}
void State::ensureCached(std::map<Address, AddressState>& _cache, Address _a, bool _requireCode, bool _forceCreate) const
void State::ensureCached(std::map<Address, Account>& _cache, Address _a, bool _requireCode, bool _forceCreate) const
{
auto it = _cache.find(_a);
if (it == _cache.end())
@ -356,11 +356,11 @@ void State::ensureCached(std::map<Address, AddressState>& _cache, Address _a, bo
if (stateBack.empty() && !_forceCreate)
return;
RLP state(stateBack);
AddressState s;
Account s;
if (state.isNull())
s = AddressState(0, AddressState::NormalCreation);
s = Account(0, Account::NormalCreation);
else
s = AddressState(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>());
s = Account(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>());
bool ok;
tie(it, ok) = _cache.insert(make_pair(_a, s));
}
@ -965,7 +965,7 @@ void State::noteSending(Address _id)
{
cwarn << "Sending from non-existant account. How did it pay!?!";
// this is impossible. but we'll continue regardless...
m_cache[_id] = AddressState(1, 0);
m_cache[_id] = Account(1, 0);
}
else
it->second.incNonce();
@ -976,7 +976,7 @@ void State::addBalance(Address _id, u256 _amount)
ensureCached(_id, false, false);
auto it = m_cache.find(_id);
if (it == m_cache.end())
m_cache[_id] = AddressState(_amount, AddressState::NormalCreation);
m_cache[_id] = Account(_amount, Account::NormalCreation);
else
it->second.addBalance(_amount);
}
@ -1057,7 +1057,7 @@ h256 State::storageRoot(Address _id) const
RLP r(s);
return r[2].toHash<h256>();
}
return h256();
return EmptyTrie;
}
bytes const& State::code(Address _contract) const
@ -1267,7 +1267,7 @@ h160 State::create(Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas,
Address newAddress = right160(sha3(rlpList(_sender, transactionsFrom(_sender) - 1)));
// Set up new account...
m_cache[newAddress] = AddressState(balance(newAddress) + _endowment, AddressState::ContractConception);
m_cache[newAddress] = Account(balance(newAddress) + _endowment, Account::ContractConception);
// Execute init code.
VM vm(*_gas);
@ -1366,7 +1366,7 @@ std::ostream& dev::eth::operator<<(std::ostream& _out, State const& _s)
for (auto i: d)
{
auto it = _s.m_cache.find(i);
AddressState* cache = it != _s.m_cache.end() ? &it->second : nullptr;
Account* cache = it != _s.m_cache.end() ? &it->second : nullptr;
string rlpString = dtr.count(i) ? trie.at(i) : "";
RLP r(rlpString);
assert(cache || r);

8
libethereum/State.h

@ -33,7 +33,7 @@
#include <libevm/FeeStructure.h>
#include <libevm/ExtVMFace.h>
#include "TransactionQueue.h"
#include "AddressState.h"
#include "Account.h"
#include "Transaction.h"
#include "Executive.h"
#include "AccountDiff.h"
@ -299,7 +299,7 @@ private:
void ensureCached(Address _a, bool _requireCode, bool _forceCreate) const;
/// Retrieve all information about a given address into a cache.
void ensureCached(std::map<Address, AddressState>& _cache, Address _a, bool _requireCode, bool _forceCreate) const;
void ensureCached(std::map<Address, Account>& _cache, Address _a, bool _requireCode, bool _forceCreate) const;
/// Commit all changes waiting in the address cache to the DB.
void commit();
@ -340,7 +340,7 @@ private:
std::set<h256> m_transactionSet; ///< The set of transaction hashes that we've included in the state.
OverlayDB m_lastTx;
mutable std::map<Address, AddressState> m_cache; ///< Our address cache. This stores the states of each address that has (or at least might have) been changed.
mutable std::map<Address, Account> m_cache; ///< Our address cache. This stores the states of each address that has (or at least might have) been changed.
BlockInfo m_previousBlock; ///< The previous block's information.
BlockInfo m_currentBlock; ///< The current block's information.
@ -365,7 +365,7 @@ private:
std::ostream& operator<<(std::ostream& _out, State const& _s);
template <class DB>
void commit(std::map<Address, AddressState> const& _cache, DB& _db, TrieDB<Address, DB>& _state)
void commit(std::map<Address, Account> const& _cache, DB& _db, TrieDB<Address, DB>& _state)
{
for (auto const& i: _cache)
if (!i.second.isAlive())

2
test/vm.cpp

@ -471,7 +471,7 @@ h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _end
}
// Set up new account...
m_cache[_newAddress] = AddressState(0, balance(_newAddress) + _endowment, h256(), h256());
m_cache[_newAddress] = Account(0, balance(_newAddress) + _endowment, h256(), h256());
// Execute init code.
VM vm(*_gas);

Loading…
Cancel
Save