diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 000000000..cdf6fc11c --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,8 @@ +--- +Checks: 'clang-diagnostic-*,clang-analyzer-*,modernize-*,bugprone-*,readability-*,-readability-implicit-bool-conversion,performance-*' +WarningsAsErrors: '' +HeaderFilterRegex: 'ethminer/.*' +CheckOptions: + - key: readability-braces-around-statements.ShortStatementLines + value: '3' +... diff --git a/.travis.yml b/.travis.yml index 42d494d1f..120f7f874 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ before_install: - | if [ "$TRAVIS_OS_NAME" = linux ]; then echo "Checking format of sourcecode..." - find . -type f -name '*.cpp' -o -name '*.h' -print0 | xargs -r0 clang-format -i + find . -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.cu' -o -name '*.cuh' \) -print0 | xargs -r0 clang-format -i git diff --color # --exit-code fi - | diff --git a/ethminer/main.cpp b/ethminer/main.cpp index b846d2ec4..775477284 100644 --- a/ethminer/main.cpp +++ b/ethminer/main.cpp @@ -130,7 +130,7 @@ class MinerCLI } #if ETH_DBUS - dbusint.send(toString(mp).c_str()); + dbusint.send(toString(Farm::f().miningProgress()).c_str()); #endif } else @@ -928,7 +928,7 @@ class MinerCLI "should " << endl << " listen on." << endl - << " --api-port INT [1 .. 65535] Default not set" << endl + << " --http-port INT [1 .. 65535] Default not set" << endl << " Set the http port, the miner should listen on all " "bound" << endl @@ -1077,7 +1077,7 @@ class MinerCLI "exits" << endl << " Must be combined with -G or -U or -X flags" << endl - << " -L,--dag-load-mode INT[0 .. 2] Default = 0" << endl + << " -L,--dag-load-mode INT[0 .. 1] Default = 0" << endl << " Set DAG load mode. Can be one of:" << endl << " 0 Parallel load mode (each GPU independently)" << endl << " 1 Sequential load mode (one GPU after another)" << endl diff --git a/libapicore/ApiServer.h b/libapicore/ApiServer.h index 4aced22a2..ee38fbf1c 100644 --- a/libapicore/ApiServer.h +++ b/libapicore/ApiServer.h @@ -30,7 +30,7 @@ class ApiConnection m_is_authenticated = false; } - ~ApiConnection() {} + ~ApiConnection() = default; void start(); diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index ca97cc52d..56f72efaf 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -123,7 +123,7 @@ inline T fromBigEndian(_In const& _bytes) inline bytes toBigEndian(u256 _val) { bytes ret(32); - toBigEndian(_val, ret); + toBigEndian(std::move(_val), ret); return ret; } inline bytes toBigEndian(u160 _val) diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index 5525bd655..12681c8a6 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -127,7 +127,10 @@ class FixedHash } /// Explicitly construct, copying from a bytes in memory with given pointer. - explicit FixedHash(byte const* _bs, ConstructFromPointerType) { memcpy(m_data.data(), _bs, N); } + explicit FixedHash(byte const* _bs, ConstructFromPointerType /*unused*/) + { + memcpy(m_data.data(), _bs, N); + } /// Explicitly construct, copying from a string. explicit FixedHash(std::string const& _s) @@ -149,10 +152,12 @@ class FixedHash bool operator<(FixedHash const& _c) const { for (unsigned i = 0; i < N; ++i) + { if (m_data[i] < _c.m_data[i]) return true; - else if (m_data[i] > _c.m_data[i]) + if (m_data[i] > _c.m_data[i]) return false; + } return false; } bool operator>=(FixedHash const& _c) const { return !operator<(_c); } diff --git a/libdevcore/Guards.h b/libdevcore/Guards.h index b8a587bfc..d40690144 100644 --- a/libdevcore/Guards.h +++ b/libdevcore/Guards.h @@ -42,7 +42,7 @@ template class Notified { public: - Notified() {} + Notified() = default; Notified(N const& _v) : m_value(_v) {} Notified(Notified const&) = delete; Notified& operator=(N const& _v) diff --git a/libdevcore/Log.cpp b/libdevcore/Log.cpp index 763b021d5..9e47d4b80 100644 --- a/libdevcore/Log.cpp +++ b/libdevcore/Log.cpp @@ -105,10 +105,11 @@ void dev::simpleDebugOut(std::string const& _s) { try { + std::ostream& os = g_logStdout ? std::cout : std::clog; if (!g_logNoColor) { - (g_logStdout ? std::cout : std::cerr) << _s + '\n'; - (g_logStdout ? std::cout : std::cerr).flush(); + os << _s + '\n'; + os.flush(); return; } bool skip = false; @@ -123,8 +124,8 @@ void dev::simpleDebugOut(std::string const& _s) ss << it; } ss << '\n'; - (g_logStdout ? std::cout : std::cerr) << ss.str(); - (g_logStdout ? std::cout : std::cerr).flush(); + os << ss.str(); + os.flush(); } catch (...) { diff --git a/libdevcore/RLP.cpp b/libdevcore/RLP.cpp index d2c9f4224..49db952d5 100644 --- a/libdevcore/RLP.cpp +++ b/libdevcore/RLP.cpp @@ -155,9 +155,9 @@ size_t RLP::length() const byte const n = m_data[0]; if (n < c_rlpDataImmLenStart) return 1; - else if (n <= c_rlpDataIndLenZero) + if (n <= c_rlpDataIndLenZero) return n - c_rlpDataImmLenStart; - else if (n < c_rlpListStart) + if (n < c_rlpListStart) { if (m_data.size() <= size_t(n - c_rlpDataIndLenZero)) BOOST_THROW_EXCEPTION(BadRLP()); @@ -210,16 +210,16 @@ size_t RLP::items() const { bytesConstRef d = payload(); size_t i = 0; - for (; d.size(); ++i) + for (; !d.empty(); ++i) d = d.cropped(sizeAsEncoded(d)); return i; } return 0; } -RLPStream& RLPStream::appendRaw(bytesConstRef _s, size_t _itemCount) +RLPStream& RLPStream::appendRaw(bytesConstRef _rlp, size_t _itemCount) { - m_out.insert(m_out.end(), _s.begin(), _s.end()); + m_out.insert(m_out.end(), _rlp.begin(), _rlp.end()); noteAppended(_itemCount); return *this; } @@ -239,32 +239,27 @@ void RLPStream::noteAppended(size_t _itemCount) m_listStack.back().first -= _itemCount; if (m_listStack.back().first) break; - else + + auto p = m_listStack.back().second; + m_listStack.pop_back(); + size_t s = m_out.size() - p; // list size + auto brs = bytesRequired(s); + unsigned encodeSize = s < c_rlpListImmLenCount ? 1 : (1 + brs); + auto os = m_out.size(); + m_out.resize(os + encodeSize); + memmove(m_out.data() + p + encodeSize, m_out.data() + p, os - p); + if (s < c_rlpListImmLenCount) + m_out[p] = (byte)(c_rlpListStart + s); + else if (c_rlpListIndLenZero + brs <= 0xff) { - auto p = m_listStack.back().second; - m_listStack.pop_back(); - size_t s = m_out.size() - p; // list size - auto brs = bytesRequired(s); - unsigned encodeSize = s < c_rlpListImmLenCount ? 1 : (1 + brs); - // cdebug << "s: " << s << ", p: " << p << ", m_out.size(): " << m_out.size() - //<< - //", encodeSize: " << encodeSize << " (br: " << brs << ")"; - auto os = m_out.size(); - m_out.resize(os + encodeSize); - memmove(m_out.data() + p + encodeSize, m_out.data() + p, os - p); - if (s < c_rlpListImmLenCount) - m_out[p] = (byte)(c_rlpListStart + s); - else if (c_rlpListIndLenZero + brs <= 0xff) - { - m_out[p] = (byte)(c_rlpListIndLenZero + brs); - byte* b = &(m_out[p + brs]); - for (; s; s >>= 8) - *(b--) = (byte)s; - } - else - BOOST_THROW_EXCEPTION( - RLPException() << errinfo_comment("itemCount too large for RLP")); + m_out[p] = (byte)(c_rlpListIndLenZero + brs); + byte* b = &(m_out[p + brs]); + for (; s; s >>= 8) + *(b--) = (byte)s; } + else + BOOST_THROW_EXCEPTION(RLPException() << errinfo_comment("itemCount too large for RLP")); + _itemCount = 1; // for all following iterations, we've effectively appended a single item // only since we completed a list. } @@ -295,9 +290,10 @@ RLPStream& RLPStream::append(bytesConstRef _s, bool _compact) size_t s = _s.size(); byte const* d = _s.data(); if (_compact) + { for (size_t i = 0; i < _s.size() && !*d; ++i, --s, ++d) - { - } + ; + } if (s == 1 && *d < c_rlpDataImmLenStart) m_out.push_back(*d); diff --git a/libdevcore/RLP.h b/libdevcore/RLP.h index 775af3ddd..2d2b8950e 100644 --- a/libdevcore/RLP.h +++ b/libdevcore/RLP.h @@ -94,7 +94,7 @@ class RLP using Strictness = int; /// Construct a null node. - RLP() {} + RLP() = default; /// Construct a node of value given in the bytes. explicit RLP(bytesConstRef _d, Strictness _s = VeryStrict); @@ -118,7 +118,7 @@ class RLP explicit operator bool() const { return !isNull(); } /// No value. - bool isNull() const { return m_data.size() == 0; } + bool isNull() const { return m_data.empty(); } /// String value. bool isData() const { return !isNull() && m_data[0] < c_rlpListStart; } @@ -191,7 +191,7 @@ class RLP bool operator!=(iterator const& _cmp) const { return !operator==(_cmp); } private: - iterator() {} + iterator() = default; iterator(RLP const& _parent, bool _begin); size_t m_remaining = 0; @@ -199,10 +199,10 @@ class RLP }; /// @brief Iterator into beginning of sub-item list (valid only if we are a list). - iterator begin() const { return iterator(*this, true); } + iterator begin() const { return {*this, true}; } /// @brief Iterator into end of sub-item list (valid only if we are a list). - iterator end() const { return iterator(*this, false); } + iterator end() const { return {*this, false}; } /// Best-effort conversion operators. explicit operator std::string() const { return toString(); } @@ -404,18 +404,18 @@ class RLPStream { public: /// Initializes empty RLPStream. - RLPStream() {} + RLPStream() = default; /// Initializes the RLPStream as a list of @a _listItems items. explicit RLPStream(size_t _listItems) { appendList(_listItems); } - ~RLPStream() {} + ~RLPStream() = default; /// Append given datum to the byte stream. RLPStream& append(unsigned _s) { return append(bigint(_s)); } RLPStream& append(u160 _s) { return append(bigint(_s)); } RLPStream& append(u256 _s) { return append(bigint(_s)); } - RLPStream& append(bigint _s); + RLPStream& append(bigint _i); RLPStream& append(bytesConstRef _s, bool _compact = false); RLPStream& append(bytes const& _s) { return append(bytesConstRef(&_s)); } RLPStream& append(std::string const& _s) { return append(bytesConstRef(_s)); } @@ -483,7 +483,7 @@ class RLPStream /// Push the node-type byte (using @a _base) along with the item count @a _count. /// @arg _count is number of characters for strings, data-bytes for ints, or items for lists. - void pushCount(size_t _count, byte _offset); + void pushCount(size_t _count, byte _base); /// Push an integer as a raw big-endian byte-stream. template diff --git a/libdevcore/Worker.h b/libdevcore/Worker.h index e87b5770f..4f2a472ae 100644 --- a/libdevcore/Worker.h +++ b/libdevcore/Worker.h @@ -45,7 +45,7 @@ enum class WorkerState class Worker { public: - Worker(std::string const& _name) : m_name(_name) {} + Worker(std::string _name) : m_name(std::move(_name)) {} Worker(Worker const&) = delete; Worker& operator=(Worker const&) = delete; diff --git a/libdevcore/vector_ref.h b/libdevcore/vector_ref.h index 2bacb92b1..00a24e58d 100644 --- a/libdevcore/vector_ref.h +++ b/libdevcore/vector_ref.h @@ -54,8 +54,7 @@ class vector_ref { if (!m_data || m_count == 0) return _c.empty(); - else - return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count * sizeof(_T)); + return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count * sizeof(_T)); } std::vector toVector() const { @@ -90,8 +89,7 @@ class vector_ref { if (!m_data) return *this; - else - return vector_ref<_T>(m_data + m_count, m_count); + return vector_ref<_T>(m_data + m_count, m_count); } /// @returns a new vector_ref which is a shifted and shortened view of the original data. /// If this goes out of bounds in any way, returns an empty vector_ref. @@ -101,8 +99,7 @@ class vector_ref if (m_data && _begin <= m_count && _count <= m_count && _begin + _count <= m_count) return vector_ref<_T>( m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); - else - return vector_ref<_T>(); + return {}; } /// @returns a new vector_ref which is a shifted view of the original data (not going beyond /// it). @@ -110,8 +107,7 @@ class vector_ref { if (m_data && _begin <= m_count) return vector_ref<_T>(m_data + _begin, m_count - _begin); - else - return vector_ref<_T>(); + return {}; } void retarget(_T* _d, size_t _s) { @@ -153,7 +149,7 @@ class vector_ref void cleanse() { static unsigned char s_cleanseCounter = 0; - uint8_t* p = (uint8_t*)begin(); + auto* p = (uint8_t*)begin(); size_t const len = (uint8_t*)end() - p; size_t loop = len; size_t count = s_cleanseCounter; @@ -193,10 +189,6 @@ class vector_ref } bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); } -#if DEV_LDB - operator ldb::Slice() const { return ldb::Slice((char const*)m_data, m_count * sizeof(_T)); } -#endif - void reset() { m_data = nullptr; diff --git a/libethash-cl/CLMiner.cpp b/libethash-cl/CLMiner.cpp index 2a852c69b..93408e2ff 100644 --- a/libethash-cl/CLMiner.cpp +++ b/libethash-cl/CLMiner.cpp @@ -876,8 +876,7 @@ bool CLMiner::initEpoch_internal() // create mining buffers ETHCL_LOG("Creating mining buffer"); m_searchBuffer.clear(); - m_searchBuffer.push_back( - cl::Buffer(m_context[0], CL_MEM_WRITE_ONLY, sizeof(SearchResults))); + m_searchBuffer.emplace_back(m_context[0], CL_MEM_WRITE_ONLY, sizeof(SearchResults)); m_dagKernel.setArg(1, m_light[0]); m_dagKernel.setArg(2, m_dag[0]); diff --git a/libethcore/Miner.h b/libethcore/Miner.h index 602509474..cf98f1a19 100644 --- a/libethcore/Miner.h +++ b/libethcore/Miner.h @@ -303,7 +303,7 @@ class Miner : public Worker : Worker(_name + std::to_string(_index)), m_index(_index) {} - virtual ~Miner() = default; + ~Miner() override = default; /** * @brief Assigns the device descriptor to this instance diff --git a/libpoolprotocols/PoolURI.cpp b/libpoolprotocols/PoolURI.cpp index d2468416d..59967e47c 100644 --- a/libpoolprotocols/PoolURI.cpp +++ b/libpoolprotocols/PoolURI.cpp @@ -20,18 +20,18 @@ #include #include -#include +#include #include using namespace dev; -typedef struct +struct SchemeAttributes { ProtocolFamily family; SecureLevel secure; unsigned version; -} SchemeAttributes; +}; static std::map s_schemes = { /* @@ -95,10 +95,8 @@ static std::string urlDecode(std::string s) return ret; } -URI::URI(const std::string uri) +URI::URI(std::string uri) : m_uri{std::move(uri)} { - m_uri = uri; - const char* curstr = m_uri.c_str(); // := [a-z\0-9\+\-\.]+, convert to lower case @@ -145,7 +143,7 @@ URI::URI(const std::string uri) userpass_flag = true; break; } - else if ('/' == *tmpstr) + if ('/' == *tmpstr) { // End of : specification break; @@ -167,7 +165,7 @@ URI::URI(const std::string uri) // which should mean: username = "username.246891" // workername = "rigname.01" // we must split username and workername before urlDecode() is called ! - auto p = m_username.find_first_of("."); + auto p = m_username.find_first_of('.'); if (p != std::string::npos) { // There should be at least one char after dot @@ -214,7 +212,7 @@ URI::URI(const std::string uri) tmpstr++; break; } - else if (!ipv6_flag && ((':' == *tmpstr) || ('/' == *tmpstr))) + if (!ipv6_flag && ((':' == *tmpstr) || ('/' == *tmpstr))) // Port number is specified. break; tmpstr++; @@ -287,7 +285,7 @@ URI::URI(const std::string uri) while (('\0' != *tmpstr) && ('#' != *tmpstr) && ('?' != *tmpstr)) tmpstr++; len = tmpstr - curstr; - if (len) + if (len > 0) { m_path.append(curstr, len); m_path = urlDecode(m_path); @@ -359,7 +357,9 @@ std::string URI::KnownSchemes(ProtocolFamily family) { std::string schemes; for (const auto& s : s_schemes) + { if ((s.second.family == family) && (s.second.version != 999)) schemes += s.first + " "; + } return schemes; } diff --git a/libpoolprotocols/PoolURI.h b/libpoolprotocols/PoolURI.h index 5538e5420..dc59dbaf4 100644 --- a/libpoolprotocols/PoolURI.h +++ b/libpoolprotocols/PoolURI.h @@ -52,7 +52,7 @@ class URI { public: URI() = delete; - URI(const std::string uri); + URI(std::string uri); std::string Scheme() const { return m_scheme; } std::string Host() const { return m_host; } diff --git a/libpoolprotocols/stratum/EthStratumClient.cpp b/libpoolprotocols/stratum/EthStratumClient.cpp index d76e3adfd..a867fd296 100644 --- a/libpoolprotocols/stratum/EthStratumClient.cpp +++ b/libpoolprotocols/stratum/EthStratumClient.cpp @@ -184,7 +184,7 @@ void EthStratumClient::init_socket() m_socket->native_handle(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout)); #else timeval tv{ - static_cast(keepAlive / 1000), + static_cast(keepAlive / 1000), static_cast(keepAlive % 1000)}; setsockopt(m_socket->native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(m_socket->native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); @@ -1226,6 +1226,7 @@ void EthStratumClient::processResponse(Json::Value& responseObject) m_current.startNonce = m_extraNonce; m_current.exSizeBytes = m_extraNonceSizeBytes; m_current_timestamp = std::chrono::steady_clock::now(); + m_current.block = -1; // This will signal to dispatch the job // at the end of the transmission. @@ -1241,6 +1242,7 @@ void EthStratumClient::processResponse(Json::Value& responseObject) // Only some eth-proxy compatible implementations carry the block number // namely ethermine.org + m_current.block = -1; if (m_conn->StratumMode() == EthStratumClient::ETHPROXY && jPrm.size() > prmIdx) { @@ -1249,10 +1251,17 @@ void EthStratumClient::processResponse(Json::Value& responseObject) m_current.block = std::stoul( jPrm.get(Json::Value::ArrayIndex(prmIdx), "").asString(), nullptr, 16); + /* check if the block number is in a valid range + A year has ~31536000 seconds + 50 years have ~1576800000 + assuming a (very fast) blocktime of 10s: + ==> in 50 years we get 157680000 (=0x9660180) blocks + */ + if (m_current.block > 0x9660180) + m_current.block = -1; } catch (const std::exception&) { - m_current.block = -1; } } diff --git a/libpoolprotocols/stratum/EthStratumClient.h b/libpoolprotocols/stratum/EthStratumClient.h index 2678b5cb1..af6650d84 100644 --- a/libpoolprotocols/stratum/EthStratumClient.h +++ b/libpoolprotocols/stratum/EthStratumClient.h @@ -52,7 +52,12 @@ class verbose_verification class EthStratumClient : public PoolClient { public: - typedef enum { STRATUM = 0, ETHPROXY, ETHEREUMSTRATUM } StratumProtocol; + enum StratumProtocol + { + STRATUM = 0, + ETHPROXY, + ETHEREUMSTRATUM + }; EthStratumClient(int worktimeout, int responsetimeout); diff --git a/libpoolprotocols/testing/SimulateClient.cpp b/libpoolprotocols/testing/SimulateClient.cpp index 4d0e053b7..8e30d8857 100644 --- a/libpoolprotocols/testing/SimulateClient.cpp +++ b/libpoolprotocols/testing/SimulateClient.cpp @@ -14,7 +14,7 @@ SimulateClient::SimulateClient(unsigned const& difficulty, unsigned const& block m_block = block; } -SimulateClient::~SimulateClient() {} +SimulateClient::~SimulateClient() = default; void SimulateClient::connect() { diff --git a/libpoolprotocols/testing/SimulateClient.h b/libpoolprotocols/testing/SimulateClient.h index 7d0b89aeb..650b3ff2a 100644 --- a/libpoolprotocols/testing/SimulateClient.h +++ b/libpoolprotocols/testing/SimulateClient.h @@ -17,7 +17,7 @@ class SimulateClient : public PoolClient, Worker { public: SimulateClient(unsigned const& difficulty, unsigned const& block); - ~SimulateClient(); + ~SimulateClient() override; void connect() override; void disconnect() override;