diff --git a/eth/main.cpp b/eth/main.cpp index a17e04e9c..b88c8ae95 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -170,7 +170,9 @@ void help() << "Client networking:" << endl << " --client-name Add a name to your client's version string (default: blank)." << endl << " -b,--bootstrap Connect to the default Ethereum peerserver." << endl - << " -x,--peers Attempt to connect to given number of peers (default: 5)." << endl + << " -x,--peers Attempt to connect to given number of peers (default: 11)." << endl + << " --peer-stretch Accepted connection multiplier (default: 7)." << endl + << " --public-ip Force public ip to given (default: auto)." << endl << " --listen-ip (:) Listen on the given IP for incoming connections (default: 0.0.0.0)." << endl << " --listen Listen on the given port for incoming connections (default: 30303)." << endl @@ -178,11 +180,17 @@ void help() << " --port Connect to remote port (default: 30303)." << endl << " --network-id Only connect to other hosts with this network id." << endl << " --upnp Use UPnP for NAT (default: on)." << endl + +// << " --peers Text list of type publickey@host[:port] (default: network)" << endl +// << " Types:" << endl +// << " default Attempt connection when no other peers are available and pinning is disable." << endl +// << " trusted Keep connected at all times." << endl +// << " --trust-peers Text list of publickeys." << endl + << " --no-discovery Disable Node discovery." << endl - << " --pin Only connect to required (trusted) peers." << endl + << " --pin Only accept or connect to trusted peers." << endl << " --hermit Equivalent to --no-discovery --pin." << endl << " --sociable Forces discovery and no pinning." << endl -// << " --require-peers List of required (trusted) peers. (experimental)" << endl << endl; MinerCLI::streamHelp(cout); cout @@ -1123,7 +1131,10 @@ int main(int argc, char** argv) string publicIP; string remoteHost; unsigned short remotePort = 30303; - unsigned peers = 11; + + HostPeerPreferences hprefs; + unsigned peers = hprefs.idealPeerCount; + unsigned peerStretch = hprefs.stretchPeerCount; bool bootstrap = false; bool disableDiscovery = false; bool pinning = false; @@ -1480,6 +1491,8 @@ int main(int argc, char** argv) g_logVerbosity = atoi(argv[++i]); else if ((arg == "-x" || arg == "--peers") && i + 1 < argc) peers = atoi(argv[++i]); + else if ((arg == "-x" || arg == "--peer-stretch") && i + 1 < argc) + peerStretch = atoi(argv[++i]); else if ((arg == "-o" || arg == "--mode") && i + 1 < argc) { string m = argv[++i]; @@ -1766,6 +1779,7 @@ int main(int argc, char** argv) cout << ethCredits(); web3.setIdealPeerCount(peers); + web3.private_setPeerStretch(peerStretch); // std::shared_ptr gasPricer = make_shared(u256(double(ether / 1000) / etherPrice), u256(blockFees * 1000)); std::shared_ptr gasPricer = make_shared(askPrice, bidPrice); eth::Client* c = nodeMode == NodeMode::Full ? web3.ethereum() : nullptr; diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index dd9ff53cf..4bd5a126c 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -283,7 +283,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* return; } - if (!peerSlotsAvailable(Ingress)) + if (!peerSlotsAvailable()) { ps->disconnect(TooManyPeers); return; @@ -405,7 +405,7 @@ void Host::runAcceptor() socket->close(); return; } - if (peerCount() > Ingress * m_idealPeerCount) + if (peerCount() > peerSlots(Ingress)) { clog(NetConnect) << "Dropping incoming connect due to maximum peer count (" << Ingress << " * ideal peer count): " << socket->remoteEndpoint(); socket->close(); diff --git a/libp2p/Host.h b/libp2p/Host.h index d015a2f65..863149899 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -119,6 +119,16 @@ struct NodeInfo std::string version; }; +struct HostPeerPreferences +{ + unsigned const idealPeerCount = 11; // Ideal number of peers to be connected to. + unsigned const stretchPeerCount = 7; // Accepted connection multiplier (max peers = ideal*stretch). + +// std::list const defaultPeers; +// std::list const requiredPeers; +// std::list const trusted; +}; + /** * @brief The Host class * Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe. @@ -173,6 +183,9 @@ public: /// Set ideal number of peers. void setIdealPeerCount(unsigned _n) { m_idealPeerCount = _n; } + /// Set multipier for max accepted connections. + void setPeerStretch(unsigned _n) { m_stretchPeers = _n; } + /// Get peer information. PeerSessionInfos peerSessionInfo() const; @@ -236,7 +249,9 @@ protected: void restoreNetwork(bytesConstRef _b); private: - enum PeerSlotRatio { Egress = 1, Ingress = 4 }; + enum PeerSlotType { Egress, Ingress }; + + unsigned peerSlots(PeerSlotType _type) { return _type == Egress ? m_idealPeerCount : m_idealPeerCount * m_stretchPeers; } bool havePeerSession(NodeId const& _id) { return !!peerSession(_id); } @@ -246,7 +261,7 @@ private: void connect(std::shared_ptr const& _p); /// Returns true if pending and connected peer count is less than maximum - bool peerSlotsAvailable(PeerSlotRatio _type) { Guard l(x_pendingNodeConns); return peerCount() + m_pendingPeerConns.size() < _type * m_idealPeerCount; } + bool peerSlotsAvailable(PeerSlotType _type = Ingress) { Guard l(x_pendingNodeConns); return peerCount() + m_pendingPeerConns.size() < peerSlots(_type); } /// Ping the peers to update the latency information and disconnect peers which have timed out. void keepAlivePeers(); @@ -314,6 +329,7 @@ private: Mutex x_connecting; ///< Mutex for m_connecting. unsigned m_idealPeerCount = 11; ///< Ideal number of peers to be connected to. + unsigned m_stretchPeers = 7; ///< Accepted connection multiplier (max peers = ideal*stretch). std::map> m_capabilities; ///< Each of the capabilities we support. diff --git a/libp2p/Network.h b/libp2p/Network.h index e70dd89ea..3b4396510 100644 --- a/libp2p/Network.h +++ b/libp2p/Network.h @@ -48,12 +48,18 @@ struct NetworkPreferences // Network Preferences with intended Public IP NetworkPreferences(std::string const& publicIP, std::string const& l = std::string(), unsigned short lp = 30303, bool u = true): publicIPAddress(publicIP), listenIPAddress(l), listenPort(lp), traverseNAT(u) { if (!publicIPAddress.empty() && !isPublicAddress(publicIPAddress)) BOOST_THROW_EXCEPTION(InvalidPublicIPAddress()); } + /// Addressing + std::string publicIPAddress; std::string listenIPAddress; unsigned short listenPort = 30303; + + + /// Preferences + bool traverseNAT = true; bool discovery = true; // Discovery is activated with network. - bool pin = false; // Only connect to trusted ("required") peers. + bool pin = false; // Only accept or connect to trusted peers. }; /** diff --git a/libwebthree/WebThree.cpp b/libwebthree/WebThree.cpp index fc436f2e3..3afa1a337 100644 --- a/libwebthree/WebThree.cpp +++ b/libwebthree/WebThree.cpp @@ -118,6 +118,11 @@ void WebThreeDirect::setIdealPeerCount(size_t _n) return m_net.setIdealPeerCount(_n); } +void WebThreeDirect::private_setPeerStretch(size_t _n) +{ + return m_net.setPeerStretch(_n); +} + bytes WebThreeDirect::saveNetwork() { return m_net.saveNetwork(); diff --git a/libwebthree/WebThree.h b/libwebthree/WebThree.h index 5e251f974..6875ad5fd 100644 --- a/libwebthree/WebThree.h +++ b/libwebthree/WebThree.h @@ -171,6 +171,9 @@ public: /// Sets the ideal number of peers. void setIdealPeerCount(size_t _n) override; + /// Sets ceiling for incoming connections to multiple of ideal peer count. + void private_setPeerStretch(size_t _n); + bool haveNetwork() const override { return m_net.haveNetwork(); } p2p::NetworkPreferences const& networkPreferences() const override;