diff --git a/libdevcore/Common.h b/libdevcore/Common.h index ce94b48a3..f78ce690e 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -149,93 +149,8 @@ using u256HashMap = std::unordered_map; // String types. using strings = std::vector; -// Fixed-length string types. -using string32 = std::array; -static const string32 ZeroString32 = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}; - // Null/Invalid values for convenience. static const u256 Invalid256 = ~(u256)0; -static const bytes NullBytes; -static const std::map EmptyMapU256U256; - -/// Interprets @a _u as a two's complement signed number and returns the resulting s256. -inline s256 u2s(u256 _u) -{ - static const bigint c_end = bigint(1) << 256; - if (boost::multiprecision::bit_test(_u, 255)) - return s256(-(c_end - _u)); - else - return s256(_u); -} - -/// @returns the two's complement signed representation of the signed number _u. -inline u256 s2u(s256 _u) -{ - static const bigint c_end = bigint(1) << 256; - if (_u >= 0) - return u256(_u); - else - return u256(c_end + _u); -} - -/// @returns the smallest n >= 0 such that (1 << n) >= _x -inline unsigned int toLog2(u256 _x) -{ - unsigned ret; - for (ret = 0; _x >>= 1; ++ret) {} - return ret; -} - -/// @returns the absolute distance between _a and _b. -template -inline N diff(N const& _a, N const& _b) -{ - return std::max(_a, _b) - std::min(_a, _b); -} - -/// RAII utility class whose destructor calls a given function. -class ScopeGuard -{ -public: - ScopeGuard(std::function _f): m_f(_f) {} - ~ScopeGuard() { m_f(); } - -private: - std::function m_f; -}; - -/// Inheritable for classes that have invariants. -class HasInvariants -{ -public: - /// Reimplement to specify the invariants. - virtual bool invariants() const = 0; -}; - -/// RAII checker for invariant assertions. -class InvariantChecker -{ -public: - InvariantChecker(HasInvariants* _this, char const* _fn, char const* _file, int _line): m_this(_this), m_function(_fn), m_file(_file), m_line(_line) { checkInvariants(_this, _fn , _file, _line, true); } - ~InvariantChecker() { checkInvariants(m_this, m_function, m_file, m_line, false); } - /// Check invariants are met, throw if not. - static void checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int line, bool _pre); - -private: - HasInvariants const* m_this; - char const* m_function; - char const* m_file; - int m_line; -}; - -/// Scope guard for invariant check in a class derived from HasInvariants. -#if ETH_DEBUG -#define DEV_INVARIANT_CHECK ::dev::InvariantChecker __dev_invariantCheck(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__) -#define DEV_INVARIANT_CHECK_HERE ::dev::InvariantChecker::checkInvariants(this, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, true) -#else -#define DEV_INVARIANT_CHECK (void)0; -#define DEV_INVARIANT_CHECK_HERE (void)0; -#endif /// Converts arbitrary value to string representation using std::stringstream. template diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index a0ef9393a..eb4ac5e00 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -98,11 +98,3 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw) } return ret; } - -std::string dev::toString(string32 const& _s) -{ - std::string ret; - for (unsigned i = 0; i < 32 && _s[i]; ++i) - ret.push_back(_s[i]); - return ret; -} diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 00e534e8b..831536a80 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -114,8 +114,6 @@ inline T fromBigEndian(_In const& _bytes) } /// Convenience functions for toBigEndian -inline std::string toBigEndianString(u256 _val) { std::string ret(32, '\0'); toBigEndian(_val, ret); return ret; } -inline std::string toBigEndianString(u160 _val) { std::string ret(20, '\0'); toBigEndian(_val, ret); return ret; } inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; } inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); return ret; } @@ -131,23 +129,6 @@ inline bytes toCompactBigEndian(T _val, unsigned _min = 0) toBigEndian(_val, ret); return ret; } -inline bytes toCompactBigEndian(byte _val, unsigned _min = 0) -{ - return (_min || _val) ? bytes{ _val } : bytes{}; -} - -/// Convenience function for toBigEndian. -/// @returns a string just big enough to represent @a _val. -template -inline std::string toCompactBigEndianString(T _val, unsigned _min = 0) -{ - static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift - int i = 0; - for (T v = _val; v; ++i, v >>= 8) {} - std::string ret(std::max(_min, i), '\0'); - toBigEndian(_val, ret); - return ret; -} /// Convenience function for conversion of a u256 to hex inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd) @@ -174,175 +155,4 @@ inline unsigned bytesRequired(T _i) return i; } -/// Trims a given number of elements from the front of a collection. -/// Only works for POD element types. -template -void trimFront(T& _t, unsigned _elements) -{ - static_assert(std::is_pod::value, ""); - memmove(_t.data(), _t.data() + _elements, (_t.size() - _elements) * sizeof(_t[0])); - _t.resize(_t.size() - _elements); -} - -/// Pushes an element on to the front of a collection. -/// Only works for POD element types. -template -void pushFront(T& _t, _U _e) -{ - static_assert(std::is_pod::value, ""); - _t.push_back(_e); - memmove(_t.data() + 1, _t.data(), (_t.size() - 1) * sizeof(_e)); - _t[0] = _e; -} - -/// Concatenate two vectors of elements of POD types. -template -inline std::vector& operator+=(std::vector::value, T>::type>& _a, std::vector const& _b) -{ - auto s = _a.size(); - _a.resize(_a.size() + _b.size()); - memcpy(_a.data() + s, _b.data(), _b.size() * sizeof(T)); - return _a; - -} - -/// Concatenate two vectors of elements. -template -inline std::vector& operator+=(std::vector::value, T>::type>& _a, std::vector const& _b) -{ - _a.reserve(_a.size() + _b.size()); - for (auto& i: _b) - _a.push_back(i); - return _a; -} - -/// Insert the contents of a container into a set -template std::set& operator+=(std::set& _a, U const& _b) -{ - for (auto const& i: _b) - _a.insert(i); - return _a; -} - -/// Insert the contents of a container into an unordered_set -template std::unordered_set& operator+=(std::unordered_set& _a, U const& _b) -{ - for (auto const& i: _b) - _a.insert(i); - return _a; -} - -/// Concatenate the contents of a container onto a vector -template std::vector& operator+=(std::vector& _a, U const& _b) -{ - for (auto const& i: _b) - _a.push_back(i); - return _a; -} - -/// Insert the contents of a container into a set -template std::set operator+(std::set _a, U const& _b) -{ - return _a += _b; -} - -/// Insert the contents of a container into an unordered_set -template std::unordered_set operator+(std::unordered_set _a, U const& _b) -{ - return _a += _b; -} - -/// Concatenate the contents of a container onto a vector -template std::vector operator+(std::vector _a, U const& _b) -{ - return _a += _b; -} - -/// Concatenate two vectors of elements. -template -inline std::vector operator+(std::vector const& _a, std::vector const& _b) -{ - std::vector ret(_a); - return ret += _b; -} - -/// Merge two sets of elements. -template -inline std::set& operator+=(std::set& _a, std::set const& _b) -{ - for (auto& i: _b) - _a.insert(i); - return _a; -} - -/// Merge two sets of elements. -template -inline std::set operator+(std::set const& _a, std::set const& _b) -{ - std::set ret(_a); - return ret += _b; -} - -template -std::unordered_map& operator+=(std::unordered_map& _x, std::unordered_map const& _y) -{ - for (auto const& i: _y) - _x.insert(i); - return _x; -} - -template -std::unordered_map operator+(std::unordered_map const& _x, std::unordered_map const& _y) -{ - std::unordered_map ret(_x); - return ret += _y; -} - -/// Make normal string from fixed-length string. -std::string toString(string32 const& _s); - -template -std::vector keysOf(std::map const& _m) -{ - std::vector ret; - for (auto const& i: _m) - ret.push_back(i.first); - return ret; -} - -template -std::vector keysOf(std::unordered_map const& _m) -{ - std::vector ret; - for (auto const& i: _m) - ret.push_back(i.first); - return ret; -} - -template -std::vector valuesOf(std::map const& _m) -{ - std::vector ret; - ret.reserve(_m.size()); - for (auto const& i: _m) - ret.push_back(i.second); - return ret; -} - -template -std::vector valuesOf(std::unordered_map const& _m) -{ - std::vector ret; - ret.reserve(_m.size()); - for (auto const& i: _m) - ret.push_back(i.second); - return ret; -} - -template -bool contains(T const& _t, V const& _v) -{ - return std::end(_t) != std::find(std::begin(_t), std::end(_t), _v); -} - } diff --git a/libdevcore/Guards.h b/libdevcore/Guards.h index e7d8c3508..817bfb30e 100644 --- a/libdevcore/Guards.h +++ b/libdevcore/Guards.h @@ -145,15 +145,5 @@ private: for (GenericGuardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) #define DEV_READ_GUARDED(MUTEX) \ for (GenericGuardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) -#define DEV_WRITE_GUARDED(MUTEX) \ - for (GenericGuardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) -#define DEV_RECURSIVE_GUARDED(MUTEX) \ - for (GenericGuardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) -#define DEV_UNGUARDED(MUTEX) \ - for (GenericUnguardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) -#define DEV_READ_UNGUARDED(MUTEX) \ - for (GenericUnguardSharedBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) -#define DEV_WRITE_UNGUARDED(MUTEX) \ - for (GenericUnguardBool __eth_l(MUTEX); __eth_l.b; __eth_l.b = false) } diff --git a/libdevcore/Log.cpp b/libdevcore/Log.cpp index 6d44cdfd6..615556e80 100644 --- a/libdevcore/Log.cpp +++ b/libdevcore/Log.cpp @@ -43,14 +43,6 @@ mutex x_logOverride; /// or equal to the currently output verbosity (g_logVerbosity). static map s_logOverride; -bool dev::isChannelVisible(std::type_info const* _ch, bool _default) -{ - Guard l(x_logOverride); - if (s_logOverride.count(_ch)) - return s_logOverride[_ch]; - return _default; -} - LogOverrideAux::LogOverrideAux(std::type_info const* _ch, bool _value): m_ch(_ch) { diff --git a/libdevcore/Log.h b/libdevcore/Log.h index 3a59499d8..2dc547dd7 100644 --- a/libdevcore/Log.h +++ b/libdevcore/Log.h @@ -70,27 +70,6 @@ private: int m_old; }; -template -class LogOverride: LogOverrideAux -{ -public: - LogOverride(bool _value): LogOverrideAux(&typeid(Channel), _value) {} -}; - -bool isChannelVisible(std::type_info const* _ch, bool _default); -template bool isChannelVisible() { return isChannelVisible(&typeid(Channel), Channel::verbosity <= g_logVerbosity); } - -/// Temporary changes system's verbosity for specific function. Restores the old verbosity when function returns. -/// Not thread-safe, use with caution! -struct VerbosityHolder -{ - VerbosityHolder(int _temporaryValue): oldLogVerbosity(g_logVerbosity) { g_logVerbosity = _temporaryValue; } - ~VerbosityHolder() { g_logVerbosity = oldLogVerbosity; } - int oldLogVerbosity; -}; - -#define ETH_THREAD_CONTEXT(name) for (std::pair __eth_thread_context(name, true); p.second; p.second = false) - class ThreadContext { public: diff --git a/libdevcore/SHA3.h b/libdevcore/SHA3.h index 71b02cb2c..00c61b721 100644 --- a/libdevcore/SHA3.h +++ b/libdevcore/SHA3.h @@ -38,33 +38,12 @@ bool sha3(bytesConstRef _input, bytesRef o_output); /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. inline h256 sha3(bytesConstRef _input) { h256 ret; sha3(_input, ret.ref()); return ret; } -inline SecureFixedHash<32> sha3Secure(bytesConstRef _input) { SecureFixedHash<32> ret; sha3(_input, ret.writable().ref()); return ret; } /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. inline h256 sha3(bytes const& _input) { return sha3(bytesConstRef(&_input)); } -inline SecureFixedHash<32> sha3Secure(bytes const& _input) { return sha3Secure(bytesConstRef(&_input)); } - -/// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a 256-bit hash. -inline h256 sha3(std::string const& _input) { return sha3(bytesConstRef(_input)); } -inline SecureFixedHash<32> sha3Secure(std::string const& _input) { return sha3Secure(bytesConstRef(_input)); } /// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash. template inline h256 sha3(FixedHash const& _input) { return sha3(_input.ref()); } -template inline SecureFixedHash<32> sha3Secure(FixedHash const& _input) { return sha3Secure(_input.ref()); } - -/// Fully secure variants are equivalent for sha3 and sha3Secure. -inline SecureFixedHash<32> sha3(bytesSec const& _input) { return sha3Secure(_input.ref()); } -inline SecureFixedHash<32> sha3Secure(bytesSec const& _input) { return sha3Secure(_input.ref()); } -template inline SecureFixedHash<32> sha3(SecureFixedHash const& _input) { return sha3Secure(_input.ref()); } -template inline SecureFixedHash<32> sha3Secure(SecureFixedHash const& _input) { return sha3Secure(_input.ref()); } - -/// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the hash as a string filled with binary data. -inline std::string sha3(std::string const& _input, bool _isNibbles) { return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes()); } - -/// Calculate SHA3-256 MAC -inline void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output) { sha3(_secret.toBytes() + _plain.toBytes()).ref().populate(_output); } - -extern h256 EmptySHA3; extern h256 EmptyListSHA3;