diff --git a/src/avalanche/peermanager.h b/src/avalanche/peermanager.h --- a/src/avalanche/peermanager.h +++ b/src/avalanche/peermanager.h @@ -127,8 +127,7 @@ /** * Node API. */ - bool addNode(NodeId nodeid, const Proof &proof, - const Delegation &delegation); + bool addNode(NodeId nodeid, Proof proof, const Delegation &delegation); bool removeNode(NodeId nodeid); bool forNode(NodeId nodeid, std::function func) const; @@ -151,7 +150,7 @@ * Provide the PeerId associated with the given proof. If the peer does not * exists, then it is created. */ - PeerId getPeerId(const Proof &proof); + PeerId getPeerId(Proof proof); /** * Remove an existing peer. @@ -182,7 +181,7 @@ std::vector getNodeIdsForPeer(PeerId peerId) const; private: - PeerSet::iterator fetchOrCreatePeer(const Proof &proof); + PeerSet::iterator fetchOrCreatePeer(Proof proof); bool addNodeToPeer(const PeerSet::iterator &it); bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count = 1); }; diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -13,9 +13,9 @@ namespace avalanche { -bool PeerManager::addNode(NodeId nodeid, const Proof &proof, +bool PeerManager::addNode(NodeId nodeid, Proof proof, const Delegation &delegation) { - auto it = fetchOrCreatePeer(proof); + auto it = fetchOrCreatePeer(std::move(proof)); if (it == peers.end()) { return false; } @@ -24,7 +24,7 @@ DelegationState state; CPubKey pubkey; - if (!delegation.verify(state, proof, pubkey)) { + if (!delegation.verify(state, it->proof, pubkey)) { return false; } @@ -180,13 +180,12 @@ } } -PeerId PeerManager::getPeerId(const Proof &proof) { - auto it = fetchOrCreatePeer(proof); +PeerId PeerManager::getPeerId(Proof proof) { + auto it = fetchOrCreatePeer(std::move(proof)); return it == peers.end() ? NO_PEER : it->peerid; } -PeerManager::PeerSet::iterator -PeerManager::fetchOrCreatePeer(const Proof &proof) { +PeerManager::PeerSet::iterator PeerManager::fetchOrCreatePeer(Proof proof) { { // Check if we already know of that peer. auto &pview = peers.get(); @@ -236,7 +235,7 @@ } // We have no peer for this proof, time to create it. - auto inserted = peers.emplace(peerid, proof); + auto inserted = peers.emplace(peerid, std::move(proof)); assert(inserted.second); return inserted.first; diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -289,8 +289,7 @@ bool registerVotes(NodeId nodeid, const Response &response, std::vector &updates); - bool addNode(NodeId nodeid, const Proof &proof, - const Delegation &delegation); + bool addNode(NodeId nodeid, Proof proof, const Delegation &delegation); bool forNode(NodeId nodeid, std::function func) const; CPubKey getSessionPubKey() const; diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -442,10 +442,10 @@ return true; } -bool Processor::addNode(NodeId nodeid, const Proof &proof, +bool Processor::addNode(NodeId nodeid, Proof proof, const Delegation &delegation) { LOCK(cs_peerManager); - return peerManager->addNode(nodeid, proof, delegation); + return peerManager->addNode(nodeid, std::move(proof), delegation); } bool Processor::forNode(NodeId nodeid, diff --git a/src/avalanche/test/peermanager_tests.cpp b/src/avalanche/test/peermanager_tests.cpp --- a/src/avalanche/test/peermanager_tests.cpp +++ b/src/avalanche/test/peermanager_tests.cpp @@ -164,13 +164,13 @@ // One peer, we always return it. Proof proof0 = buildRandomProof(MIN_VALID_PROOF_SCORE); Delegation dg0 = DelegationBuilder(proof0).build(); - pm.addNode(node0, proof0, dg0); + pm.addNode(node0, std::move(proof0), dg0); BOOST_CHECK_EQUAL(pm.selectNode(), node0); // Two peers, verify ratio. Proof proof1 = buildRandomProof(2 * MIN_VALID_PROOF_SCORE); Delegation dg1 = DelegationBuilder(proof1).build(); - pm.addNode(node1, proof1, dg1); + pm.addNode(node1, std::move(proof1), dg1); std::unordered_map results = {}; for (int i = 0; i < 10000; i++) { @@ -184,7 +184,7 @@ // Three peers, verify ratio. Proof proof2 = buildRandomProof(MIN_VALID_PROOF_SCORE); Delegation dg2 = DelegationBuilder(proof2).build(); - pm.addNode(node2, proof2, dg2); + pm.addNode(node2, std::move(proof2), dg2); results.clear(); for (int i = 0; i < 10000; i++) { @@ -206,8 +206,8 @@ for (int i = 0; i < 4; i++) { Proof p = buildRandomProof(100); peerids[i] = pm.getPeerId(p); - BOOST_CHECK( - pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); + BOOST_CHECK(pm.addNode(InsecureRand32(), std::move(p), + DelegationBuilder(p).build())); } BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); @@ -239,8 +239,8 @@ for (int i = 0; i < 4; i++) { Proof p = buildRandomProof(100); peerids[i + 4] = pm.getPeerId(p); - BOOST_CHECK( - pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); + BOOST_CHECK(pm.addNode(InsecureRand32(), std::move(p), + DelegationBuilder(p).build())); } BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); @@ -282,8 +282,8 @@ for (int i = 0; i < 4; i++) { Proof p = buildRandomProof(100); peerids[i] = pm.getPeerId(p); - BOOST_CHECK( - pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); + BOOST_CHECK(pm.addNode(InsecureRand32(), std::move(p), + DelegationBuilder(p).build())); } // Remove all peers. @@ -314,7 +314,7 @@ // Add 4 nodes. for (int i = 0; i < 4; i++) { - BOOST_CHECK(pm.addNode(i, proof, dg)); + BOOST_CHECK(pm.addNode(i, std::move(proof), dg)); } for (int i = 0; i < 100; i++) { @@ -349,7 +349,7 @@ // as chances of being picked are 1 in 10 million. Proof altproof = buildRandomProof(MIN_VALID_PROOF_SCORE); Delegation altdg = DelegationBuilder(altproof).build(); - BOOST_CHECK(pm.addNode(3, altproof, altdg)); + BOOST_CHECK(pm.addNode(3, std::move(altproof), altdg)); int node3selected = 0; for (int i = 0; i < 100; i++) { diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -129,7 +129,7 @@ bool addNode(NodeId nodeid) { Proof proof = GetProof(); - return m_processor->addNode(nodeid, proof, + return m_processor->addNode(nodeid, std::move(proof), DelegationBuilder(proof).build()); } @@ -141,7 +141,7 @@ std::array nodes; for (CNode *&n : nodes) { n = ConnectNode(NODE_AVALANCHE); - BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); + BOOST_CHECK(pm.addNode(n->GetId(), std::move(proof), dg)); } return nodes; @@ -750,7 +750,7 @@ std::array nodes; for (auto &n : nodes) { n = ConnectNode(NODE_AVALANCHE); - BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); + BOOST_CHECK(pm.addNode(n->GetId(), std::move(proof), dg)); } // Add a block to poll diff --git a/src/rpc/avalanche.cpp b/src/rpc/avalanche.cpp --- a/src/rpc/avalanche.cpp +++ b/src/rpc/avalanche.cpp @@ -88,7 +88,7 @@ return false; } - return g_avalanche->addNode(nodeid, proof, + return g_avalanche->addNode(nodeid, std::move(proof), avalanche::DelegationBuilder(proof).build()); }