From 4c1a04a254ec7dae74af3b9a47e198539cf91afa Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Thu, 4 Feb 2021 06:01:26 +0300 Subject: [PATCH] llmq: Avoid writing commitments to evodb and altering caches when all we want is to check block candidate validity (#3980) * llmq: Avoid writing commitments to evodb and altering caches when all we want is to check block candidate validity * tests: call `getblocktemplate` to trigger `CreateNewBlock` before quorum commitment is mined --- src/evo/specialtx.cpp | 2 +- src/llmq/quorums_blockprocessor.cpp | 10 +++++++--- src/llmq/quorums_blockprocessor.h | 4 ++-- test/functional/test_framework/test_framework.py | 1 + 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/evo/specialtx.cpp b/src/evo/specialtx.cpp index aa0d7b97c0a31..71475e403925a 100644 --- a/src/evo/specialtx.cpp +++ b/src/evo/specialtx.cpp @@ -114,7 +114,7 @@ bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, CV int64_t nTime2 = GetTimeMicros(); nTimeLoop += nTime2 - nTime1; LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeLoop * 0.000001); - if (!llmq::quorumBlockProcessor->ProcessBlock(block, pindex, state)) { + if (!llmq::quorumBlockProcessor->ProcessBlock(block, pindex, state, fJustCheck)) { // pass the state returned by the function above return false; } diff --git a/src/llmq/quorums_blockprocessor.cpp b/src/llmq/quorums_blockprocessor.cpp index e79dac6c6230b..3754faf889eaa 100644 --- a/src/llmq/quorums_blockprocessor.cpp +++ b/src/llmq/quorums_blockprocessor.cpp @@ -120,7 +120,7 @@ void CQuorumBlockProcessor::ProcessMessage(CNode* pfrom, const std::string& strC } } -bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state) +bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, bool fJustCheck) { AssertLockHeld(cs_main); @@ -165,7 +165,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, const CBlockIndex* for (auto& p : qcs) { auto& qc = p.second; - if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state)) { + if (!ProcessCommitment(pindex->nHeight, blockHash, qc, state, fJustCheck)) { return false; } } @@ -183,7 +183,7 @@ static std::tuple BuildInversedHeigh return std::make_tuple(DB_MINED_COMMITMENT_BY_INVERSED_HEIGHT, llmqType, htobe32(std::numeric_limits::max() - nMinedHeight)); } -bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state) +bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck) { auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)qc.llmqType); @@ -225,6 +225,10 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH return state.DoS(100, false, REJECT_INVALID, "bad-qc-invalid"); } + if (fJustCheck) { + return true; + } + // Store commitment in DB auto cacheKey = std::make_pair(params.type, quorumHash); evoDb.Write(std::make_pair(DB_MINED_COMMITMENT, cacheKey), std::make_pair(qc, blockHash)); diff --git a/src/llmq/quorums_blockprocessor.h b/src/llmq/quorums_blockprocessor.h index e8aae0934c0c6..3f298c7cea40b 100644 --- a/src/llmq/quorums_blockprocessor.h +++ b/src/llmq/quorums_blockprocessor.h @@ -42,7 +42,7 @@ class CQuorumBlockProcessor void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv); - bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state); + bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, bool fJustCheck); bool UndoBlock(const CBlock& block, const CBlockIndex* pindex); void AddMinableCommitment(const CFinalCommitment& fqc); @@ -59,7 +59,7 @@ class CQuorumBlockProcessor private: static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::map& ret, CValidationState& state); - bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state); + bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck); static bool IsMiningPhase(Consensus::LLMQType llmqType, int nHeight); bool IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight); static uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight); diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 4f2fa0c08fda7..f7475493470dd 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -1044,6 +1044,7 @@ def mine_quorum(self, expected_connections=None, expected_members=None, expected self.log.info("Mining final commitment") self.bump_mocktime(1, nodes=nodes) + self.nodes[0].getblocktemplate() # this calls CreateNewBlock self.nodes[0].generate(1) sync_blocks(nodes)