diff --git a/eth/main.cpp b/eth/main.cpp index 4b23ef62a..bc143154e 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -168,6 +168,9 @@ void help() << " --port Connect to remote port (default: 30303)." << endl << " --network-id Only connect to other hosts with this network id (default:0)." << endl << " --upnp Use UPnP for NAT (default: on)." << endl + << " --no-discovery Disable Node discovery. (experimental)" << endl + << " --pin Only connect to required (trusted) peers. (experimental)" << endl +// << " --require-peers List of required (trusted) peers. (experimental)" << endl << endl; MinerCLI::streamHelp(cout); cout @@ -304,6 +307,8 @@ int main(int argc, char** argv) unsigned short remotePort = 30303; unsigned peers = 11; bool bootstrap = false; + bool disableDiscovery = false; + bool pinning = false; unsigned networkId = 0; /// Mining params @@ -592,6 +597,16 @@ int main(int argc, char** argv) } else if (arg == "-b" || arg == "--bootstrap") bootstrap = true; + else if (arg == "--no-discovery") + if (bootstrap) + { + cerr << "-b/--bootstrap cannot be used with --no-discovery." << endl; + return -1; + } + else + disableDiscovery = true; + else if (arg == "--pin") + pinning = true; else if (arg == "-f" || arg == "--force-mining") forceMining = true; else if (arg == "-i" || arg == "--interactive") @@ -684,6 +699,8 @@ int main(int argc, char** argv) StructuredLogger::get().initialize(structuredLogging, structuredLoggingFormat, structuredLoggingURL); VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter); auto netPrefs = publicIP.empty() ? NetworkPreferences(listenIP ,listenPort, upnp) : NetworkPreferences(publicIP, listenIP ,listenPort, upnp); + netPrefs.discovery = !disableDiscovery; + netPrefs.pin = pinning; auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp"); std::string clientImplString = "++eth/" + clientName + "v" + dev::Version + "-" + string(DEV_QUOTED(ETH_COMMIT_HASH)).substr(0, 8) + (ETH_CLEAN_REPO ? "" : "*") + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM) + (jit ? "/JIT" : ""); dev::WebThreeDirect web3( diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 55389ed1b..b0d246458 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -307,7 +307,8 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e) { clog(NetP2PNote) << "p2p.host.nodeTable.events.NodeEntryDropped " << _n; RecursiveGuard l(x_sessions); - m_peers.erase(_n); + if (m_peers.count(_n) && !m_peers[_n]->required) + m_peers.erase(_n); } } @@ -628,7 +629,7 @@ void Host::run(boost::system::error_code const&) { RecursiveGuard l(x_sessions); for (auto p: m_peers) - if (p.second->shouldReconnect() && !havePeerSession(p.second->id)) + if (p.second->shouldReconnect() && !havePeerSession(p.second->id) && (!m_netPrefs.pin || p.second->required)) toConnect.push_back(p.second); } @@ -679,7 +680,7 @@ void Host::startedWorking() else clog(NetP2PNote) << "p2p.start.notice id:" << id() << "TCP Listen port is invalid or unavailable."; - shared_ptr nodeTable(new NodeTable(m_ioService, m_alias, NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()))); + shared_ptr nodeTable(new NodeTable(m_ioService, m_alias, NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()), m_netPrefs.discovery)); nodeTable->setEventHandler(new HostNodeTableHandler(*this)); m_nodeTable = nodeTable; restoreNetwork(&m_restoreNetwork); diff --git a/libp2p/Network.h b/libp2p/Network.h index d02ce3cbe..97c631a7c 100644 --- a/libp2p/Network.h +++ b/libp2p/Network.h @@ -51,6 +51,8 @@ struct NetworkPreferences std::string publicIPAddress; std::string listenIPAddress; unsigned short listenPort = 30303; + bool discovery = true; // Discovery is activated with network. + bool pin = false; // Only connect to trusted ("required") peers. bool traverseNAT = true; }; diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 6344dc263..491795274 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -40,14 +40,15 @@ const char* NodeTableIngress::name() { return "<connect(); - doRefreshBuckets(boost::system::error_code()); + if (!m_disabled) + { + m_socketPointer->connect(); + doRefreshBuckets(boost::system::error_code()); + } } NodeTable::~NodeTable() diff --git a/libp2p/NodeTable.h b/libp2p/NodeTable.h index 0ec13c828..bca14e5d4 100644 --- a/libp2p/NodeTable.h +++ b/libp2p/NodeTable.h @@ -130,7 +130,7 @@ public: enum NodeRelation { Unknown = 0, Known }; /// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. - NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint); + NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint, bool _disabled = false); ~NodeTable(); /// Returns distance based on xor metric two node ids. Used by NodeEntry and NodeTable. @@ -271,6 +271,8 @@ private: boost::asio::deadline_timer m_bucketRefreshTimer; ///< Timer which schedules and enacts bucket refresh. boost::asio::deadline_timer m_evictionCheckTimer; ///< Timer for handling node evictions. + + bool m_disabled; ///< Disable discovery. }; inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable) diff --git a/libp2p/Peer.cpp b/libp2p/Peer.cpp index 6f368a4b4..a8b4a993d 100644 --- a/libp2p/Peer.cpp +++ b/libp2p/Peer.cpp @@ -38,6 +38,8 @@ bool Peer::shouldReconnect() const unsigned Peer::fallbackSeconds() const { + if (required) + return 5; switch (m_lastDisconnect) { case BadProtocol: