Skip to content

Commit c5f3672

Browse files
committed
[refactor] Move ComputeBlockVersion into VersionBitsCache
This also changes ComputeBlockVersion to take the versionbits cache mutex once, rather than once for each versionbits deployment.
1 parent 4a69b4d commit c5f3672

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

src/miner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
121121
assert(pindexPrev != nullptr);
122122
nHeight = pindexPrev->nHeight + 1;
123123

124-
pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
124+
pblock->nVersion = g_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
125125
// -regtest only: allow overriding block.nVersion with
126126
// -blockversion=N to test forking scenarios
127127
if (chainparams.MineBlocksOnDemand())

src/test/versionbits_tests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
1616
static int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; }
1717

18+
static int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
19+
{
20+
return g_versionbitscache.ComputeBlockVersion(pindexPrev, params);
21+
}
22+
1823
static const std::string StateName(ThresholdState state)
1924
{
2025
switch (state) {

src/validation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
16271627
return pindex->nHeight >= params.MinBIP9WarningHeight &&
16281628
((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
16291629
((pindex->nVersion >> bit) & 1) != 0 &&
1630-
((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
1630+
((g_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
16311631
}
16321632
};
16331633

src/versionbits.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ uint32_t VersionBitsCache::Mask(const Consensus::Params& params, Consensus::Depl
212212
return VersionBitsConditionChecker(pos).Mask(params);
213213
}
214214

215-
extern VersionBitsCache g_versionbitscache; // removed in next commit
216-
217-
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
215+
int32_t VersionBitsCache::ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
218216
{
217+
LOCK(m_mutex);
219218
int32_t nVersion = VERSIONBITS_TOP_BITS;
220219

221220
for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
222-
ThresholdState state = g_versionbitscache.State(pindexPrev, params, static_cast<Consensus::DeploymentPos>(i));
221+
Consensus::DeploymentPos pos = static_cast<Consensus::DeploymentPos>(i);
222+
ThresholdState state = VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, m_caches[pos]);
223223
if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) {
224-
nVersion |= g_versionbitscache.Mask(params, static_cast<Consensus::DeploymentPos>(i));
224+
nVersion |= Mask(params, pos);
225225
}
226226
}
227227

src/versionbits.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ class VersionBitsCache
9393
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
9494
int StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
9595

96+
/** Determine what nVersion a new block should use
97+
*/
98+
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
99+
96100
void Clear();
97101
};
98102

99-
/**
100-
* Determine what nVersion a new block should use.
101-
*/
102-
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
103-
104103
#endif // BITCOIN_VERSIONBITS_H

0 commit comments

Comments
 (0)