Browse Source

Merge pull request #2755 from subtly/netFix

require / trusted nodes from CLI
cl-refactor
Gav Wood 10 years ago
parent
commit
79e655f38b
  1. 84
      eth/main.cpp
  2. 2
      libp2p/Host.cpp
  3. 10
      libp2p/Host.h
  4. 4
      libp2p/Network.h

84
eth/main.cpp

@ -128,9 +128,9 @@ void help()
<< " --bootstrap Connect to the default Ethereum peerservers (default unless --no-discovery used)." << endl
<< " --no-bootstrap Do not connect to the default Ethereum peerservers (default only when --no-discovery is used)." << endl
<< " -x,--peers <number> Attempt to connect to given number of peers (default: 11)." << endl
<< " --peer-stretch <number> Accepted connection multiplier (default: 7)." << endl
<< " --peer-stretch <number> Accepted connection multiplier (default: 7)." << endl
<< " --public-ip <ip> Force public ip to given (default: auto)." << endl
<< " --public-ip <ip> Force advertised public ip to given (default: auto)." << endl
<< " --listen-ip <ip>(:<port>) Listen on the given IP for incoming connections (default: 0.0.0.0)." << endl
<< " --listen <port> Listen on the given port for incoming connections (default: 30303)." << endl
<< " -r,--remote <host>(:<port>) Connect to remote host (default: none)." << endl
@ -138,11 +138,12 @@ void help()
<< " --network-id <n> Only connect to other hosts with this network id." << endl
<< " --upnp <on/off> Use UPnP for NAT (default: on)." << endl
// << " --peers <filename> 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 <filename> Text list of publickeys." << endl
<< " --peerset <list> 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 <filename> Space delimited list of publickeys." << endl
<< " --no-discovery Disable Node discovery, implies --no-bootstrap." << endl
<< " --pin Only accept or connect to trusted peers." << endl
@ -309,9 +310,9 @@ int main(int argc, char** argv)
string remoteHost;
unsigned short remotePort = 30303;
HostPeerPreferences hprefs;
unsigned peers = hprefs.idealPeerCount;
unsigned peerStretch = hprefs.stretchPeerCount;
unsigned peers = 11;
unsigned peerStretch = 7;
std::map<NodeId, pair<NodeIPEndpoint,bool>> preferredNodes;
bool bootstrap = true;
bool disableDiscovery = false;
bool pinning = false;
@ -675,6 +676,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<string> each;
boost::split(each, 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<string> typeAndKeyAtHostAndPort;
boost::split(typeAndKeyAtHostAndPort, p, 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<string> 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] = make_pair(NodeIPEndpoint(bi::address::from_string(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];
@ -1019,6 +1077,12 @@ int main(int argc, char** argv)
}
#endif
for (auto const& p: preferredNodes)
if (p.second.second)
web3.requirePeer(p.first, p.second.first);
else
web3.addNode(p.first, p.second.first);
if (bootstrap)
for (auto const& i: Host::pocHosts())
web3.requirePeer(i.first, i.second);

2
libp2p/Host.cpp

@ -449,7 +449,7 @@ string Host::pocHost()
std::unordered_map<Public, std::string> const& Host::pocHosts()
{
static const std::unordered_map<Public, std::string> c_ret = {
{ Public("487611428e6c99a11a9795a6abe7b529e81315ca6aad66e2a2fc76e3adf263faba0d35466c2f8f68d561dbefa8878d4df5f1f2ddb1fbeab7f42ffb8cd328bd4a"), "poc-9.ethdev.com:30303" },
{ Public("979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9"), "poc-9.ethdev.com:30303" },
{ Public("a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c"), "52.16.188.185:30303" },
{ Public("7f25d3eab333a6b98a8b5ed68d962bb22c876ffcd5561fca54e3c2ef27f754df6f7fd7c9b74cc919067abac154fb8e1f8385505954f161ae440abc355855e034"), "54.207.93.166:30303" }
};

10
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<NodeId> const defaultPeers;
// std::list<NodeId> const requiredPeers;
// std::list<NodeId> const trusted;
};
/**
* @brief The Host class
* Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe.

4
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

Loading…
Cancel
Save