From 54a21ab3bd2f5c6435cfff82d64000a9980d8847 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 3 Sep 2014 16:51:22 +0200 Subject: [PATCH] Quick commit. --- libethcore/BlockInfo.h | 2 +- libethential/FixedHash.h | 4 +++- libethereum/EthereumHost.cpp | 1 + libethereum/EthereumHost.h | 2 +- libethereum/EthereumPeer.cpp | 4 +++- libethereum/EthereumPeer.h | 6 ++---- libethnet/CMakeLists.txt | 3 --- libethnet/Common.cpp | 5 +++-- libethnet/Common.h | 17 +++++++++++++++-- libethnet/PeerHost.cpp | 25 +++++++++++++------------ libethnet/PeerHost.h | 19 ++++++++++--------- libethnet/PeerSession.cpp | 9 +++++---- libethnet/PeerSession.h | 7 +++---- libethnet/UPnP.cpp | 3 ++- libethnet/UPnP.h | 2 +- libwhisper/WhisperPeer.cpp | 1 + libwhisper/WhisperPeer.h | 9 +++++---- 17 files changed, 69 insertions(+), 50 deletions(-) diff --git a/libethcore/BlockInfo.h b/libethcore/BlockInfo.h index cf9735c7d..d88b9d09f 100644 --- a/libethcore/BlockInfo.h +++ b/libethcore/BlockInfo.h @@ -48,7 +48,7 @@ extern u256 c_genesisDifficulty; * and calculateGasLimit() and the object serialised to RLP with fillStream. To determine the * header hash without the nonce (for mining), the method headerHashWithoutNonce() is provided. * - * The defualt constructor creates an empty object, which can be tested against with the boolean + * The default constructor creates an empty object, which can be tested against with the boolean * conversion operator. */ struct BlockInfo diff --git a/libethential/FixedHash.h b/libethential/FixedHash.h index e13345728..828e3aef6 100644 --- a/libethential/FixedHash.h +++ b/libethential/FixedHash.h @@ -127,7 +127,7 @@ public: /// @returns a randomly-valued hash template - static FixedHash random(Engine& _eng = s_fixedHashEngine) + static FixedHash random(Engine& _eng) { FixedHash ret; for (auto& i: ret.m_data) @@ -135,6 +135,8 @@ public: return ret; } + static FixedHash random() { return random(s_fixedHashEngine); } + /// A generic std::hash compatible function object. struct hash { diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 584aaeb4d..e318bd66d 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -36,6 +36,7 @@ #include "EthereumPeer.h" using namespace std; using namespace eth; +using namespace p2p; EthereumHost::EthereumHost(BlockChain const& _ch, u256 _networkId): HostCapability(), diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h index d2c28644b..e8aa57892 100644 --- a/libethereum/EthereumHost.h +++ b/libethereum/EthereumHost.h @@ -45,7 +45,7 @@ class BlockQueue; * @brief The EthereumHost class * @warning None of this is thread-safe. You have been warned. */ -class EthereumHost: public HostCapability +class EthereumHost: public p2p::HostCapability { friend class EthereumPeer; diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp index 0e651c651..75983a2bf 100644 --- a/libethereum/EthereumPeer.cpp +++ b/libethereum/EthereumPeer.cpp @@ -29,10 +29,12 @@ #include "EthereumHost.h" using namespace std; using namespace eth; +using namespace p2p; #define clogS(X) eth::LogOutputStream(false) << "| " << std::setw(2) << session()->socketId() << "] " -EthereumPeer::EthereumPeer(PeerSession* _s, HostCapabilityFace* _h): PeerCapability(_s, _h) +EthereumPeer::EthereumPeer(PeerSession* _s, HostCapabilityFace* _h): + PeerCapability(_s, _h) { sendStatus(); } diff --git a/libethereum/EthereumPeer.h b/libethereum/EthereumPeer.h index 57df62a11..2aea2bdb3 100644 --- a/libethereum/EthereumPeer.h +++ b/libethereum/EthereumPeer.h @@ -34,18 +34,16 @@ namespace eth { -class HostCapabilityFace; - /** * @brief The EthereumPeer class * @todo Document fully. */ -class EthereumPeer: public PeerCapability +class EthereumPeer: public p2p::PeerCapability { friend class EthereumHost; public: - EthereumPeer(PeerSession* _s, HostCapabilityFace* _h); + EthereumPeer(p2p::PeerSession* _s, p2p::HostCapabilityFace* _h); virtual ~EthereumPeer(); static std::string name() { return "eth"; } diff --git a/libethnet/CMakeLists.txt b/libethnet/CMakeLists.txt index a694ab205..79d07d7e4 100644 --- a/libethnet/CMakeLists.txt +++ b/libethnet/CMakeLists.txt @@ -17,9 +17,6 @@ file(GLOB HEADERS "*.h") include_directories(..) -target_link_libraries(${EXECUTABLE} evm) -target_link_libraries(${EXECUTABLE} lll) -target_link_libraries(${EXECUTABLE} ethcore) target_link_libraries(${EXECUTABLE} secp256k1) if(MINIUPNPC_LS) target_link_libraries(${EXECUTABLE} ${MINIUPNPC_LS}) diff --git a/libethnet/Common.cpp b/libethnet/Common.cpp index f0f0e4d2d..6226df296 100644 --- a/libethnet/Common.cpp +++ b/libethnet/Common.cpp @@ -26,12 +26,13 @@ #include "PeerHost.h" using namespace std; using namespace eth; +using namespace p2p; // Helper function to determine if an address falls within one of the reserved ranges // For V4: // Class A "10.*", Class B "172.[16->31].*", Class C "192.168.*" // Not implemented yet for V6 -bool eth::isPrivateAddress(bi::address _addressToCheck) +bool p2p::isPrivateAddress(bi::address _addressToCheck) { if (_addressToCheck.is_v4()) { @@ -47,7 +48,7 @@ bool eth::isPrivateAddress(bi::address _addressToCheck) return false; } -std::string eth::reasonOf(DisconnectReason _r) +std::string p2p::reasonOf(DisconnectReason _r) { switch (_r) { diff --git a/libethnet/Common.h b/libethnet/Common.h index ed12cb81f..da7dc0a53 100644 --- a/libethnet/Common.h +++ b/libethnet/Common.h @@ -29,16 +29,29 @@ #include #include #include +#include namespace ba = boost::asio; namespace bi = boost::asio::ip; namespace eth { +class RLP; +class RLPStream; +} + +namespace p2p +{ + +using eth::LogChannel; +using eth::bytes; +using eth::h256; +using eth::h512; +using eth::bytesConstRef; +using eth::RLP; +using eth::RLPStream; bool isPrivateAddress(bi::address _addressToCheck); -class RLP; -class RLPStream; class PeerHost; class PeerSession; diff --git a/libethnet/PeerHost.cpp b/libethnet/PeerHost.cpp index 8806aba07..00d4705ee 100644 --- a/libethnet/PeerHost.cpp +++ b/libethnet/PeerHost.cpp @@ -40,6 +40,7 @@ #include "UPnP.h" using namespace std; using namespace eth; +using namespace p2p; // Addresses we will skip during network interface discovery // Use a vector as the list is small @@ -58,13 +59,13 @@ PeerHost::PeerHost(std::string const& _clientVersion, unsigned short _port, stri m_localNetworking(_localNetworking), m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), _port)), m_socket(m_ioService), - m_key(KeyPair::create()) + m_id(h512::random()) { populateAddresses(); determinePublic(_publicAddress, _upnp); ensureAccepting(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << m_key.address().abridged(); + clog(NetNote) << "Id:" << m_id.abridged(); } PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddress, bool _upnp, bool _localNetworking): @@ -73,7 +74,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre m_localNetworking(_localNetworking), m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)), m_socket(m_ioService), - m_key(KeyPair::create()) + m_id(h512::random()) { m_listenPort = m_acceptor.local_endpoint().port(); @@ -82,7 +83,7 @@ PeerHost::PeerHost(std::string const& _clientVersion, string const& _publicAddre determinePublic(_publicAddress, _upnp); ensureAccepting(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << m_key.address().abridged(); + clog(NetNote) << "Id:" << m_id.abridged(); } PeerHost::PeerHost(std::string const& _clientVersion): @@ -90,12 +91,12 @@ PeerHost::PeerHost(std::string const& _clientVersion): m_listenPort(0), m_acceptor(m_ioService, bi::tcp::endpoint(bi::tcp::v4(), 0)), m_socket(m_ioService), - m_key(KeyPair::create()) + m_id(h512::random()) { // populate addresses. populateAddresses(); m_lastPeersRequest = chrono::steady_clock::time_point::min(); - clog(NetNote) << "Id:" << m_key.address().abridged(); + clog(NetNote) << "Id:" << m_id.abridged(); } PeerHost::~PeerHost() @@ -271,11 +272,11 @@ void PeerHost::populateAddresses() #endif } -std::map PeerHost::potentialPeers() +std::map PeerHost::potentialPeers() { - std::map ret; + std::map ret; if (!m_public.address().is_unspecified()) - ret.insert(make_pair(m_key.pub(), m_public)); + ret.insert(make_pair(m_id, m_public)); Guard l(x_peers); for (auto i: m_peers) if (auto j = i.second.lock()) @@ -361,7 +362,7 @@ void PeerHost::connect(bi::tcp::endpoint const& _ep) }); } -bool PeerHost::havePeer(Public _id) const +bool PeerHost::havePeer(h512 _id) const { Guard l(x_peers); @@ -413,7 +414,7 @@ void PeerHost::prunePeers() { Guard l(x_peers); // We'll keep at most twice as many as is ideal, halfing what counts as "too young to kill" until we get there. - for (uint old = 15000; m_peers.size() > m_idealPeerCount * 2 && old > 100; old /= 2) + for (unsigned old = 15000; m_peers.size() > m_idealPeerCount * 2 && old > 100; old /= 2) while (m_peers.size() > m_idealPeerCount) { // look for worst peer to kick off @@ -492,7 +493,7 @@ void PeerHost::restorePeers(bytesConstRef _b) { for (auto i: RLP(_b)) { - auto k = (Public)i[2]; + auto k = (h512)i[2]; if (!m_incomingPeers.count(k)) { m_incomingPeers.insert(make_pair(k, make_pair(bi::tcp::endpoint(bi::address_v4(i[0].toArray()), i[1].toInt()), 0))); diff --git a/libethnet/PeerHost.h b/libethnet/PeerHost.h index 012700091..e9e1a5987 100644 --- a/libethnet/PeerHost.h +++ b/libethnet/PeerHost.h @@ -29,18 +29,19 @@ #include #include #include -#include #include "Common.h" namespace ba = boost::asio; namespace bi = boost::asio::ip; -namespace eth +namespace p2p { class RLPStream; class TransactionQueue; class BlockQueue; +using eth::Guard; + /** * @brief The PeerHost class * Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe. @@ -84,7 +85,7 @@ public: void process(); /// @returns true iff we have the a peer of the given id. - bool havePeer(Public _id) const; + bool havePeer(h512 _id) const; /// Set ideal number of peers. void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; } @@ -107,7 +108,7 @@ public: /// Deserialise the data and populate the set of known peers. void restorePeers(bytesConstRef _b); - h512 id() const { return m_key.pub(); } + h512 id() const { return m_id; } void registerPeer(std::shared_ptr _s, std::vector const& _caps); @@ -123,7 +124,7 @@ protected: void growPeers(); void prunePeers(); - std::map potentialPeers(); + std::map potentialPeers(); std::string m_clientVersion; @@ -136,13 +137,13 @@ protected: UPnP* m_upnp = nullptr; bi::tcp::endpoint m_public; - KeyPair m_key; + h512 m_id; mutable std::mutex x_peers; - mutable std::map> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method. + mutable std::map> m_peers; // mutable because we flush zombie entries (null-weakptrs) as regular maintenance from a const method. - std::map> m_incomingPeers; // TODO: does this need a lock? - std::vector m_freePeers; + std::map> m_incomingPeers; // TODO: does this need a lock? + std::vector m_freePeers; std::chrono::steady_clock::time_point m_lastPeersRequest; unsigned m_idealPeerCount = 5; diff --git a/libethnet/PeerSession.cpp b/libethnet/PeerSession.cpp index 7ddc61eec..1f19bd032 100644 --- a/libethnet/PeerSession.cpp +++ b/libethnet/PeerSession.cpp @@ -27,6 +27,7 @@ #include "PeerHost.h" using namespace std; using namespace eth; +using namespace p2p; #define clogS(X) eth::LogOutputStream(false) << "| " << std::setw(2) << m_socket.native_handle() << "] " @@ -74,7 +75,7 @@ bool PeerSession::interpret(RLP const& _r) { case HelloPacket: { - m_protocolVersion = _r[1].toInt(); + m_protocolVersion = _r[1].toInt(); auto clientVersion = _r[2].toString(); auto caps = _r[3].toVector(); m_listenPort = _r[4].toInt(); @@ -156,14 +157,14 @@ bool PeerSession::interpret(RLP const& _r) { bi::address_v4 peerAddress(_r[i][0].toHash>().asArray()); auto ep = bi::tcp::endpoint(peerAddress, _r[i][1].toInt()); - Public id = _r[i][2].toHash(); + h512 id = _r[i][2].toHash(); if (isPrivateAddress(peerAddress) && !m_server->m_localNetworking) goto CONTINUE; clogS(NetAllDetail) << "Checking: " << ep << "(" << id.abridged() << ")"; // check that it's not us or one we already know: - if (id && (m_server->m_key.pub() == id || m_server->havePeer(id) || m_server->m_incomingPeers.count(id))) + if (id && (m_server->m_id == id || m_server->havePeer(id) || m_server->m_incomingPeers.count(id))) goto CONTINUE; // check that we're not already connected to addr: @@ -336,7 +337,7 @@ void PeerSession::start() << m_server->m_clientVersion << m_server->caps() << m_server->m_public.port() - << m_server->m_key.pub(); + << m_server->m_id; sealAndSend(s); ping(); getPeers(); diff --git a/libethnet/PeerSession.h b/libethnet/PeerSession.h index 73e0764f3..50fb86e3d 100644 --- a/libethnet/PeerSession.h +++ b/libethnet/PeerSession.h @@ -27,10 +27,9 @@ #include #include #include -#include #include "Common.h" -namespace eth +namespace p2p { /** @@ -89,10 +88,10 @@ private: mutable bi::tcp::socket m_socket; ///< Mutable to ask for native_handle(). std::array m_data; PeerInfo m_info; - Public m_id; + h512 m_id; bytes m_incoming; - uint m_protocolVersion; + unsigned m_protocolVersion; unsigned short m_listenPort; ///< Port that the remote client is listening on for connections. Useful for giving to peers. std::chrono::steady_clock::time_point m_ping; diff --git a/libethnet/UPnP.cpp b/libethnet/UPnP.cpp index 427450e03..ce0286681 100644 --- a/libethnet/UPnP.cpp +++ b/libethnet/UPnP.cpp @@ -34,6 +34,7 @@ #include using namespace std; using namespace eth; +using namespace p2p; UPnP::UPnP() { @@ -126,7 +127,7 @@ int UPnP::addRedirect(char const* _addr, int _port) return _port; // Failed - now try (random external, port internal) and cycle up to 10 times. - for (uint i = 0; i < 10; ++i) + for (unsigned i = 0; i < 10; ++i) { _port = rand() % 65535 - 1024 + 1024; sprintf(port_str, "%d", _port); diff --git a/libethnet/UPnP.h b/libethnet/UPnP.h index 836e350b0..8f118bae4 100644 --- a/libethnet/UPnP.h +++ b/libethnet/UPnP.h @@ -29,7 +29,7 @@ struct UPNPUrls; struct IGDdatas; -namespace eth +namespace p2p { class UPnP diff --git a/libwhisper/WhisperPeer.cpp b/libwhisper/WhisperPeer.cpp index 04c02821e..b59ae9c4a 100644 --- a/libwhisper/WhisperPeer.cpp +++ b/libwhisper/WhisperPeer.cpp @@ -25,6 +25,7 @@ #include using namespace std; using namespace eth; +using namespace p2p; using namespace shh; #define clogS(X) eth::LogOutputStream(false) << "| " << std::setw(2) << session()->socketId() << "] " diff --git a/libwhisper/WhisperPeer.h b/libwhisper/WhisperPeer.h index 8228f6a3d..9853399ae 100644 --- a/libwhisper/WhisperPeer.h +++ b/libwhisper/WhisperPeer.h @@ -34,9 +34,10 @@ namespace shh { -using eth::PeerSession; -using eth::HostCapabilityFace; -using eth::HostCapability; +using p2p::PeerSession; +using p2p::HostCapabilityFace; +using p2p::HostCapability; +using p2p::PeerCapability; struct Message { @@ -63,7 +64,7 @@ struct Message /** */ -class WhisperPeer: public eth::PeerCapability +class WhisperPeer: public PeerCapability { friend class WhisperHost;