Browse Source

Merge pull request #2180 from subtly/netFix

require peer changes
cl-refactor
Gav Wood 10 years ago
parent
commit
505aec7c50
  1. 11
      eth/main.cpp
  2. 38
      libp2p/Host.cpp
  3. 2
      libp2p/Network.h
  4. 8
      libp2p/NodeTable.cpp
  5. 4
      libp2p/NodeTable.h
  6. 2
      libp2p/Peer.cpp

11
eth/main.cpp

@ -171,6 +171,9 @@ void help()
<< " --port <port> Connect to remote port (default: 30303)." << endl << " --port <port> Connect to remote port (default: 30303)." << endl
<< " --network-id <n> Only connect to other hosts with this network id (default:0)." << endl << " --network-id <n> Only connect to other hosts with this network id (default:0)." << endl
<< " --upnp <on/off> Use UPnP for NAT (default: on)." << endl << " --upnp <on/off> 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 <peers.json> List of required (trusted) peers. (experimental)" << endl
<< endl; << endl;
MinerCLI::streamHelp(cout); MinerCLI::streamHelp(cout);
cout cout
@ -307,6 +310,8 @@ int main(int argc, char** argv)
unsigned short remotePort = 30303; unsigned short remotePort = 30303;
unsigned peers = 11; unsigned peers = 11;
bool bootstrap = false; bool bootstrap = false;
bool disableDiscovery = false;
bool pinning = false;
unsigned networkId = 0; unsigned networkId = 0;
/// Mining params /// Mining params
@ -595,6 +600,10 @@ int main(int argc, char** argv)
} }
else if (arg == "-b" || arg == "--bootstrap") else if (arg == "-b" || arg == "--bootstrap")
bootstrap = true; bootstrap = true;
else if (arg == "--no-discovery")
disableDiscovery = true;
else if (arg == "--pin")
pinning = true;
else if (arg == "-f" || arg == "--force-mining") else if (arg == "-f" || arg == "--force-mining")
forceMining = true; forceMining = true;
else if (arg == "-i" || arg == "--interactive") else if (arg == "-i" || arg == "--interactive")
@ -687,6 +696,8 @@ int main(int argc, char** argv)
StructuredLogger::get().initialize(structuredLogging, structuredLoggingFormat, structuredLoggingURL); StructuredLogger::get().initialize(structuredLogging, structuredLoggingFormat, structuredLoggingURL);
VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter); VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter);
auto netPrefs = publicIP.empty() ? NetworkPreferences(listenIP ,listenPort, upnp) : NetworkPreferences(publicIP, listenIP ,listenPort, upnp); 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"); auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp");
dev::WebThreeDirect web3( dev::WebThreeDirect web3(
WebThreeDirect::composeClientVersion("++eth", clientName), WebThreeDirect::composeClientVersion("++eth", clientName),

38
libp2p/Host.cpp

@ -326,6 +326,7 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e)
{ {
clog(NetP2PNote) << "p2p.host.nodeTable.events.NodeEntryDropped " << _n; clog(NetP2PNote) << "p2p.host.nodeTable.events.NodeEntryDropped " << _n;
RecursiveGuard l(x_sessions); RecursiveGuard l(x_sessions);
if (m_peers.count(_n) && !m_peers[_n]->required)
m_peers.erase(_n); m_peers.erase(_n);
} }
} }
@ -637,28 +638,41 @@ void Host::run(boost::system::error_code const&)
// updated. // disconnectLatePeers(); // updated. // disconnectLatePeers();
// todo: update peerSlotsAvailable() // todo: update peerSlotsAvailable()
unsigned pendingCount = 0;
DEV_GUARDED(x_pendingNodeConns)
pendingCount = m_pendingPeerConns.size();
int openSlots = m_idealPeerCount - peerCount() - pendingCount;
if (openSlots > 0)
{
list<shared_ptr<Peer>> toConnect; list<shared_ptr<Peer>> toConnect;
unsigned reqConn = 0;
{ {
RecursiveGuard l(x_sessions); RecursiveGuard l(x_sessions);
for (auto p: m_peers) for (auto const& p: m_peers)
if (p.second->shouldReconnect() && !havePeerSession(p.second->id)) {
bool haveSession = havePeerSession(p.second->id);
bool required = p.second->required;
if (haveSession && required)
reqConn++;
else if (!haveSession && p.second->shouldReconnect() && (!m_netPrefs.pin || required))
toConnect.push_back(p.second); toConnect.push_back(p.second);
} }
}
for (auto p: toConnect) for (auto p: toConnect)
if (openSlots--) if (p->required && reqConn++ < m_idealPeerCount)
connect(p);
if (!m_netPrefs.pin)
{
unsigned pendingCount = 0;
DEV_GUARDED(x_pendingNodeConns)
pendingCount = m_pendingPeerConns.size();
int openSlots = m_idealPeerCount - peerCount() - pendingCount + reqConn;
if (openSlots > 0)
{
for (auto p: toConnect)
if (!p->required && openSlots--)
connect(p); connect(p);
else
break;
m_nodeTable->discover(); m_nodeTable->discover();
} }
}
auto runcb = [this](boost::system::error_code const& error) { run(error); }; auto runcb = [this](boost::system::error_code const& error) { run(error); };
m_timer->expires_from_now(boost::posix_time::milliseconds(c_timerInterval)); m_timer->expires_from_now(boost::posix_time::milliseconds(c_timerInterval));
@ -698,7 +712,7 @@ void Host::startedWorking()
else else
clog(NetP2PNote) << "p2p.start.notice id:" << id() << "TCP Listen port is invalid or unavailable."; clog(NetP2PNote) << "p2p.start.notice id:" << id() << "TCP Listen port is invalid or unavailable.";
shared_ptr<NodeTable> nodeTable(new NodeTable(m_ioService, m_alias, NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()))); shared_ptr<NodeTable> nodeTable(new NodeTable(m_ioService, m_alias, NodeIPEndpoint(bi::address::from_string(listenAddress()), listenPort(), listenPort()), m_netPrefs.discovery));
nodeTable->setEventHandler(new HostNodeTableHandler(*this)); nodeTable->setEventHandler(new HostNodeTableHandler(*this));
m_nodeTable = nodeTable; m_nodeTable = nodeTable;
restoreNetwork(&m_restoreNetwork); restoreNetwork(&m_restoreNetwork);

