Browse Source

Merge remote-tracking branch 'upstream/develop' into stateTests

cl-refactor
Christoph Jentzsch 10 years ago
parent
commit
5f451a38d3
  1. 6
      alethzero/MainWin.cpp
  2. 4
      astylerc
  3. 2
      libdevcore/Common.cpp
  4. 2
      libethcore/CommonEth.cpp
  5. 24
      libethereum/AddressState.h
  6. 2
      libethereum/BlockChain.cpp
  7. 2
      libethereum/Executive.cpp
  8. 22
      libethereum/State.cpp
  9. 8
      libethereum/State.h

6
alethzero/MainWin.cpp

@ -974,7 +974,7 @@ void Main::refreshBlockChain()
auto b = bc.block(h); auto b = bc.block(h);
for (auto const& i: RLP(b)[1]) for (auto const& i: RLP(b)[1])
{ {
Transaction t(i[0].data()); Transaction t(i.data());
if (bm || transactionMatch(filter, t)) if (bm || transactionMatch(filter, t))
{ {
QString s = t.receiveAddress ? QString s = t.receiveAddress ?
@ -1235,13 +1235,13 @@ void Main::on_blocks_currentItemChanged()
else else
s << "<br/>Pre: <i>Nothing is before the Gensesis</i>"; s << "<br/>Pre: <i>Nothing is before the Gensesis</i>";
for (auto const& i: block[1]) for (auto const& i: block[1])
s << "<br/>" << sha3(i[0].data()).abridged() << ": <b>" << i[1].toHash<h256>() << "</b> [<b>" << i[2].toInt<u256>() << "</b> used]"; s << "<br/>" << sha3(i.data()).abridged();// << ": <b>" << i[1].toHash<h256>() << "</b> [<b>" << i[2].toInt<u256>() << "</b> used]";
s << "<br/>Post: <b>" << info.stateRoot << "</b>"; s << "<br/>Post: <b>" << info.stateRoot << "</b>";
} }
else else
{ {
unsigned txi = item->data(Qt::UserRole + 1).toInt(); unsigned txi = item->data(Qt::UserRole + 1).toInt();
Transaction tx(block[1][txi][0].data()); Transaction tx(block[1][txi].data());
auto ss = tx.safeSender(); auto ss = tx.safeSender();
h256 th = sha3(rlpList(ss, tx.nonce)); h256 th = sha3(rlpList(ss, tx.nonce));
s << "<h3>" << th << "</h3>"; s << "<h3>" << th << "</h3>";

4
astylerc

@ -6,6 +6,6 @@ min-conditional-indent=1
pad-oper pad-oper
pad-header pad-header
unpad-paren unpad-paren
delete-empty-lines
align-pointer=type align-pointer=type
keep-one-line-blocks
close-templates

2
libdevcore/Common.cpp

@ -27,7 +27,7 @@ using namespace dev;
namespace dev namespace dev
{ {
char const* Version = "0.7.7"; char const* Version = "0.7.8";
} }

2
libethcore/CommonEth.cpp

@ -34,7 +34,7 @@ namespace dev
namespace eth namespace eth
{ {
const unsigned c_protocolVersion = 37; const unsigned c_protocolVersion = 38;
const unsigned c_databaseVersion = 3; const unsigned c_databaseVersion = 3;
static const vector<pair<u256, string>> g_units = static const vector<pair<u256, string>> g_units =

24
libethereum/AddressState.h

@ -23,6 +23,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/RLP.h> #include <libdevcore/RLP.h>
#include <libdevcrypto/TrieDB.h>
#include <libdevcrypto/SHA3.h> #include <libdevcrypto/SHA3.h>
namespace dev namespace dev
@ -35,10 +36,19 @@ namespace eth
class AddressState class AddressState
{ {
public: public:
AddressState(): m_isAlive(false), m_nonce(0), m_balance(0) {} 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
/// 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) {}
/// 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) {}
/// 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) {} AddressState(u256 _nonce, u256 _balance, h256 _contractRoot, h256 _codeHash): m_isAlive(true), m_nonce(_nonce), m_balance(_balance), m_storageRoot(_contractRoot), m_codeHash(_codeHash) {}
void kill() { m_isAlive = false; m_storageOverlay.clear(); m_codeHash = EmptySHA3; m_storageRoot = h256(); m_balance = 0; m_nonce = 0; } 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; } bool isAlive() const { return m_isAlive; }
u256& balance() { return m_balance; } u256& balance() { return m_balance; }
@ -62,17 +72,17 @@ public:
void noteCode(bytesConstRef _code) { assert(sha3(_code) == m_codeHash); m_codeCache = _code.toBytes(); } void noteCode(bytesConstRef _code) { assert(sha3(_code) == m_codeHash); m_codeCache = _code.toBytes(); }
private: private:
bool m_isAlive; bool m_isAlive = false;
u256 m_nonce; u256 m_nonce = 0;
u256 m_balance; u256 m_balance = 0;
/// The base storage root. Used with the state DB to give a base to the storage. m_storageOverlay is overlaid on this and takes precedence for all values set. /// The base storage root. Used with the state DB to give a base to the storage. m_storageOverlay is overlaid on this and takes precedence for all values set.
h256 m_storageRoot; h256 m_storageRoot = EmptyTrie;
/// If 0 then we're in the limbo where we're running the initialisation code. We expect a setCode() at some point later. /// If 0 then we're in the limbo where we're running the initialisation code. We expect a setCode() at some point later.
/// If EmptySHA3, then m_code, which should be empty, is valid. /// If EmptySHA3, then m_code, which should be empty, is valid.
/// If anything else, then m_code is valid iff it's not empty, otherwise, State::ensureCached() needs to be called with the correct args. /// If anything else, then m_code is valid iff it's not empty, otherwise, State::ensureCached() needs to be called with the correct args.
h256 m_codeHash; h256 m_codeHash = EmptySHA3;
// TODO: change to unordered_map. // TODO: change to unordered_map.
std::map<u256, u256> m_storageOverlay; std::map<u256, u256> m_storageOverlay;

2
libethereum/BlockChain.cpp

@ -67,7 +67,7 @@ std::map<Address, AddressState> const& dev::eth::genesisState()
"6c386a4b26f73c802f34673f7248bb118f97424a", "6c386a4b26f73c802f34673f7248bb118f97424a",
"e4157b34ea9615cfbde6b4fda419828124b70c78" "e4157b34ea9615cfbde6b4fda419828124b70c78"
})) }))
s_ret[Address(fromHex(i))] = AddressState(0, u256(1) << 200, h256(), EmptySHA3); s_ret[Address(fromHex(i))] = AddressState(u256(1) << 200, AddressState::NormalCreation);
return s_ret; return s_ret;
} }

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))); m_newAddress = right160(sha3(rlpList(_sender, m_s.transactionsFrom(_sender) - 1)));
// Set up new account... // 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] = AddressState(0, m_s.balance(m_newAddress) + _endowment, EmptyTrie, h256());
// Execute _init. // Execute _init.
m_vm = new VM(_gas); m_vm = new VM(_gas);

