Skip to content

Commit bb5c24b

Browse files
committed
validation: move g_versionbitscache into ChainstateManager
1 parent eca22c7 commit bb5c24b

File tree

8 files changed

+36
-33
lines changed

8 files changed

+36
-33
lines changed

src/deploymentstatus.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include <type_traits>
1111

12-
VersionBitsCache g_versionbitscache;
13-
1412
/* Basic sanity checking for BuriedDeployment/DeploymentPos enums and
1513
* ValidDeployment check */
1614

src/deploymentstatus.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,27 @@
1010

1111
#include <limits>
1212

13-
/** Global cache for versionbits deployment status */
14-
extern VersionBitsCache g_versionbitscache;
15-
1613
/** Determine if a deployment is active for the next block */
17-
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache = g_versionbitscache)
14+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache)
1815
{
1916
assert(Consensus::ValidDeployment(dep));
2017
return (pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1) >= params.DeploymentHeight(dep);
2118
}
2219

23-
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache = g_versionbitscache)
20+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
2421
{
2522
assert(Consensus::ValidDeployment(dep));
2623
return ThresholdState::ACTIVE == versionbitscache.State(pindexPrev, params, dep);
2724
}
2825

2926
/** Determine if a deployment is active for this block */
30-
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache = g_versionbitscache)
27+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep, [[maybe_unused]] VersionBitsCache& versionbitscache)
3128
{
3229
assert(Consensus::ValidDeployment(dep));
3330
return index.nHeight >= params.DeploymentHeight(dep);
3431
}
3532