2
libp2p/Network.h

@ -52,6 +52,8 @@ struct NetworkPreferences
std::string listenIPAddress; std::string listenIPAddress;
unsigned short listenPort = 30303; unsigned short listenPort = 30303;
bool traverseNAT = true; bool traverseNAT = true;
bool discovery = true; // Discovery is activated with network.
bool pin = false; // Only connect to trusted ("required") peers.
}; };
/** /**

8
libp2p/NodeTable.cpp

@ -40,14 +40,15 @@ const char* NodeTableIngress::name() { return "<<P"; }
NodeEntry::NodeEntry(NodeId const& _src, Public const& _pubk, NodeIPEndpoint const& _gw): Node(_pubk, _gw), distance(NodeTable::distance(_src, _pubk)) {} NodeEntry::NodeEntry(NodeId const& _src, Public const& _pubk, NodeIPEndpoint const& _gw): Node(_pubk, _gw), distance(NodeTable::distance(_src, _pubk)) {}
NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint): NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint, bool _enabled):
m_node(Node(_alias.pub(), _endpoint)), m_node(Node(_alias.pub(), _endpoint)),
m_secret(_alias.sec()), m_secret(_alias.sec()),
m_io(_io), m_io(_io),
m_socket(new NodeSocket(m_io, *this, (bi::udp::endpoint)m_node.endpoint)), m_socket(new NodeSocket(m_io, *this, (bi::udp::endpoint)m_node.endpoint)),
m_socketPointer(m_socket.get()), m_socketPointer(m_socket.get()),
m_bucketRefreshTimer(m_io), m_bucketRefreshTimer(m_io),
m_evictionCheckTimer(m_io) m_evictionCheckTimer(m_io),
m_disabled(!_enabled)
{ {
for (unsigned i = 0; i < s_bins; i++) for (unsigned i = 0; i < s_bins; i++)
{ {
@ -55,9 +56,12 @@ NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint
m_state[i].modified = chrono::steady_clock::now() - chrono::seconds(1); m_state[i].modified = chrono::steady_clock::now() - chrono::seconds(1);
} }
if (!m_disabled)
{
m_socketPointer->connect(); m_socketPointer->connect();
doRefreshBuckets(boost::system::error_code()); doRefreshBuckets(boost::system::error_code());
} }
}
NodeTable::~NodeTable() NodeTable::~NodeTable()
{ {

4
libp2p/NodeTable.h

@ -130,7 +130,7 @@ public:
enum NodeRelation { Unknown = 0, Known }; enum NodeRelation { Unknown = 0, Known };
/// Constructor requiring host for I/O, credentials, and IP Address and port to listen on. /// 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 _enabled = true);
~NodeTable(); ~NodeTable();
/// Returns distance based on xor metric two node ids. Used by NodeEntry and 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_bucketRefreshTimer; ///< Timer which schedules and enacts bucket refresh.
boost::asio::deadline_timer m_evictionCheckTimer; ///< Timer for handling node evictions. 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) inline std::ostream& operator<<(std::ostream& _out, NodeTable const& _nodeTable)

2
libp2p/Peer.cpp

@ -38,6 +38,8 @@ bool Peer::shouldReconnect() const
unsigned Peer::fallbackSeconds() const unsigned Peer::fallbackSeconds() const
{ {
if (required)
return 5;
switch (m_lastDisconnect) switch (m_lastDisconnect)
{ {
case BadProtocol: case BadProtocol:

Loading…
Cancel
Save