From 53da2881e95d3fb40220386cf2dec7de8e5991ae Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 20 Apr 2015 17:58:10 +0200 Subject: [PATCH] Fix for mining, possibly. --- libethereum/Client.cpp | 9 --------- libethereum/State.h | 42 ++++++++++++++++++++---------------------- mix/MixClient.cpp | 2 +- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 7ab4a99f4..b39a15e22 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -205,20 +205,11 @@ void Client::startedWorking() // TODO: currently it contains keys for *all* blocks. Make it remove old ones. cdebug << "startedWorking()"; - cdebug << m_bc.number() << m_bc.currentHash(); - cdebug << "Pre:" << m_preMine.info(); - cdebug << "Post:" << m_postMine.info(); - cdebug << "Pre:" << m_preMine.info().headerHash(WithoutNonce) << "; Post:" << m_postMine.info().headerHash(WithoutNonce); - ETH_WRITE_GUARDED(x_preMine) m_preMine.sync(m_bc); ETH_WRITE_GUARDED(x_postMine) ETH_READ_GUARDED(x_preMine) m_postMine = m_preMine; - - cdebug << "Pre:" << m_preMine.info(); - cdebug << "Post:" << m_postMine.info(); - cdebug << "Pre:" << m_preMine.info().headerHash(WithoutNonce) << "; Post:" << m_postMine.info().headerHash(WithoutNonce); } void Client::doneWorking() diff --git a/libethereum/State.h b/libethereum/State.h index 28b005243..1b36cbceb 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -161,15 +161,18 @@ public: /// This may be called multiple times and without issue. void commitToMine(BlockChain const& _bc); + /// @returns true iff commitToMine() has been called without any subsequest transactions added &c. + bool isCommittedToMine() const { return m_committedToMine; } + /// Pass in a solution to the proof-of-work. - /// @returns true iff the given nonce is a proof-of-work for this State's block. + /// @returns true iff we were previously committed to mining. template bool completeMine(typename PoW::Solution const& _result) { - PoW::assignResult(_result, m_currentBlock); + if (!m_committedToMine) + return false; - // if (!m_pow.verify(m_currentBlock)) - // return false; + PoW::assignResult(_result, m_currentBlock); cnote << "Completed" << m_currentBlock.headerHash(WithoutNonce).abridged() << m_currentBlock.nonce.abridged() << m_currentBlock.difficulty << PoW::verify(m_currentBlock); @@ -178,24 +181,6 @@ public: return true; } - /** Commit to DB and build the final block if the previous call to mine()'s result is completion. - * Typically looks like: - * @code - * while (notYetMined) - * { - * // lock - * commitToMine(_blockChain); // will call uncommitToMine if a repeat. - * // unlock - * MineInfo info; - * for (info.completed = false; !info.completed; info = mine()) {} - * } - * // lock - * completeMine(); - * // unlock - * @endcode - */ - void completeMine(); - /// Get the complete current block, including valid nonce. /// Only valid after mine() returns true. bytes const& blockData() const { return m_currentBytes; } @@ -327,6 +312,19 @@ public: void resetCurrent(); private: + /** Commit to DB and build the final block if the previous call to mine()'s result is completion. + * Typically looks like: + * @code + * while (notYetMined) + * { + * // lock + * commitToMine(_blockChain); // will call uncommitToMine if a repeat. + * completeMine(); + * // unlock + * @endcode + */ + void completeMine(); + /// Undo the changes to the state for committing to mine. void uncommitToMine(); diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index 35d7dc413..b95dc5cf3 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -250,7 +250,7 @@ void MixClient::mine() { WriteGuard l(x_state); m_state.commitToMine(bc()); - m_state.completeMine(); + m_state.completeMine(Ethash::Solution()); bc().import(m_state.blockData(), m_stateDB, ImportRequirements::Default & ~ImportRequirements::ValidNonce); /* GenericFarm f;