diff --git a/libethereum/BlockChainSync.h b/libethereum/BlockChainSync.h index 4e328d5ba..dc8971a1b 100644 --- a/libethereum/BlockChainSync.h +++ b/libethereum/BlockChainSync.h @@ -113,6 +113,9 @@ protected: /// Request blocks from peer if needed void requestBlocks(std::shared_ptr _peer); +private: + EthereumHost& m_host; + protected: Handler<> m_bqRoomAvailable; ///< Triggered once block queue mutable RecursiveMutex x_sync; @@ -124,8 +127,6 @@ private: static char const* const s_stateNames[static_cast(SyncState::Size)]; bool invariants() const override = 0; void logNewBlock(h256 const& _h); - - EthereumHost& m_host; }; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index aed6f60d6..2631a28cb 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -116,9 +116,9 @@ void Client::init(p2p::Host* _extNet, std::string const& _dbPath, WithExisting _ m_gp->update(bc()); - auto host = _extNet->registerCapability(new EthereumHost(bc(), m_tq, m_bq, _networkId)); + auto host = _extNet->registerCapability(make_shared(bc(), m_tq, m_bq, _networkId)); m_host = host; - _extNet->addCapability(host, EthereumHost::staticName(), EthereumHost::c_oldProtocolVersion); //TODO: remove this one v61+ protocol is common + _extNet->addCapability(host, EthereumHost::staticName(), EthereumHost::c_oldProtocolVersion); //TODO: remove this once v61+ protocol is common if (_dbPath.size()) Defaults::setDBPath(_dbPath); diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index 7eb37ff99..64b29a8ba 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -39,7 +39,7 @@ const char* VMTraceChannel::name() { return "EVM"; } const char* ExecutiveWarnChannel::name() { return WarnChannel::name(); } StandardTrace::StandardTrace(): - m_trace(new Json::Value(Json::arrayValue)) + m_trace(make_shared(Json::arrayValue)) {} bool changesMemory(Instruction _inst) diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 64d10b172..69437c392 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -237,9 +237,9 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* { // peer doesn't exist, try to get port info from node table if (Node n = m_nodeTable->node(_id)) - p.reset(new Peer(n)); + p = make_shared(n); else - p.reset(new Peer(Node(_id, UnspecifiedNodeIPEndpoint))); + p = make_shared(Node(_id, UnspecifiedNodeIPEndpoint)); m_peers[_id] = p; } } @@ -294,7 +294,7 @@ void Host::startPeerSession(Public const& _id, RLP const& _rlp, RLPXFrameCoder* for (auto const& i: caps) if (haveCapability(i)) { - ps->m_capabilities[i] = shared_ptr(m_capabilities[i]->newPeerCapability(ps, o, i)); + ps->m_capabilities[i] = m_capabilities[i]->newPeerCapability(ps, o, i); o += m_capabilities[i]->messageCount(); } ps->start(); @@ -323,7 +323,7 @@ void Host::onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e) } else { - p.reset(new Peer(n)); + p = make_shared(n); m_peers[_n] = p; clog(NetP2PNote) << "p2p.host.peers.events.peerAdded " << _n << p->endpoint; } @@ -491,14 +491,14 @@ void Host::requirePeer(NodeId const& _n, NodeIPEndpoint const& _endpoint) } else { - p.reset(new Peer(node)); + p = make_shared(node); m_peers[_n] = p; } } else if (m_nodeTable) { m_nodeTable->addNode(node); - shared_ptr t(new boost::asio::deadline_timer(m_ioService)); + auto t = make_shared(m_ioService); t->expires_from_now(boost::posix_time::milliseconds(600)); t->async_wait([this, _n](boost::system::error_code const& _ec) { @@ -712,7 +712,12 @@ 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()), m_netPrefs.discovery)); + auto nodeTable = make_shared( + 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/Host.h b/libp2p/Host.h index 863149899..2ba39e095 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -161,7 +161,7 @@ public: static std::unordered_map const& pocHosts(); /// Register a peer-capability; all new peer connections will have this capability. - template std::shared_ptr registerCapability(T* _t) { _t->m_host = this; std::shared_ptr ret(_t); m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = ret; return ret; } + template std::shared_ptr registerCapability(std::shared_ptr const& _t) { _t->m_host = this; m_capabilities[std::make_pair(T::staticName(), T::staticVersion())] = _t; return _t; } template void addCapability(std::shared_ptr const & _p, std::string const& _name, u256 const& _version) { m_capabilities[std::make_pair(_name, _version)] = _p; } bool haveCapability(CapDesc const& _name) const { return m_capabilities.count(_name) != 0; } diff --git a/libp2p/HostCapability.h b/libp2p/HostCapability.h index 7032e31cb..61f046ed3 100644 --- a/libp2p/HostCapability.h +++ b/libp2p/HostCapability.h @@ -23,6 +23,7 @@ #pragma once +#include #include "Peer.h" #include "Common.h" @@ -53,7 +54,7 @@ protected: virtual u256 version() const = 0; CapDesc capDesc() const { return std::make_pair(name(), version()); } virtual unsigned messageCount() const = 0; - virtual Capability* newPeerCapability(std::shared_ptr _s, unsigned _idOffset, CapDesc const& _cap) = 0; + virtual std::shared_ptr newPeerCapability(std::shared_ptr const& _s, unsigned _idOffset, CapDesc const& _cap) = 0; virtual void onStarting() {} virtual void onStopping() {} @@ -77,7 +78,7 @@ protected: virtual std::string name() const { return PeerCap::name(); } virtual u256 version() const { return PeerCap::version(); } virtual unsigned messageCount() const { return PeerCap::messageCount(); } - virtual Capability* newPeerCapability(std::shared_ptr _s, unsigned _idOffset, CapDesc const& _cap) { return new PeerCap(_s, this, _idOffset, _cap); } + virtual std::shared_ptr newPeerCapability(std::shared_ptr const& _s, unsigned _idOffset, CapDesc const& _cap) { return std::make_shared(_s, this, _idOffset, _cap); } }; } diff --git a/libp2p/Network.cpp b/libp2p/Network.cpp index b44ac1e0a..ef44c912d 100644 --- a/libp2p/Network.cpp +++ b/libp2p/Network.cpp @@ -169,10 +169,10 @@ bi::tcp::endpoint Network::traverseNAT(std::set const& _ifAddresses { asserts(_listenPort != 0); - UPnP* upnp = nullptr; + unique_ptr upnp; try { - upnp = new UPnP; + upnp.reset(new UPnP); } // let m_upnp continue as null - we handle it properly. catch (...) {} @@ -200,9 +200,6 @@ bi::tcp::endpoint Network::traverseNAT(std::set const& _ifAddresses } else clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place)."; - - if (upnp) - delete upnp; } return upnpEP; diff --git a/libp2p/NodeTable.cpp b/libp2p/NodeTable.cpp index 44ab0aa99..9395466f4 100644 --- a/libp2p/NodeTable.cpp +++ b/libp2p/NodeTable.cpp @@ -43,7 +43,7 @@ NodeEntry::NodeEntry(NodeId const& _src, Public const& _pubk, NodeIPEndpoint con NodeTable::NodeTable(ba::io_service& _io, KeyPair const& _alias, NodeIPEndpoint const& _endpoint, bool _enabled): m_node(Node(_alias.pub(), _endpoint)), m_secret(_alias.sec()), - m_socket(new NodeSocket(_io, *this, (bi::udp::endpoint)m_node.endpoint)), + m_socket(make_shared(_io, *reinterpret_cast(this), (bi::udp::endpoint)m_node.endpoint)), m_socketPointer(m_socket.get()), m_timers(_io) { @@ -81,7 +81,7 @@ shared_ptr NodeTable::addNode(Node const& _node, NodeRelation _relati { if (_relation == Known) { - shared_ptr ret(new NodeEntry(m_node.id, _node.id, _node.endpoint)); + auto ret = make_shared(m_node.id, _node.id, _node.endpoint); ret->pending = false; DEV_GUARDED(x_nodes) m_nodes[_node.id] = ret; @@ -107,7 +107,7 @@ shared_ptr NodeTable::addNode(Node const& _node, NodeRelation _relati if (m_nodes.count(_node.id)) return m_nodes[_node.id]; - shared_ptr ret(new NodeEntry(m_node.id, _node.id, _node.endpoint)); + auto ret = make_shared(m_node.id, _node.id, _node.endpoint); DEV_GUARDED(x_nodes) m_nodes[_node.id] = ret; clog(NodeTableConnect) << "addNode pending for" << _node.endpoint; @@ -167,7 +167,7 @@ void NodeTable::doDiscover(NodeId _node, unsigned _round, shared_ptr>()); + _tried = make_shared>>(); auto nearest = nearestNodeEntries(_node); list> tried; diff --git a/libp2p/RLPxHandshake.h b/libp2p/RLPxHandshake.h index 82d2a6f44..cbdc1da41 100644 --- a/libp2p/RLPxHandshake.h +++ b/libp2p/RLPxHandshake.h @@ -130,4 +130,4 @@ protected: }; } -} \ No newline at end of file +} diff --git a/libp2p/UPnP.cpp b/libp2p/UPnP.cpp index 36795c540..ebb1a8b6a 100644 --- a/libp2p/UPnP.cpp +++ b/libp2p/UPnP.cpp @@ -40,8 +40,8 @@ using namespace dev::p2p; UPnP::UPnP() { #if ETH_MINIUPNPC - m_urls.reset(new UPNPUrls); - m_data.reset(new IGDdatas); + m_urls = make_shared(); + m_data = make_shared(); m_ok = false; diff --git a/libp2p/UPnP.h b/libp2p/UPnP.h index 4d53a998b..9ee01eacc 100644 --- a/libp2p/UPnP.h +++ b/libp2p/UPnP.h @@ -50,8 +50,8 @@ public: private: std::set m_reg; bool m_ok; - std::shared_ptr m_urls; - std::shared_ptr m_data; + std::shared_ptr m_urls; + std::shared_ptr m_data; }; } diff --git a/libwebthree/WebThree.cpp b/libwebthree/WebThree.cpp index ba5990bf3..eef2c199e 100644 --- a/libwebthree/WebThree.cpp +++ b/libwebthree/WebThree.cpp @@ -64,7 +64,7 @@ WebThreeDirect::WebThreeDirect( } if (_interfaces.count("shh")) - m_whisper = m_net.registerCapability(new WhisperHost); + m_whisper = m_net.registerCapability(make_shared()); } WebThreeDirect::~WebThreeDirect() diff --git a/test/libp2p/capability.cpp b/test/libp2p/capability.cpp index e8c69655f..e60f7f131 100644 --- a/test/libp2p/capability.cpp +++ b/test/libp2p/capability.cpp @@ -110,11 +110,11 @@ BOOST_AUTO_TEST_CASE(capability) NetworkPreferences prefs2(localhost, 30302, false); Host host1("Test", prefs1); - auto thc1 = host1.registerCapability(new TestHostCapability()); + auto thc1 = host1.registerCapability(make_shared()); host1.start(); Host host2("Test", prefs2); - auto thc2 = host2.registerCapability(new TestHostCapability()); + auto thc2 = host2.registerCapability(make_shared()); host2.start(); int const step = 10; diff --git a/test/libwhisper/shhrpc.cpp b/test/libwhisper/shhrpc.cpp index f14aea6b2..e7937f5c9 100644 --- a/test/libwhisper/shhrpc.cpp +++ b/test/libwhisper/shhrpc.cpp @@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(basic) NetworkPreferences prefs2("127.0.0.1", port2, false); string const version2 = "shhrpc-host2"; Host host2(version2, prefs2); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); for (unsigned i = 0; i < 3000 && !host2.haveNetwork(); i += step) @@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(send) Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); web3->startNetwork(); @@ -239,7 +239,7 @@ BOOST_AUTO_TEST_CASE(receive) uint16_t port2 = 30338; Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); web3->startNetwork(); @@ -379,7 +379,7 @@ BOOST_AUTO_TEST_CASE(server) uint16_t port2 = 30339; Host host2("shhrpc-host2", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); b = jsonrpcServer->admin_net_start(sess2); diff --git a/test/libwhisper/whisperDB.cpp b/test/libwhisper/whisperDB.cpp index 892714393..3d7b09915 100644 --- a/test/libwhisper/whisperDB.cpp +++ b/test/libwhisper/whisperDB.cpp @@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE(messages) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared(true)); preexisting = wh->all(); cnote << preexisting.size() << "preexisting messages in DB"; wh->installWatch(BuildTopic("test")); @@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(messages) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared(true)); map m2 = wh->all(); wh->installWatch(BuildTopic("test")); BOOST_REQUIRE_EQUAL(m1.size(), m2.size()); @@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(corruptedData) { p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost(true)); + auto wh = h.registerCapability(make_shared(true)); m = wh->all(); BOOST_REQUIRE(m.end() == m.find(x)); } @@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(filters) { WhisperFiltersDB db; p2p::Host h("Test"); - auto wh = h.registerCapability(new WhisperHost()); + auto wh = h.registerCapability(make_shared()); wh->installWatch(BuildTopic("t1")); wh->installWatch(BuildTopic("t2")); db.saveTopicsToDB(*wh, persistID); @@ -243,7 +243,7 @@ BOOST_AUTO_TEST_CASE(filters) Host host1("Test", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared()); host1.start(); WhisperFiltersDB db; auto watches = db.restoreTopicsFromDB(whost1.get(), persistID); @@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(filters) Host host2("Test", NetworkPreferences("127.0.0.1", 30309, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); for (unsigned i = 0; i < 3000 && !host1.haveNetwork(); i += step) diff --git a/test/libwhisper/whisperTopic.cpp b/test/libwhisper/whisperTopic.cpp index 39728fb17..39d953e31 100644 --- a/test/libwhisper/whisperTopic.cpp +++ b/test/libwhisper/whisperTopic.cpp @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(topic) uint16_t port1 = 30311; Host host1("Test", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared()); host1.start(); bool host1Ready = false; @@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(topic) Host host2("Test", NetworkPreferences("127.0.0.1", 30310, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); for (unsigned i = 0; i < 3000 && (!host1.haveNetwork() || !host2.haveNetwork()); i += step) @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(forwarding) uint16_t port1 = 30312; Host host1("Listner", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE(forwarding) uint16_t port2 = 30313; Host host2("Forwarder", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); host2.start(); while (!host2.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE(forwarding) Host ph("Sender", NetworkPreferences("127.0.0.1", 30314, false)); ph.setIdealPeerCount(1); - shared_ptr wh = ph.registerCapability(new WhisperHost()); + shared_ptr wh = ph.registerCapability(make_shared()); ph.start(); ph.addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), port2, port2)); while (!ph.haveNetwork()) @@ -237,7 +237,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) uint16_t port1 = 30315; Host host1("Forwarder", NetworkPreferences("127.0.0.1", port1, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -266,7 +266,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { Host host2("Sender", NetworkPreferences("127.0.0.1", 30316, false)); host2.setIdealPeerCount(1); - shared_ptr whost2 = host2.registerCapability(new WhisperHost()); + shared_ptr whost2 = host2.registerCapability(make_shared()); host2.start(); while (!host2.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { Host ph("Listener", NetworkPreferences("127.0.0.1", 30317, false)); ph.setIdealPeerCount(1); - shared_ptr wh = ph.registerCapability(new WhisperHost()); + shared_ptr wh = ph.registerCapability(make_shared()); ph.start(); while (!ph.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(2)); @@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) Host host1("first", NetworkPreferences("127.0.0.1", 30319, false)); host1.setIdealPeerCount(1); - auto whost1 = host1.registerCapability(new WhisperHost()); + auto whost1 = host1.registerCapability(make_shared()); host1.start(); while (!host1.haveNetwork()) this_thread::sleep_for(chrono::milliseconds(10)); @@ -328,7 +328,7 @@ BOOST_AUTO_TEST_CASE(topicAdvertising) uint16_t port2 = 30318; Host host2("second", NetworkPreferences("127.0.0.1", port2, false)); host2.setIdealPeerCount(1); - auto whost2 = host2.registerCapability(new WhisperHost()); + auto whost2 = host2.registerCapability(make_shared()); unsigned w2 = whost2->installWatch(BuildTopicMask("test2")); host2.start(); @@ -399,7 +399,7 @@ BOOST_AUTO_TEST_CASE(selfAddressed) BuildTopicMask mask(text); Host host("first", NetworkPreferences("127.0.0.1", 30320, false)); - auto wh = host.registerCapability(new WhisperHost()); + auto wh = host.registerCapability(make_shared()); auto watch = wh->installWatch(BuildTopicMask(text)); unsigned const sample = 0xFEED;