From b27765f3589329a54ef6ab74ad0dfb40bbd745fb Mon Sep 17 00:00:00 2001 From: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:52:33 -0500 Subject: [PATCH] refactor: further spanification of Dash code (#5586) ## Issue being fixed or feature implemented Use Spans instead of const std::vector& ## What was done? Replaced with Span ## How Has This Been Tested? Building, ran a few tests ## Breaking Changes Should be none, please review potential lifetime issues in bls_worker; it scares me a bit and I don't understand how we know these won't dangle. ## Checklist: _Go over all the following points, and put an `x` in all the boxes that apply._ - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ --- src/bench/bls.cpp | 50 +++++++++++++-------------- src/bench/bls_dkg.cpp | 4 +-- src/bls/bls.h | 16 +++------ src/bls/bls_worker.cpp | 70 +++++++++++++++++++------------------- src/bls/bls_worker.h | 36 ++++++++++---------- src/coinjoin/coinjoin.cpp | 4 +-- src/evo/simplifiedmns.cpp | 6 ++-- src/llmq/dkgsession.cpp | 8 ++--- src/llmq/dkgsession.h | 12 +++---- src/llmq/dkgsessionmgr.cpp | 6 ++-- src/llmq/dkgsessionmgr.h | 2 +- src/llmq/quorums.cpp | 18 +++++----- src/llmq/quorums.h | 4 +-- src/llmq/snapshot.cpp | 2 +- src/llmq/snapshot.h | 2 +- src/span.h | 10 ++++++ 16 files changed, 127 insertions(+), 123 deletions(-) diff --git a/src/bench/bls.cpp b/src/bench/bls.cpp index d219e10a72937..e55ef7df4be46 100644 --- a/src/bench/bls.cpp +++ b/src/bench/bls.cpp @@ -10,7 +10,7 @@ #include static void BuildTestVectors(size_t count, size_t invalidCount, - BLSPublicKeyVector& pubKeys, BLSSecretKeyVector& secKeys, BLSSignatureVector& sigs, + std::vector& pubKeys, std::vector& secKeys, std::vector& sigs, std::vector& msgHashes, std::vector& invalid) { @@ -95,9 +95,9 @@ static void BLS_Sign_Normal(benchmark::Bench& bench) static void BLS_Verify_Normal(benchmark::Bench& bench) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -120,9 +120,9 @@ static void BLS_Verify_Normal(benchmark::Bench& bench) static void BLS_Verify_LargeBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -148,9 +148,9 @@ static void BLS_Verify_LargeBlock1000(benchmark::Bench& bench) static void BLS_Verify_LargeBlockSelfAggregated(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -175,9 +175,9 @@ static void BLS_Verify_LargeBlockSelfAggregated1000(benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock(size_t txCount, benchmark::Bench& bench, uint32_t epoch_iters) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(txCount, 0, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -203,9 +203,9 @@ static void BLS_Verify_LargeAggregatedBlock1000(benchmark::Bench& bench) static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& bench) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(1000, 0, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -224,7 +224,7 @@ static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& ben // Benchmark. bench.minEpochIterations(10).run([&] { - BLSPublicKeyVector nonvalidatedPubKeys; + std::vector nonvalidatedPubKeys; std::vector nonvalidatedHashes; nonvalidatedPubKeys.reserve(pubKeys.size()); nonvalidatedHashes.reserve(msgHashes.size()); @@ -249,9 +249,9 @@ static void BLS_Verify_LargeAggregatedBlock1000PreVerified(benchmark::Bench& ben static void BLS_Verify_Batched(benchmark::Bench& bench) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid); @@ -266,8 +266,8 @@ static void BLS_Verify_Batched(benchmark::Bench& bench) return; } - BLSPublicKeyVector testPubKeys; - BLSSignatureVector testSigs; + std::vector testPubKeys; + std::vector testSigs; std::vector testMsgHashes; testPubKeys.reserve(batchSize); testSigs.reserve(batchSize); @@ -305,9 +305,9 @@ static void BLS_Verify_Batched(benchmark::Bench& bench) static void BLS_Verify_BatchedParallel(benchmark::Bench& bench) { - BLSPublicKeyVector pubKeys; - BLSSecretKeyVector secKeys; - BLSSignatureVector sigs; + std::vector pubKeys; + std::vector secKeys; + std::vector sigs; std::vector msgHashes; std::vector invalid; BuildTestVectors(1000, 10, pubKeys, secKeys, sigs, msgHashes, invalid); diff --git a/src/bench/bls_dkg.cpp b/src/bench/bls_dkg.cpp index 1721b31ad62df..bf9ce51338fb0 100644 --- a/src/bench/bls_dkg.cpp +++ b/src/bench/bls_dkg.cpp @@ -11,7 +11,7 @@ struct Member { CBLSId id; BLSVerificationVectorPtr vvec; - BLSSecretKeyVector skShares; + std::vector skShares; }; class DKG @@ -21,7 +21,7 @@ class DKG std::vector members; std::vector receivedVvecs; - BLSSecretKeyVector receivedSkShares; + std::vector receivedSkShares; BLSVerificationVectorPtr quorumVvec; CBLSWorker blsWorker; diff --git a/src/bls/bls.h b/src/bls/bls.h index 9cf66a9ea6176..714da5934e516 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -57,7 +57,7 @@ class CBLSWrapper static constexpr size_t SerSize = _SerSize; explicit CBLSWrapper() = default; - explicit CBLSWrapper(const std::vector& vecBytes) : CBLSWrapper() + explicit CBLSWrapper(Span vecBytes) : CBLSWrapper() { SetByteVector(vecBytes, bls::bls_legacy_scheme.load()); } @@ -103,7 +103,7 @@ class CBLSWrapper *(static_cast(this)) = C(); } - void SetByteVector(const std::vector& vecBytes, const bool specificLegacyScheme) + void SetByteVector(Span vecBytes, const bool specificLegacyScheme) { if (vecBytes.size() != SerSize) { Reset(); @@ -114,7 +114,7 @@ class CBLSWrapper Reset(); } else { try { - impl = ImplType::FromBytes(bls::Bytes(vecBytes), specificLegacyScheme); + impl = ImplType::FromBytes(bls::Bytes(vecBytes.data(), vecBytes.size()), specificLegacyScheme); fValid = true; } catch (...) { Reset(); @@ -179,7 +179,7 @@ class CBLSWrapper template inline void Unserialize(Stream& s, const bool specificLegacyScheme) { - std::vector vecBytes(SerSize, 0); + std::array vecBytes{}; s.read(reinterpret_cast(vecBytes.data()), SerSize); SetByteVector(vecBytes, specificLegacyScheme); @@ -586,13 +586,7 @@ class CBLSLazyPublicKeyVersionWrapper { }; #endif -using BLSIdVector = std::vector; -using BLSVerificationVector = std::vector; -using BLSPublicKeyVector = std::vector; -using BLSSecretKeyVector = std::vector; -using BLSSignatureVector = std::vector; - -using BLSVerificationVectorPtr = std::shared_ptr; +using BLSVerificationVectorPtr = std::shared_ptr>; bool BLSInit(); diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index dd60e68b44407..cb06ec3028508 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -13,7 +13,7 @@ #include template -bool VerifyVectorHelper(const std::vector& vec, size_t start, size_t count) +bool VerifyVectorHelper(Span vec, size_t start, size_t count) { if (start == 0 && count == 0) { count = vec.size(); @@ -74,10 +74,10 @@ void CBLSWorker::Stop() workerPool.stop(true); } -bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& ids, BLSVerificationVectorPtr& vvecRet, BLSSecretKeyVector& skSharesRet) +bool CBLSWorker::GenerateContributions(int quorumThreshold, Span ids, BLSVerificationVectorPtr& vvecRet, std::vector& skSharesRet) { - auto svec = BLSSecretKeyVector((size_t)quorumThreshold); - vvecRet = std::make_shared((size_t)quorumThreshold); + auto svec = std::vector((size_t)quorumThreshold); + vvecRet = std::make_shared>((size_t)quorumThreshold); skSharesRet.resize(ids.size()); for (int i = 0; i < quorumThreshold; i++) { @@ -147,7 +147,7 @@ struct Aggregator : public std::enable_shared_from_this> { // TP can either be a pointer or a reference template - Aggregator(const std::vector& _inputVec, + Aggregator(Span _inputVec, size_t start, size_t count, bool _parallel, ctpl::thread_pool& _workerPool, @@ -175,7 +175,7 @@ struct Aggregator : public std::enable_shared_from_this> { if (inputVec->size() == 1) { doneCallback(*(*inputVec)[0]); } else { - doneCallback(SyncAggregate(*inputVec, 0, inputVec->size())); + doneCallback(SyncAggregate(Span{*inputVec}, 0, inputVec->size())); } return; } @@ -187,7 +187,7 @@ struct Aggregator : public std::enable_shared_from_this> { if (inputVec->size() == 1) { doneCallback(*(*inputVec)[0]); } else { - doneCallback(SyncAggregate(*inputVec, 0, inputVec->size())); + doneCallback(SyncAggregate(Span{*inputVec}, 0, inputVec->size())); } }); return; @@ -236,7 +236,7 @@ struct Aggregator : public std::enable_shared_from_this> { r = *rem[0]; } else { // multiple intermediate results left which did not add up to a new batch. aggregate them now - r = SyncAggregate(rem, 0, rem.size()); + r = SyncAggregate(Span{rem}, 0, rem.size()); } // all items which are left in the queue are intermediate results, so we must delete them @@ -258,7 +258,7 @@ struct Aggregator : public std::enable_shared_from_this> { void SyncAggregateAndPushAggQueue(const std::shared_ptr>& vec, size_t start, size_t count, bool del) { // aggregate vec and push the intermediate result onto the work queue - PushAggQueue(SyncAggregate(*vec, start, count)); + PushAggQueue(SyncAggregate(Span{*vec}, start, count)); if (del) { for (size_t i = 0; i < count; i++) { delete (*vec)[start + i]; @@ -304,7 +304,7 @@ struct Aggregator : public std::enable_shared_from_this> { } template - T SyncAggregate(const std::vector& vec, size_t start, size_t count) + T SyncAggregate(Span vec, size_t start, size_t count) { T result = *vec[start]; for (size_t j = 1; j < count; j++) { @@ -336,11 +336,11 @@ struct VectorAggregator : public std::enable_shared_from_this; using VectorType = std::vector; using VectorPtrType = std::shared_ptr; - using VectorVectorType = std::vector; + using VectorVectorType = Span; using DoneCallback = std::function; DoneCallback doneCallback; - const VectorVectorType& vecs; + VectorVectorType vecs; size_t start; size_t count; bool parallel; @@ -351,7 +351,7 @@ struct VectorAggregator : public std::enable_shared_from_thisshared_from_this()); - auto aggregator = std::make_shared(std::move(tmp), 0, count, parallel, workerPool, [self, i](const T& agg) {self->CheckDone(agg, i);}); + auto aggregator = std::make_shared(Span{tmp}, 0, count, parallel, workerPool, [self, i](const T& agg) {self->CheckDone(agg, i);}); aggregator->Start(); } } @@ -412,8 +412,8 @@ struct ContributionVerifier : public std::enable_shared_from_this& vvecs; - const BLSSecretKeyVector& skShares; + Span vvecs; + Span skShares; size_t batchSize; bool parallel; bool aggregated; @@ -427,8 +427,8 @@ struct ContributionVerifier : public std::enable_shared_from_this verifyDoneCount{0}; std::function&)> doneCallback; - ContributionVerifier(CBLSId _forId, const std::vector& _vvecs, - const BLSSecretKeyVector& _skShares, size_t _batchSize, + ContributionVerifier(CBLSId _forId, Span _vvecs, + Span _skShares, size_t _batchSize, bool _parallel, bool _aggregated, ctpl::thread_pool& _workerPool, std::function&)> _doneCallback) : forId(std::move(_forId)), @@ -493,7 +493,7 @@ struct ContributionVerifier : public std::enable_shared_from_thisshared_from_this()); auto vvecAgg = std::make_shared>(vvecs, batchState.start, batchState.count, parallel, workerPool, [this, self, batchIdx] (const BLSVerificationVectorPtr& vvec) {HandleAggVvecDone(batchIdx, vvec);}); - auto skShareAgg = std::make_shared>(skShares, batchState.start, batchState.count, parallel, workerPool, [this, self, batchIdx] (const CBLSSecretKey& skShare) {HandleAggSkShareDone(batchIdx, skShare);}); + auto skShareAgg = std::make_shared>(Span{skShares}, batchState.start, batchState.count, parallel, workerPool, [this, self, batchIdx] (const CBLSSecretKey& skShare) {HandleAggSkShareDone(batchIdx, skShare);}); vvecAgg->Start(); skShareAgg->Start(); @@ -594,7 +594,7 @@ struct ContributionVerifier : public std::enable_shared_from_this& vvecs, +void CBLSWorker::AsyncBuildQuorumVerificationVector(Span vvecs, size_t start, size_t count, bool parallel, std::function doneCallback) { @@ -614,7 +614,7 @@ void CBLSWorker::AsyncBuildQuorumVerificationVector(const std::vectorStart(); } -std::future CBLSWorker::AsyncBuildQuorumVerificationVector(const std::vector& vvecs, +std::future CBLSWorker::AsyncBuildQuorumVerificationVector(Span vvecs, size_t start, size_t count, bool parallel) { auto p = BuildFutureDoneCallback(); @@ -622,7 +622,7 @@ std::future CBLSWorker::AsyncBuildQuorumVerificationVe return std::move(p.second); } -BLSVerificationVectorPtr CBLSWorker::BuildQuorumVerificationVector(const std::vector& vvecs, +BLSVerificationVectorPtr CBLSWorker::BuildQuorumVerificationVector(Span vvecs, size_t start, size_t count, bool parallel) { return AsyncBuildQuorumVerificationVector(vvecs, start, count, parallel).get(); @@ -630,7 +630,7 @@ BLSVerificationVectorPtr CBLSWorker::BuildQuorumVerificationVector(const std::ve template void AsyncAggregateHelper(ctpl::thread_pool& workerPool, - const std::vector& vec, size_t start, size_t count, bool parallel, + Span vec, size_t start, size_t count, bool parallel, std::function doneCallback) { if (start == 0 && count == 0) { @@ -649,14 +649,14 @@ void AsyncAggregateHelper(ctpl::thread_pool& workerPool, agg->Start(); } -void CBLSWorker::AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, +void CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, size_t start, size_t count, bool parallel, std::function doneCallback) { AsyncAggregateHelper(workerPool, secKeys, start, count, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, +std::future CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, size_t start, size_t count, bool parallel) { auto p = BuildFutureDoneCallback(); @@ -664,20 +664,20 @@ std::future CBLSWorker::AsyncAggregateSecretKeys(const BLSSecretK return std::move(p.second); } -CBLSSecretKey CBLSWorker::AggregateSecretKeys(const BLSSecretKeyVector& secKeys, +CBLSSecretKey CBLSWorker::AggregateSecretKeys(Span secKeys, size_t start, size_t count, bool parallel) { return AsyncAggregateSecretKeys(secKeys, start, count, parallel).get(); } -void CBLSWorker::AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, +void CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, size_t start, size_t count, bool parallel, std::function doneCallback) { AsyncAggregateHelper(workerPool, pubKeys, start, count, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, +std::future CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, size_t start, size_t count, bool parallel) { auto p = BuildFutureDoneCallback(); @@ -685,14 +685,14 @@ std::future CBLSWorker::AsyncAggregatePublicKeys(const BLSPublicK return std::move(p.second); } -void CBLSWorker::AsyncAggregateSigs(const BLSSignatureVector& sigs, +void CBLSWorker::AsyncAggregateSigs(Span sigs, size_t start, size_t count, bool parallel, std::function doneCallback) { AsyncAggregateHelper(workerPool, sigs, start, count, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregateSigs(const BLSSignatureVector& sigs, +std::future CBLSWorker::AsyncAggregateSigs(Span sigs, size_t start, size_t count, bool parallel) { auto p = BuildFutureDoneCallback(); @@ -707,7 +707,7 @@ CBLSPublicKey CBLSWorker::BuildPubKeyShare(const BLSVerificationVectorPtr& vvec, return pkShare; } -void CBLSWorker::AsyncVerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, +void CBLSWorker::AsyncVerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel, bool aggregated, std::function&)> doneCallback) { if (!forId.IsValid() || !VerifyVerificationVectors(vvecs)) { @@ -721,7 +721,7 @@ void CBLSWorker::AsyncVerifyContributionShares(const CBLSId& forId, const std::v verifier->Start(); } -std::future > CBLSWorker::AsyncVerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, +std::future > CBLSWorker::AsyncVerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel, bool aggregated) { auto p = BuildFutureDoneCallback >(); @@ -729,7 +729,7 @@ std::future > CBLSWorker::AsyncVerifyContributionShares(const return std::move(p.second); } -std::vector CBLSWorker::VerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, +std::vector CBLSWorker::VerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel, bool aggregated) { return AsyncVerifyContributionShares(forId, vvecs, skShares, parallel, aggregated).get(); @@ -757,12 +757,12 @@ std::future CBLSWorker::AsyncVerifyContributionShare(const CBLSId& forId, return workerPool.push(f); } -bool CBLSWorker::VerifyVerificationVector(const BLSVerificationVector& vvec, size_t start, size_t count) +bool CBLSWorker::VerifyVerificationVector(Span vvec, size_t start, size_t count) { return VerifyVectorHelper(vvec, start, count); } -bool CBLSWorker::VerifyVerificationVectors(const std::vector& vvecs, +bool CBLSWorker::VerifyVerificationVectors(Span vvecs, size_t start, size_t count) { if (start == 0 && count == 0) { diff --git a/src/bls/bls_worker.h b/src/bls/bls_worker.h index 25f137732b6e2..881f9af1b6db6 100644 --- a/src/bls/bls_worker.h +++ b/src/bls/bls_worker.h @@ -55,7 +55,7 @@ class CBLSWorker void Start(); void Stop(); - bool GenerateContributions(int threshold, const BLSIdVector& ids, BLSVerificationVectorPtr& vvecRet, BLSSecretKeyVector& skSharesRet); + bool GenerateContributions(int threshold, Span ids, BLSVerificationVectorPtr& vvecRet, std::vector& skSharesRet); // The following functions are all used to aggregate verification (public key) vectors // Inputs are in the following form: @@ -69,12 +69,12 @@ class CBLSWorker // [ a1+a2+a3+a4, b1+b2+b3+b4, c1+c2+c3+c4, d1+d2+d3+d4] // Multiple things can be parallelized here. For example, all 4 entries in the result vector can be calculated in parallel // Also, each individual vector can be split into multiple batches and aggregating the batches can also be parallelized. - void AsyncBuildQuorumVerificationVector(const std::vector& vvecs, + void AsyncBuildQuorumVerificationVector(Span vvecs, size_t start, size_t count, bool parallel, std::function doneCallback); - std::future AsyncBuildQuorumVerificationVector(const std::vector& vvecs, + std::future AsyncBuildQuorumVerificationVector(Span vvecs, size_t start, size_t count, bool parallel); - BLSVerificationVectorPtr BuildQuorumVerificationVector(const std::vector& vvecs, + BLSVerificationVectorPtr BuildQuorumVerificationVector(Span vvecs, size_t start = 0, size_t count = 0, bool parallel = true); // The following functions are all used to aggregate single vectors @@ -82,23 +82,23 @@ class CBLSWorker // [a, b, c, d], // The result is simply a+b+c+d // Aggregation is parallelized by splitting up the input vector into multiple batches and then aggregating the individual batch results - void AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, + void AsyncAggregateSecretKeys(Span, size_t start, size_t count, bool parallel, std::function doneCallback); - std::future AsyncAggregateSecretKeys(const BLSSecretKeyVector& secKeys, + std::future AsyncAggregateSecretKeys(Span secKeys, size_t start, size_t count, bool parallel); - CBLSSecretKey AggregateSecretKeys(const BLSSecretKeyVector& secKeys, size_t start = 0, size_t count = 0, bool parallel = true); + CBLSSecretKey AggregateSecretKeys(Span secKeys, size_t start = 0, size_t count = 0, bool parallel = true); - void AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, + void AsyncAggregatePublicKeys(Span pubKeys, size_t start, size_t count, bool parallel, std::function doneCallback); - std::future AsyncAggregatePublicKeys(const BLSPublicKeyVector& pubKeys, + std::future AsyncAggregatePublicKeys(Span pubKeys, size_t start, size_t count, bool parallel); - void AsyncAggregateSigs(const BLSSignatureVector& sigs, + void AsyncAggregateSigs(Span sigs, size_t start, size_t count, bool parallel, std::function doneCallback); - std::future AsyncAggregateSigs(const BLSSignatureVector& sigs, + std::future AsyncAggregateSigs(Span sigs, size_t start, size_t count, bool parallel); // Calculate public key share from public key vector and id. Not parallelized @@ -110,18 +110,18 @@ class CBLSWorker // result per batch is a single aggregated verification vector and a single aggregated contribution, which are then // verified with VerifyContributionShare. If verification of the aggregated inputs is successful, the whole batch // is marked as valid. If the batch verification fails, the individual entries are verified in a non-aggregated manner - void AsyncVerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, + void AsyncVerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel, bool aggregated, std::function&)> doneCallback); - std::future > AsyncVerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, + std::future > AsyncVerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel, bool aggregated); - std::vector VerifyContributionShares(const CBLSId& forId, const std::vector& vvecs, const BLSSecretKeyVector& skShares, + std::vector VerifyContributionShares(const CBLSId& forId, Span vvecs, Span skShares, bool parallel = true, bool aggregated = true); std::future AsyncVerifyContributionShare(const CBLSId& forId, const BLSVerificationVectorPtr& vvec, const CBLSSecretKey& skContribution); // Simple verification of vectors. Checks x.IsValid() for every entry and checks for duplicate entries - static bool VerifyVerificationVector(const BLSVerificationVector& vvec, size_t start = 0, size_t count = 0); - static bool VerifyVerificationVectors(const std::vector& vvecs, size_t start = 0, size_t count = 0); + static bool VerifyVerificationVector(Span vvec, size_t start = 0, size_t count = 0); + static bool VerifyVerificationVectors(Span vvecs, size_t start = 0, size_t count = 0); // Internally batched signature signing and verification void AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, const SignDoneCallback& doneCallback); @@ -151,13 +151,13 @@ class CBLSWorkerCache explicit CBLSWorkerCache(CBLSWorker& _worker) : worker(_worker) {} - BLSVerificationVectorPtr BuildQuorumVerificationVector(const uint256& cacheKey, const std::vector& vvecs) + BLSVerificationVectorPtr BuildQuorumVerificationVector(const uint256& cacheKey, Span vvecs) { return GetOrBuild(cacheKey, vvecCache, [this, &vvecs]() { return worker.BuildQuorumVerificationVector(vvecs); }); } - CBLSSecretKey AggregateSecretKeys(const uint256& cacheKey, const BLSSecretKeyVector& skShares) + CBLSSecretKey AggregateSecretKeys(const uint256& cacheKey, Span skShares) { return GetOrBuild(cacheKey, secretKeyShareCache, [this, &skShares]() { return worker.AggregateSecretKeys(skShares); diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 61a5f65159078..f64aedc304c03 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -65,7 +65,7 @@ bool CCoinJoinQueue::Sign() bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const { bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip()); - if (!CBLSSignature(vchSig).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) { + if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) { LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n"); return false; } @@ -114,7 +114,7 @@ bool CCoinJoinBroadcastTx::Sign() bool CCoinJoinBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const { bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip()); - if (!CBLSSignature(vchSig).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) { + if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) { LogPrint(BCLog::COINJOIN, "CCoinJoinBroadcastTx::CheckSignature -- VerifyInsecure() failed\n"); return false; } diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index 7dd4d8e9b8c26..319902522485a 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -211,11 +211,11 @@ bool CSimplifiedMNListDiff::BuildQuorumChainlockInfo(const CBlockIndex* blockInd sig = cbcl.value().first; } // Get the range of indexes (values) for the current key and merge them into a single std::set - const auto [begin, end] = workBaseBlockIndexMap.equal_range(it->first); + const auto [it_begin, it_end] = workBaseBlockIndexMap.equal_range(it->first); std::set idx_set; - std::transform(begin, end, std::inserter(idx_set, idx_set.end()), [](const auto& pair) { return pair.second; }); + std::transform(it_begin, it_end, std::inserter(idx_set, idx_set.end()), [](const auto& pair) { return pair.second; }); // Advance the iterator to the next key - it = end; + it = it_end; // Different CBlockIndex can contain the same CL sig in CbTx (both non-null or null during the first blocks after v20 activation) // Hence, we need to merge the std::set if another std::set already exists for the same sig. diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index 9586b7df5d3f8..88b11ef3a41bb 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -57,7 +57,7 @@ CDKGMember::CDKGMember(const CDeterministicMNCPtr& _dmn, size_t _idx) : } -bool CDKGSession::Init(const CBlockIndex* _pQuorumBaseBlockIndex, const std::vector& mns, const uint256& _myProTxHash, int _quorumIndex) +bool CDKGSession::Init(const CBlockIndex* _pQuorumBaseBlockIndex, Span mns, const uint256& _myProTxHash, int _quorumIndex) { m_quorum_base_block_index = _pQuorumBaseBlockIndex; quorumIndex = _quorumIndex; @@ -353,7 +353,7 @@ void CDKGSession::VerifyPendingContributions() std::vector memberIndexes; std::vector vvecs; - BLSSecretKeyVector skContributions; + std::vector skContributions; for (const auto& idx : pend) { const auto& m = members[idx]; @@ -941,7 +941,7 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages) cxxtimer::Timer t1(true); std::vector memberIndexes; std::vector vvecs; - BLSSecretKeyVector skContributions; + std::vector skContributions; if (!dkgManager.GetVerifiedContributions(params.type, m_quorum_base_block_index, qc.validMembers, memberIndexes, vvecs, skContributions)) { logger.Batch("failed to get valid contributions"); return; @@ -1106,7 +1106,7 @@ void CDKGSession::ReceiveMessage(const CDKGPrematureCommitment& qc, bool& retBan std::vector memberIndexes; std::vector vvecs; - BLSSecretKeyVector skContributions; + std::vector skContributions; BLSVerificationVectorPtr quorumVvec; if (dkgManager.GetVerifiedContributions(params.type, m_quorum_base_block_index, qc.validMembers, memberIndexes, vvecs, skContributions)) { quorumVvec = cache.BuildQuorumVerificationVector(::SerializeHash(memberIndexes), vvecs); diff --git a/src/llmq/dkgsession.h b/src/llmq/dkgsession.h index bea6b441c8d0a..6989b8e2ae66a 100644 --- a/src/llmq/dkgsession.h +++ b/src/llmq/dkgsession.h @@ -59,7 +59,7 @@ class CDKGContribution template inline void Unserialize(Stream& s) { - BLSVerificationVector tmp1; + std::vector tmp1; CBLSIESMultiRecipientObjects tmp2; s >> llmqType; @@ -69,7 +69,7 @@ class CDKGContribution s >> tmp2; s >> sig; - vvec = std::make_shared(std::move(tmp1)); + vvec = std::make_shared>(std::move(tmp1)); contributions = std::make_shared>(std::move(tmp2)); } @@ -273,12 +273,12 @@ class CDKGSession std::map membersMap; std::set relayMembers; BLSVerificationVectorPtr vvecContribution; - BLSSecretKeyVector m_sk_contributions; + std::vector m_sk_contributions; - BLSIdVector memberIds; + std::vector memberIds; std::vector receivedVvecs; // these are not necessarily verified yet. Only trust in what was written to the DB - BLSSecretKeyVector receivedSkContributions; + std::vector receivedSkContributions; /// Contains the received unverified/encrypted DKG contributions std::vector>> vecEncryptedContributions; @@ -307,7 +307,7 @@ class CDKGSession CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager, CDKGDebugManager& _dkgDebugManager, CConnman& _connman) : params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager), dkgDebugManager(_dkgDebugManager), connman(_connman) {} - bool Init(const CBlockIndex* pQuorumBaseBlockIndex, const std::vector& mns, const uint256& _myProTxHash, int _quorumIndex); + bool Init(const CBlockIndex* pQuorumBaseBlockIndex, Span mns, const uint256& _myProTxHash, int _quorumIndex); [[nodiscard]] std::optional GetMyMemberIndex() const { return myIdx; } diff --git a/src/llmq/dkgsessionmgr.cpp b/src/llmq/dkgsessionmgr.cpp index 81b59dead2b2d..fb20dbb36889d 100644 --- a/src/llmq/dkgsessionmgr.cpp +++ b/src/llmq/dkgsessionmgr.cpp @@ -67,7 +67,7 @@ void CDKGSessionManager::MigrateDKG() while (pcursor->Valid()) { decltype(start_vvec) k; - BLSVerificationVector v; + std::vector v; if (!pcursor->GetKey(k) || std::get<0>(k) != DB_VVEC) { break; @@ -377,7 +377,7 @@ void CDKGSessionManager::WriteEncryptedContributions(Consensus::LLMQType llmqTyp db->Write(std::make_tuple(DB_ENC_CONTRIB, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), contributions); } -bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, BLSSecretKeyVector& skContributionsRet) const +bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, std::vector& skContributionsRet) const { LOCK(contributionsCacheCs); auto members = utils::GetAllQuorumMembers(llmqType, pQuorumBaseBlockIndex); @@ -394,7 +394,7 @@ bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, ContributionsCacheKey cacheKey = {llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash}; auto it = contributionsCache.find(cacheKey); if (it == contributionsCache.end()) { - auto vvecPtr = std::make_shared(); + auto vvecPtr = std::make_shared>(); CBLSSecretKey skContribution; if (!db->Read(std::make_tuple(DB_VVEC, llmqType, pQuorumBaseBlockIndex->GetBlockHash(), proTxHash), *vvecPtr)) { return false; diff --git a/src/llmq/dkgsessionmgr.h b/src/llmq/dkgsessionmgr.h index 9b8c45b42485a..d90162ccf2fdc 100644 --- a/src/llmq/dkgsessionmgr.h +++ b/src/llmq/dkgsessionmgr.h @@ -83,7 +83,7 @@ class CDKGSessionManager // Contributions are written while in the DKG void WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec); void WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const CBLSSecretKey& skContribution); - bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, BLSSecretKeyVector& skContributionsRet) const; + bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const std::vector& validMembers, std::vector& memberIndexesRet, std::vector& vvecsRet, std::vector& skContributionsRet) const; /// Write encrypted (unverified) DKG contributions for the member with the given proTxHash to the llmqDb void WriteEncryptedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, const uint256& proTxHash, const CBLSIESMultiRecipientObjects& contributions); /// Read encrypted (unverified) DKG contributions for the member with the given proTxHash from the llmqDb diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index e0e44bbf805cd..23553aca95539 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -76,15 +76,15 @@ CQuorum::CQuorum(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker) : { } -void CQuorum::Init(CFinalCommitmentPtr _qc, const CBlockIndex* _pQuorumBaseBlockIndex, const uint256& _minedBlockHash, const std::vector& _members) +void CQuorum::Init(CFinalCommitmentPtr _qc, const CBlockIndex* _pQuorumBaseBlockIndex, const uint256& _minedBlockHash, Span _members) { qc = std::move(_qc); m_quorum_base_block_index = _pQuorumBaseBlockIndex; - members = _members; + members = std::vector(_members.begin(), _members.end()); minedBlockHash = _minedBlockHash; } -bool CQuorum::SetVerificationVector(const BLSVerificationVector& quorumVecIn) +bool CQuorum::SetVerificationVector(const std::vector& quorumVecIn) { const auto quorumVecInSerialized = ::SerializeHash(quorumVecIn); @@ -92,7 +92,7 @@ bool CQuorum::SetVerificationVector(const BLSVerificationVector& quorumVecIn) if (quorumVecInSerialized != qc->quorumVvecHash) { return false; } - quorumVvec = std::make_shared(quorumVecIn); + quorumVvec = std::make_shared>(quorumVecIn); return true; } @@ -173,9 +173,9 @@ bool CQuorum::ReadContributions(CEvoDB& evoDb) { uint256 dbKey = MakeQuorumKey(*this); - BLSVerificationVector qv; + std::vector qv; if (evoDb.Read(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), qv)) { - WITH_LOCK(cs, quorumVvec = std::make_shared(std::move(qv))); + WITH_LOCK(cs, quorumVvec = std::make_shared>(std::move(qv))); } else { return false; } @@ -411,7 +411,7 @@ bool CQuorumManager::BuildQuorumContributions(const CFinalCommitmentPtr& fqc, co { std::vector memberIndexes; std::vector vvecs; - BLSSecretKeyVector skContributions; + std::vector skContributions; if (!dkgManager.GetVerifiedContributions((Consensus::LLMQType)fqc->llmqType, quorum->m_quorum_base_block_index, fqc->validMembers, memberIndexes, vvecs, skContributions)) { return false; } @@ -766,7 +766,7 @@ void CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, C // Check if request has QUORUM_VERIFICATION_VECTOR data if (request.GetDataMask() & CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR) { - BLSVerificationVector verificationVector; + std::vector verificationVector; vRecv >> verificationVector; if (pQuorum->SetVerificationVector(verificationVector)) { @@ -794,7 +794,7 @@ void CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, C std::vector> vecEncrypted; vRecv >> vecEncrypted; - BLSSecretKeyVector vecSecretKeys; + std::vector vecSecretKeys; vecSecretKeys.resize(vecEncrypted.size()); auto secret = WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator); for (const auto i : irange::range(vecEncrypted.size())) { diff --git a/src/llmq/quorums.h b/src/llmq/quorums.h index 128b2ffdcb207..6be46ef7656e4 100644 --- a/src/llmq/quorums.h +++ b/src/llmq/quorums.h @@ -189,9 +189,9 @@ class CQuorum public: CQuorum(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker); ~CQuorum() = default; - void Init(CFinalCommitmentPtr _qc, const CBlockIndex* _pQuorumBaseBlockIndex, const uint256& _minedBlockHash, const std::vector& _members); + void Init(CFinalCommitmentPtr _qc, const CBlockIndex* _pQuorumBaseBlockIndex, const uint256& _minedBlockHash, Span _members); - bool SetVerificationVector(const BLSVerificationVector& quorumVecIn); + bool SetVerificationVector(const std::vector& quorumVecIn); bool SetSecretKeyShare(const CBLSSecretKey& secretKeyShare); bool HasVerificationVector() const; diff --git a/src/llmq/snapshot.cpp b/src/llmq/snapshot.cpp index aa96a60db02a8..c5509d3ef4384 100644 --- a/src/llmq/snapshot.cpp +++ b/src/llmq/snapshot.cpp @@ -321,7 +321,7 @@ bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotat return true; } -uint256 GetLastBaseBlockHash(const std::vector& baseBlockIndexes, const CBlockIndex* blockIndex) +uint256 GetLastBaseBlockHash(Span baseBlockIndexes, const CBlockIndex* blockIndex) { uint256 hash; for (const auto baseBlock : baseBlockIndexes) { diff --git a/src/llmq/snapshot.h b/src/llmq/snapshot.h index c4117b954dd55..234ab766b7120 100644 --- a/src/llmq/snapshot.h +++ b/src/llmq/snapshot.h @@ -209,7 +209,7 @@ class CQuorumRotationInfo bool BuildQuorumRotationInfo(const CGetQuorumRotationInfo& request, CQuorumRotationInfo& response, const CQuorumManager& qman, const CQuorumBlockProcessor& quorumBlockProcessor, std::string& errorRet); -uint256 GetLastBaseBlockHash(const std::vector& baseBlockIndexes, const CBlockIndex* blockIndex); +uint256 GetLastBaseBlockHash(Span baseBlockIndexes, const CBlockIndex* blockIndex); class CQuorumSnapshotManager { diff --git a/src/span.h b/src/span.h index fafa5faa0322b..6ad3da60e309d 100644 --- a/src/span.h +++ b/src/span.h @@ -280,4 +280,14 @@ template constexpr auto UCharSpanCast(Span s) -> Span constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward(v)})) { return UCharSpanCast(Span{std::forward(v)}); } +template +[[nodiscard]] constexpr auto begin(const Span& span) noexcept -> C* { + return span.begin(); +} + +template +[[nodiscard]] constexpr auto end(const Span& span) noexcept -> C* { + return span.end(); +} + #endif