From 736557ca4c614d215365e02b8eafdf60418b1444 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 4 Mar 2014 12:32:20 -0600 Subject: [PATCH] Various headers from the repotting. Log has documentation. --- libethereum/CommonData.h | 186 +++++++++++++++++++++++++++++++ libethereum/CommonEth.h | 137 +++++++++++++++++++++++ libethereum/CommonIO.h | 223 ++++++++++++++++++++++++++++++++++++++ libethereum/FixedHash.h | 195 +++++++++++++++++++++++++++++++++ libethereum/Log.h | 134 +++++++++++++++++++++++ libethereum/PeerServer.h | 133 +++++++++++++++++++++++ libethereum/PeerSession.h | 90 +++++++++++++++ 7 files changed, 1098 insertions(+) create mode 100644 libethereum/CommonData.h create mode 100644 libethereum/CommonEth.h create mode 100644 libethereum/CommonIO.h create mode 100644 libethereum/FixedHash.h create mode 100644 libethereum/Log.h create mode 100644 libethereum/PeerServer.h create mode 100644 libethereum/PeerSession.h diff --git a/libethereum/CommonData.h b/libethereum/CommonData.h new file mode 100644 index 000000000..ca1fad1a6 --- /dev/null +++ b/libethereum/CommonData.h @@ -0,0 +1,186 @@ +/* + 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 Common.h + * @author Gav Wood + * @date 2014 + * + * Shared algorithms and data types. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include "Common.h" + +namespace eth +{ + +// String conversion functions, mainly to/from hex/nibble/byte representations. + +/// Convert a series of bytes to the corresponding string of hex duplets. +/// @param _w specifies the width of each of the elements. Defaults to two - enough to represent a byte. +/// @example toHex("A\x69") == "4169" +template +std::string toHex(_T const& _data, int _w = 2) +{ + std::ostringstream ret; + for (auto i: _data) + ret << std::hex << std::setfill('0') << std::setw(_w) << (int)(typename std::make_unsigned::type)i; + return ret.str(); +} + +/// Converts a (printable) ASCII hex character into the correspnding integer value. +/// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5 +int fromHex(char _i); + +/// Converts a (printable) ASCII hex string into the corresponding byte stream. +/// @example fromHex("41626261") == asBytes("Abba") +bytes fromHex(std::string const& _s); + +/// Converts byte array to a string containing the same (binary) data. Unless +/// the byte array happens to contain ASCII data, this won't be printable. +inline std::string asString(bytes const& _b) +{ + return std::string((char const*)_b.data(), (char const*)(_b.data() + _b.size())); +} + +/// Converts a string to a byte array containing the string's (byte) data. +inline bytes asBytes(std::string const& _b) +{ + return bytes((byte const*)_b.data(), (byte const*)(_b.data() + _b.size())); +} + +/// Converts a string into the big-endian base-16 stream of integers (NOT ASCII). +/// @example asNibbles("A")[0] == 4 && asNibbles("A")[1] == 1 +bytes asNibbles(std::string const& _s); + + +// Big-endian to/from host endian conversion functions. + +/// Converts a templated integer value to the big-endian byte-stream represented on a templated collection. +/// The size of the collection object will be unchanged. If it is too small, it will not represent the +/// value properly, if too big then the additional elements will be zeroed out. +/// @a _Out will typically be either std::string or bytes. +/// @a _T will typically by uint, u160, u256 or bigint. +template +inline void toBigEndian(_T _val, _Out& o_out) +{ + for (auto i = o_out.size(); i-- != 0; _val >>= 8) + o_out[i] = (typename _Out::value_type)(uint8_t)_val; +} + +/// Converts a big-endian byte-stream represented on a templated collection to a templated integer value. +/// @a _In will typically be either std::string or bytes. +/// @a _T will typically by uint, u160, u256 or bigint. +template +inline _T fromBigEndian(_In const& _bytes) +{ + _T ret = 0; + for (auto i: _bytes) + ret = (ret << 8) | (byte)(typename std::make_unsigned::type)i; + return ret; +} + +/// 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; } + +/// Convenience function for toBigEndian. +/// @returns a string just big enough to represent @a _val. +template +inline std::string toCompactBigEndianString(_T _val) +{ + int i = 0; + for (_T v = _val; v; ++i, v >>= 8) {} + std::string ret(i, '\0'); + toBigEndian(_val, ret); + return ret; +} + + +// Algorithms for string and string-like collections. + +/// Escapes a string into the C-string representation. +/// @p _all if true will escape all characters, not just the unprintable ones. +std::string escaped(std::string const& _s, bool _all = true); + +/// Determines the length of the common prefix of the two collections given. +/// @returns the number of elements both @a _t and @a _u share, in order, at the beginning. +/// @example commonPrefix("Hello world!", "Hello, world!") == 5 +template +uint commonPrefix(_T const& _t, _U const& _u) +{ + uint s = std::min(_t.size(), _u.size()); + for (uint i = 0;; ++i) + if (i == s || _t[i] != _u[i]) + return i; + return s; +} + +/// Creates a random, printable, word. +std::string randomWord(); + + +// General datatype convenience functions. + +/// Trims a given number of elements from the front of a collection. +/// Only works for POD element types. +template +void trimFront(_T& _t, uint _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. _T must be POD. +template +inline std::vector<_T>& operator+=(std::vector::value, _T>::type>& _a, std::vector<_T> 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. _T must be POD. +template +inline std::vector<_T> operator+(std::vector::value, _T>::type> const& _a, std::vector<_T> const& _b) +{ + std::vector<_T> ret(_a); + return ret += _b; +} + +} diff --git a/libethereum/CommonEth.h b/libethereum/CommonEth.h new file mode 100644 index 000000000..1605f00f7 --- /dev/null +++ b/libethereum/CommonEth.h @@ -0,0 +1,137 @@ +/* + 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 CommonEth.h + * @author Gav Wood + * @date 2014 + * + * Ethereum-specific data structures & algorithms. + */ + +#pragma once + +#include "Common.h" +#include "FixedHash.h" + +namespace eth +{ + +/// A secret key: 32 bytes. +/// @NOTE This is not endian-specific; it's just a bunch of bytes. +using Secret = h256; + +/// A public key: 64 bytes. +/// @NOTE This is not endian-specific; it's just a bunch of bytes. +using Public = h512; + +/// An Ethereum address: 20 bytes. +/// @NOTE This is not endian-specific; it's just a bunch of bytes. +using Address = h160; + +/// A vector of Ethereum addresses. +using Addresses = h160s; + +/// User-friendly string representation of the amount _b in wei. +std::string formatBalance(u256 _b); + +/// Get information concerning the currency denominations. +std::vector> const& units(); + +// The various denominations; here for ease of use where needed within code. +static const u256 Uether = ((((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000000; +static const u256 Vether = ((((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000; +static const u256 Dether = ((((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000; +static const u256 Nether = (((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000000; +static const u256 Yether = (((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000000; +static const u256 Zether = (((u256(1000000000) * 1000000000) * 1000000000) * 1000000000) * 1000; +static const u256 Eether = ((u256(1000000000) * 1000000000) * 1000000000) * 1000000000; +static const u256 Pether = ((u256(1000000000) * 1000000000) * 1000000000) * 1000000; +static const u256 Tether = ((u256(1000000000) * 1000000000) * 1000000000) * 1000; +static const u256 Gether = (u256(1000000000) * 1000000000) * 1000000000; +static const u256 Mether = (u256(1000000000) * 1000000000) * 1000000; +static const u256 Kether = (u256(1000000000) * 1000000000) * 1000; +static const u256 ether = u256(1000000000) * 1000000000; +static const u256 finney = u256(1000000000) * 1000000; +static const u256 szabo = u256(1000000000) * 1000; +static const u256 Gwei = u256(1000000000); +static const u256 Mwei = u256(1000000); +static const u256 Kwei = u256(1000); +static const u256 wei = u256(1); + +/// Convert a private key into the public key equivalent. +/// @returns 0 if it's not a valid private key. +Address toAddress(h256 _private); + +/// Simple class that represents a "key pair". +/// All of the data of the class can be regenerated from the secret key (m_secret) alone. +/// Actually stores a tuplet of secret, public and address (the right 160-bits of the public). +class KeyPair +{ +public: + /// Null constructor. + KeyPair() {} + + /// Normal constructor - populates object from the given secret key. + KeyPair(Secret _k); + + /// Create a new, randomly generated object. + static KeyPair create(); + + /// Retrieve the secret key. + Secret const& secret() const { return m_secret; } + /// Retrieve the secret key. + Secret const& sec() const { return m_secret; } + + /// Retrieve the public key. + Public const& pub() const { return m_public; } + + /// Retrieve the associated address of the public key. + Address const& address() const { return m_address; } + +private: + Secret m_secret; + Public m_public; + Address m_address; +}; + + +// SHA-3 convenience routines. + +/// Calculate SHA3-256 hash of the given input and load it into the given output. +void sha3(bytesConstRef _input, bytesRef _output); + +/// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the hash as a string filled with binary data. +std::string sha3(std::string const& _input, bool _isNibbles); + +/// Calculate SHA3-256 hash of the given input, returning as a byte array. +bytes sha3Bytes(bytesConstRef _input); + +/// Calculate SHA3-256 hash of the given input (presented as a binary string), returning as a byte array. +inline bytes sha3Bytes(std::string const& _input) { return sha3Bytes((std::string*)&_input); } + +/// Calculate SHA3-256 hash of the given input, returning as a byte array. +inline bytes sha3Bytes(bytes const& _input) { return sha3Bytes((bytes*)&_input); } + +/// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. +h256 sha3(bytesConstRef _input); + +/// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. +inline h256 sha3(bytes const& _input) { return sha3(bytesConstRef((bytes*)&_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)); } + +} diff --git a/libethereum/CommonIO.h b/libethereum/CommonIO.h new file mode 100644 index 000000000..a388fa8f9 --- /dev/null +++ b/libethereum/CommonIO.h @@ -0,0 +1,223 @@ +/* + 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 CommonIO.h + * @author Gav Wood + * @date 2014 + * + * File & stream I/O routines. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common.h" + +namespace eth +{ + +/// Retrieve and returns the contents of the given file. If the file doesn't exist or isn't readable, returns an empty bytes. +bytes contents(std::string const& _file); + +/// Write the given binary data into the given file, replacing the file if it pre-exists. +void writeFile(std::string const& _file, bytes const& _data); + +/// Converts arbitrary value to string representation using std::stringstream. +template +std::string toString(_T const& _t) +{ + std::ostringstream o; + o << _t; + return o.str(); +} + +// Stream I/O functions. +// Provides templated stream I/O for all STL collections so they can be shifted on to any iostream-like interface. + +template struct StreamOut { static S& bypass(S& _out, T const& _t) { _out << _t; return _out; } }; +template struct StreamOut { static S& bypass(S& _out, uint8_t const& _t) { _out << (int)_t; return _out; } }; + +template +inline S& streamout(S& _out, std::vector const& _e) +{ + _out << "["; + if (!_e.empty()) + { + StreamOut::bypass(_out, _e.front()); + for (auto i = ++_e.begin(); i != _e.end(); ++i) + StreamOut::bypass(_out << ",", *i); + } + _out << "]"; + return _out; +} + +template inline std::ostream& operator<<(std::ostream& _out, std::vector const& _e) { streamout(_out, _e); return _out; } + +template +inline S& streamout(S& _out, std::array const& _e) +{ + _out << "["; + if (!_e.empty()) + { + StreamOut::bypass(_out, _e.front()); + auto i = _e.begin(); + for (++i; i != _e.end(); ++i) + StreamOut::bypass(_out << ",", *i); + } + _out << "]"; + return _out; +} +template inline std::ostream& operator<<(std::ostream& _out, std::array const& _e) { streamout(_out, _e); return _out; } + +template +inline S& streamout(S& _out, std::array const& _e) +{ + _out << "["; + if (!_e.empty()) + { + StreamOut::bypass(_out, _e.front()); + auto i = _e.begin(); + for (++i; i != _e.end(); ++i) + StreamOut::bypass(_out << ",", *i); + } + _out << "]"; + return _out; +} +template inline std::ostream& operator<<(std::ostream& _out, std::array const& _e) { streamout(_out, _e); return _out; } + +template +inline S& streamout(S& _out, std::list const& _e) +{ + _out << "["; + if (!_e.empty()) + { + _out << _e.front(); + for (auto i = ++_e.begin(); i != _e.end(); ++i) + _out << "," << *i; + } + _out << "]"; + return _out; +} +template inline std::ostream& operator<<(std::ostream& _out, std::list const& _e) { streamout(_out, _e); return _out; } + +template +inline S& streamout(S& _out, std::pair const& _e) +{ + _out << "(" << _e.first << "," << _e.second << ")"; + return _out; +} +template inline std::ostream& operator<<(std::ostream& _out, std::pair const& _e) { streamout(_out, _e); return _out; } + +template +inline S& streamout(S& _out, std::tuple const& _t) +{ + _out << "(" << std::get<0>(_t) << "," << std::get<1>(_t) << "," << std::get<2>(_t) << ")"; + return _out; +} +template inline std::ostream& operator<<(std::ostream& _out, std::tuple const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::map const& _v) +{ + if (_v.empty()) + return _out << "{}"; + int i = 0; + for (auto p: _v) + _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::map const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::unordered_map const& _v) +{ + if (_v.empty()) + return _out << "{}"; + int i = 0; + for (auto p: _v) + _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::unordered_map const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::set const& _v) +{ + if (_v.empty()) + return _out << "{}"; + int i = 0; + for (auto p: _v) + _out << (!(i++) ? "{ " : ", ") << p; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::set const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::unordered_set const& _v) +{ + if (_v.empty()) + return _out << "{}"; + int i = 0; + for (auto p: _v) + _out << (!(i++) ? "{ " : ", ") << p; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::unordered_set const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::multiset const& _v) +{ + if (_v.empty()) + return _out << "{}"; + int i = 0; + for (auto p: _v) + _out << (!(i++) ? "{ " : ", ") << p; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::multiset const& _e) { streamout(_out, _e); return _out; } + +template +S& streamout(S& _out, std::multimap const& _v) +{ + if (_v.empty()) + return _out << "{}"; + T l; + int i = 0; + for (auto p: _v) + if (!(i++)) + _out << "{ " << (l = p.first) << " => " << p.second; + else if (l == p.first) + _out << ", " << p.second; + else + _out << "; " << (l = p.first) << " => " << p.second; + return _out << " }"; +} +template inline std::ostream& operator<<(std::ostream& _out, std::multimap const& _e) { streamout(_out, _e); return _out; } + +template _S& operator<<(_S& _out, std::shared_ptr<_T> const& _p) { if (_p) _out << "@" << (*_p); else _out << "nullptr"; return _out; } + +} diff --git a/libethereum/FixedHash.h b/libethereum/FixedHash.h new file mode 100644 index 000000000..6fcf0ea2b --- /dev/null +++ b/libethereum/FixedHash.h @@ -0,0 +1,195 @@ +/* + 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 FixedHash.h + * @author Gav Wood + * @date 2014 + * + * The FixedHash fixed-size "hash" container type. + */ + +#pragma once + +#include +#include +#include "CommonData.h" + +namespace eth +{ + +/// Fixed-size raw-byte array container type, with an API optimised for storing hashes. +/// Transparently converts to/from the corresponding arithmetic type; this will +/// assume the data contained in the hash is big-endian. +template +class FixedHash +{ + /// The corresponding arithmetic type. + using Arith = boost::multiprecision::number>; + +public: + /// The size of the container. + enum { size = N }; + + /// A dummy flag to avoid accidental construction from pointer. + enum ConstructFromPointerType { ConstructFromPointer }; + + /// Method to convert from a string. + enum ConstructFromStringType { FromHex, FromBinary }; + + /// Construct an empty hash. + FixedHash() { m_data.fill(0); } + + /// Convert from the corresponding arithmetic type. + FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); } + + /// Explicitly construct, copying from a byte array. + explicit FixedHash(bytes const& _b) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min(_b.size(), N)); } + + /// Explicitly construct, copying from a bytes in memory with given pointer. + explicit FixedHash(byte const* _bs, ConstructFromPointerType) { memcpy(m_data.data(), _bs, N); } + + /// Explicitly construct, copying from a string. + explicit FixedHash(std::string const& _s, ConstructFromStringType _t = FromHex): FixedHash(_t == FromHex ? fromHex(_s) : asBytes(_s)) {} + + /// Convert to arithmetic type. + operator Arith() const { return fromBigEndian(m_data); } + + /// @returns true iff this is the empty hash. + operator bool() const { return ((Arith)*this) != 0; } + + // The obvious comparison operators. + bool operator==(FixedHash const& _c) const { return m_data == _c.m_data; } + bool operator!=(FixedHash const& _c) const { return m_data != _c.m_data; } + bool operator<(FixedHash const& _c) const { return m_data < _c.m_data; } + + // The obvious binary operators. + FixedHash& operator^=(FixedHash const& _c) { for (auto i = 0; i < N; ++i) m_data[i] ^= _c.m_data[i]; return *this; } + FixedHash operator^(FixedHash const& _c) const { return FixedHash(*this) ^= _c; } + FixedHash& operator|=(FixedHash const& _c) { for (auto i = 0; i < N; ++i) m_data[i] |= _c.m_data[i]; return *this; } + FixedHash operator|(FixedHash const& _c) const { return FixedHash(*this) |= _c; } + FixedHash& operator&=(FixedHash const& _c) { for (auto i = 0; i < N; ++i) m_data[i] &= _c.m_data[i]; return *this; } + FixedHash operator&(FixedHash const& _c) const { return FixedHash(*this) &= _c; } + FixedHash& operator~() { for (auto i = 0; i < N; ++i) m_data[i] = ~m_data[i]; return *this; } + + /// @returns a particular byte from the hash. + byte& operator[](unsigned _i) { return m_data[_i]; } + /// @returns a particular byte from the hash. + byte operator[](unsigned _i) const { return m_data[_i]; } + + /// @returns an abridged version of the hash as a user-readable hex string. + std::string abridged() const { return toHex(ref().cropped(0, 4)) + ".."; } + + /// @returns a mutable byte vector_ref to the object's data. + bytesRef ref() { return bytesRef(m_data.data(), N); } + + /// @returns a constant byte vector_ref to the object's data. + bytesConstRef ref() const { return bytesConstRef(m_data.data(), N); } + + /// @returns a mutable byte pointer to the object's data. + byte* data() { return m_data.data(); } + + /// @returns a constant byte pointer to the object's data. + byte const* data() const { return m_data.data(); } + + /// @returns a copy of the object's data as a byte vector. + bytes asBytes() const { return bytes(data(), data() + N); } + + /// @returns a mutable reference to the object's data as an STL array. + std::array& asArray() { return m_data; } + + /// @returns a constant reference to the object's data as an STL array. + std::array const& asArray() const { return m_data; } + + /// A generic std::hash compatible function object. + struct hash + { + /// Make a hash of the object's data. + size_t operator()(FixedHash const& value) const + { + size_t h = 0; + for (auto i: value.m_data) + h = (h << 5 - h) + i; + return h; + } + }; + +private: + std::array m_data; ///< The binary data. +}; + + +/// Fast equality operator for h256. +template<> inline bool FixedHash<32>::operator==(FixedHash<32> const& _other) const +{ + const uint64_t* hash1 = (const uint64_t*)this->data(); + const uint64_t* hash2 = (const uint64_t*)_other.data(); + return (hash1[0] == hash2[0]) && (hash1[1] == hash2[1]) && (hash1[2] == hash2[2]) && (hash1[3] == hash2[3]); +} + +/// Fast std::hash compatible hash function object for h256. +template<> inline size_t FixedHash<32>::hash::operator()(FixedHash<32> const& value) const +{ + const uint64_t*data = (const uint64_t*)value.data(); + uint64_t hash = data[0]; + hash ^= data[1]; + hash ^= data[2]; + hash ^= data[3]; + return (size_t)hash; +} + +/// Stream I/O for the FixedHash class. +template +inline std::ostream& operator<<(std::ostream& _out, FixedHash const& _h) +{ + _out << std::noshowbase << std::hex << std::setfill('0'); + for (unsigned i = 0; i < N; ++i) + _out << std::setw(2) << (int)_h[i]; + _out << std::dec; + return _out; +} + +// Common types of FixedHash. +using h512 = FixedHash<64>; +using h256 = FixedHash<32>; +using h160 = FixedHash<20>; +using h256s = std::vector; +using h160s = std::vector; +using h256Set = std::set; +using h160Set = std::set; + +/// Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes. +inline h160 right160(h256 const& _t) +{ + h160 ret; + memcpy(ret.data(), _t.data() + 12, 20); + return ret; +} + +/// Convert the given value into h160 (160-bit unsigned integer) using the left 20 bytes. +inline h160 left160(h256 const& _t) +{ + h160 ret; + memcpy(&ret[0], _t.data(), 20); + return ret; +} + +} + +namespace std +{ + /// Forward std::hash to eth::h256::hash. + template<> struct hash: eth::h256::hash {}; +} diff --git a/libethereum/Log.h b/libethereum/Log.h new file mode 100644 index 000000000..52684fae1 --- /dev/null +++ b/libethereum/Log.h @@ -0,0 +1,134 @@ +/* + 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 Log.h + * @author Gav Wood + * @date 2014 + * + * The logging subsystem. + */ + +#pragma once + +#include +#include +#include +#include "vector_ref.h" + +namespace eth +{ + +/// The null output stream. Used when logging is disabled. +class NullOutputStream +{ +public: + template NullOutputStream& operator<<(T const&) { return *this; } +}; + +/// A simple log-output function that prints log messages to stdout. +void simpleDebugOut(std::string const&, char const* ); + +/// The logging system's current verbosity. +extern int g_logVerbosity; + +/// The current method that the logging system uses to output the log messages. Defaults to simpleDebugOut(). +extern std::function g_logPost; + +/// Map of Log Channel types to bool, false forces the channel to be disabled, true forces it to be enabled. +/// If a channel has no entry, then it will output as long as its verbosity (LogChannel::verbosity) is less than +/// or equal to the currently output verbosity (g_logVerbosity). +extern std::map g_logOverride; + +/// Associate a name with each thread for nice logging. +struct ThreadLocalLogName +{ + ThreadLocalLogName(std::string _name) { m_name.reset(new std::string(_name)); }; + boost::thread_specific_ptr m_name; +}; + +/// The current thread's name. +extern ThreadLocalLogName t_logThreadName; + +/// Set the current thread's log name. +inline void setThreadName(char const* _n) { t_logThreadName.m_name.reset(new std::string(_n)); } + +/// The default logging channels. Each has an associated verbosity and three-letter prefix (name() ). +/// Channels should inherit from LogChannel and define name() and verbosity. +struct LogChannel { static const char* name() { return " "; } static const int verbosity = 1; }; +struct LeftChannel: public LogChannel { static const char* name() { return "<<<"; } }; +struct RightChannel: public LogChannel { static const char* name() { return ">>>"; } }; +struct WarnChannel: public LogChannel { static const char* name() { return "!!!"; } static const int verbosity = 0; }; +struct NoteChannel: public LogChannel { static const char* name() { return "***"; } }; +struct DebugChannel: public LogChannel { static const char* name() { return "---"; } static const int verbosity = 0; }; + +/// Logging class, iostream-like, that can be shifted to. +template +class LogOutputStream +{ +public: + /// Construct a new object. + /// If _term is true the the prefix info is terminated with a ']' character; if not it ends only with a '|' character. + LogOutputStream(bool _term = true) + { + std::type_info const* i = &typeid(Id); + auto it = g_logOverride.find(i); + if ((it != g_logOverride.end() && it->second == true) || (it == g_logOverride.end() && Id::verbosity <= g_logVerbosity)) + { + time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + char buf[24]; + if (strftime(buf, 24, "%X", localtime(&rawTime)) == 0) + buf[0] = '\0'; // empty if case strftime fails + m_sstr << Id::name() << " [ " << buf << " | " << *(t_logThreadName.m_name.get()) << (_term ? " ] " : ""); + } + } + + /// Destructor. Posts the accrued log entry to the g_logPost function. + ~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(m_sstr.str(), Id::name()); } + + /// Shift arbitrary data to the log. Spaces will be added between items as required. + template LogOutputStream& operator<<(T const& _t) { if (Id::verbosity <= g_logVerbosity) { if (_AutoSpacing && m_sstr.str().size() && m_sstr.str().back() != ' ') m_sstr << " "; m_sstr << _t; } return *this; } + +private: + std::stringstream m_sstr; ///< The accrued log entry. +}; + +// Simple cout-like stream objects for accessing common log channels. +// Dirties the global namespace, but oh so convenient... +#define cnote eth::LogOutputStream() +#define cwarn eth::LogOutputStream() + +// Null stream-like objects. +#define ndebug if (true) {} else eth::NullOutputStream() +#define nlog(X) if (true) {} else eth::NullOutputStream() +#define nslog(X) if (true) {} else eth::NullOutputStream() + +// Kill debugging log channel when we're in release mode. +#if NDEBUG +#define cdebug ndebug +#else +#define cdebug eth::LogOutputStream() +#endif + +// Kill all logs when when NLOG is defined. +#if NLOG +#define clog(X) nlog(X) +#define cslog(X) nslog(X) +#else +#define clog(X) eth::LogOutputStream() +#define cslog(X) eth::LogOutputStream() +#endif + +} diff --git a/libethereum/PeerServer.h b/libethereum/PeerServer.h new file mode 100644 index 000000000..98214feb7 --- /dev/null +++ b/libethereum/PeerServer.h @@ -0,0 +1,133 @@ +/* + 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 PeerServer.h + * @author Gav Wood + * @date 2014 + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include "PeerNetwork.h" +#include "CommonEth.h" +namespace ba = boost::asio; +namespace bi = boost::asio::ip; + +namespace eth +{ + +class PeerServer +{ + friend class PeerSession; + +public: + /// Start server, listening for connections on the given port. + PeerServer(std::string const& _clientVersion, BlockChain const& _ch, uint _networkId, unsigned short _port, NodeMode _m = NodeMode::Full, std::string const& _publicAddress = std::string(), bool _upnp = true); + /// Start server, but don't listen. + PeerServer(std::string const& _clientVersion, uint _networkId, NodeMode _m = NodeMode::Full); + ~PeerServer(); + + static unsigned protocolVersion(); + unsigned networkId() { return m_networkId; } + + /// Connect to a peer explicitly. + void connect(std::string const& _addr, unsigned short _port = 30303) noexcept; + void connect(bi::tcp::endpoint const& _ep); + + /// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network. + bool sync(BlockChain& _bc, TransactionQueue&, Overlay& _o); + bool sync(); + + /// Conduct I/O, polling, syncing, whatever. + /// Ideally all time-consuming I/O is done in a background thread or otherwise asynchronously, but you get this call every 100ms or so anyway. + /// This won't touch alter the blockchain. + void process() { if (isInitialised()) m_ioService.poll(); } + + /// Set ideal number of peers. + void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; } + + void setMode(NodeMode _m) { m_mode = _m; } + + /// Get peer information. + std::vector peers() const; + + /// Get number of peers connected; equivalent to, but faster than, peers().size(). + size_t peerCount() const { return m_peers.size(); } + + /// Ping the peers, to update the latency information. + void pingAll(); + + /// Get the port we're listening on currently. + unsigned short listenPort() const { return m_public.port(); } + + bytes savePeers() const; + void restorePeers(bytesConstRef _b); + +private: + void seal(bytes& _b); + void populateAddresses(); + void determinePublic(std::string const& _publicAddress, bool _upnp); + void ensureAccepting(); + + /// Check to see if the network peer-state initialisation has happened. + bool isInitialised() const { return m_latestBlockSent; } + /// Initialises the network peer-state, doing the stuff that needs to be once-only. @returns true if it really was first. + bool ensureInitialised(BlockChain& _bc, TransactionQueue& _tq); + + std::map potentialPeers(); + + std::string m_clientVersion; + NodeMode m_mode = NodeMode::Full; + + unsigned short m_listenPort; + + BlockChain const* m_chain = nullptr; + ba::io_service m_ioService; + bi::tcp::acceptor m_acceptor; + bi::tcp::socket m_socket; + + UPnP* m_upnp = nullptr; + bi::tcp::endpoint m_public; + KeyPair m_key; + + unsigned m_networkId; + std::map> m_peers; + + std::vector m_incomingTransactions; + std::vector m_incomingBlocks; + std::vector m_unknownParentBlocks; + std::vector m_freePeers; + std::map> m_incomingPeers; + + h256 m_latestBlockSent; + std::set m_transactionsSent; + + std::chrono::steady_clock::time_point m_lastPeersRequest; + unsigned m_idealPeerCount = 5; + + std::vector m_addresses; + std::vector m_peerAddresses; + + bool m_accepting = false; +}; + +} diff --git a/libethereum/PeerSession.h b/libethereum/PeerSession.h new file mode 100644 index 000000000..0621f4f92 --- /dev/null +++ b/libethereum/PeerSession.h @@ -0,0 +1,90 @@ +/* + 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 PeerSession.h + * @author Gav Wood + * @date 2014 + */ + +#pragma once + +#include +#include +#include +#include +#include "RLP.h" +#include "CommonEth.h" +#include "PeerNetwork.h" + +namespace eth +{ + +class PeerSession: public std::enable_shared_from_this +{ + friend class PeerServer; + +public: + PeerSession(PeerServer* _server, bi::tcp::socket _socket, uint _rNId, bi::address _peerAddress, unsigned short _peerPort = 0); + ~PeerSession(); + + void start(); + void disconnect(int _reason); + + void ping(); + + bool isOpen() const { return m_socket.is_open(); } + + bi::tcp::endpoint endpoint() const; ///< for other peers to connect to. + +private: + void dropped(); + void doRead(); + void doWrite(std::size_t length); + bool interpret(RLP const& _r); + + /// @returns true iff the _msg forms a valid message for sending or receiving on the network. + static bool checkPacket(bytesConstRef _msg); + + static RLPStream& prep(RLPStream& _s); + void sealAndSend(RLPStream& _s); + void sendDestroy(bytes& _msg); + void send(bytesConstRef _msg); + PeerServer* m_server; + + bi::tcp::socket m_socket; + std::array m_data; + PeerInfo m_info; + Public m_id; + + bytes m_incoming; + uint m_protocolVersion; + uint m_networkId; + uint m_reqNetworkId; + unsigned short m_listenPort; ///< Port that the remote client is listening on for connections. Useful for giving to peers. + uint m_caps; + + std::chrono::steady_clock::time_point m_ping; + std::chrono::steady_clock::time_point m_connect; + std::chrono::steady_clock::time_point m_disconnect; + + uint m_rating; + bool m_requireTransactions; + + std::set m_knownBlocks; + std::set m_knownTransactions; +}; + +}