forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
quorums.cpp
1281 lines (1102 loc) · 54.6 KB
/
quorums.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018-2024 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <llmq/quorums.h>
#include <llmq/commitment.h>
#include <llmq/blockprocessor.h>
#include <llmq/dkgsession.h>
#include <llmq/dkgsessionmgr.h>
#include <llmq/options.h>
#include <llmq/params.h>
#include <llmq/utils.h>
#include <evo/deterministicmns.h>
#include <evo/evodb.h>
#include <evo/specialtx.h>
#include <chainparams.h>
#include <dbwrapper.h>
#include <masternode/node.h>
#include <masternode/sync.h>
#include <net.h>
#include <netmessagemaker.h>
#include <univalue.h>
#include <util/irange.h>
#include <util/time.h>
#include <util/underlying.h>
#include <validation.h>
#include <cxxtimer.hpp>
namespace llmq
{
static const std::string DB_QUORUM_SK_SHARE = "q_Qsk";
static const std::string DB_QUORUM_QUORUM_VVEC = "q_Qqvvec";
RecursiveMutex cs_data_requests;
static std::unordered_map<CQuorumDataRequestKey, CQuorumDataRequest, StaticSaltedHasher> mapQuorumDataRequests GUARDED_BY(cs_data_requests);
// forward declaration to avoid circular dependency
uint256 BuildSignHash(Consensus::LLMQType llmqType, const uint256& quorumHash, const uint256& id, const uint256& msgHash);
static uint256 MakeQuorumKey(const CQuorum& q)
{
CHashWriter hw(SER_NETWORK, 0);
hw << q.params.type;
hw << q.qc->quorumHash;
for (const auto& dmn : q.members) {
hw << dmn->proTxHash;
}
return hw.GetHash();
}
std::string CQuorumDataRequest::GetErrorString() const
{
switch (nError) {
case (Errors::NONE):
return "NONE";
case (Errors::QUORUM_TYPE_INVALID):
return "QUORUM_TYPE_INVALID";
case (Errors::QUORUM_BLOCK_NOT_FOUND):
return "QUORUM_BLOCK_NOT_FOUND";
case (Errors::QUORUM_NOT_FOUND):
return "QUORUM_NOT_FOUND";
case (Errors::MASTERNODE_IS_NO_MEMBER):
return "MASTERNODE_IS_NO_MEMBER";
case (Errors::QUORUM_VERIFICATION_VECTOR_MISSING):
return "QUORUM_VERIFICATION_VECTOR_MISSING";
case (Errors::ENCRYPTED_CONTRIBUTIONS_MISSING):
return "ENCRYPTED_CONTRIBUTIONS_MISSING";
case (Errors::UNDEFINED):
return "UNDEFINED";
default:
return "UNDEFINED";
}
return "UNDEFINED";
}
CQuorum::CQuorum(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker) : params(_params), blsCache(_blsWorker)
{
}
void CQuorum::Init(CFinalCommitmentPtr _qc, const CBlockIndex* _pQuorumBaseBlockIndex, const uint256& _minedBlockHash, Span<CDeterministicMNCPtr> _members)
{
qc = std::move(_qc);
m_quorum_base_block_index = _pQuorumBaseBlockIndex;
members = std::vector(_members.begin(), _members.end());
minedBlockHash = _minedBlockHash;
}
bool CQuorum::SetVerificationVector(const std::vector<CBLSPublicKey>& quorumVecIn)
{
const auto quorumVecInSerialized = ::SerializeHash(quorumVecIn);
LOCK(cs_vvec_shShare);
if (quorumVecInSerialized != qc->quorumVvecHash) {
return false;
}
quorumVvec = std::make_shared<std::vector<CBLSPublicKey>>(quorumVecIn);
return true;
}
bool CQuorum::SetSecretKeyShare(const CBLSSecretKey& secretKeyShare, const CActiveMasternodeManager& mn_activeman)
{
if (!secretKeyShare.IsValid() || (secretKeyShare.GetPublicKey() != GetPubKeyShare(GetMemberIndex(mn_activeman.GetProTxHash())))) {
return false;
}
LOCK(cs_vvec_shShare);
skShare = secretKeyShare;
return true;
}
bool CQuorum::IsMember(const uint256& proTxHash) const
{
return ranges::any_of(members, [&proTxHash](const auto& dmn){
return dmn->proTxHash == proTxHash;
});
}
bool CQuorum::IsValidMember(const uint256& proTxHash) const
{
for (const auto i : irange::range(members.size())) {
// cppcheck-suppress useStlAlgorithm
if (members[i]->proTxHash == proTxHash) {
return qc->validMembers[i];
}
}
return false;
}
CBLSPublicKey CQuorum::GetPubKeyShare(size_t memberIdx) const
{
LOCK(cs_vvec_shShare);
if (!HasVerificationVectorInternal() || memberIdx >= members.size() || !qc->validMembers[memberIdx]) {
return CBLSPublicKey();
}
const auto& m = members[memberIdx];
return blsCache.BuildPubKeyShare(m->proTxHash, quorumVvec, CBLSId(m->proTxHash));
}
bool CQuorum::HasVerificationVector() const {
LOCK(cs_vvec_shShare);
return HasVerificationVectorInternal();
}
bool CQuorum::HasVerificationVectorInternal() const {
AssertLockHeld(cs_vvec_shShare);
return quorumVvec != nullptr;
}
CBLSSecretKey CQuorum::GetSkShare() const
{
LOCK(cs_vvec_shShare);
return skShare;
}
int CQuorum::GetMemberIndex(const uint256& proTxHash) const
{
for (const auto i : irange::range(members.size())) {
// cppcheck-suppress useStlAlgorithm
if (members[i]->proTxHash == proTxHash) {
return int(i);
}
}
return -1;
}
void CQuorum::WriteContributions(CDBWrapper& db) const
{
uint256 dbKey = MakeQuorumKey(*this);
LOCK(cs_vvec_shShare);
if (HasVerificationVectorInternal()) {
CDataStream s(SER_DISK, CLIENT_VERSION);
WriteCompactSize(s, quorumVvec->size());
for (auto& pubkey : *quorumVvec) {
s << CBLSPublicKeyVersionWrapper(pubkey, false);
}
db.Write(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s);
}
if (skShare.IsValid()) {
db.Write(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
}
}
bool CQuorum::ReadContributions(const CDBWrapper& db)
{
uint256 dbKey = MakeQuorumKey(*this);
CDataStream s(SER_DISK, CLIENT_VERSION);
if (!db.ReadDataStream(std::make_pair(DB_QUORUM_QUORUM_VVEC, dbKey), s)) {
return false;
}
size_t vvec_size = ReadCompactSize(s);
CBLSPublicKey pubkey;
std::vector<CBLSPublicKey> qv;
for ([[maybe_unused]] size_t _ : irange::range(vvec_size)) {
s >> CBLSPublicKeyVersionWrapper(pubkey, false);
qv.emplace_back(pubkey);
}
LOCK(cs_vvec_shShare);
quorumVvec = std::make_shared<std::vector<CBLSPublicKey>>(std::move(qv));
// We ignore the return value here as it is ok if this fails. If it fails, it usually means that we are not a
// member of the quorum but observed the whole DKG process to have the quorum verification vector.
db.Read(std::make_pair(DB_QUORUM_SK_SHARE, dbKey), skShare);
return true;
}
CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman,
CDKGSessionManager& _dkgManager, CEvoDB& _evoDb,
CQuorumBlockProcessor& _quorumBlockProcessor, CQuorumSnapshotManager& qsnapman,
const CActiveMasternodeManager* const mn_activeman, const CMasternodeSync& mn_sync,
const CSporkManager& sporkman, bool unit_tests, bool wipe) :
db(std::make_unique<CDBWrapper>(unit_tests ? "" : (gArgs.GetDataDirNet() / "llmq" / "quorumdb"), 1 << 20,
unit_tests, wipe)),
blsWorker(_blsWorker),
m_chainstate(chainstate),
m_dmnman(dmnman),
dkgManager(_dkgManager),
quorumBlockProcessor(_quorumBlockProcessor),
m_qsnapman(qsnapman),
m_mn_activeman(mn_activeman),
m_mn_sync(mn_sync),
m_sporkman(sporkman)
{
utils::InitQuorumsCache(mapQuorumsCache, false);
quorumThreadInterrupt.reset();
MigrateOldQuorumDB(_evoDb);
}
CQuorumManager::~CQuorumManager() { Stop(); }
void CQuorumManager::Start()
{
int workerCount = std::thread::hardware_concurrency() / 2;
workerCount = std::max(std::min(1, workerCount), 4);
workerPool.resize(workerCount);
RenameThreadPool(workerPool, "q-mngr");
}
void CQuorumManager::Stop()
{
quorumThreadInterrupt();
workerPool.clear_queue();
workerPool.stop(true);
}
void CQuorumManager::TriggerQuorumDataRecoveryThreads(CConnman& connman, const CBlockIndex* pIndex) const
{
if ((m_mn_activeman == nullptr && !IsWatchQuorumsEnabled()) || !QuorumDataRecoveryEnabled() || pIndex == nullptr) {
return;
}
const std::map<Consensus::LLMQType, QvvecSyncMode> mapQuorumVvecSync = GetEnabledQuorumVvecSyncEntries();
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Process block %s\n", __func__, pIndex->GetBlockHash().ToString());
for (const auto& params : Params().GetConsensus().llmqs) {
const auto vecQuorums = ScanQuorums(params.type, pIndex, params.keepOldConnections);
// First check if we are member of any quorum of this type
const uint256 proTxHash = m_mn_activeman != nullptr ? m_mn_activeman->GetProTxHash() : uint256();
bool fWeAreQuorumTypeMember = ranges::any_of(vecQuorums, [&proTxHash](const auto& pQuorum) {
return pQuorum->IsValidMember(proTxHash);
});
for (const auto& pQuorum : vecQuorums) {
// If there is already a thread running for this specific quorum skip it
if (pQuorum->fQuorumDataRecoveryThreadRunning) {
continue;
}
uint16_t nDataMask{0};
const bool fWeAreQuorumMember = pQuorum->IsValidMember(proTxHash);
const bool fSyncForTypeEnabled = mapQuorumVvecSync.count(pQuorum->qc->llmqType) > 0;
const QvvecSyncMode syncMode = fSyncForTypeEnabled ? mapQuorumVvecSync.at(pQuorum->qc->llmqType) : QvvecSyncMode::Invalid;
const bool fSyncCurrent = syncMode == QvvecSyncMode::Always || (syncMode == QvvecSyncMode::OnlyIfTypeMember && fWeAreQuorumTypeMember);
if ((fWeAreQuorumMember || (fSyncForTypeEnabled && fSyncCurrent)) && !pQuorum->HasVerificationVector()) {
nDataMask |= llmq::CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR;
}
if (fWeAreQuorumMember && !pQuorum->GetSkShare().IsValid()) {
nDataMask |= llmq::CQuorumDataRequest::ENCRYPTED_CONTRIBUTIONS;
}
if (nDataMask == 0) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- No data needed from (%d, %s) at height %d\n",
__func__, ToUnderlying(pQuorum->qc->llmqType), pQuorum->qc->quorumHash.ToString(), pIndex->nHeight);
continue;
}
// Finally start the thread which triggers the requests for this quorum
StartQuorumDataRecoveryThread(connman, pQuorum, pIndex, nDataMask);
}
}
}
void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, CConnman& connman, bool fInitialDownload) const
{
if (!m_mn_sync.IsBlockchainSynced()) return;
for (const auto& params : Params().GetConsensus().llmqs) {
CheckQuorumConnections(connman, params, pindexNew);
}
if (m_mn_activeman != nullptr || IsWatchQuorumsEnabled()) {
// Cleanup expired data requests
LOCK(cs_data_requests);
auto it = mapQuorumDataRequests.begin();
while (it != mapQuorumDataRequests.end()) {
if (it->second.IsExpired(/*add_bias=*/true)) {
it = mapQuorumDataRequests.erase(it);
} else {
++it;
}
}
}
TriggerQuorumDataRecoveryThreads(connman, pindexNew);
StartCleanupOldQuorumDataThread(pindexNew);
}
void CQuorumManager::CheckQuorumConnections(CConnman& connman, const Consensus::LLMQParams& llmqParams,
const CBlockIndex* pindexNew) const
{
if (m_mn_activeman == nullptr && !IsWatchQuorumsEnabled()) return;
auto lastQuorums = ScanQuorums(llmqParams.type, pindexNew, (size_t)llmqParams.keepOldConnections);
auto connmanQuorumsToDelete = connman.GetMasternodeQuorums(llmqParams.type);
// don't remove connections for the currently in-progress DKG round
if (IsQuorumRotationEnabled(llmqParams, pindexNew)) {
int cycleIndexTipHeight = pindexNew->nHeight % llmqParams.dkgInterval;
int cycleQuorumBaseHeight = pindexNew->nHeight - cycleIndexTipHeight;
std::stringstream ss;
for (const auto quorumIndex : irange::range(llmqParams.signingActiveQuorumCount)) {
if (quorumIndex <= cycleIndexTipHeight) {
int curDkgHeight = cycleQuorumBaseHeight + quorumIndex;
auto curDkgBlock = pindexNew->GetAncestor(curDkgHeight)->GetBlockHash();
ss << curDkgHeight << ":" << curDkgBlock.ToString() << " | ";
connmanQuorumsToDelete.erase(curDkgBlock);
}
}
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for rotated quorums: [%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, ss.str());
} else {
int curDkgHeight = pindexNew->nHeight - (pindexNew->nHeight % llmqParams.dkgInterval);
auto curDkgBlock = pindexNew->GetAncestor(curDkgHeight)->GetBlockHash();
connmanQuorumsToDelete.erase(curDkgBlock);
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, curDkgHeight, curDkgBlock.ToString());
}
const uint256 myProTxHash = m_mn_activeman != nullptr ? m_mn_activeman->GetProTxHash() : uint256();
bool isISType = llmqParams.type == Params().GetConsensus().llmqTypeDIP0024InstantSend;
bool watchOtherISQuorums = isISType && !myProTxHash.IsNull() &&
ranges::any_of(lastQuorums, [&myProTxHash](const auto& old_quorum){
return old_quorum->IsMember(myProTxHash);
});
for (const auto& quorum : lastQuorums) {
if (utils::EnsureQuorumConnections(llmqParams, connman, m_dmnman, m_sporkman, m_qsnapman,
m_dmnman.GetListAtChainTip(), quorum->m_quorum_base_block_index, myProTxHash,
/* is_masternode = */ m_mn_activeman != nullptr)) {
if (connmanQuorumsToDelete.erase(quorum->qc->quorumHash) > 0) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString());
}
} else if (watchOtherISQuorums && !quorum->IsMember(myProTxHash)) {
std::unordered_set<uint256, StaticSaltedHasher> connections;
const auto& cindexes = utils::CalcDeterministicWatchConnections(llmqParams.type, quorum->m_quorum_base_block_index, quorum->members.size(), 1);
for (auto idx : cindexes) {
connections.emplace(quorum->members[idx]->proTxHash);
}
if (!connections.empty()) {
if (!connman.HasMasternodeQuorumNodes(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash())) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] adding mn inter-quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString());
connman.SetMasternodeQuorumNodes(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash(), connections);
connman.SetMasternodeQuorumRelayMembers(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash(), connections);
}
if (connmanQuorumsToDelete.erase(quorum->qc->quorumHash) > 0) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn inter-quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString());
}
}
}
}
for (const auto& quorumHash : connmanQuorumsToDelete) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- removing masternodes quorum connections for quorum %s:\n", __func__, quorumHash.ToString());
connman.RemoveMasternodeQuorumNodes(llmqParams.type, quorumHash);
}
}
CQuorumPtr CQuorumManager::BuildQuorumFromCommitment(const Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool populate_cache) const
{
const uint256& quorumHash{pQuorumBaseBlockIndex->GetBlockHash()};
uint256 minedBlockHash;
CFinalCommitmentPtr qc = quorumBlockProcessor.GetMinedCommitment(llmqType, quorumHash, minedBlockHash);
if (qc == nullptr) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- No mined commitment for llmqType[%d] nHeight[%d] quorumHash[%s]\n", __func__, ToUnderlying(llmqType), pQuorumBaseBlockIndex->nHeight, pQuorumBaseBlockIndex->GetBlockHash().ToString());
return nullptr;
}
assert(qc->quorumHash == pQuorumBaseBlockIndex->GetBlockHash());
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
auto quorum = std::make_shared<CQuorum>(llmq_params_opt.value(), blsWorker);
auto members = utils::GetAllQuorumMembers(qc->llmqType, m_dmnman, m_qsnapman, pQuorumBaseBlockIndex);
quorum->Init(std::move(qc), pQuorumBaseBlockIndex, minedBlockHash, members);
bool hasValidVvec = false;
if (WITH_LOCK(cs_db, return quorum->ReadContributions(*db))) {
hasValidVvec = true;
} else {
if (BuildQuorumContributions(quorum->qc, quorum)) {
WITH_LOCK(cs_db, quorum->WriteContributions(*db));
hasValidVvec = true;
} else {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] quorumIndex[%d] quorum.ReadContributions and BuildQuorumContributions for quorumHash[%s] failed\n", __func__, ToUnderlying(llmqType), quorum->qc->quorumIndex, quorum->qc->quorumHash.ToString());
}
}
if (hasValidVvec && populate_cache) {
// pre-populate caches in the background
// recovering public key shares is quite expensive and would result in serious lags for the first few signing
// sessions if the shares would be calculated on-demand
StartCachePopulatorThread(quorum);
}
WITH_LOCK(cs_map_quorums, mapQuorumsCache[llmqType].insert(quorumHash, quorum));
return quorum;
}
bool CQuorumManager::BuildQuorumContributions(const CFinalCommitmentPtr& fqc, const std::shared_ptr<CQuorum>& quorum) const
{
std::vector<uint16_t> memberIndexes;
std::vector<BLSVerificationVectorPtr> vvecs;
std::vector<CBLSSecretKey> skContributions;
if (!dkgManager.GetVerifiedContributions((Consensus::LLMQType)fqc->llmqType, quorum->m_quorum_base_block_index, fqc->validMembers, memberIndexes, vvecs, skContributions)) {
return false;
}
cxxtimer::Timer t2(true);
quorum->SetVerificationVector(blsWorker.BuildQuorumVerificationVector(vvecs));
if (!quorum->HasVerificationVector()) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- failed to build quorumVvec\n", __func__);
// without the quorum vvec, there can't be a skShare, so we fail here. Failure is not fatal here, as it still
// allows to use the quorum as a non-member (verification through the quorum pub key)
return false;
}
if (!quorum->SetSecretKeyShare(blsWorker.AggregateSecretKeys(skContributions), *m_mn_activeman)) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- failed to build skShare\n", __func__);
// We don't bail out here as this is not a fatal error and still allows us to recover public key shares (as we
// have a valid quorum vvec at this point)
}
t2.stop();
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- built quorum vvec and skShare. time=%d\n", __func__, t2.count());
return true;
}
bool CQuorumManager::HasQuorum(Consensus::LLMQType llmqType, const CQuorumBlockProcessor& quorum_block_processor, const uint256& quorumHash)
{
return quorum_block_processor.HasMinedCommitment(llmqType, quorumHash);
}
bool CQuorumManager::RequestQuorumData(CNode* pfrom, CConnman& connman, CQuorumCPtr pQuorum, uint16_t nDataMask,
const uint256& proTxHash) const
{
if (pfrom == nullptr) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Invalid pfrom: nullptr\n", __func__);
return false;
}
if (pfrom->nVersion < LLMQ_DATA_MESSAGES_VERSION) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Version must be %d or greater.\n", __func__, LLMQ_DATA_MESSAGES_VERSION);
return false;
}
if (pfrom->GetVerifiedProRegTxHash().IsNull()) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- pfrom is not a verified masternode\n", __func__);
return false;
}
const Consensus::LLMQType llmqType = pQuorum->qc->llmqType;
if (!Params().GetLLMQ(llmqType).has_value()) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Invalid llmqType: %d\n", __func__, ToUnderlying(llmqType));
return false;
}
const CBlockIndex* pindex{pQuorum->m_quorum_base_block_index};
if (pindex == nullptr) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Invalid m_quorum_base_block_index : nullptr\n", __func__);
return false;
}
LOCK(cs_data_requests);
const CQuorumDataRequestKey key(pfrom->GetVerifiedProRegTxHash(), true, pindex->GetBlockHash(), llmqType);
const CQuorumDataRequest request(llmqType, pindex->GetBlockHash(), nDataMask, proTxHash);
auto [old_pair, inserted] = mapQuorumDataRequests.emplace(key, request);
if (!inserted) {
if (old_pair->second.IsExpired(/*add_bias=*/true)) {
old_pair->second = request;
} else {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Already requested\n", __func__);
return false;
}
}
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- sending QGETDATA quorumHash[%s] llmqType[%d] proRegTx[%s]\n", __func__, key.quorumHash.ToString(),
ToUnderlying(key.llmqType), key.proRegTx.ToString());
CNetMsgMaker msgMaker(pfrom->GetCommonVersion());
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::QGETDATA, request));
return true;
}
std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, size_t nCountRequested) const
{
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip());
return ScanQuorums(llmqType, pindex, nCountRequested);
}
std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqType, const CBlockIndex* pindexStart, size_t nCountRequested) const
{
if (pindexStart == nullptr || nCountRequested == 0 || !IsQuorumTypeEnabled(llmqType, pindexStart)) {
return {};
}
gsl::not_null<const CBlockIndex*> pindexStore{pindexStart};
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
// Quorum sets can only change during the mining phase of DKG.
// Find the closest known block index.
const int quorumCycleStartHeight = pindexStart->nHeight - (pindexStart->nHeight % llmq_params_opt->dkgInterval);
const int quorumCycleMiningStartHeight = quorumCycleStartHeight + llmq_params_opt->dkgMiningWindowStart;
const int quorumCycleMiningEndHeight = quorumCycleStartHeight + llmq_params_opt->dkgMiningWindowEnd;
if (pindexStart->nHeight < quorumCycleMiningStartHeight) {
// too early for this cycle, use the previous one
// bail out if it's below genesis block
if (quorumCycleMiningEndHeight < llmq_params_opt->dkgInterval) return {};
pindexStore = pindexStart->GetAncestor(quorumCycleMiningEndHeight - llmq_params_opt->dkgInterval);
} else if (pindexStart->nHeight > quorumCycleMiningEndHeight) {
// we are past the mining phase of this cycle, use it
pindexStore = pindexStart->GetAncestor(quorumCycleMiningEndHeight);
}
// everything else is inside the mining phase of this cycle, no pindexStore adjustment needed
gsl::not_null<const CBlockIndex*> pIndexScanCommitments{pindexStore};
size_t nScanCommitments{nCountRequested};
std::vector<CQuorumCPtr> vecResultQuorums;
{
LOCK(cs_scan_quorums);
if (scanQuorumsCache.empty()) {
for (const auto& llmq : Params().GetConsensus().llmqs) {
// NOTE: We store it for each block hash in the DKG mining phase here
// and not for a single quorum hash per quorum like we do for other caches.
// And we only do this for max_cycles() of the most recent quorums
// because signing by old quorums requires the exact quorum hash to be specified
// and quorum scanning isn't needed there.
scanQuorumsCache.try_emplace(llmq.type, llmq.max_cycles(llmq.keepOldConnections) * (llmq.dkgMiningWindowEnd - llmq.dkgMiningWindowStart));
}
}
auto& cache = scanQuorumsCache[llmqType];
bool fCacheExists = cache.get(pindexStore->GetBlockHash(), vecResultQuorums);
if (fCacheExists) {
// We have exactly what requested so just return it
if (vecResultQuorums.size() == nCountRequested) {
return vecResultQuorums;
}
// If we have more cached than requested return only a subvector
if (vecResultQuorums.size() > nCountRequested) {
return {vecResultQuorums.begin(), vecResultQuorums.begin() + nCountRequested};
}
// If we have cached quorums but not enough, subtract what we have from the count and the set correct index where to start
// scanning for the rests
if (!vecResultQuorums.empty()) {
nScanCommitments -= vecResultQuorums.size();
// bail out if it's below genesis block
if (vecResultQuorums.back()->m_quorum_base_block_index->pprev == nullptr) return {};
pIndexScanCommitments = vecResultQuorums.back()->m_quorum_base_block_index->pprev;
}
} else {
// If there is nothing in cache request at least keepOldConnections because this gets cached then later
nScanCommitments = std::max(nCountRequested, static_cast<size_t>(llmq_params_opt->keepOldConnections));
}
}
// Get the block indexes of the mined commitments to build the required quorums from
std::vector<const CBlockIndex*> pQuorumBaseBlockIndexes{ llmq_params_opt->useRotation ?
quorumBlockProcessor.GetMinedCommitmentsIndexedUntilBlock(llmqType, pIndexScanCommitments, nScanCommitments) :
quorumBlockProcessor.GetMinedCommitmentsUntilBlock(llmqType, pIndexScanCommitments, nScanCommitments)
};
vecResultQuorums.reserve(vecResultQuorums.size() + pQuorumBaseBlockIndexes.size());
for (auto& pQuorumBaseBlockIndex : pQuorumBaseBlockIndexes) {
assert(pQuorumBaseBlockIndex);
// populate cache for keepOldConnections most recent quorums only
bool populate_cache = vecResultQuorums.size() < static_cast<size_t>(llmq_params_opt->keepOldConnections);
// We assume that every quorum asked for is available to us on hand, if this
// fails then we can assume that something has gone wrong and we should stop
// trying to process any further and return a blank.
auto quorum = GetQuorum(llmqType, pQuorumBaseBlockIndex, populate_cache);
if (!quorum) {
LogPrintf("%s: ERROR! Unexpected missing quorum with llmqType=%d, blockHash=%s, populate_cache=%s\n",
__func__, ToUnderlying(llmqType), pQuorumBaseBlockIndex->GetBlockHash().ToString(),
populate_cache ? "true" : "false");
return {};
}
vecResultQuorums.emplace_back(quorum);
}
const size_t nCountResult{vecResultQuorums.size()};
if (nCountResult > 0) {
LOCK(cs_scan_quorums);
// Don't cache more than keepOldConnections elements
// because signing by old quorums requires the exact quorum hash
// to be specified and quorum scanning isn't needed there.
auto& cache = scanQuorumsCache[llmqType];
const size_t nCacheEndIndex = std::min(nCountResult, static_cast<size_t>(llmq_params_opt->keepOldConnections));
cache.emplace(pindexStore->GetBlockHash(), {vecResultQuorums.begin(), vecResultQuorums.begin() + nCacheEndIndex});
}
// Don't return more than nCountRequested elements
const size_t nResultEndIndex = std::min(nCountResult, nCountRequested);
return {vecResultQuorums.begin(), vecResultQuorums.begin() + nResultEndIndex};
}
CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const
{
const CBlockIndex* pQuorumBaseBlockIndex = [&]() {
// Lock contention may still be high here; consider using a shared lock
// We cannot hold cs_quorumBaseBlockIndexCache the whole time as that creates lock-order inversion with cs_main;
// We cannot aquire cs_main if we have cs_quorumBaseBlockIndexCache held
const CBlockIndex* pindex;
if (!WITH_LOCK(cs_quorumBaseBlockIndexCache, return quorumBaseBlockIndexCache.get(quorumHash, pindex))) {
pindex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(quorumHash));
if (pindex) {
LOCK(cs_quorumBaseBlockIndexCache);
quorumBaseBlockIndexCache.insert(quorumHash, pindex);
}
}
return pindex;
}();
if (!pQuorumBaseBlockIndex) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- block %s not found\n", __func__, quorumHash.ToString());
return nullptr;
}
return GetQuorum(llmqType, pQuorumBaseBlockIndex);
}
CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool populate_cache) const
{
auto quorumHash = pQuorumBaseBlockIndex->GetBlockHash();
// we must check this before we look into the cache. Reorgs might have happened which would mean we might have
// cached quorums which are not in the active chain anymore
if (!HasQuorum(llmqType, quorumBlockProcessor, quorumHash)) {
return nullptr;
}
CQuorumPtr pQuorum;
if (LOCK(cs_map_quorums); mapQuorumsCache[llmqType].get(quorumHash, pQuorum)) {
return pQuorum;
}
return BuildQuorumFromCommitment(llmqType, pQuorumBaseBlockIndex, populate_cache);
}
size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex) const
{
assert(m_mn_activeman);
auto mns = m_dmnman.GetListForBlock(pIndex);
std::vector<uint256> vecProTxHashes;
vecProTxHashes.reserve(mns.GetValidMNsCount());
mns.ForEachMN(true, [&](auto& pMasternode) {
vecProTxHashes.emplace_back(pMasternode.proTxHash);
});
std::sort(vecProTxHashes.begin(), vecProTxHashes.end());
size_t nIndex{0};
{
auto my_protx_hash = m_mn_activeman->GetProTxHash();
for (const auto i : irange::range(vecProTxHashes.size())) {
// cppcheck-suppress useStlAlgorithm
if (my_protx_hash == vecProTxHashes[i]) {
nIndex = i;
break;
}
}
}
return nIndex % pQuorum->qc->validMembers.size();
}
PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& connman, const std::string& msg_type, CDataStream& vRecv)
{
auto strFunc = __func__;
auto errorHandler = [&](const std::string& strError, int nScore = 10) -> PeerMsgRet {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- %s: %s, from peer=%d\n", strFunc, msg_type, strError, pfrom.GetId());
if (nScore > 0) {
return tl::unexpected{nScore};
}
return {};
};
if (msg_type == NetMsgType::QGETDATA) {
if (m_mn_activeman == nullptr || (pfrom.GetVerifiedProRegTxHash().IsNull() && !pfrom.qwatch)) {
return errorHandler("Not a verified masternode or a qwatch connection");
}
CQuorumDataRequest request;
vRecv >> request;
auto sendQDATA = [&](CQuorumDataRequest::Errors nError,
bool request_limit_exceeded,
const CDataStream& body = CDataStream(SER_NETWORK, PROTOCOL_VERSION)) -> PeerMsgRet {
PeerMsgRet ret{};
switch (nError) {
case (CQuorumDataRequest::Errors::NONE):
case (CQuorumDataRequest::Errors::QUORUM_TYPE_INVALID):
case (CQuorumDataRequest::Errors::QUORUM_BLOCK_NOT_FOUND):
case (CQuorumDataRequest::Errors::QUORUM_NOT_FOUND):
case (CQuorumDataRequest::Errors::MASTERNODE_IS_NO_MEMBER):
case (CQuorumDataRequest::Errors::UNDEFINED):
if (request_limit_exceeded) ret = errorHandler("Request limit exceeded", 25);
break;
case (CQuorumDataRequest::Errors::QUORUM_VERIFICATION_VECTOR_MISSING):
case (CQuorumDataRequest::Errors::ENCRYPTED_CONTRIBUTIONS_MISSING):
// Do not punish limit exceed if we don't have the requested data
break;
}
request.SetError(nError);
CDataStream ssResponse(SER_NETWORK, pfrom.GetCommonVersion(), request, body);
connman.PushMessage(&pfrom, CNetMsgMaker(pfrom.GetCommonVersion()).Make(NetMsgType::QDATA, ssResponse));
return ret;
};
bool request_limit_exceeded(false);
{
LOCK2(cs_main, cs_data_requests);
const CQuorumDataRequestKey key(pfrom.GetVerifiedProRegTxHash(), false, request.GetQuorumHash(), request.GetLLMQType());
auto it = mapQuorumDataRequests.find(key);
if (it == mapQuorumDataRequests.end()) {
it = mapQuorumDataRequests.emplace(key, request).first;
} else if (it->second.IsExpired(/*add_bias=*/false)) {
it->second = request;
} else {
request_limit_exceeded = true;
}
}
if (!Params().GetLLMQ(request.GetLLMQType()).has_value()) {
return sendQDATA(CQuorumDataRequest::Errors::QUORUM_TYPE_INVALID, request_limit_exceeded);
}
const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(request.GetQuorumHash()));
if (pQuorumBaseBlockIndex == nullptr) {
return sendQDATA(CQuorumDataRequest::Errors::QUORUM_BLOCK_NOT_FOUND, request_limit_exceeded);
}
const auto pQuorum = GetQuorum(request.GetLLMQType(), pQuorumBaseBlockIndex);
if (pQuorum == nullptr) {
return sendQDATA(CQuorumDataRequest::Errors::QUORUM_NOT_FOUND, request_limit_exceeded);
}
CDataStream ssResponseData(SER_NETWORK, pfrom.GetCommonVersion());
// Check if request wants QUORUM_VERIFICATION_VECTOR data
if (request.GetDataMask() & CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR) {
if (!pQuorum->HasVerificationVector()) {
return sendQDATA(CQuorumDataRequest::Errors::QUORUM_VERIFICATION_VECTOR_MISSING, request_limit_exceeded);
}
WITH_LOCK(pQuorum->cs_vvec_shShare, ssResponseData << *pQuorum->quorumVvec);
}
// Check if request wants ENCRYPTED_CONTRIBUTIONS data
if (request.GetDataMask() & CQuorumDataRequest::ENCRYPTED_CONTRIBUTIONS) {
int memberIdx = pQuorum->GetMemberIndex(request.GetProTxHash());
if (memberIdx == -1) {
return sendQDATA(CQuorumDataRequest::Errors::MASTERNODE_IS_NO_MEMBER, request_limit_exceeded);
}
std::vector<CBLSIESEncryptedObject<CBLSSecretKey>> vecEncrypted;
if (!dkgManager.GetEncryptedContributions(request.GetLLMQType(), pQuorumBaseBlockIndex, pQuorum->qc->validMembers, request.GetProTxHash(), vecEncrypted)) {
return sendQDATA(CQuorumDataRequest::Errors::ENCRYPTED_CONTRIBUTIONS_MISSING, request_limit_exceeded);
}
ssResponseData << vecEncrypted;
}
return sendQDATA(CQuorumDataRequest::Errors::NONE, request_limit_exceeded, ssResponseData);
}
if (msg_type == NetMsgType::QDATA) {
if ((m_mn_activeman == nullptr && !IsWatchQuorumsEnabled()) || pfrom.GetVerifiedProRegTxHash().IsNull()) {
return errorHandler("Not a verified masternode and -watchquorums is not enabled");
}
CQuorumDataRequest request;
vRecv >> request;
{
LOCK2(cs_main, cs_data_requests);
const CQuorumDataRequestKey key(pfrom.GetVerifiedProRegTxHash(), true, request.GetQuorumHash(), request.GetLLMQType());
auto it = mapQuorumDataRequests.find(key);
if (it == mapQuorumDataRequests.end()) {
return errorHandler("Not requested");
}
if (it->second.IsProcessed()) {
return errorHandler("Already received");
}
if (request != it->second) {
return errorHandler("Not like requested");
}
it->second.SetProcessed();
}
if (request.GetError() != CQuorumDataRequest::Errors::NONE) {
return errorHandler(strprintf("Error %d (%s)", request.GetError(), request.GetErrorString()), 0);
}
CQuorumPtr pQuorum;
{
if (LOCK(cs_map_quorums); !mapQuorumsCache[request.GetLLMQType()].get(request.GetQuorumHash(), pQuorum)) {
return errorHandler("Quorum not found", 0); // Don't bump score because we asked for it
}
}
// Check if request has QUORUM_VERIFICATION_VECTOR data
if (request.GetDataMask() & CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR) {
std::vector<CBLSPublicKey> verificationVector;
vRecv >> verificationVector;
if (pQuorum->SetVerificationVector(verificationVector)) {
StartCachePopulatorThread(pQuorum);
} else {
return errorHandler("Invalid quorum verification vector");
}
}
// Check if request has ENCRYPTED_CONTRIBUTIONS data
if (request.GetDataMask() & CQuorumDataRequest::ENCRYPTED_CONTRIBUTIONS) {
assert(m_mn_activeman);
if (WITH_LOCK(pQuorum->cs_vvec_shShare, return pQuorum->quorumVvec->size() != size_t(pQuorum->params.threshold))) {
return errorHandler("No valid quorum verification vector available", 0); // Don't bump score because we asked for it
}
int memberIdx = pQuorum->GetMemberIndex(request.GetProTxHash());
if (memberIdx == -1) {
return errorHandler("Not a member of the quorum", 0); // Don't bump score because we asked for it
}
std::vector<CBLSIESEncryptedObject<CBLSSecretKey>> vecEncrypted;
vRecv >> vecEncrypted;
std::vector<CBLSSecretKey> vecSecretKeys;
vecSecretKeys.resize(vecEncrypted.size());
for (const auto i : irange::range(vecEncrypted.size())) {
if (!m_mn_activeman->Decrypt(vecEncrypted[i], memberIdx, vecSecretKeys[i], PROTOCOL_VERSION)) {
return errorHandler("Failed to decrypt");
}
}
CBLSSecretKey secretKeyShare = blsWorker.AggregateSecretKeys(vecSecretKeys);
if (!pQuorum->SetSecretKeyShare(secretKeyShare, *m_mn_activeman)) {
return errorHandler("Invalid secret key share received");
}
}
WITH_LOCK(cs_db, pQuorum->WriteContributions(*db));
return {};
}
return {};
}
void CQuorumManager::StartCachePopulatorThread(const CQuorumCPtr pQuorum) const
{
if (!pQuorum->HasVerificationVector()) {
return;
}
cxxtimer::Timer t(true);
LogPrint(BCLog::LLMQ, "CQuorumManager::StartCachePopulatorThread -- type=%d height=%d hash=%s start\n",
ToUnderlying(pQuorum->params.type),
pQuorum->m_quorum_base_block_index->nHeight,
pQuorum->m_quorum_base_block_index->GetBlockHash().ToString());
// when then later some other thread tries to get keys, it will be much faster
workerPool.push([pQuorum, t, this](int threadId) {
for (const auto i : irange::range(pQuorum->members.size())) {
if (quorumThreadInterrupt) {
break;
}
if (pQuorum->qc->validMembers[i]) {
pQuorum->GetPubKeyShare(i);
}
}
LogPrint(BCLog::LLMQ, "CQuorumManager::StartCachePopulatorThread -- type=%d height=%d hash=%s done. time=%d\n",
ToUnderlying(pQuorum->params.type),
pQuorum->m_quorum_base_block_index->nHeight,
pQuorum->m_quorum_base_block_index->GetBlockHash().ToString(),
t.count());
});
}
void CQuorumManager::StartQuorumDataRecoveryThread(CConnman& connman, const CQuorumCPtr pQuorum,
const CBlockIndex* pIndex, uint16_t nDataMaskIn) const
{
assert(m_mn_activeman);
if (pQuorum->fQuorumDataRecoveryThreadRunning) {
LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Already running\n", __func__);
return;
}
pQuorum->fQuorumDataRecoveryThreadRunning = true;
workerPool.push([&connman, pQuorum, pIndex, nDataMaskIn, this](int threadId) {
size_t nTries{0};
uint16_t nDataMask{nDataMaskIn};
int64_t nTimeLastSuccess{0};
uint256* pCurrentMemberHash{nullptr};
std::vector<uint256> vecMemberHashes;
const size_t nMyStartOffset{GetQuorumRecoveryStartOffset(pQuorum, pIndex)};
const int64_t nRequestTimeout{10};
auto printLog = [&](const std::string& strMessage) {
const std::string strMember{pCurrentMemberHash == nullptr ? "nullptr" : pCurrentMemberHash->ToString()};
LogPrint(BCLog::LLMQ, "CQuorumManager::StartQuorumDataRecoveryThread -- %s - for llmqType %d, quorumHash %s, nDataMask (%d/%d), pCurrentMemberHash %s, nTries %d\n",
strMessage, ToUnderlying(pQuorum->qc->llmqType), pQuorum->qc->quorumHash.ToString(), nDataMask, nDataMaskIn, strMember, nTries);
};
printLog("Start");
while (!m_mn_sync.IsBlockchainSynced() && !quorumThreadInterrupt) {
quorumThreadInterrupt.sleep_for(std::chrono::seconds(nRequestTimeout));
}
if (quorumThreadInterrupt) {
printLog("Aborted");
return;
}
vecMemberHashes.reserve(pQuorum->qc->validMembers.size());
for (auto& member : pQuorum->members) {
if (pQuorum->IsValidMember(member->proTxHash) && member->proTxHash != m_mn_activeman->GetProTxHash()) {
vecMemberHashes.push_back(member->proTxHash);
}
}
std::sort(vecMemberHashes.begin(), vecMemberHashes.end());
printLog("Try to request");
while (nDataMask > 0 && !quorumThreadInterrupt) {
if (nDataMask & llmq::CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR &&
pQuorum->HasVerificationVector()) {
nDataMask &= ~llmq::CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR;
printLog("Received quorumVvec");
}
if (nDataMask & llmq::CQuorumDataRequest::ENCRYPTED_CONTRIBUTIONS && pQuorum->GetSkShare().IsValid()) {
nDataMask &= ~llmq::CQuorumDataRequest::ENCRYPTED_CONTRIBUTIONS;
printLog("Received skShare");
}
if (nDataMask == 0) {
printLog("Success");
break;
}
if ((GetTime<std::chrono::seconds>().count() - nTimeLastSuccess) > nRequestTimeout) {
if (nTries >= vecMemberHashes.size()) {
printLog("All tried but failed");
break;
}
// Access the member list of the quorum with the calculated offset applied to balance the load equally
pCurrentMemberHash = &vecMemberHashes[(nMyStartOffset + nTries++) % vecMemberHashes.size()];
{
LOCK(cs_data_requests);
const CQuorumDataRequestKey key(*pCurrentMemberHash, true, pQuorum->qc->quorumHash, pQuorum->qc->llmqType);
auto it = mapQuorumDataRequests.find(key);
if (it != mapQuorumDataRequests.end() && !it->second.IsExpired(/*add_bias=*/true)) {
printLog("Already asked");
continue;
}
}
// Sleep a bit depending on the start offset to balance out multiple requests to same masternode
quorumThreadInterrupt.sleep_for(std::chrono::milliseconds(nMyStartOffset * 100));
nTimeLastSuccess = GetTime<std::chrono::seconds>().count();
connman.AddPendingMasternode(*pCurrentMemberHash);
printLog("Connect");
}