Browse Source

fixed block generation on mine

cl-refactor
arkpar 10 years ago
parent
commit
950305bb79
  1. 5
      libethereum/Client.cpp
  2. 43
      libethereum/State.cpp
  3. 7
      libethereum/State.h
  4. 13
      mix/MixClient.cpp
  5. 1
      mix/MixClient.h
  6. 4
      test/stateOriginal.cpp

5
libethereum/Client.cpp

@ -360,12 +360,13 @@ 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(m_bc); _s.commitToMine();
} }
else else
{ {
@ -373,7 +374,7 @@ void Client::setupState(State& _s)
} }
} }
else else
_s.commitToMine(m_bc); _s.commitToMine();
} }
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)

43
libethereum/State.cpp

@ -639,7 +639,8 @@ void State::uncommitToMine()
bool State::amIJustParanoid(BlockChain const& _bc) bool State::amIJustParanoid(BlockChain const& _bc)
{ {
commitToMine(_bc); setUncles(_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);
@ -682,22 +683,8 @@ LogBloom State::logBloom() const
return ret; return ret;
} }
// @returns the block that represents the difference between m_previousBlock and m_currentBlock. void State::setUncles(BlockChain const& _bc)
// (i.e. all the transactions we executed).
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())
@ -716,11 +703,26 @@ void State::commitToMine(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();
@ -750,12 +752,13 @@ void State::commitToMine(BlockChain const& _bc)
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);

7
libethereum/State.h

@ -105,13 +105,16 @@ 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(BlockChain const& _bc); void commitToMine();
/// 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.
@ -123,7 +126,7 @@ public:
* Typically looks like: * Typically looks like:
* @code * @code
* // lock * // lock
* commitToMine(blockchain); * commitToMine();
* // unlock * // unlock
* MineInfo info; * MineInfo info;
* for (info.complete = false; !info.complete; info = mine()) {} * for (info.complete = false; !info.complete; info = mine()) {}

13
mix/MixClient.cpp

@ -56,12 +56,16 @@ 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.resize(256);
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;
@ -163,12 +167,15 @@ 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(BlockChain()); m_state.commitToMine();
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,6 +145,7 @@ 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;
}; };
} }

4
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(bc); s.commitToMine();
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,7 +74,7 @@ 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(bc); s.commitToMine();
while (!s.mine(100).completed) {} while (!s.mine(100).completed) {}
s.completeMine(); s.completeMine();
bc.attemptImport(s.blockData(), stateDB); bc.attemptImport(s.blockData(), stateDB);

Loading…
Cancel
Save