From 19c9ae935a8d93a37ad78c0ade494e355f39c657 Mon Sep 17 00:00:00 2001 From: subtly Date: Fri, 7 Aug 2015 21:16:53 +0200 Subject: [PATCH] --peerset CLI option for required (static) peers --- eth/main.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++---- libp2p/Host.h | 10 ------- libp2p/Network.h | 4 ++- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index 371c54c04..b2425ba31 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -181,11 +181,12 @@ void help() << " --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 + << " --peerset Space delimited list of type type:publickey@ipAddress[:port]" << endl + << " Types:" << endl + << " default Attempt connection when no other peers are available and pinning is disable." << endl + << " require Keep connected at all times." << endl +// TODO: +// << " --trust-peers Space delimited list of publickeys." << endl << " --no-discovery Disable Node discovery." << endl << " --pin Only accept or connect to trusted peers." << endl @@ -359,6 +360,7 @@ int main(int argc, char** argv) HostPeerPreferences hprefs; unsigned peers = hprefs.idealPeerCount; unsigned peerStretch = hprefs.stretchPeerCount; + std::map preferredNodes; bool bootstrap = false; bool disableDiscovery = false; bool pinning = false; @@ -717,6 +719,63 @@ int main(int argc, char** argv) peers = atoi(argv[++i]); else if (arg == "--peer-stretch" && i + 1 < argc) peerStretch = atoi(argv[++i]); + else if (arg == "--peerset" && i + 1 < argc) + { + string peerset = argv[++i]; + if (peerset.empty()) + { + cerr << "--peerset argument must not be empty"; + return -1; + } + + vector each; + boost::split(strs, peerset, boost::is_any_of("\t ")); + for (auto const& p: each) + { + string type; + string pubk; + string hostIP; + unsigned short port = c_defaultListenPort; + + // type:key@ip[:port] + vector typeAndKeyAtHostAndPort; + boost::split(typeAndKeyAtHostAndPort, peerset, boost::is_any_of(":")); + if (typeAndKeyAtHostAndPort.size() < 2 || typeAndKeyAtHostAndPort.size() > 3) + continue; + + type = typeAndKeyAtHostAndPort[0]; + if (typeAndKeyAtHostAndPort.size() == 3) + port = (uint16_t)atoi(typeAndKeyAtHostAndPort[2].c_str()); + + vector keyAndHost; + boost::split(keyAndHost, typeAndKeyAtHostAndPort[1], boost::is_any_of("@")); + if (keyAndHost.size() != 2) + continue; + pubk = keyAndHost[0]; + if (pubk.size() != 40) + continue; + hostIP = keyAndHost[1]; + + // todo: use Network::resolveHost() + if (hostIP.size() < 4 /* g.it */) + continue; + + bool required = type == "required"; + if (!required && type != "default") + continue; + + Public publicKey(fromHex(pubk)); + try + { + preferredNodes[publicKey] = Node(pubk, NodeIPEndpoint(bi::address(hostIP), port, port), required); + } + catch (...) + { + cerr << "Unrecognized peerset: " << peerset << endl; + return -1; + } + } + } else if ((arg == "-o" || arg == "--mode") && i + 1 < argc) { string m = argv[++i]; @@ -1002,6 +1061,7 @@ int main(int argc, char** argv) } cout << ethCredits(); + web3.setIdealPeerCount(peers); web3.setPeerStretch(peerStretch); // std::shared_ptr gasPricer = make_shared(u256(double(ether / 1000) / etherPrice), u256(blockFees * 1000)); @@ -1051,12 +1111,18 @@ int main(int argc, char** argv) } #endif + for (auto const& p: preferredNodes) + if (p.second.required) + web3.requirePeer(p.first, p.second.endpoint); + else + web3.addNode(p.first, p.second.endpoint); + if (bootstrap) for (auto const& i: Host::pocHosts()) web3.requirePeer(i.first, i.second); if (!remoteHost.empty()) web3.addNode(p2p::NodeId(), remoteHost + ":" + toString(remotePort)); - + signal(SIGABRT, &sighandler); signal(SIGTERM, &sighandler); signal(SIGINT, &sighandler); diff --git a/libp2p/Host.h b/libp2p/Host.h index 863149899..31eac5319 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -119,16 +119,6 @@ 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. diff --git a/libp2p/Network.h b/libp2p/Network.h index 3b4396510..c9c62aace 100644 --- a/libp2p/Network.h +++ b/libp2p/Network.h @@ -37,6 +37,8 @@ namespace dev namespace p2p { +static const unsigned short c_defaultListenPort = 30303; + struct NetworkPreferences { // Default Network Preferences @@ -52,7 +54,7 @@ struct NetworkPreferences std::string publicIPAddress; std::string listenIPAddress; - unsigned short listenPort = 30303; + unsigned short listenPort = c_defaultListenPort; /// Preferences