mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
[refactor] Move ComputeBlockVersion into VersionBitsCache
This also changes ComputeBlockVersion to take the versionbits cache mutex once, rather than once for each versionbits deployment.
This commit is contained in:
parent
4a69b4dbe0
commit
c5f36725e8
5 changed files with 16 additions and 12 deletions
|
@ -121,7 +121,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
|
||||||
assert(pindexPrev != nullptr);
|
assert(pindexPrev != nullptr);
|
||||||
nHeight = pindexPrev->nHeight + 1;
|
nHeight = pindexPrev->nHeight + 1;
|
||||||
|
|
||||||
pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
|
pblock->nVersion = g_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
|
||||||
// -regtest only: allow overriding block.nVersion with
|
// -regtest only: allow overriding block.nVersion with
|
||||||
// -blockversion=N to test forking scenarios
|
// -blockversion=N to test forking scenarios
|
||||||
if (chainparams.MineBlocksOnDemand())
|
if (chainparams.MineBlocksOnDemand())
|
||||||
|
|
|
@ -15,6 +15,11 @@
|
||||||
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
|
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
|
||||||
static int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; }
|
static int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; }
|
||||||
|
|
||||||
|
static int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
|
||||||
|
{
|
||||||
|
return g_versionbitscache.ComputeBlockVersion(pindexPrev, params);
|
||||||
|
}
|
||||||
|
|
||||||
static const std::string StateName(ThresholdState state)
|
static const std::string StateName(ThresholdState state)
|
||||||
{
|
{
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
|
|
@ -1627,7 +1627,7 @@ public:
|
||||||
return pindex->nHeight >= params.MinBIP9WarningHeight &&
|
return pindex->nHeight >= params.MinBIP9WarningHeight &&
|
||||||
((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
|
((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
|
||||||
((pindex->nVersion >> bit) & 1) != 0 &&
|
((pindex->nVersion >> bit) & 1) != 0 &&
|
||||||
((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
|
((g_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -212,16 +212,16 @@ uint32_t VersionBitsCache::Mask(const Consensus::Params& params, Consensus::Depl
|
||||||
return VersionBitsConditionChecker(pos).Mask(params);
|
return VersionBitsConditionChecker(pos).Mask(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern VersionBitsCache g_versionbitscache; // removed in next commit
|
int32_t VersionBitsCache::ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
|
||||||
|
|
||||||
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
|
|
||||||
{
|
{
|
||||||
|
LOCK(m_mutex);
|
||||||
int32_t nVersion = VERSIONBITS_TOP_BITS;
|
int32_t nVersion = VERSIONBITS_TOP_BITS;
|
||||||
|
|
||||||
for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
|
for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
|
||||||
ThresholdState state = g_versionbitscache.State(pindexPrev, params, static_cast<Consensus::DeploymentPos>(i));
|
Consensus::DeploymentPos pos = static_cast<Consensus::DeploymentPos>(i);
|
||||||
|
ThresholdState state = VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, m_caches[pos]);
|
||||||
if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) {
|
if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) {
|
||||||
nVersion |= g_versionbitscache.Mask(params, static_cast<Consensus::DeploymentPos>(i));
|
nVersion |= Mask(params, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -93,12 +93,11 @@ public:
|
||||||
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
|
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
|
||||||
int StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
|
int StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
|
||||||
|
|
||||||
|
/** Determine what nVersion a new block should use
|
||||||
|
*/
|
||||||
|
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine what nVersion a new block should use.
|
|
||||||
*/
|
|
||||||
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params);
|
|
||||||
|
|
||||||
#endif // BITCOIN_VERSIONBITS_H
|
#endif // BITCOIN_VERSIONBITS_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue