Browse Source

Revert State class changes for Mix.

cl-refactor
Gav Wood 10 years ago
parent
commit
114db8ab36
  1. 7
      alethzero/MainWin.cpp
  2. 21
      libethcore/CommonEth.cpp
  3. 2
      libethcore/CommonEth.h
  4. 7
      libethereum/Client.cpp
  5. 41
      libethereum/State.cpp
  6. 10
      libethereum/State.h
  7. 15
      mix/MixClient.cpp
  8. 1
      mix/MixClient.h
  9. 7
      test/stateOriginal.cpp

7
alethzero/MainWin.cpp

@ -1209,8 +1209,9 @@ string Main::renderDiff(StateDiff const& _d) const
if (ad.balance) if (ad.balance)
{ {
s << "<br/>" << indent << "Balance " << dec << formatBalance(ad.balance.to()); s << "<br/>" << indent << "Balance " << dec << ad.balance.to() << " [=" << formatBalance(ad.balance.to()) << "]";
s << " <b>" << showpos << (((dev::bigint)ad.balance.to()) - ((dev::bigint)ad.balance.from())) << noshowpos << "</b>"; auto d = (((dev::bigint)ad.balance.to()) - ((dev::bigint)ad.balance.from()));
s << " <b>" << showpos << dec << d << " [=" << formatBalance(d) << "]" << noshowpos << "</b>";
} }
if (ad.nonce) if (ad.nonce)
{ {
@ -1219,7 +1220,7 @@ string Main::renderDiff(StateDiff const& _d) const
} }
if (ad.code) if (ad.code)
{ {
s << "<br/>" << indent << "Code " << hex << ad.code.to().size() << " bytes"; s << "<br/>" << indent << "Code " << dec << ad.code.to().size() << " bytes";
if (ad.code.from().size()) if (ad.code.from().size())
s << " (" << ad.code.from().size() << " bytes)"; s << " (" << ad.code.from().size() << " bytes)";
} }

21
libethcore/CommonEth.cpp

@ -63,22 +63,31 @@ vector<pair<u256, string>> const& units()
return g_units; return g_units;
} }
std::string formatBalance(u256 _b) std::string formatBalance(bigint const& _b)
{ {
ostringstream ret; ostringstream ret;
if (_b > g_units[0].first * 10000) u256 b;
if (_b < 0)
{ {
ret << (_b / g_units[0].first) << " " << g_units[0].second; ret << "-";
b = (u256)-_b;
}
else
b = (u256)_b;
if (b > g_units[0].first * 10000)
{
ret << (b / g_units[0].first) << " " << g_units[0].second;
return ret.str(); return ret.str();
} }
ret << setprecision(5); ret << setprecision(5);
for (auto const& i: g_units) for (auto const& i: g_units)
if (i.first != 1 && _b >= i.first * 100) if (i.first != 1 && b >= i.first * 100)
{ {
ret << (double(_b / (i.first / 1000)) / 1000.0) << " " << i.second; ret << (double(b / (i.first / 1000)) / 1000.0) << " " << i.second;
return ret.str(); return ret.str();
} }
ret << _b << " wei"; ret << b << " wei";
return ret.str(); return ret.str();
} }

2
libethcore/CommonEth.h

@ -39,7 +39,7 @@ extern const unsigned c_protocolVersion;
extern const unsigned c_databaseVersion; extern const unsigned c_databaseVersion;
/// User-friendly string representation of the amount _b in wei. /// User-friendly string representation of the amount _b in wei.
std::string formatBalance(u256 _b); std::string formatBalance(bigint const& _b);
/// Get information concerning the currency denominations. /// Get information concerning the currency denominations.
std::vector<std::pair<u256, std::string>> const& units(); std::vector<std::pair<u256, std::string>> const& units();

7
libethereum/Client.cpp

@ -226,7 +226,7 @@ void Client::uninstallWatch(unsigned _i)
void Client::noteChanged(h256Set const& _filters) void Client::noteChanged(h256Set const& _filters)
{ {
Guard l(m_filterLock); Guard l(m_filterLock);
cnote << "noteChanged(" << _filters << ")"; // cnote << "noteChanged(" << _filters << ")";
// accrue all changes left in each filter into the watches. // accrue all changes left in each filter into the watches.
for (auto& i: m_watches) for (auto& i: m_watches)
if (_filters.count(i.second.id)) if (_filters.count(i.second.id))
@ -361,13 +361,12 @@ void Client::setupState(State& _s)
cwork << "SETUP MINE"; cwork << "SETUP MINE";
_s = m_postMine; _s = m_postMine;
} }
_s.setUncles(m_bc);
if (m_paranoia) if (m_paranoia)
{ {
if (_s.amIJustParanoid(m_bc)) if (_s.amIJustParanoid(m_bc))
{ {
cnote << "I'm just paranoid. Block is fine."; cnote << "I'm just paranoid. Block is fine.";
_s.commitToMine(); _s.commitToMine(m_bc);
} }
else else
{ {
@ -375,7 +374,7 @@ void Client::setupState(State& _s)
} }
} }
else else
_s.commitToMine(); _s.commitToMine(m_bc);
} }
void Client::transact(Secret _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice) void Client::transact(Secret _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice)