36-
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache = g_versionbitscache)
33+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
3734
{
3835
assert(Consensus::ValidDeployment(dep));
3936
return DeploymentActiveAfter(index.pprev, params, dep, versionbitscache);

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
126126
assert(pindexPrev != nullptr);
127127
nHeight = pindexPrev->nHeight + 1;
128128

129-
pblock->nVersion = g_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
129+
pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
130130
// -regtest only: allow overriding block.nVersion with
131131
// -blockversion=N to test forking scenarios
132132
if (chainparams.MineBlocksOnDemand()) {

src/rpc/blockchain.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,8 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, UniValue& softfo
10991099

11001100
UniValue bip9(UniValue::VOBJ);
11011101

1102-
const ThresholdState next_state = g_versionbitscache.State(blockindex, chainman.GetConsensus(), id);
1103-
const ThresholdState current_state = g_versionbitscache.State(blockindex->pprev, chainman.GetConsensus(), id);
1102+
const ThresholdState next_state = chainman.m_versionbitscache.State(blockindex, chainman.GetConsensus(), id);
1103+
const ThresholdState current_state = chainman.m_versionbitscache.State(blockindex->pprev, chainman.GetConsensus(), id);
11041104

11051105
const bool has_signal = (ThresholdState::STARTED == current_state || ThresholdState::LOCKED_IN == current_state);
11061106

@@ -1114,14 +1114,14 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, UniValue& softfo
11141114

11151115
// BIP9 status
11161116
bip9.pushKV("status", get_state_name(current_state));
1117-
bip9.pushKV("since", g_versionbitscache.StateSinceHeight(blockindex->pprev, chainman.GetConsensus(), id));
1117+
bip9.pushKV("since", chainman.m_versionbitscache.StateSinceHeight(blockindex->pprev, chainman.GetConsensus(), id));
11181118
bip9.pushKV("status_next", get_state_name(next_state));
11191119

11201120
// BIP9 signalling status, if applicable
11211121
if (has_signal) {
11221122
UniValue statsUV(UniValue::VOBJ);
11231123
std::vector<bool> signals;
1124-
BIP9Stats statsStruct = g_versionbitscache.Statistics(blockindex, chainman.GetConsensus(), id, &signals);
1124+
BIP9Stats statsStruct = chainman.m_versionbitscache.Statistics(blockindex, chainman.GetConsensus(), id, &signals);
11251125
statsUV.pushKV("period", statsStruct.period);
11261126
statsUV.pushKV("elapsed", statsStruct.elapsed);
11271127
statsUV.pushKV("count", statsStruct.count);
@@ -1142,7 +1142,7 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, UniValue& softfo
11421142
UniValue rv(UniValue::VOBJ);
11431143
rv.pushKV("type", "bip9");
11441144
if (ThresholdState::ACTIVE == next_state) {
1145-
rv.pushKV("height", g_versionbitscache.StateSinceHeight(blockindex, chainman.GetConsensus(), id));
1145+
rv.pushKV("height", chainman.m_versionbitscache.StateSinceHeight(blockindex, chainman.GetConsensus(), id));
11461146
}
11471147
rv.pushKV("active", ThresholdState::ACTIVE == next_state);
11481148
rv.pushKV("bip9", bip9);

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,15 +836,15 @@ static RPCHelpMan getblocktemplate()
836836
UniValue vbavailable(UniValue::VOBJ);
837837
for (int j = 0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
838838
Consensus::DeploymentPos pos = Consensus::DeploymentPos(j);
839-
ThresholdState state = g_versionbitscache.State(pindexPrev, consensusParams, pos);
839+
ThresholdState state = chainman.m_versionbitscache.State(pindexPrev, consensusParams, pos);
840840
switch (state) {
841841
case ThresholdState::DEFINED:
842842
case ThresholdState::FAILED:
843843
// Not exposed to GBT at all
844844
break;
845845
case ThresholdState::LOCKED_IN:
846846
// Ensure bit is set in block version
847-
pblock->nVersion |= g_versionbitscache.Mask(consensusParams, pos);
847+
pblock->nVersion |= chainman.m_versionbitscache.Mask(consensusParams, pos);
848848
[[fallthrough]];
849849
case ThresholdState::STARTED:
850850
{
@@ -853,7 +853,7 @@ static RPCHelpMan getblocktemplate()
853853
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
854854
if (!vbinfo.gbt_force) {
855855
// If the client doesn't support this, don't indicate it in the [default] version
856-
pblock->nVersion &= ~g_versionbitscache.Mask(consensusParams, pos);
856+
pblock->nVersion &= ~chainman.m_versionbitscache.Mask(consensusParams, pos);
857857
}
858858
}
859859
break;

src/test/versionbits_tests.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <chain.h>
66
#include <chainparams.h>
77
#include <consensus/params.h>
8-
#include <deploymentstatus.h>
98
#include <test/util/setup_common.h>
109
#include <validation.h>
1110
#include <versionbits.h>
@@ -414,6 +413,8 @@ static void check_computeblockversion(VersionBitsCache& versionbitscache, const
414413

415414
BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
416415
{
416+
VersionBitsCache vbcache; // don't use chainman versionbitscache since we want custom chain params
417+
417418
// check that any deployment on any chain can conceivably reach both
418419
// ACTIVE and FAILED states in roughly the way we expect
419420
for (const auto& chain_name : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::SIGNET, CBaseChainParams::REGTEST}) {
@@ -426,10 +427,10 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
426427
// not take precedence over STARTED/LOCKED_IN. So all softforks on
427428
// the same bit might overlap, even when non-overlapping start-end
428429
// times are picked.
429-
const uint32_t dep_mask{g_versionbitscache.Mask(chainParams->GetConsensus(), dep)};
430+
const uint32_t dep_mask{vbcache.Mask(chainParams->GetConsensus(), dep)};
430431
BOOST_CHECK(!(chain_all_vbits & dep_mask));
431432
chain_all_vbits |= dep_mask;
432-
check_computeblockversion(g_versionbitscache, chainParams->GetConsensus(), dep);
433+
check_computeblockversion(vbcache, chainParams->GetConsensus(), dep);
433434
}
434435
}
435436

@@ -439,7 +440,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
439440
ArgsManager args;
440441
args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999"); // January 1, 2008 - December 31, 2008
441442
const auto chainParams = CreateChainParams(args, CBaseChainParams::REGTEST);
442-
check_computeblockversion(g_versionbitscache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
443+
check_computeblockversion(vbcache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
443444
}
444445

445446
{
@@ -449,7 +450,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
449450
ArgsManager args;
450451
args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999:403200"); // January 1, 2008 - December 31, 2008, min act height 403200
451452
const auto chainParams = CreateChainParams(args, CBaseChainParams::REGTEST);
452-
check_computeblockversion(g_versionbitscache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
453+
check_computeblockversion(vbcache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
453454
}
454455
}
455456

src/validation.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,10 +1907,11 @@ void StopScriptCheckWorkerThreads()
19071907
class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
19081908
{
19091909
private:
1910-
int bit;
1910+
const ChainstateManager& m_chainman;
1911+
int m_bit;
19111912

19121913
public:
1913-
explicit WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
1914+
explicit WarningBitsConditionChecker(const ChainstateManager& chainman, int bit) : m_chainman{chainman}, m_bit(bit) {}
19141915

19151916
int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
19161917
int64_t EndTime(const Consensus::Params& params) const override { return std::numeric_limits<int64_t>::max(); }
@@ -1921,8 +1922,8 @@ class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
19211922
{
19221923
return pindex->nHeight >= params.MinBIP9WarningHeight &&
19231924
((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
1924-
((pindex->nVersion >> bit) & 1) != 0 &&
1925-
((g_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
1925+
((pindex->nVersion >> m_bit) & 1) != 0 &&
1926+
((m_chainman.m_versionbitscache.ComputeBlockVersion(pindex->pprev, params) >> m_bit) & 1) == 0;
19261927
}
19271928
};
19281929

@@ -2556,7 +2557,7 @@ void CChainState::UpdateTip(const CBlockIndex* pindexNew)
25562557
if (!this->IsInitialBlockDownload()) {
25572558
const CBlockIndex* pindex = pindexNew;
25582559
for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2559-
WarningBitsConditionChecker checker(bit);
2560+
WarningBitsConditionChecker checker(m_chainman, bit);
25602561
ThresholdState state = checker.GetStateFor(pindex, m_params.GetConsensus(), warningcache.at(bit));
25612562
if (state == ThresholdState::ACTIVE || state == ThresholdState::LOCKED_IN) {
25622563
const bilingual_str warning = strprintf(_("Unknown new rules activated (versionbit %i)"), bit);
@@ -5216,9 +5217,9 @@ ChainstateManager::~ChainstateManager()
52165217
{
52175218
LOCK(::cs_main);
52185219

5219-
// TODO: The version bits cache and warning cache should probably become
5220-
// non-globals
5221-
g_versionbitscache.Clear();
5220+
m_versionbitscache.Clear();
5221+
5222+
// TODO: The warning cache should probably become non-global
52225223
for (auto& i : warningcache) {
52235224
i.clear();
52245225
}

src/validation.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <util/check.h>
2929
#include <util/hasher.h>
3030
#include <util/translation.h>
31+
#include <versionbits.h>
3132

3233
#include <atomic>
3334
#include <map>
@@ -937,6 +938,11 @@ class ChainstateManager
937938
return m_blockman.m_block_index;
938939
}
939940

941+
/**
942+
* Track versionbit status
943+
*/
944+
mutable VersionBitsCache m_versionbitscache;
945+
940946
//! @returns true if a snapshot-based chainstate is in use. Also implies
941947
//! that a background validation chainstate is also in use.
942948
bool IsSnapshotActive() const;
@@ -1008,13 +1014,13 @@ class ChainstateManager
10081014
template<typename DEP>
10091015
bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const ChainstateManager& chainman, DEP dep)
10101016
{
1011-
return DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), dep);
1017+
return DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
10121018
}
10131019

10141020
template<typename DEP>
10151021
bool DeploymentActiveAt(const CBlockIndex& index, const ChainstateManager& chainman, DEP dep)
10161022
{
1017-
return DeploymentActiveAt(index, chainman.GetConsensus(), dep);
1023+
return DeploymentActiveAt(index, chainman.GetConsensus(), dep, chainman.m_versionbitscache);
10181024
}
10191025

10201026
template<typename DEP>

0 commit comments

Comments
 (0)