diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index e49baa1be..98efeb843 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -383,11 +383,12 @@ string Host::pocHost() void Host::addNode(NodeId const& _node, bi::address const& _addr, unsigned short _udpNodePort, unsigned short _tcpPeerPort) { - // TODO: p2p clean this up (bring tested acceptor code over from network branch) - while (isWorking() && !m_run) - this_thread::sleep_for(chrono::milliseconds(50)); - if (!m_run) - return; + // return if network is stopped while waiting on Host::run() or nodeTable to start + while (!haveNetwork()) + if(isWorking()) + this_thread::sleep_for(chrono::milliseconds(50)); + else + return; if (_tcpPeerPort < 30300 || _tcpPeerPort > 30305) cwarn << "Non-standard port being recorded: " << _tcpPeerPort; @@ -636,8 +637,9 @@ void Host::startedWorking() else clog(NetNote) << "p2p.start.notice id:" << id().abridged() << "TCP Listen port is invalid or unavailable."; - m_nodeTable.reset(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort())); - m_nodeTable->setEventHandler(new HostNodeTableHandler(*this)); + shared_ptr nodeTable(new NodeTable(m_ioService, m_alias, bi::address::from_string(listenAddress()), listenPort())); + nodeTable->setEventHandler(new HostNodeTableHandler(*this)); + m_nodeTable = nodeTable; restoreNetwork(&m_restoreNetwork); clog(NetNote) << "p2p.started id:" << id().abridged(); diff --git a/libp2p/Host.h b/libp2p/Host.h index 753be3150..e4d43f233 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -141,9 +141,12 @@ public: /// Resets acceptor, socket, and IO service. Called by deallocator. void stop(); - /// @returns if network is running. - bool isStarted() const { return m_run; } + /// @returns if network has been started. + bool isStarted() const { return isWorking(); } + /// @returns if network is started and interactive. + bool haveNetwork() const { return m_run && !!m_nodeTable; } + NodeId id() const { return m_alias.pub(); } /// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. diff --git a/libwebthree/WebThree.cpp b/libwebthree/WebThree.cpp index a74d7fa55..8ea2133f0 100644 --- a/libwebthree/WebThree.cpp +++ b/libwebthree/WebThree.cpp @@ -74,7 +74,7 @@ WebThreeDirect::~WebThreeDirect() void WebThreeDirect::setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers) { - auto had = haveNetwork(); + auto had = isNetworkStarted(); if (had) stopNetwork(); m_net.setNetworkPreferences(_n, _dropPeers); diff --git a/libwebthree/WebThree.h b/libwebthree/WebThree.h index 92feb8d40..87cf62d4a 100644 --- a/libwebthree/WebThree.h +++ b/libwebthree/WebThree.h @@ -163,7 +163,7 @@ public: /// Sets the ideal number of peers. void setIdealPeerCount(size_t _n) override; - bool haveNetwork() const override { return m_net.isStarted(); } + bool haveNetwork() const override { return m_net.haveNetwork(); } void setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers = false) override; diff --git a/test/peer.cpp b/test/peer.cpp index 904de0e9d..2aeb99469 100644 --- a/test/peer.cpp +++ b/test/peer.cpp @@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(save_nodes) h->setIdealPeerCount(10); // starting host is required so listenport is available h->start(); - while (!h->isStarted()) + while (!h->haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); hosts.push_back(h); }