41
libethereum/State.cpp

@ -639,8 +639,7 @@ void State::uncommitToMine()
bool State::amIJustParanoid(BlockChain const& _bc) bool State::amIJustParanoid(BlockChain const& _bc)
{ {
setUncles(_bc); commitToMine(_bc);
commitToMine();
// Update difficulty according to timestamp. // Update difficulty according to timestamp.
m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock); m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock);
@ -683,8 +682,20 @@ LogBloom State::logBloom() const
return ret; return ret;
} }
void State::setUncles(BlockChain const& _bc) void State::commitToMine(BlockChain const& _bc)
{ {
uncommitToMine();
// cnote << "Committing to mine on block" << m_previousBlock.hash.abridged();
#ifdef ETH_PARANOIA
commit();
cnote << "Pre-reward stateRoot:" << m_state.root();
#endif
m_lastTx = m_db;
Addresses uncleAddresses;
RLPStream unclesData; RLPStream unclesData;
unsigned unclesCount = 0; unsigned unclesCount = 0;
if (m_previousBlock != BlockChain::genesis()) if (m_previousBlock != BlockChain::genesis())
@ -703,26 +714,11 @@ void State::setUncles(BlockChain const& _bc)
BlockInfo ubi(_bc.block(u)); BlockInfo ubi(_bc.block(u));
ubi.streamRLP(unclesData, WithNonce); ubi.streamRLP(unclesData, WithNonce);
++unclesCount; ++unclesCount;
uncleAddresses.push_back(ubi.coinbaseAddress);
} }
} }
} }
RLPStream(unclesCount).appendRaw(unclesData.out(), unclesCount).swapOut(m_currentUncles);
m_currentBlock.sha3Uncles = sha3(m_currentUncles);
}
void State::commitToMine()
{
uncommitToMine();
// cnote << "Committing to mine on block" << m_previousBlock.hash.abridged();
#ifdef ETH_PARANOIA
commit();
cnote << "Pre-reward stateRoot:" << m_state.root();
#endif
m_lastTx = m_db;
MemoryDB tm; MemoryDB tm;
GenericTrieDB<MemoryDB> transactionsTrie(&tm); GenericTrieDB<MemoryDB> transactionsTrie(&tm);
transactionsTrie.init(); transactionsTrie.init();
@ -752,13 +748,12 @@ void State::commitToMine()
txs.swapOut(m_currentTxs); txs.swapOut(m_currentTxs);
RLPStream(unclesCount).appendRaw(unclesData.out(), unclesCount).swapOut(m_currentUncles);
m_currentBlock.transactionsRoot = transactionsTrie.root(); m_currentBlock.transactionsRoot = transactionsTrie.root();
m_currentBlock.receiptsRoot = receiptsTrie.root(); m_currentBlock.receiptsRoot = receiptsTrie.root();
m_currentBlock.logBloom = logBloom(); m_currentBlock.logBloom = logBloom();
m_currentBlock.sha3Uncles = sha3(m_currentUncles);
Addresses uncleAddresses;
for (const auto& r: RLP(m_currentUncles))
uncleAddresses.push_back(BlockInfo::fromHeader(r.data()).coinbaseAddress);
// Apply rewards last of all. // Apply rewards last of all.
applyRewards(uncleAddresses); applyRewards(uncleAddresses);

10
libethereum/State.h

@ -105,16 +105,13 @@ public:
/// @returns true if all is ok. If it's false, worry. /// @returns true if all is ok. If it's false, worry.
bool amIJustParanoid(BlockChain const& _bc); bool amIJustParanoid(BlockChain const& _bc);
/// @brief Loads current block uncles from blockchain
void setUncles(BlockChain const& _bc);
/// Prepares the current state for mining. /// Prepares the current state for mining.
/// Commits all transactions into the trie, compiles uncles and transactions list, applies all /// Commits all transactions into the trie, compiles uncles and transactions list, applies all
/// rewards and populates the current block header with the appropriate hashes. /// rewards and populates the current block header with the appropriate hashes.
/// The only thing left to do after this is to actually mine(). /// The only thing left to do after this is to actually mine().
/// ///
/// This may be called multiple times and without issue. /// This may be called multiple times and without issue.
void commitToMine(); void commitToMine(BlockChain const& _bc);
/// Attempt to find valid nonce for block that this state represents. /// Attempt to find valid nonce for block that this state represents.
/// This function is thread-safe. You can safely have other interactions with this object while it is happening. /// This function is thread-safe. You can safely have other interactions with this object while it is happening.
@ -125,11 +122,14 @@ public:
/** Commit to DB and build the final block if the previous call to mine()'s result is completion. /** Commit to DB and build the final block if the previous call to mine()'s result is completion.
* Typically looks like: * Typically looks like:
* @code * @code
* while (notYetMined)
* {
* // lock * // lock
* commitToMine(); * commitToMine(_blockChain); // will call uncommitToMine if a repeat.
* // unlock * // unlock
* MineInfo info; * MineInfo info;
* for (info.complete = false; !info.complete; info = mine()) {} * for (info.complete = false; !info.complete; info = mine()) {}
* }
* // lock * // lock
* completeMine(); * completeMine();
* // unlock * // unlock

15
mix/MixClient.cpp

@ -56,16 +56,15 @@ void MixClient::resetState(u256 _balance)
genesis.state = m_state; genesis.state = m_state;
Block open; Block open;
m_blocks = Blocks { genesis, open }; //last block contains a list of pending transactions to be finalized m_blocks = Blocks { genesis, open }; //last block contains a list of pending transactions to be finalized
m_lastHashes.clear(); // m_lastHashes.clear();
m_lastHashes.resize(256); // m_lastHashes.resize(256);
m_lastHashes[0] = genesis.hash; // m_lastHashes[0] = genesis.hash;
} }
void MixClient::executeTransaction(Transaction const& _t, State& _state) void MixClient::executeTransaction(Transaction const& _t, State& _state)
{ {
bytes rlp = _t.rlp(); bytes rlp = _t.rlp();
Executive execution(_state, LastHashes(), 0);
Executive execution(_state, m_lastHashes, 0);
execution.setup(&rlp); execution.setup(&rlp);
std::vector<MachineState> machineStates; std::vector<MachineState> machineStates;
std::vector<unsigned> levels; std::vector<unsigned> levels;
@ -165,14 +164,12 @@ void MixClient::mine()
Block& block = m_blocks.back(); Block& block = m_blocks.back();
m_state.mine(0, true); m_state.mine(0, true);
m_state.completeMine(); m_state.completeMine();
m_state.commitToMine(); m_state.commitToMine(BlockChain());
m_state.cleanup(true);
block.state = m_state; block.state = m_state;
block.info = m_state.info(); block.info = m_state.info();
block.hash = block.info.hash; block.hash = block.info.hash;
m_state.cleanup(true);
m_blocks.push_back(Block()); m_blocks.push_back(Block());
m_lastHashes.insert(m_lastHashes.begin(), block.hash);
m_lastHashes.resize(256);
h256Set changed { dev::eth::PendingChangedFilter, dev::eth::ChainChangedFilter }; h256Set changed { dev::eth::PendingChangedFilter, dev::eth::ChainChangedFilter };
noteChanged(changed); noteChanged(changed);

1
mix/MixClient.h

@ -145,7 +145,6 @@ private:
std::map<h256, dev::eth::InstalledFilter> m_filters; std::map<h256, dev::eth::InstalledFilter> m_filters;
std::map<unsigned, dev::eth::ClientWatch> m_watches; std::map<unsigned, dev::eth::ClientWatch> m_watches;
Blocks m_blocks; Blocks m_blocks;
eth::LastHashes m_lastHashes;
}; };
} }

7
test/stateOriginal.cpp

@ -51,7 +51,7 @@ int stateTest()
cout << s; cout << s;
// Mine to get some ether! // Mine to get some ether!
s.commitToMine(); s.commitToMine(bc);
while (!s.mine(100).completed) {} while (!s.mine(100).completed) {}
s.completeMine(); s.completeMine();
bc.attemptImport(s.blockData(), stateDB); bc.attemptImport(s.blockData(), stateDB);
@ -74,8 +74,9 @@ int stateTest()
cout << s; cout << s;
// Mine to get some ether and set in stone. // Mine to get some ether and set in stone.
s.commitToMine(); s.commitToMine(bc);
while (!s.mine(100).completed) {} s.commitToMine(bc);
while (!s.mine(50).completed) { s.commitToMine(bc); }
s.completeMine(); s.completeMine();
bc.attemptImport(s.blockData(), stateDB); bc.attemptImport(s.blockData(), stateDB);

Loading…
Cancel
Save