Skip to content

Commit 5090031

Browse files
committed
refactor: only code move for CalcCbTxMerkleRootMNList from cbtx.h to simplifiedmns.h
1 parent ce682f2 commit 5090031

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

src/evo/cbtx.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,50 +87,6 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CCbTx& cbTx, const CBlockIn
8787
return true;
8888
}
8989

90-
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, CSimplifiedMNList&& sml, BlockValidationState& state)
91-
{
92-
try {
93-
static std::atomic<int64_t> nTimeMerkle = 0;
94-
95-
int64_t nTime1 = GetTimeMicros();
96-
97-
static Mutex cached_mutex;
98-
static CSimplifiedMNList smlCached GUARDED_BY(cached_mutex);
99-
static uint256 merkleRootCached GUARDED_BY(cached_mutex);
100-
static bool mutatedCached GUARDED_BY(cached_mutex) {false};
101-
102-
LOCK(cached_mutex);
103-
if (sml == smlCached) {
104-
merkleRootRet = merkleRootCached;
105-
if (mutatedCached) {
106-
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "mutated-cached-calc-cb-mnmerkleroot");
107-
}
108-
return true;
109-
}
110-
111-
bool mutated = false;
112-
merkleRootRet = sml.CalcMerkleRoot(&mutated);
113-
114-
int64_t nTime2 = GetTimeMicros();
115-
nTimeMerkle += nTime2 - nTime1;
116-
LogPrint(BCLog::BENCHMARK, " - CalcMerkleRoot: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1),
117-
nTimeMerkle * 0.000001);
118-
119-
smlCached = std::move(sml);
120-
merkleRootCached = merkleRootRet;
121-
mutatedCached = mutated;
122-
123-
if (mutated) {
124-
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "mutated-calc-cb-mnmerkleroot");
125-
}
126-
127-
return true;
128-
} catch (const std::exception& e) {
129-
LogPrintf("%s -- failed: %s\n", __func__, e.what());
130-
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "failed-calc-cb-mnmerkleroot");
131-
}
132-
}
133-
13490
using QcHashMap = std::map<Consensus::LLMQType, std::vector<uint256>>;
13591
using QcIndexedHashMap = std::map<Consensus::LLMQType, std::map<int16_t, uint256>>;
13692

src/evo/cbtx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ bool CheckCbTx(const CCbTx& cbTx, const CBlockIndex* pindexPrev, TxValidationSta
6969
bool CheckCbTxMerkleRoots(const CBlock& block, const CCbTx& cbTx, const CBlockIndex* pindex,
7070
const llmq::CQuorumBlockProcessor& quorum_block_processor, CSimplifiedMNList&& sml,
7171
BlockValidationState& state);
72-
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, CSimplifiedMNList&& sml, BlockValidationState& state);
7372
bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPrev,
7473
const llmq::CQuorumBlockProcessor& quorum_block_processor, uint256& merkleRootRet,
7574
BlockValidationState& state);

src/evo/simplifiedmns.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,47 @@ bool BuildSimplifiedMNListDiff(CDeterministicMNManager& dmnman, const Chainstate
402402

403403
return true;
404404
}
405+
406+
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, CSimplifiedMNList&& sml, BlockValidationState& state)
407+
{
408+
try {
409+
static std::atomic<int64_t> nTimeMerkle = 0;
410+
411+
int64_t nTime1 = GetTimeMicros();
412+
413+
static Mutex cached_mutex;
414+
static CSimplifiedMNList smlCached GUARDED_BY(cached_mutex);
415+
static uint256 merkleRootCached GUARDED_BY(cached_mutex);
416+
static bool mutatedCached GUARDED_BY(cached_mutex){false};
417+
418+
LOCK(cached_mutex);
419+
if (sml == smlCached) {
420+
merkleRootRet = merkleRootCached;
421+
if (mutatedCached) {
422+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "mutated-cached-calc-cb-mnmerkleroot");
423+
}
424+
return true;
425+
}
426+
427+
bool mutated = false;
428+
merkleRootRet = sml.CalcMerkleRoot(&mutated);
429+
430+
int64_t nTime2 = GetTimeMicros();
431+
nTimeMerkle += nTime2 - nTime1;
432+
LogPrint(BCLog::BENCHMARK, " - CalcMerkleRoot: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1),
433+
nTimeMerkle * 0.000001);
434+
435+
smlCached = std::move(sml);
436+
merkleRootCached = merkleRootRet;
437+
mutatedCached = mutated;
438+
439+
if (mutated) {
440+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "mutated-calc-cb-mnmerkleroot");
441+
}
442+
443+
return true;
444+
} catch (const std::exception& e) {
445+
LogPrintf("%s -- failed: %s\n", __func__, e.what());
446+
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "failed-calc-cb-mnmerkleroot");
447+
}
448+
}

src/evo/simplifiedmns.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,6 @@ bool BuildSimplifiedMNListDiff(CDeterministicMNManager& dmnman, const Chainstate
179179
const uint256& baseBlockHash, const uint256& blockHash, CSimplifiedMNListDiff& mnListDiffRet,
180180
std::string& errorRet, bool extended = false) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
181181

182+
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, CSimplifiedMNList&& sml, BlockValidationState& state);
183+
182184
#endif // BITCOIN_EVO_SIMPLIFIEDMNS_H

0 commit comments

Comments
 (0)