forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathblockprocessor.cpp
More file actions
812 lines (658 loc) · 33.2 KB
/
Copy pathblockprocessor.cpp
File metadata and controls
812 lines (658 loc) · 33.2 KB
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
// Copyright (c) 2018-2025 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/blockprocessor.h>
#include <llmq/commitment.h>
#include <llmq/options.h>
#include <llmq/utils.h>
#include <evo/evodb.h>
#include <evo/specialtx.h>
#include <chain.h>
#include <chainparams.h>
#include <checkqueue.h>
#include <consensus/params.h>
#include <consensus/validation.h>
#include <deploymentstatus.h>
#include <net.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <saltedhasher.h>
#include <sync.h>
#include <util/irange.h>
#include <util/underlying.h>
#include <validation.h>
#include <map>
static void PreComputeQuorumMembers(CDeterministicMNManager& dmnman, llmq::CQuorumSnapshotManager& qsnapman,
const CBlockIndex* pindex, bool reset_cache)
{
for (const Consensus::LLMQParams& params : llmq::GetEnabledQuorumParams(pindex->pprev)) {
if (llmq::IsQuorumRotationEnabled(params, pindex) && (pindex->nHeight % params.dkgInterval == 0)) {
llmq::utils::GetAllQuorumMembers(params.type, dmnman, qsnapman, pindex, reset_cache);
}
}
}
namespace llmq
{
static const std::string DB_MINED_COMMITMENT = "q_mc";
static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT = "q_mcih";
static const std::string DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT_Q_INDEXED = "q_mcihi";
static const std::string DB_BEST_BLOCK_UPGRADE = "q_bbu2";
CQuorumBlockProcessor::CQuorumBlockProcessor(CChainState& chainstate, CDeterministicMNManager& dmnman, CEvoDB& evoDb,
CQuorumSnapshotManager& qsnapman) :
m_chainstate{chainstate},
m_dmnman{dmnman},
m_evoDb{evoDb},
m_qsnapman{qsnapman}
{
utils::InitQuorumsCache(mapHasMinedCommitmentCache);
int bls_threads = gArgs.GetIntArg("-parbls", DEFAULT_BLSCHECK_THREADS);
if (bls_threads <= 0) {
// -parbls=0 means autodetect (number of cores - 1 validator threads)
// -parbls=-n means "leave n cores free" (number of cores - n - 1 validator threads)
bls_threads += GetNumCores();
}
// Subtract 1 because the main thread counts towards the par threads
bls_threads = std::max(bls_threads - 1, 0);
// Number of script-checking threads <= MAX_BLSCHECK_THREADS
bls_threads = std::min(bls_threads, MAX_BLSCHECK_THREADS);
LogPrintf("BLS verification uses %d additional threads\n", bls_threads);
m_bls_queue.StartWorkerThreads(bls_threads);
}
CQuorumBlockProcessor::~CQuorumBlockProcessor()
{
m_bls_queue.StopWorkerThreads();
}
MessageProcessingResult CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_view msg_type,
CDataStream& vRecv)
{
if (msg_type != NetMsgType::QFCOMMITMENT) {
return {};
}
CFinalCommitment qc;
vRecv >> qc;
MessageProcessingResult ret;
ret.m_to_erase = CInv{MSG_QUORUM_FINAL_COMMITMENT, ::SerializeHash(qc)};
if (qc.IsNull()) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- null commitment from peer=%d\n", __func__, peer.GetId());
ret.m_error = MisbehavingError{100};
return ret;
}
const auto& llmq_params_opt = Params().GetLLMQ(qc.llmqType);
if (!llmq_params_opt.has_value()) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- invalid commitment type %d from peer=%d\n", __func__,
ToUnderlying(qc.llmqType), peer.GetId());
ret.m_error = MisbehavingError{100};
return ret;
}
auto type = qc.llmqType;
// Verify that quorumHash is part of the active chain and that it's the first block in the DKG interval
const CBlockIndex* pQuorumBaseBlockIndex;
{
LOCK(::cs_main);
pQuorumBaseBlockIndex = m_chainstate.m_blockman.LookupBlockIndex(qc.quorumHash);
if (pQuorumBaseBlockIndex == nullptr) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- unknown block %s in commitment, peer=%d\n", __func__,
qc.quorumHash.ToString(), peer.GetId());
// can't really punish the node here, as we might simply be the one that is on the wrong chain or not
// fully synced
return ret;
}
if (m_chainstate.m_chain.Tip()->GetAncestor(pQuorumBaseBlockIndex->nHeight) != pQuorumBaseBlockIndex) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s not in active chain, peer=%d\n", __func__,
qc.quorumHash.ToString(), peer.GetId());
// same, can't punish
return ret;
}
if (int quorumHeight = pQuorumBaseBlockIndex->nHeight - (pQuorumBaseBlockIndex->nHeight % llmq_params_opt->dkgInterval) + int(qc.quorumIndex);
quorumHeight != pQuorumBaseBlockIndex->nHeight) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s is not the first block in the DKG interval, peer=%d\n", __func__,
qc.quorumHash.ToString(), peer.GetId());
ret.m_error = MisbehavingError{100};
return ret;
}
if (pQuorumBaseBlockIndex->nHeight < (m_chainstate.m_chain.Height() - llmq_params_opt->dkgInterval)) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- block %s is too old, peer=%d\n", __func__,
qc.quorumHash.ToString(), peer.GetId());
// TODO: enable punishment in some future version when all/most nodes are running with this fix
// ret.m_error = MisbehavingError{100};
return ret;
}
if (HasMinedCommitment(type, qc.quorumHash)) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- commitment for quorum hash[%s], type[%d], quorumIndex[%d] is already mined, peer=%d\n",
__func__, qc.quorumHash.ToString(), ToUnderlying(type), qc.quorumIndex, peer.GetId());
// NOTE: do not punish here
return ret;
}
}
{
// Check if we already got a better one locally
// We do this before verifying the commitment to avoid DoS
LOCK(minableCommitmentsCs);
auto k = std::make_pair(type, qc.quorumHash);
auto it = minableCommitmentsByQuorum.find(k);
if (it != minableCommitmentsByQuorum.end()) {
auto jt = minableCommitments.find(it->second);
if (jt != minableCommitments.end() && jt->second.CountSigners() <= qc.CountSigners()) {
return ret;
}
}
}
if (!qc.Verify(m_dmnman, m_qsnapman, pQuorumBaseBlockIndex, /*checkSigs=*/true)) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- commitment for quorum %s:%d is not valid quorumIndex[%d] nversion[%d], peer=%d\n",
__func__, qc.quorumHash.ToString(),
ToUnderlying(qc.llmqType), qc.quorumIndex, qc.nVersion, peer.GetId());
ret.m_error = MisbehavingError{100};
return ret;
}
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- received commitment for quorum %s:%d, validMembers=%d, signers=%d, peer=%d\n", __func__,
qc.quorumHash.ToString(), ToUnderlying(qc.llmqType), qc.CountValidMembers(), qc.CountSigners(), peer.GetId());
if (auto inv_opt = AddMineableCommitment(qc)) {
ret.m_inventory.emplace_back(inv_opt.value());
}
return ret;
}
bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, BlockValidationState& state, bool fJustCheck, bool fBLSChecks)
{
AssertLockHeld(::cs_main);
const auto blockHash = pindex->GetBlockHash();
if (!DeploymentActiveAt(*pindex, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0003)) {
m_evoDb.Write(DB_BEST_BLOCK_UPGRADE, blockHash);
return true;
}
PreComputeQuorumMembers(m_dmnman, m_qsnapman, pindex, /*reset_cache=*/false);
std::multimap<Consensus::LLMQType, CFinalCommitment> qcs;
if (!GetCommitmentsFromBlock(block, pindex, qcs, state)) {
return false;
}
// The following checks make sure that there is always a (possibly null) commitment while in the mining phase
// until the first non-null commitment has been mined. After the non-null commitment, no other commitments are
// allowed, including null commitments.
// Note: must only check quorums that were enabled at the _previous_ block height to match mining logic
for (const Consensus::LLMQParams& params : GetEnabledQuorumParams(pindex->pprev)) {
// skip these checks when replaying blocks after the crash
if (m_chainstate.m_chain.Tip() == nullptr) {
break;
}
const size_t numCommitmentsRequired = GetNumCommitmentsRequired(params, pindex->nHeight);
const auto numCommitmentsInNewBlock = qcs.count(params.type);
if (numCommitmentsRequired < numCommitmentsInNewBlock) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-not-allowed");
}
if (numCommitmentsRequired > numCommitmentsInNewBlock) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-missing");
}
if (IsQuorumRotationEnabled(params, pindex)) {
LogPrintf("[ProcessBlock] h[%d] numCommitmentsRequired[%d] numCommitmentsInNewBlock[%d]\n", pindex->nHeight, numCommitmentsRequired, numCommitmentsInNewBlock);
}
}
if (fBLSChecks) {
CCheckQueueControl<utils::BlsCheck> queue_control(&m_bls_queue);
for (const auto& [_, qc] : qcs) {
if (qc.IsNull()) continue;
const auto* pQuorumBaseBlockIndex = m_chainstate.m_blockman.LookupBlockIndex(qc.quorumHash);
if (pQuorumBaseBlockIndex == nullptr) {
LogPrint(BCLog::LLMQ, "[ProcessBlock] h[%d] unexpectedly failed due to no known pindex for hash[%s]\n",
pindex->nHeight, qc.quorumHash.ToString());
return false;
}
qc.VerifySignatureAsync(m_dmnman, m_qsnapman, pQuorumBaseBlockIndex, &queue_control);
}
if (!queue_control.Wait()) {
// at least one check failed
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-invalid");
}
}
for (const auto& [_, qc] : qcs) {
if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state, fJustCheck)) {
LogPrintf("[ProcessBlock] failed h[%d] llmqType[%d] version[%d] quorumIndex[%d] quorumHash[%s]\n", pindex->nHeight, ToUnderlying(qc.llmqType), qc.nVersion, qc.quorumIndex, qc.quorumHash.ToString());
return false;
}
}
m_evoDb.Write(DB_BEST_BLOCK_UPGRADE, blockHash);
return true;
}
// We store a mapping from minedHeight->quorumHeight in the DB
// minedHeight is inversed so that entries are traversable in reversed order
static std::tuple<std::string, Consensus::LLMQType, uint32_t> BuildInversedHeightKey(Consensus::LLMQType llmqType, int nMinedHeight)
{
// nMinedHeight must be converted to big endian to make it comparable when serialized
return std::make_tuple(DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT, llmqType, htobe32_internal(std::numeric_limits<uint32_t>::max() - nMinedHeight));
}
static std::tuple<std::string, Consensus::LLMQType, int, uint32_t> BuildInversedHeightKeyIndexed(Consensus::LLMQType llmqType, int nMinedHeight, int quorumIndex)
{
// nMinedHeight must be converted to big endian to make it comparable when serialized
return std::make_tuple(DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT_Q_INDEXED, llmqType, quorumIndex, htobe32_internal(std::numeric_limits<uint32_t>::max() - nMinedHeight));
}
static bool IsMiningPhase(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight)
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
// Note: This function can be called for new blocks
assert(nHeight <= active_chain.Height() + 1);
int quorumCycleStartHeight = nHeight - (nHeight % llmqParams.dkgInterval);
int quorumCycleMiningStartHeight = quorumCycleStartHeight + llmqParams.dkgMiningWindowStart;
int quorumCycleMiningEndHeight = quorumCycleStartHeight + llmqParams.dkgMiningWindowEnd;
return nHeight >= quorumCycleMiningStartHeight && nHeight <= quorumCycleMiningEndHeight;
}
bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc,
BlockValidationState& state, bool fJustCheck)
{
AssertLockHeld(::cs_main);
const auto& llmq_params_opt = Params().GetLLMQ(qc.llmqType);
if (!llmq_params_opt.has_value()) {
LogPrint(BCLog::LLMQ, "%s -- invalid commitment type %d\n", __func__, ToUnderlying(qc.llmqType));
return false;
}
const auto& llmq_params = llmq_params_opt.value();
uint256 quorumHash = GetQuorumBlockHash(llmq_params, m_chainstate.m_chain, nHeight, qc.quorumIndex);
LogPrint(BCLog::LLMQ, /* Continued */
"%s -- processing commitment for block height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, "
"validMembers=%d, quorumPublicKey=%s "
"fJustCheck[%d] processing commitment from block.\n",
__func__, nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(), qc.CountSigners(),
qc.CountValidMembers(), qc.quorumPublicKey.ToString(), fJustCheck);
// skip `bad-qc-block` checks below when replaying blocks after the crash
if (m_chainstate.m_chain.Tip() == nullptr) {
quorumHash = qc.quorumHash;
}
if (quorumHash.IsNull()) {
LogPrint(BCLog::LLMQ, /* Continued */
"%s -- height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%d, "
"quorumPublicKey=%s quorumHash is null.\n",
__func__, nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(), qc.CountSigners(),
qc.CountValidMembers(), qc.quorumPublicKey.ToString());
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-block");
}
if (quorumHash != qc.quorumHash) {
LogPrint(BCLog::LLMQ, /* Continued */
"%s -- height=%d, type=%d, quorumIndex=%d, quorumHash=%s, qc.quorumHash=%s signers=%s, "
"validMembers=%d, quorumPublicKey=%s non equal quorumHash.\n",
__func__, nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(),
qc.quorumHash.ToString(), qc.CountSigners(), qc.CountValidMembers(), qc.quorumPublicKey.ToString());
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-block");
}
if (qc.IsNull()) {
if (!qc.VerifyNull()) {
LogPrint(BCLog::LLMQ, /* Continued */
"%s -- height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%dqc "
"verifynull failed.\n",
__func__, nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(),
qc.CountSigners(), qc.CountValidMembers());
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-invalid-null");
}
return true;
}
if (HasMinedCommitment(llmq_params.type, quorumHash)) {
// should not happen as it's already handled in ProcessBlock
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-dup");
}
if (!IsMiningPhase(llmq_params, m_chainstate.m_chain, nHeight)) {
// should not happen as it's already handled in ProcessBlock
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-height");
}
const auto* pQuorumBaseBlockIndex = m_chainstate.m_blockman.LookupBlockIndex(qc.quorumHash);
if (pQuorumBaseBlockIndex == nullptr) {
LogPrint(BCLog::LLMQ, "%s -- unexpectedly failed due to no known pindex for hash[%s]\n", __func__,
qc.quorumHash.ToString());
return false;
}
// we don't validate signatures here; they already validated on previous step
if (!qc.Verify(m_dmnman, m_qsnapman, pQuorumBaseBlockIndex, /*checksigs=*/false)) {
LogPrint(BCLog::LLMQ, /* Continued */
"%s -- height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%d, "
"quorumPublicKey=%s qc verify failed.\n",
__func__, nHeight, ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString(), qc.CountSigners(),
qc.CountValidMembers(), qc.quorumPublicKey.ToString());
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-invalid");
}
if (fJustCheck) {
return true;
}
bool rotation_enabled = IsQuorumRotationEnabled(llmq_params, pQuorumBaseBlockIndex);
if (rotation_enabled) {
LogPrint(BCLog::LLMQ, "%s -- height[%d] pQuorumBaseBlockIndex[%d] quorumIndex[%d] qversion[%d] Built\n",
__func__, nHeight, pQuorumBaseBlockIndex->nHeight, qc.quorumIndex, qc.nVersion);
}
// Store commitment in DB
auto cacheKey = std::make_pair(llmq_params.type, quorumHash);
m_evoDb.Write(std::make_pair(DB_MINED_COMMITMENT, cacheKey), std::make_pair(qc, blockHash));
if (rotation_enabled) {
m_evoDb.Write(BuildInversedHeightKeyIndexed(llmq_params.type, nHeight, int(qc.quorumIndex)), pQuorumBaseBlockIndex->nHeight);
} else {
m_evoDb.Write(BuildInversedHeightKey(llmq_params.type, nHeight), pQuorumBaseBlockIndex->nHeight);
}
{
LOCK(minableCommitmentsCs);
mapHasMinedCommitmentCache[qc.llmqType].erase(qc.quorumHash);
minableCommitmentsByQuorum.erase(cacheKey);
minableCommitments.erase(::SerializeHash(qc));
}
LogPrint(BCLog::LLMQ, "%s -- processed commitment from block. type=%d, quorumIndex=%d, quorumHash=%s\n", __func__,
ToUnderlying(qc.llmqType), qc.quorumIndex, quorumHash.ToString());
return true;
}
bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex)
{
AssertLockHeld(::cs_main);
PreComputeQuorumMembers(m_dmnman, m_qsnapman, pindex, /*reset_cache=*/true);
std::multimap<Consensus::LLMQType, CFinalCommitment> qcs;
if (BlockValidationState dummy; !GetCommitmentsFromBlock(block, pindex, qcs, dummy)) {
return false;
}
for (auto& [_, qc2] : qcs) {
auto& qc = qc2; // cannot capture structured binding into lambda
if (qc.IsNull()) {
continue;
}
m_evoDb.Erase(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash)));
const auto& llmq_params_opt = Params().GetLLMQ(qc.llmqType);
assert(llmq_params_opt.has_value());
if (IsQuorumRotationEnabled(llmq_params_opt.value(), pindex)) {
m_evoDb.Erase(BuildInversedHeightKeyIndexed(qc.llmqType, pindex->nHeight, int(qc.quorumIndex)));
} else {
m_evoDb.Erase(BuildInversedHeightKey(qc.llmqType, pindex->nHeight));
}
WITH_LOCK(minableCommitmentsCs, mapHasMinedCommitmentCache[qc.llmqType].erase(qc.quorumHash));
// if a reorg happened, we should allow to mine this commitment later
AddMineableCommitment(qc);
}
m_evoDb.Write(DB_BEST_BLOCK_UPGRADE, pindex->pprev->GetBlockHash());
return true;
}
bool CQuorumBlockProcessor::GetCommitmentsFromBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex, std::multimap<Consensus::LLMQType, CFinalCommitment>& ret, BlockValidationState& state)
{
AssertLockHeld(::cs_main);
const auto& consensus = Params().GetConsensus();
ret.clear();
for (const auto& tx : block.vtx) {
if (tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
const auto opt_qc = GetTxPayload<CFinalCommitmentTxPayload>(*tx);
if (!opt_qc) {
// should not happen as it was verified before processing the block
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s height=%d GetTxPayload fails\n", __func__, pindex->nHeight);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-payload");
}
auto& qc = *opt_qc;
const auto& llmq_params_opt = Params().GetLLMQ(qc.commitment.llmqType);
if (!llmq_params_opt.has_value()) {
// should not happen as it was verified before processing the block
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-commitment-type");
}
// only allow one commitment per type and per block (This was changed with rotation)
if (!IsQuorumRotationEnabled(llmq_params_opt.value(), pindex)) {
if (ret.count(qc.commitment.llmqType) != 0) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-dup");
}
}
ret.emplace(qc.commitment.llmqType, std::move(qc.commitment));
}
}
if (pindex->nHeight < consensus.DIP0003Height && !ret.empty()) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-premature");
}
return true;
}
size_t CQuorumBlockProcessor::GetNumCommitmentsRequired(const Consensus::LLMQParams& llmqParams, int nHeight) const
{
AssertLockHeld(::cs_main);
if (!IsMiningPhase(llmqParams, m_chainstate.m_chain, nHeight)) return 0;
// Note: This function can be called for new blocks
assert(nHeight <= m_chainstate.m_chain.Height() + 1);
const auto *const pindex = m_chainstate.m_chain.Height() < nHeight ? m_chainstate.m_chain.Tip() : m_chainstate.m_chain.Tip()->GetAncestor(nHeight);
bool rotation_enabled = IsQuorumRotationEnabled(llmqParams, pindex);
size_t quorums_num = rotation_enabled ? llmqParams.signingActiveQuorumCount : 1;
size_t ret{0};
for (const auto quorumIndex : irange::range(quorums_num)) {
uint256 quorumHash = GetQuorumBlockHash(llmqParams, m_chainstate.m_chain, nHeight, quorumIndex);
if (!quorumHash.IsNull() && !HasMinedCommitment(llmqParams.type, quorumHash)) ++ret;
}
return ret;
}
// WARNING: This method returns uint256() on the first block of the DKG interval (because the block hash is not known yet)
uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& llmqParams, const CChain& active_chain, int nHeight, int quorumIndex)
{
AssertLockHeld(::cs_main);
int quorumStartHeight = nHeight - (nHeight % llmqParams.dkgInterval) + quorumIndex;
uint256 quorumBlockHash;
if (!GetBlockHash(active_chain, quorumBlockHash, quorumStartHeight)) {
LogPrint(BCLog::LLMQ, "[GetQuorumBlockHash] llmqType[%d] h[%d] qi[%d] quorumStartHeight[%d] quorumHash[EMPTY]\n", ToUnderlying(llmqParams.type), nHeight, quorumIndex, quorumStartHeight);
return {};
}
return quorumBlockHash;
}
bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const
{
bool fExists;
if (LOCK(minableCommitmentsCs); mapHasMinedCommitmentCache[llmqType].get(quorumHash, fExists)) {
return fExists;
}
fExists = m_evoDb.Exists(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash)));
LOCK(minableCommitmentsCs);
mapHasMinedCommitmentCache[llmqType].insert(quorumHash, fExists);
return fExists;
}
std::pair<CFinalCommitment, uint256> CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType,
const uint256& quorumHash) const
{
auto key = std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash));
std::pair<CFinalCommitment, uint256> ret;
if (!m_evoDb.Read(key, ret)) {
return {CFinalCommitment{}, uint256::ZERO};
}
return ret;
}
// The returned quorums are in reversed order, so the most recent one is at index 0
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pindex, size_t maxCount) const
{
AssertLockNotHeld(m_evoDb.cs);
LOCK(m_evoDb.cs);
auto dbIt = m_evoDb.GetCurTransaction().NewIteratorUniquePtr();
auto firstKey = BuildInversedHeightKey(llmqType, pindex->nHeight);
auto lastKey = BuildInversedHeightKey(llmqType, 0);
dbIt->Seek(firstKey);
std::vector<const CBlockIndex*> ret;
ret.reserve(maxCount);
while (dbIt->Valid() && ret.size() < maxCount) {
decltype(firstKey) curKey;
int quorumHeight;
if (!dbIt->GetKey(curKey) || curKey >= lastKey) {
break;
}
if (std::get<0>(curKey) != DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT || std::get<1>(curKey) != llmqType) {
break;
}
if (uint32_t nMinedHeight = std::numeric_limits<uint32_t>::max() - be32toh_internal(std::get<2>(curKey));
nMinedHeight > static_cast<uint32_t>(pindex->nHeight)) {
break;
}
if (!dbIt->GetValue(quorumHeight)) {
break;
}
const auto* pQuorumBaseBlockIndex = pindex->GetAncestor(quorumHeight);
assert(pQuorumBaseBlockIndex);
ret.emplace_back(pQuorumBaseBlockIndex);
dbIt->Next();
}
return ret;
}
std::optional<const CBlockIndex*> CQuorumBlockProcessor::GetLastMinedCommitmentsByQuorumIndexUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, int quorumIndex, size_t cycle) const
{
AssertLockNotHeld(m_evoDb.cs);
LOCK(m_evoDb.cs);
auto dbIt = m_evoDb.GetCurTransaction().NewIteratorUniquePtr();
auto firstKey = BuildInversedHeightKeyIndexed(llmqType, pindex->nHeight, quorumIndex);
auto lastKey = BuildInversedHeightKeyIndexed(llmqType, 0, quorumIndex);
size_t currentCycle = 0;
dbIt->Seek(firstKey);
while (dbIt->Valid()) {
decltype(firstKey) curKey;
int quorumHeight;
if (!dbIt->GetKey(curKey) || curKey >= lastKey) {
return std::nullopt;
}
if (std::get<0>(curKey) != DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT_Q_INDEXED || std::get<1>(curKey) != llmqType) {
return std::nullopt;
}
if (uint32_t nMinedHeight = std::numeric_limits<uint32_t>::max() - be32toh_internal(std::get<3>(curKey));
nMinedHeight > static_cast<uint32_t>(pindex->nHeight)) {
return std::nullopt;
}
if (!dbIt->GetValue(quorumHeight)) {
return std::nullopt;
}
const auto* pQuorumBaseBlockIndex = pindex->GetAncestor(quorumHeight);
assert(pQuorumBaseBlockIndex);
if (currentCycle == cycle) {
return std::make_optional(pQuorumBaseBlockIndex);
}
currentCycle++;
dbIt->Next();
}
return std::nullopt;
}
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetLastMinedCommitmentsPerQuorumIndexUntilBlock(
Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t cycle) const
{
const auto& llmq_params_opt = Params().GetLLMQ(llmqType);
assert(llmq_params_opt.has_value());
std::vector<const CBlockIndex*> ret;
for (const auto quorumIndex : irange::range(llmq_params_opt->signingActiveQuorumCount)) {
std::optional<const CBlockIndex*> q = GetLastMinedCommitmentsByQuorumIndexUntilBlock(llmqType, pindex, quorumIndex, cycle);
if (q.has_value()) {
ret.emplace_back(q.value());
}
}
return ret;
}
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsIndexedUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount) const
{
std::vector<const CBlockIndex*> ret;
size_t cycle = 0;
while (ret.size() < maxCount) {
std::vector<const CBlockIndex*> cycleRet = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(llmqType, pindex, cycle);
if (cycleRet.empty()) {
return ret;
}
size_t needToCopy = maxCount - ret.size();
std::copy_n(cycleRet.begin(), std::min(needToCopy, cycleRet.size()), std::back_inserter(ret));
cycle++;
}
return ret;
}
// The returned quorums are in reversed order, so the most recent one is at index 0
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> CQuorumBlockProcessor::GetMinedAndActiveCommitmentsUntilBlock(gsl::not_null<const CBlockIndex*> pindex) const
{
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> ret;
for (const auto& params : Params().GetConsensus().llmqs) {
auto& commitments = ret[params.type];
if (IsQuorumRotationEnabled(params, pindex)) {
commitments = GetLastMinedCommitmentsPerQuorumIndexUntilBlock(params.type, pindex, 0);
} else {
commitments = GetMinedCommitmentsUntilBlock(params.type, pindex, params.signingActiveQuorumCount);
}
}
return ret;
}
bool CQuorumBlockProcessor::HasMineableCommitment(const uint256& hash) const
{
LOCK(minableCommitmentsCs);
return minableCommitments.count(hash) != 0;
}
std::optional<CInv> CQuorumBlockProcessor::AddMineableCommitment(const CFinalCommitment& fqc)
{
const uint256 commitmentHash = ::SerializeHash(fqc);
const bool relay = [&]() {
LOCK(minableCommitmentsCs);
auto k = std::make_pair(fqc.llmqType, fqc.quorumHash);
auto [itInserted, successfullyInserted] = minableCommitmentsByQuorum.try_emplace(k, commitmentHash);
if (successfullyInserted) {
minableCommitments.try_emplace(commitmentHash, fqc);
return true;
} else {
auto& insertedQuorumHash = itInserted->second;
const auto& oldFqc = minableCommitments.at(insertedQuorumHash);
if (fqc.CountSigners() > oldFqc.CountSigners()) {
// new commitment has more signers, so override the known one
insertedQuorumHash = commitmentHash;
minableCommitments.erase(insertedQuorumHash);
minableCommitments.try_emplace(commitmentHash, fqc);
return true;
}
}
return false;
}();
return relay ? std::make_optional(CInv{MSG_QUORUM_FINAL_COMMITMENT, commitmentHash}) : std::nullopt;
}
std::optional<llmq::CFinalCommitment> CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmentHash) const
{
LOCK(minableCommitmentsCs);
auto it = minableCommitments.find(commitmentHash);
if (it == minableCommitments.end()) {
return std::nullopt;
}
return it->second;
}
// Will return nullopt if no commitment should be mined
// Will return a null commitment if no mineable commitment is known and none was mined yet
std::optional<std::vector<CFinalCommitment>> CQuorumBlockProcessor::GetMineableCommitments(const Consensus::LLMQParams& llmqParams, int nHeight) const
{
AssertLockHeld(::cs_main);
std::vector<CFinalCommitment> ret;
if (GetNumCommitmentsRequired(llmqParams, nHeight) == 0) {
// no commitment required
return std::nullopt;
}
// Note: This function can be called for new blocks
assert(nHeight <= m_chainstate.m_chain.Height() + 1);
const auto *const pindex = m_chainstate.m_chain.Height() < nHeight ? m_chainstate.m_chain.Tip() : m_chainstate.m_chain.Tip()->GetAncestor(nHeight);
bool rotation_enabled = IsQuorumRotationEnabled(llmqParams, pindex);
bool basic_bls_enabled{DeploymentActiveAfter(pindex, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)};
size_t quorums_num = rotation_enabled ? llmqParams.signingActiveQuorumCount : 1;
std::stringstream ss;
for (const auto quorumIndex : irange::range(quorums_num)) {
CFinalCommitment cf;
uint256 quorumHash = GetQuorumBlockHash(llmqParams, m_chainstate.m_chain, nHeight, quorumIndex);
if (quorumHash.IsNull()) {
break;
}
if (HasMinedCommitment(llmqParams.type, quorumHash)) continue;
LOCK(minableCommitmentsCs);
auto k = std::make_pair(llmqParams.type, quorumHash);
if (auto it = minableCommitmentsByQuorum.find(k); it == minableCommitmentsByQuorum.end()) {
// null commitment required
cf = CFinalCommitment(llmqParams, quorumHash);
cf.quorumIndex = static_cast<int16_t>(quorumIndex);
cf.nVersion = CFinalCommitment::GetVersion(rotation_enabled, basic_bls_enabled);
ss << "{ created nversion[" << cf.nVersion << "] quorumIndex[" << cf.quorumIndex << "] }";
} else {
cf = minableCommitments.at(it->second);
ss << "{ cached nversion[" << cf.nVersion << "] quorumIndex[" << cf.quorumIndex << "] }";
}
ret.push_back(std::move(cf));
}
LogPrint(BCLog::LLMQ, "GetMineableCommitments cf height[%d] content: %s\n", nHeight, ss.str());
if (ret.empty()) {
return std::nullopt;
}
return std::make_optional(ret);
}
bool CQuorumBlockProcessor::GetMineableCommitmentsTx(const Consensus::LLMQParams& llmqParams, int nHeight, std::vector<CTransactionRef>& ret) const
{
AssertLockHeld(::cs_main);
std::optional<std::vector<CFinalCommitment>> qcs = GetMineableCommitments(llmqParams, nHeight);
if (!qcs.has_value()) {
return false;
}
for (const auto& f : qcs.value()) {
CFinalCommitmentTxPayload qc;
qc.nHeight = nHeight;
qc.commitment = f;
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_QUORUM_COMMITMENT;
SetTxPayload(tx, qc);
ret.push_back(MakeTransactionRef(tx));
}
return true;
}
} // namespace llmq