From 1c66ac3f2a0e61a22e627d05fbfc36ecd85b5dc6 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 5 Oct 2023 20:06:27 +0700 Subject: [PATCH] refactor: remove start/count in bls-worker funcs due to spanification (#5599) ## Issue being fixed or feature implemented Follow-up changes for this PR: https://github.com/dashpay/dash/pull/5586/ ## What was done? Span has already "pointer + start + length", extra start/count variables in function signatures are just duplicates. ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes N/A ## Checklist: - [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 --- src/bench/bls_dkg.cpp | 2 +- src/bls/bls_worker.cpp | 123 +++++++++++++++-------------------------- src/bls/bls_worker.h | 32 ++++------- 3 files changed, 58 insertions(+), 99 deletions(-) diff --git a/src/bench/bls_dkg.cpp b/src/bench/bls_dkg.cpp index bf9ce51338fb0..a0bc685175f7e 100644 --- a/src/bench/bls_dkg.cpp +++ b/src/bench/bls_dkg.cpp @@ -82,7 +82,7 @@ class DKG ReceiveVvecs(); bench.minEpochIterations(epoch_iters).run([&] { - quorumVvec = blsWorker.BuildQuorumVerificationVector(receivedVvecs, 0, 0, false); + quorumVvec = blsWorker.BuildQuorumVerificationVector(receivedVvecs, false); }); } diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index cb06ec3028508..77986a288c903 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -13,17 +13,14 @@ #include template -bool VerifyVectorHelper(Span vec, size_t start, size_t count) +bool VerifyVectorHelper(Span vec) { - if (start == 0 && count == 0) { - count = vec.size(); - } std::set set; - for (size_t i = start; i < start + count; i++) { - if (!vec[i].IsValid()) + for (auto item : vec) { + if (!item.IsValid()) return false; // check duplicates - if (!set.emplace(vec[i].GetHash()).second) { + if (!set.emplace(item.GetHash()).second) { return false; } } @@ -147,18 +144,16 @@ struct Aggregator : public std::enable_shared_from_this> { // TP can either be a pointer or a reference template - Aggregator(Span _inputVec, - size_t start, size_t count, - bool _parallel, + Aggregator(Span _inputSpan, bool _parallel, ctpl::thread_pool& _workerPool, DoneCallback _doneCallback) : - inputVec(std::make_shared>(count)), + inputVec(std::make_shared>(_inputSpan.size())), parallel(_parallel), workerPool(_workerPool), doneCallback(std::move(_doneCallback)) { - for (size_t i = 0; i < count; i++) { - (*inputVec)[i] = pointer(_inputVec[start + i]); + for (size_t i = 0; i < _inputSpan.size(); i++) { + (*inputVec)[i] = pointer(_inputSpan[i]); } } @@ -341,8 +336,6 @@ struct VectorAggregator : public std::enable_shared_from_this tmp(count); - for (size_t j = 0; j < count; j++) { - tmp[j] = &(*vecs[start + j])[i]; + std::vector tmp(vecs.size()); + for (size_t j = 0; j < vecs.size(); j++) { + tmp[j] = &(*vecs[j])[i]; } auto self(this->shared_from_this()); - auto aggregator = std::make_shared(Span{tmp}, 0, count, parallel, workerPool, [self, i](const T& agg) {self->CheckDone(agg, i);}); + auto aggregator = std::make_shared(Span{tmp}, parallel, workerPool, [self, i](const T& agg) {self->CheckDone(agg, i);}); aggregator->Start(); } } @@ -492,8 +482,8 @@ 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>(Span{skShares}, batchState.start, batchState.count, parallel, workerPool, [this, self, batchIdx] (const CBLSSecretKey& skShare) {HandleAggSkShareDone(batchIdx, skShare);}); + auto vvecAgg = std::make_shared>(vvecs.subspan(batchState.start, batchState.count), parallel, workerPool, [this, self, batchIdx] (const BLSVerificationVectorPtr& vvec) {HandleAggVvecDone(batchIdx, vvec);}); + auto skShareAgg = std::make_shared>(Span{skShares}.subspan(batchState.start, batchState.count), parallel, workerPool, [this, self, batchIdx] (const CBLSSecretKey& skShare) {HandleAggSkShareDone(batchIdx, skShare);}); vvecAgg->Start(); skShareAgg->Start(); @@ -594,109 +584,92 @@ struct ContributionVerifier : public std::enable_shared_from_this vvecs, - size_t start, size_t count, bool parallel, +void CBLSWorker::AsyncBuildQuorumVerificationVector(Span vvecs, bool parallel, std::function doneCallback) { - if (start == 0 && count == 0) { - count = vvecs.size(); - } - if (vvecs.empty() || count == 0 || start > vvecs.size() || start + count > vvecs.size()) { + if (vvecs.empty()) { doneCallback(nullptr); return; } - if (!VerifyVerificationVectors(vvecs, start, count)) { + if (!VerifyVerificationVectors(vvecs)) { doneCallback(nullptr); return; } - auto agg = std::make_shared>(vvecs, start, count, parallel, workerPool, std::move(doneCallback)); + auto agg = std::make_shared>(vvecs, parallel, workerPool, std::move(doneCallback)); agg->Start(); } -std::future CBLSWorker::AsyncBuildQuorumVerificationVector(Span vvecs, - size_t start, size_t count, bool parallel) +std::future CBLSWorker::AsyncBuildQuorumVerificationVector(Span vvecs, bool parallel) { auto p = BuildFutureDoneCallback(); - AsyncBuildQuorumVerificationVector(vvecs, start, count, parallel, std::move(p.first)); + AsyncBuildQuorumVerificationVector(vvecs, parallel, std::move(p.first)); return std::move(p.second); } -BLSVerificationVectorPtr CBLSWorker::BuildQuorumVerificationVector(Span vvecs, - size_t start, size_t count, bool parallel) +BLSVerificationVectorPtr CBLSWorker::BuildQuorumVerificationVector(Span vvecs, bool parallel) { - return AsyncBuildQuorumVerificationVector(vvecs, start, count, parallel).get(); + return AsyncBuildQuorumVerificationVector(vvecs, parallel).get(); } template -void AsyncAggregateHelper(ctpl::thread_pool& workerPool, - Span vec, size_t start, size_t count, bool parallel, +void AsyncAggregateHelper(ctpl::thread_pool& workerPool, Span vec, bool parallel, std::function doneCallback) { - if (start == 0 && count == 0) { - count = vec.size(); - } - if (vec.empty() || count == 0 || start > vec.size() || start + count > vec.size()) { + if (vec.empty()) { doneCallback(T()); return; } - if (!VerifyVectorHelper(vec, start, count)) { + if (!VerifyVectorHelper(vec)) { doneCallback(T()); return; } - auto agg = std::make_shared>(vec, start, count, parallel, workerPool, std::move(doneCallback)); + auto agg = std::make_shared>(vec, parallel, workerPool, std::move(doneCallback)); agg->Start(); } -void CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, - size_t start, size_t count, bool parallel, +void CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, secKeys, start, count, parallel, std::move(doneCallback)); + AsyncAggregateHelper(workerPool, secKeys, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, - size_t start, size_t count, bool parallel) +std::future CBLSWorker::AsyncAggregateSecretKeys(Span secKeys, bool parallel) { auto p = BuildFutureDoneCallback(); - AsyncAggregateSecretKeys(secKeys, start, count, parallel, std::move(p.first)); + AsyncAggregateSecretKeys(secKeys, parallel, std::move(p.first)); return std::move(p.second); } -CBLSSecretKey CBLSWorker::AggregateSecretKeys(Span secKeys, - size_t start, size_t count, bool parallel) +CBLSSecretKey CBLSWorker::AggregateSecretKeys(Span secKeys, bool parallel) { - return AsyncAggregateSecretKeys(secKeys, start, count, parallel).get(); + return AsyncAggregateSecretKeys(secKeys, parallel).get(); } -void CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, - size_t start, size_t count, bool parallel, +void CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, pubKeys, start, count, parallel, std::move(doneCallback)); + AsyncAggregateHelper(workerPool, pubKeys, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, - size_t start, size_t count, bool parallel) +std::future CBLSWorker::AsyncAggregatePublicKeys(Span pubKeys, bool parallel) { auto p = BuildFutureDoneCallback(); - AsyncAggregatePublicKeys(pubKeys, start, count, parallel, std::move(p.first)); + AsyncAggregatePublicKeys(pubKeys, parallel, std::move(p.first)); return std::move(p.second); } -void CBLSWorker::AsyncAggregateSigs(Span sigs, - size_t start, size_t count, bool parallel, +void CBLSWorker::AsyncAggregateSigs(Span sigs, bool parallel, std::function doneCallback) { - AsyncAggregateHelper(workerPool, sigs, start, count, parallel, std::move(doneCallback)); + AsyncAggregateHelper(workerPool, sigs, parallel, std::move(doneCallback)); } -std::future CBLSWorker::AsyncAggregateSigs(Span sigs, - size_t start, size_t count, bool parallel) +std::future CBLSWorker::AsyncAggregateSigs(Span sigs, bool parallel) { auto p = BuildFutureDoneCallback(); - AsyncAggregateSigs(sigs, start, count, parallel, std::move(p.first)); + AsyncAggregateSigs(sigs, parallel, std::move(p.first)); return std::move(p.second); } @@ -757,25 +730,19 @@ std::future CBLSWorker::AsyncVerifyContributionShare(const CBLSId& forId, return workerPool.push(f); } -bool CBLSWorker::VerifyVerificationVector(Span vvec, size_t start, size_t count) +bool CBLSWorker::VerifyVerificationVector(Span vvec) { - return VerifyVectorHelper(vvec, start, count); + return VerifyVectorHelper(vvec); } -bool CBLSWorker::VerifyVerificationVectors(Span vvecs, - size_t start, size_t count) +bool CBLSWorker::VerifyVerificationVectors(Span vvecs) { - if (start == 0 && count == 0) { - count = vvecs.size(); - } - std::set set; - for (size_t i = 0; i < count; i++) { - const auto& vvec = vvecs[start + i]; + for (const auto& vvec : vvecs) { if (vvec == nullptr) { return false; } - if (vvec->size() != vvecs[start]->size()) { + if (vvec->size() != vvecs[0]->size()) { return false; } for (size_t j = 0; j < vvec->size(); j++) { diff --git a/src/bls/bls_worker.h b/src/bls/bls_worker.h index 881f9af1b6db6..6ed2e456e7d1b 100644 --- a/src/bls/bls_worker.h +++ b/src/bls/bls_worker.h @@ -69,13 +69,10 @@ 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(Span vvecs, - size_t start, size_t count, bool parallel, + void AsyncBuildQuorumVerificationVector(Span vvecs, bool parallel, std::function doneCallback); - std::future AsyncBuildQuorumVerificationVector(Span vvecs, - size_t start, size_t count, bool parallel); - BLSVerificationVectorPtr BuildQuorumVerificationVector(Span vvecs, - size_t start = 0, size_t count = 0, bool parallel = true); + std::future AsyncBuildQuorumVerificationVector(Span vvecs, bool parallel); + BLSVerificationVectorPtr BuildQuorumVerificationVector(Span vvecs, bool parallel = true); // The following functions are all used to aggregate single vectors // Inputs are in the following form: @@ -83,23 +80,18 @@ class CBLSWorker // 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(Span, - size_t start, size_t count, bool parallel, + bool parallel, std::function doneCallback); - std::future AsyncAggregateSecretKeys(Span secKeys, - size_t start, size_t count, bool parallel); - CBLSSecretKey AggregateSecretKeys(Span secKeys, size_t start = 0, size_t count = 0, bool parallel = true); + std::future AsyncAggregateSecretKeys(Span secKeys, bool parallel); + CBLSSecretKey AggregateSecretKeys(Span secKeys, bool parallel = true); - void AsyncAggregatePublicKeys(Span pubKeys, - size_t start, size_t count, bool parallel, + void AsyncAggregatePublicKeys(Span pubKeys, bool parallel, std::function doneCallback); - std::future AsyncAggregatePublicKeys(Span pubKeys, - size_t start, size_t count, bool parallel); + std::future AsyncAggregatePublicKeys(Span pubKeys, bool parallel); - void AsyncAggregateSigs(Span sigs, - size_t start, size_t count, bool parallel, + void AsyncAggregateSigs(Span sigs, bool parallel, std::function doneCallback); - std::future AsyncAggregateSigs(Span sigs, - size_t start, size_t count, bool parallel); + std::future AsyncAggregateSigs(Span sigs, bool parallel); // Calculate public key share from public key vector and id. Not parallelized static CBLSPublicKey BuildPubKeyShare(const BLSVerificationVectorPtr& vvec, const CBLSId& id); @@ -120,8 +112,8 @@ class CBLSWorker 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(Span vvec, size_t start = 0, size_t count = 0); - static bool VerifyVerificationVectors(Span vvecs, size_t start = 0, size_t count = 0); + static bool VerifyVerificationVector(Span vvec); + static bool VerifyVerificationVectors(Span vvecs); // Internally batched signature signing and verification void AsyncSign(const CBLSSecretKey& secKey, const uint256& msgHash, const SignDoneCallback& doneCallback);