Skip to content

Commit c887cff

Browse files
committed
merge bitcoin#24595: move g_versionbitscache global to ChainstateManager
includes: - d603f1d - eca22c7 - bb5c24b
1 parent 8d53f31 commit c887cff

File tree

9 files changed

+89
-65
lines changed

9 files changed

+89
-65
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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,30 @@
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)
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)
20+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
2421
{
2522
assert(Consensus::ValidDeployment(dep));
26-
return ThresholdState::ACTIVE == g_versionbitscache.State(pindexPrev, params, dep);
23+
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)
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)
33+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
3734
{
3835
assert(Consensus::ValidDeployment(dep));
39-
return DeploymentActiveAfter(index.pprev, params, dep);
36+
return DeploymentActiveAfter(index.pprev, params, dep, versionbitscache);
4037
}
4138

4239
/** Determine if a deployment is enabled (can ever be active) */

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
212212
nBlockMaxSize = std::max<unsigned int>(1000, std::min<unsigned int>(MaxBlockSize(fDIP0001Active_context) - 1000, nBlockMaxSize));
213213
nBlockMaxSigOps = MaxBlockSigOps(fDIP0001Active_context);
214214

215-
pblock->nVersion = g_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
215+
pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
216216
// Non-mainnet only: allow overriding block.nVersion with
217217
// -blockversion=N to test forking scenarios
218218
if (Params().NetworkIDString() != CBaseChainParams::MAIN) {

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, const std:
13901390
if (!DeploymentEnabled(chainman, id)) return;
13911391

13921392
UniValue bip9(UniValue::VOBJ);
1393-
const ThresholdState thresholdState = g_versionbitscache.State(active_chain_tip, chainman.GetConsensus(), id);
1393+
const ThresholdState thresholdState = chainman.m_versionbitscache.State(active_chain_tip, chainman.GetConsensus(), id);
13941394
switch (thresholdState) {
13951395
case ThresholdState::DEFINED: bip9.pushKV("status", "defined"); break;
13961396
case ThresholdState::STARTED: bip9.pushKV("status", "started"); break;
@@ -1409,11 +1409,11 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, const std:
14091409
if (auto it = signals.find(chainman.GetConsensus().vDeployments[id].bit); it != signals.end()) {
14101410
bip9.pushKV("ehf_height", it->second);
14111411
}
1412-
int64_t since_height = g_versionbitscache.StateSinceHeight(active_chain_tip, chainman.GetConsensus(), id);
1412+
int64_t since_height = chainman.m_versionbitscache.StateSinceHeight(active_chain_tip, chainman.GetConsensus(), id);
14131413
bip9.pushKV("since", since_height);
14141414
if (has_signal) {
14151415
UniValue statsUV(UniValue::VOBJ);
1416-
BIP9Stats statsStruct = g_versionbitscache.Statistics(active_chain_tip, chainman.GetConsensus(), id);
1416+
BIP9Stats statsStruct = chainman.m_versionbitscache.Statistics(active_chain_tip, chainman.GetConsensus(), id);
14171417
statsUV.pushKV("period", statsStruct.period);
14181418
statsUV.pushKV("elapsed", statsStruct.elapsed);
14191419
statsUV.pushKV("count", statsStruct.count);

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,15 @@ static RPCHelpMan getblocktemplate()
876876
UniValue vbavailable(UniValue::VOBJ);
877877
for (int j = 0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
878878
Consensus::DeploymentPos pos = Consensus::DeploymentPos(j);
879-
ThresholdState state = g_versionbitscache.State(pindexPrev, consensusParams, pos);
879+
ThresholdState state = chainman.m_versionbitscache.State(pindexPrev, consensusParams, pos);
880880
switch (state) {
881881
case ThresholdState::DEFINED:
882882
case ThresholdState::FAILED:
883883
// Not exposed to GBT at all
884884
break;
885885
case ThresholdState::LOCKED_IN:
886886
// Ensure bit is set in block version
887-
pblock->nVersion |= g_versionbitscache.Mask(consensusParams, pos);
887+
pblock->nVersion |= chainman.m_versionbitscache.Mask(consensusParams, pos);
888888
[[fallthrough]];
889889
case ThresholdState::STARTED:
890890
{
@@ -893,7 +893,7 @@ static RPCHelpMan getblocktemplate()
893893
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
894894
if (!vbinfo.gbt_force) {
895895
// If the client doesn't support this, don't indicate it in the [default] version
896-
pblock->nVersion &= ~g_versionbitscache.Mask(consensusParams, pos);
896+
pblock->nVersion &= ~chainman.m_versionbitscache.Mask(consensusParams, pos);
897897
}
898898
}
899899
break;

src/test/dynamic_activation_thresholds_tests.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ struct TestChainDATSetup : public TestChainSetup
5050
}
5151
LOCK(cs_main);
5252
if (expected_lockin) {
53-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::LOCKED_IN);
53+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
54+
consensus_params, deployment_id),
55+
ThresholdState::LOCKED_IN);
5456
} else {
55-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::STARTED);
57+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
58+
consensus_params, deployment_id),
59+
ThresholdState::STARTED);
5660
}
5761
}
5862

@@ -64,7 +68,9 @@ struct TestChainDATSetup : public TestChainSetup
6468
{
6569
LOCK(cs_main);
6670
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Height(), window - 2);
67-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::DEFINED);
71+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
72+
consensus_params, deployment_id),
73+
ThresholdState::DEFINED);
6874
}
6975

7076
CreateAndProcessBlock({}, coinbasePubKey);
@@ -73,8 +79,13 @@ struct TestChainDATSetup : public TestChainSetup
7379
LOCK(cs_main);
7480
// Advance from DEFINED to STARTED at height = window - 1
7581
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Height(), window - 1);
76-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::STARTED);
77-
BOOST_CHECK_EQUAL(g_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id).threshold, threshold(0));
82+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
83+
consensus_params, deployment_id),
84+
ThresholdState::STARTED);
85+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache
86+
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
87+
.threshold,
88+
threshold(0));
7889
// Next block should be signaling by default
7990
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
8091
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
@@ -90,17 +101,25 @@ struct TestChainDATSetup : public TestChainSetup
90101
// Still STARTED but with a (potentially) new threshold
91102
LOCK(cs_main);
92103
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Height(), window * (i + 2) - 1);
93-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::STARTED);
94-
const auto vbts = g_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id);
104+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
105+
consensus_params, deployment_id),
106+
ThresholdState::STARTED);
107+
const auto vbts = m_node.chainman->m_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(),
108+
consensus_params, deployment_id);
95109
BOOST_CHECK_EQUAL(vbts.threshold, threshold(i + 1));
96110
BOOST_CHECK(vbts.threshold <= th_start);
97111
BOOST_CHECK(vbts.threshold >= th_end);
98112
}
99113
}
100114
if (LOCK(cs_main); check_activation_at_min) {
101-
BOOST_CHECK_EQUAL(g_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id).threshold, th_end);
115+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache
116+
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
117+
.threshold,
118+
th_end);
102119
} else {
103-
BOOST_CHECK(g_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id).threshold > th_end);
120+
BOOST_CHECK(m_node.chainman->m_versionbitscache
121+
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
122+
.threshold > th_end);
104123
}
105124

106125
// activate
@@ -110,7 +129,9 @@ struct TestChainDATSetup : public TestChainSetup
110129
}
111130
{
112131
LOCK(cs_main);
113-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::ACTIVE);
132+
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
133+
consensus_params, deployment_id),
134+
ThresholdState::ACTIVE);
114135
}
115136

116137
}

0 commit comments

Comments
 (0)