diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 1da6b7d9a..235da2230 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include diff --git a/libethcore/BlockInfo.cpp b/libethcore/BlockInfo.cpp index f6d5731b3..536f684f2 100644 --- a/libethcore/BlockInfo.cpp +++ b/libethcore/BlockInfo.cpp @@ -24,7 +24,7 @@ #include #include #include -#include "Dagger.h" +#include "ProofOfWork.h" #include "Exceptions.h" #include "BlockInfo.h" using namespace std; @@ -99,7 +99,7 @@ void BlockInfo::populateFromHeader(RLP const& _header, bool _checkNonce) } // check it hashes according to proof of work or that it's the genesis block. - if (_checkNonce && parentHash && !Dagger::verify(headerHashWithoutNonce(), nonce, difficulty)) + if (_checkNonce && parentHash && !ProofOfWork::verify(headerHashWithoutNonce(), nonce, difficulty)) BOOST_THROW_EXCEPTION(InvalidBlockNonce(headerHashWithoutNonce(), nonce, difficulty)); if (gasUsed > gasLimit) diff --git a/libethcore/Dagger.h b/libethcore/Dagger.h deleted file mode 100644 index c32ef38be..000000000 --- a/libethcore/Dagger.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - This file is part of cpp-ethereum. - - cpp-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - cpp-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with cpp-ethereum. If not, see . -*/ -/** @file Dagger.h - * @author Gav Wood - * @date 2014 - * - * Dagger algorithm. Or not. - */ - -#pragma once - -#include -#include "CommonEth.h" - -#define FAKE_DAGGER 1 - -namespace dev -{ -namespace eth -{ - -struct MineInfo -{ - void combine(MineInfo const& _m) { requirement = std::max(requirement, _m.requirement); best = std::min(best, _m.best); hashes += _m.hashes; completed = completed || _m.completed; } - double requirement = 0; - double best = 1e99; - unsigned hashes = 0; - bool completed = false; -}; - -#if FAKE_DAGGER - -class Dagger -{ -public: - static h256 eval(h256 const& _root, h256 const& _nonce) { h256 b[2] = { _root, _nonce }; return sha3(bytesConstRef((byte const*)&b[0], 64)); } - static bool verify(h256 const& _root, h256 const& _nonce, u256 const& _difficulty) { return (bigint)(u256)eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; } - - MineInfo mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false); - - h256 m_last; -}; - -#else - -/// Functions are not re-entrant. If you want to multi-thread, then use different classes for each thread. -class Dagger -{ -public: - Dagger(); - ~Dagger(); - - static u256 bound(u256 const& _difficulty); - static h256 eval(h256 const& _root, u256 const& _nonce); - static bool verify(h256 const& _root, u256 const& _nonce, u256 const& _difficulty); - - bool mine(u256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool const& _continue = bool(true)); - -private: - - static h256 node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i); - - h256 m_root; - u256 m_nonce; -}; - -#endif - -} -} diff --git a/libethcore/Dagger.cpp b/libethcore/ProofOfWork.cpp similarity index 50% rename from libethcore/Dagger.cpp rename to libethcore/ProofOfWork.cpp index 956557b64..7d3916fd3 100644 --- a/libethcore/Dagger.cpp +++ b/libethcore/ProofOfWork.cpp @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ -/** @file Dagger.cpp +/** @file ProofOfWork.cpp * @author Gav Wood * @date 2014 */ @@ -28,7 +28,7 @@ #include #include #include -#include "Dagger.h" +#include "ProofOfWork.h" using namespace std; using namespace std::chrono; @@ -37,88 +37,8 @@ namespace dev namespace eth { -#if FAKE_DAGGER - -MineInfo Dagger::mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout, bool _continue, bool _turbo) -{ - MineInfo ret; - static std::mt19937_64 s_eng((time(0) + (unsigned)m_last)); - u256 s = (m_last = h256::random(s_eng)); - - bigint d = (bigint(1) << 256) / _difficulty; - ret.requirement = log2((double)d); - - // 2^ 0 32 64 128 256 - // [--------*-------------------------] - // - // evaluate until we run out of time - auto startTime = steady_clock::now(); - if (!_turbo) - this_thread::sleep_for(chrono::milliseconds(_msTimeout * 90 / 100)); - for (; (steady_clock::now() - startTime) < milliseconds(_msTimeout) && _continue; s++, ret.hashes++) - { - o_solution = (h256)s; - auto e = (bigint)(u256)eval(_root, o_solution); - ret.best = min(ret.best, log2((double)e)); - if (e <= d) - { - ret.completed = true; - break; - } - } - - if (ret.completed) - assert(verify(_root, o_solution, _difficulty)); - - return ret; -} - -#else - -Dagger::Dagger() -{ -} - -Dagger::~Dagger() -{ -} - -u256 Dagger::bound(u256 const& _difficulty) -{ - return (u256)((bigint(1) << 256) / _difficulty); -} - -bool Dagger::verify(h256 const& _root, u256 const& _nonce, u256 const& _difficulty) -{ - return eval(_root, _nonce) < bound(_difficulty); -} - -bool Dagger::mine(u256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout, bool const& _continue) -{ - // restart search if root has changed - if (m_root != _root) - { - m_root = _root; - m_nonce = 0; - } - - // compute bound - u256 const b = bound(_difficulty); - - // evaluate until we run out of time - for (auto startTime = steady_clock::now(); (steady_clock::now() - startTime) < milliseconds(_msTimeout) && _continue; m_nonce += 1) - { - if (eval(_root, m_nonce) < b) - { - o_solution = m_nonce; - return true; - } - } - return false; -} - template -inline void update(_T& _sha, u256 const& _value) +static inline void update(_T& _sha, u256 const& _value) { int i = 0; for (u256 v = _value; v; ++i, v >>= 8) {} @@ -129,7 +49,7 @@ inline void update(_T& _sha, u256 const& _value) } template -inline void update(_T& _sha, h256 const& _value) +static inline void update(_T& _sha, h256 const& _value) { int i = 0; byte const* data = _value.data(); @@ -138,14 +58,14 @@ inline void update(_T& _sha, h256 const& _value) } template -inline h256 get(_T& _sha) +static inline h256 get(_T& _sha) { h256 ret; _sha.TruncatedFinal(&ret[0], 32); return ret; } -h256 Dagger::node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i) +h256 DaggerEvaluator::node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i) { if (_L == _i) return _root; @@ -166,9 +86,9 @@ h256 Dagger::node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fas return get(bsha); } -h256 Dagger::eval(h256 const& _root, u256 const& _nonce) +h256 DaggerEvaluator::eval(h256 const& _root, h256 const& _nonce) { - h256 extranonce = _nonce >> 26; // with xn = floor(n / 2^26) -> assuming this is with xn = floor(N / 2^26) + h256 extranonce = (u256)_nonce >> 26; // with xn = floor(n / 2^26) -> assuming this is with xn = floor(N / 2^26) CryptoPP::SHA3_256 bsha; for (uint_fast32_t k = 0; k < 4; ++k) { @@ -185,7 +105,6 @@ h256 Dagger::eval(h256 const& _root, u256 const& _nonce) return get(bsha); } -#endif } } #endif diff --git a/libethcore/ProofOfWork.h b/libethcore/ProofOfWork.h new file mode 100644 index 000000000..d942d06f7 --- /dev/null +++ b/libethcore/ProofOfWork.h @@ -0,0 +1,117 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file ProofOfWork.h + * @author Gav Wood + * @date 2014 + * + * ProofOfWork algorithm. Or not. + */ + +#pragma once + +#include +#include +#include +#include +#include "CommonEth.h" + +#define FAKE_DAGGER 1 + +namespace dev +{ +namespace eth +{ + +struct MineInfo +{ + void combine(MineInfo const& _m) { requirement = std::max(requirement, _m.requirement); best = std::min(best, _m.best); hashes += _m.hashes; completed = completed || _m.completed; } + double requirement = 0; + double best = 1e99; + unsigned hashes = 0; + bool completed = false; +}; + +template +class ProofOfWorkEngine: public Evaluator +{ +public: + static bool verify(h256 const& _root, h256 const& _nonce, u256 const& _difficulty) { return (bigint)(u256)Evaluator::eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; } + + inline MineInfo mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout = 100, bool _continue = true, bool _turbo = false); + +protected: + h256 m_last; +}; + +class SHA3Evaluator +{ +public: + static h256 eval(h256 const& _root, h256 const& _nonce) { h256 b[2] = { _root, _nonce }; return sha3(bytesConstRef((byte const*)&b[0], 64)); } +}; + +// TODO: class ARPoWEvaluator + +class DaggerEvaluator +{ +public: + static h256 eval(h256 const& _root, h256 const& _nonce); + +private: + static h256 node(h256 const& _root, h256 const& _xn, uint_fast32_t _L, uint_fast32_t _i); +}; + +using SHA3ProofOfWork = ProofOfWorkEngine; + +using ProofOfWork = SHA3ProofOfWork; + +template +MineInfo ProofOfWorkEngine::mine(h256& o_solution, h256 const& _root, u256 const& _difficulty, unsigned _msTimeout, bool _continue, bool _turbo) +{ + MineInfo ret; + static std::mt19937_64 s_eng((time(0) + (unsigned)m_last)); + u256 s = (m_last = h256::random(s_eng)); + + bigint d = (bigint(1) << 256) / _difficulty; + ret.requirement = log2((double)d); + + // 2^ 0 32 64 128 256 + // [--------*-------------------------] + // + // evaluate until we run out of time + auto startTime = std::chrono::steady_clock::now(); + if (!_turbo) + std::this_thread::sleep_for(std::chrono::milliseconds(_msTimeout * 90 / 100)); + for (; (std::chrono::steady_clock::now() - startTime) < std::chrono::milliseconds(_msTimeout) && _continue; s++, ret.hashes++) + { + o_solution = (h256)s; + auto e = (bigint)(u256)Evaluator::eval(_root, o_solution); + ret.best = std::min(ret.best, log2((double)e)); + if (e <= d) + { + ret.completed = true; + break; + } + } + + if (ret.completed) + assert(verify(_root, o_solution, _difficulty)); + + return ret; +} + +} +} diff --git a/libethcore/_libethcore.cpp b/libethcore/_libethcore.cpp index 477034b9e..93eaf0d16 100644 --- a/libethcore/_libethcore.cpp +++ b/libethcore/_libethcore.cpp @@ -2,6 +2,6 @@ #include "All.h" #include "BlockInfo.cpp" #include "CommonEth.cpp" -#include "Dagger.cpp" +#include "ProofOfWork.cpp" #include "Exceptions.cpp" #endif diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 8e0619a43..d8c588118 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include "State.h" #include "Defaults.h" diff --git a/libethereum/Client.h b/libethereum/Client.h index e7bbc1ca3..8ec65c199 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -31,7 +31,6 @@ #include #include #include -#include #include #include "BlockChain.h" #include "TransactionQueue.h" diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 3fed4ae23..8f2794c12 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include "BlockChain.h" #include "Defaults.h" @@ -837,7 +836,7 @@ MineInfo State::mine(unsigned _msTimeout, bool _turbo) m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock); // TODO: Miner class that keeps dagger between mine calls (or just non-polling mining). - auto ret = m_dagger.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHashWithoutNonce(), m_currentBlock.difficulty, _msTimeout, true, _turbo); + auto ret = m_pow.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHashWithoutNonce(), m_currentBlock.difficulty, _msTimeout, true, _turbo); if (!ret.completed) m_currentBytes.clear(); diff --git a/libethereum/State.h b/libethereum/State.h index c73fc95dc..6914b9f5e 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include "TransactionQueue.h" @@ -322,7 +322,7 @@ private: Address m_ourAddress; ///< Our address (i.e. the address to which fees go). - Dagger m_dagger; + ProofOfWork m_pow; u256 m_blockReward; diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index 2c0a6d87b..733c5259c 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -234,6 +234,8 @@ bool Session::interpret(RLP const& _r) } m_node = m_server->noteNode(id, bi::tcp::endpoint(m_socket.remote_endpoint().address(), listenPort), Origin::Self, false, !m_node || m_node->id == id ? NodeId() : m_node->id); + if (m_node->isOffline()) + m_node->lastConnected = chrono::system_clock::now(); m_knownNodes.extendAll(m_node->index); m_knownNodes.unionWith(m_node->index); diff --git a/libqethereum/QEthereum.cpp b/libqethereum/QEthereum.cpp index 89cd71792..e85d381f8 100644 --- a/libqethereum/QEthereum.cpp +++ b/libqethereum/QEthereum.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/libqethereum/QmlEthereum.cpp b/libqethereum/QmlEthereum.cpp index 89578a29a..a7ed3df4d 100644 --- a/libqethereum/QmlEthereum.cpp +++ b/libqethereum/QmlEthereum.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/test/dagger.cpp b/test/dagger.cpp index 555c5d776..9422b6a96 100644 --- a/test/dagger.cpp +++ b/test/dagger.cpp @@ -17,12 +17,12 @@ /** @file dagger.cpp * @author Gav Wood * @date 2014 - * Dagger test functions. + * ProofOfWork test functions. */ #include #include -#include +#include using namespace std; using namespace std::chrono; using namespace dev; @@ -30,20 +30,20 @@ using namespace dev::eth; int daggerTest() { - cnote << "Testing Dagger..."; + cnote << "Testing ProofOfWork..."; // Test dagger { auto s = steady_clock::now(); - cout << hex << Dagger().eval((h256)(u256)1, (h256)(u256)0); + cout << hex << ProofOfWork().eval((h256)(u256)1, (h256)(u256)0); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; - cout << hex << Dagger().eval((h256)(u256)1, (h256)(u256)1); + cout << hex << ProofOfWork().eval((h256)(u256)1, (h256)(u256)1); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; } { auto s = steady_clock::now(); - cout << hex << Dagger().eval((h256)(u256)1, (h256)(u256)0); + cout << hex << ProofOfWork().eval((h256)(u256)1, (h256)(u256)0); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; - cout << hex << Dagger().eval((h256)(u256)1, (h256)(u256)1); + cout << hex << ProofOfWork().eval((h256)(u256)1, (h256)(u256)1); cout << " " << dec << duration_cast(steady_clock::now() - s).count() << " ms" << endl; } return 0; diff --git a/third/MainWin.cpp b/third/MainWin.cpp index 399fc8396..d21077f86 100644 --- a/third/MainWin.cpp +++ b/third/MainWin.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include