diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -189,14 +189,14 @@ return nullptr; } - peerData = std::make_unique(); - Proof proof; - if (!Proof::FromHex(proof, argsman.GetArg("-avaproof", ""), error)) { + auto proof = RCUPtr::make(); + if (!Proof::FromHex(*proof, argsman.GetArg("-avaproof", ""), error)) { // error is set by FromHex return nullptr; } - peerData->proof = std::make_shared(std::move(proof)); + peerData = std::make_unique(); + peerData->proof = std::move(proof); if (!VerifyProof(*peerData->proof, error)) { // error is set by VerifyProof return nullptr; diff --git a/src/avalanche/proof.h b/src/avalanche/proof.h --- a/src/avalanche/proof.h +++ b/src/avalanche/proof.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -114,6 +115,8 @@ uint32_t score; void computeScore(); + IMPLEMENT_RCU_REFCOUNT(uint64_t); + public: Proof() : sequence(0), expirationTime(0), master(), stakes(), @@ -129,6 +132,13 @@ computeProofId(); computeScore(); } + Proof(Proof &&other) + : sequence(other.sequence), expirationTime(other.expirationTime), + master(std::move(other.master)), stakes(std::move(other.stakes)), + payoutScriptPubKey(std::move(other.payoutScriptPubKey)), + signature(std::move(other.signature)), + limitedProofId(std::move(other.limitedProofId)), + proofid(std::move(other.proofid)), score(other.score) {} SERIALIZE_METHODS(Proof, obj) { READWRITE(obj.sequence, obj.expirationTime, obj.master, obj.stakes); @@ -169,7 +179,7 @@ bool verify(ProofValidationState &state, const CCoinsView &view) const; }; -using ProofRef = std::shared_ptr; +using ProofRef = RCUPtr; } // namespace avalanche diff --git a/src/avalanche/proofbuilder.cpp b/src/avalanche/proofbuilder.cpp --- a/src/avalanche/proofbuilder.cpp +++ b/src/avalanche/proofbuilder.cpp @@ -53,9 +53,9 @@ signedStakes.push_back(handle.value().sign(commitment)); } - return std::make_shared( - sequence, expirationTime, masterKey.GetPubKey(), - std::move(signedStakes), payoutScriptPubKey, std::move(proofSignature)); + return ProofRef::make(sequence, expirationTime, masterKey.GetPubKey(), + std::move(signedStakes), payoutScriptPubKey, + std::move(proofSignature)); } LimitedProofId ProofBuilder::getLimitedProofId() const { 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 @@ -874,12 +874,11 @@ "96527eae083f1f24625f049d9e54bb9a2102a93d98bf42ab90cfc0bf9e7c634ed76a7" "3e95b02cacfd357b64e4fb6c92e92dd00"); bilingual_str error; - Proof badProof; - BOOST_CHECK(Proof::FromHex(badProof, badProofHex, error)); + auto badProof = RCUPtr::make(); + BOOST_CHECK(Proof::FromHex(*badProof, badProofHex, error)); ProofRegistrationState state; - BOOST_CHECK( - !pm.registerProof(std::make_shared(std::move(badProof)), state)); + BOOST_CHECK(!pm.registerProof(badProof, state)); BOOST_CHECK(state.GetResult() == ProofRegistrationResult::INVALID); } diff --git a/src/avalanche/test/util.cpp b/src/avalanche/test/util.cpp --- a/src/avalanche/test/util.cpp +++ b/src/avalanche/test/util.cpp @@ -78,9 +78,9 @@ signedStakes.push_back(handle.value().sign(commitment)); } - return std::make_shared( - pb.sequence, pb.expirationTime, pb.masterKey.GetPubKey(), - std::move(signedStakes), pb.payoutScriptPubKey, SchnorrSig()); + return ProofRef::make(pb.sequence, pb.expirationTime, + pb.masterKey.GetPubKey(), std::move(signedStakes), + pb.payoutScriptPubKey, SchnorrSig()); } ProofId TestProofBuilder::getDuplicatedStakeProofId(ProofBuilder &pb) { @@ -115,9 +115,9 @@ signedStakes.push_back(signedStake); } - return std::make_shared( - pb.sequence, pb.expirationTime, pb.masterKey.GetPubKey(), - std::move(signedStakes), pb.payoutScriptPubKey, SchnorrSig()); + return ProofRef::make(pb.sequence, pb.expirationTime, + pb.masterKey.GetPubKey(), std::move(signedStakes), + pb.payoutScriptPubKey, SchnorrSig()); } } // namespace avalanche diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -5112,7 +5112,7 @@ } if (msg_type == NetMsgType::AVAPROOF) { - auto proof = std::make_shared(); + auto proof = RCUPtr::make(); vRecv >> *proof; const avalanche::ProofId &proofid = proof->getId(); diff --git a/src/rpc/avalanche.cpp b/src/rpc/avalanche.cpp --- a/src/rpc/avalanche.cpp +++ b/src/rpc/avalanche.cpp @@ -124,7 +124,7 @@ const NodeId nodeid = request.params[0].get_int64(); CPubKey key = ParsePubKey(request.params[1]); - auto proof = std::make_shared(); + auto proof = RCUPtr::make(); NodeContext &node = EnsureNodeContext(request.context); verifyProofOrThrow(node, *proof, request.params[2].get_str()); @@ -938,7 +938,7 @@ "Avalanche is not initialized"); } - auto proof = std::make_shared(); + auto proof = RCUPtr::make(); NodeContext &node = EnsureNodeContext(request.context); // Verify the proof. Note that this is redundant with the