22
libethereum/State.cpp

@ -324,6 +324,9 @@ StateDiff State::diff(State const& _c) const
for (auto i: _c.m_cache) for (auto i: _c.m_cache)
ads.insert(i.first); ads.insert(i.first);
cnote << *this;
cnote << _c;
for (auto i: ads) for (auto i: ads)
{ {
auto it = m_cache.find(i); auto it = m_cache.find(i);
@ -355,7 +358,7 @@ void State::ensureCached(std::map<Address, AddressState>& _cache, Address _a, bo
RLP state(stateBack); RLP state(stateBack);
AddressState s; AddressState s;
if (state.isNull()) if (state.isNull())
s = AddressState(0, 0, EmptyTrie, EmptySHA3); s = AddressState(0, AddressState::NormalCreation);
else else
s = AddressState(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>()); s = AddressState(state[0].toInt<u256>(), state[1].toInt<u256>(), state[2].toHash<h256>(), state[3].toHash<h256>());
bool ok; bool ok;
@ -622,8 +625,6 @@ u256 State::enact(bytesConstRef _block, BlockChain const* _bc, bool _checkNonce)
RLPStream k; RLPStream k;
k << i; k << i;
RLPStream txrlp;
m_transactions[i].streamRLP(txrlp);
transactionsTrie.insert(&k.out(), tr.data()); transactionsTrie.insert(&k.out(), tr.data());
// cnote << m_state.root() << m_state; // cnote << m_state.root() << m_state;
@ -960,8 +961,12 @@ void State::noteSending(Address _id)
{ {
ensureCached(_id, false, false); ensureCached(_id, false, false);
auto it = m_cache.find(_id); auto it = m_cache.find(_id);
if (it == m_cache.end()) if (asserts(it != m_cache.end()))
m_cache[_id] = AddressState(1, 0, h256(), EmptySHA3); {
cwarn << "Sending from non-existant account. How did it pay!?!";
// this is impossible. but we'll continue regardless...
m_cache[_id] = AddressState(1, 0);
}
else else
it->second.incNonce(); it->second.incNonce();
} }
@ -971,7 +976,7 @@ void State::addBalance(Address _id, u256 _amount)
ensureCached(_id, false, false); ensureCached(_id, false, false);
auto it = m_cache.find(_id); auto it = m_cache.find(_id);
if (it == m_cache.end()) if (it == m_cache.end())
m_cache[_id] = AddressState(0, _amount, h256(), EmptySHA3); m_cache[_id] = AddressState(_amount, AddressState::NormalCreation);
else else
it->second.addBalance(_amount); it->second.addBalance(_amount);
} }
@ -1084,7 +1089,7 @@ bool State::isTrieGood(bool _enforceRefs, bool _requireNoLeftOvers) const
RLP r(i.second); RLP r(i.second);
TrieDB<h256, OverlayDB> storageDB(const_cast<OverlayDB*>(&m_db), r[2].toHash<h256>()); // promise not to alter OverlayDB. TrieDB<h256, OverlayDB> storageDB(const_cast<OverlayDB*>(&m_db), r[2].toHash<h256>()); // promise not to alter OverlayDB.
for (auto const& j: storageDB) { (void)j; } for (auto const& j: storageDB) { (void)j; }
if (!e && r[3].toHash<h256>() && m_db.lookup(r[3].toHash<h256>()).empty()) if (!e && r[3].toHash<h256>() != EmptySHA3 && m_db.lookup(r[3].toHash<h256>()).empty())
return false; return false;
} }
} }
@ -1262,7 +1267,7 @@ h160 State::create(Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas,
Address newAddress = right160(sha3(rlpList(_sender, transactionsFrom(_sender) - 1))); Address newAddress = right160(sha3(rlpList(_sender, transactionsFrom(_sender) - 1)));
// Set up new account... // Set up new account...
m_cache[newAddress] = AddressState(0, balance(newAddress) + _endowment, h256(), h256()); m_cache[newAddress] = AddressState(balance(newAddress) + _endowment, AddressState::ContractConception);
// Execute init code. // Execute init code.
VM vm(*_gas); VM vm(*_gas);
@ -1376,7 +1381,6 @@ std::ostream& dev::eth::operator<<(std::ostream& _out, State const& _s)
stringstream contout; stringstream contout;
/// For POC6, 3rd value of account is code and will be empty if code is not present.
if ((cache && cache->codeBearing()) || (!cache && r && !r[3].isEmpty())) if ((cache && cache->codeBearing()) || (!cache && r && !r[3].isEmpty()))
{ {
std::map<u256, u256> mem; std::map<u256, u256> mem;

8
libethereum/State.h

@ -375,7 +375,10 @@ void commit(std::map<Address, AddressState> const& _cache, DB& _db, TrieDB<Addre
s << i.second.nonce() << i.second.balance(); s << i.second.nonce() << i.second.balance();
if (i.second.storage().empty()) if (i.second.storage().empty())
s.append(i.second.baseRoot(), false, true); {
assert(i.second.baseRoot());
s.append(i.second.baseRoot());
}
else else
{ {
TrieDB<h256, DB> storageDB(&_db, i.second.baseRoot()); TrieDB<h256, DB> storageDB(&_db, i.second.baseRoot());
@ -384,7 +387,8 @@ void commit(std::map<Address, AddressState> const& _cache, DB& _db, TrieDB<Addre
storageDB.insert(j.first, rlp(j.second)); storageDB.insert(j.first, rlp(j.second));
else else
storageDB.remove(j.first); storageDB.remove(j.first);
s.append(storageDB.root(), false, true); assert(storageDB.root());
s.append(storageDB.root());
} }
if (i.second.isFreshCode()) if (i.second.isFreshCode())

Loading…
Cancel
Save