Skip to content

Commit 1c00d0a

Browse files
committed
merge bitcoin#24595: move g_versionbitscache global to ChainstateManager
includes: - d603f1d - eca22c7 - bb5c24b
1 parent 7fd5745 commit 1c00d0a

File tree

9 files changed

+97
-71
lines changed

9 files changed

+97
-71
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,8 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, const std::unord
14321432

14331433
UniValue bip9(UniValue::VOBJ);
14341434

1435-
const ThresholdState next_state = g_versionbitscache.State(blockindex, chainman.GetConsensus(), id);
1436-
const ThresholdState current_state = g_versionbitscache.State(blockindex->pprev, chainman.GetConsensus(), id);
1435+
const ThresholdState next_state = chainman.m_versionbitscache.State(blockindex, chainman.GetConsensus(), id);
1436+
const ThresholdState current_state = chainman.m_versionbitscache.State(blockindex->pprev, chainman.GetConsensus(), id);
14371437

14381438
const bool has_signal = (ThresholdState::STARTED == current_state || ThresholdState::LOCKED_IN == current_state);
14391439

@@ -1451,15 +1451,15 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, const std::unord
14511451

14521452
// BIP9 status
14531453
bip9.pushKV("status", get_state_name(current_state));
1454-
int64_t since_height = g_versionbitscache.StateSinceHeight(blockindex->pprev, chainman.GetConsensus(), id);
1454+
int64_t since_height = chainman.m_versionbitscache.StateSinceHeight(blockindex->pprev, chainman.GetConsensus(), id);
14551455
bip9.pushKV("since", since_height);
14561456
bip9.pushKV("status_next", get_state_name(next_state));
14571457

14581458
// BIP9 signalling status, if applicable
14591459
if (has_signal) {
14601460
UniValue statsUV(UniValue::VOBJ);
14611461
std::vector<bool> signals;
1462-
BIP9Stats statsStruct = g_versionbitscache.Statistics(blockindex, chainman.GetConsensus(), id, &signals);
1462+
BIP9Stats statsStruct = chainman.m_versionbitscache.Statistics(blockindex, chainman.GetConsensus(), id, &signals);
14631463
statsUV.pushKV("period", statsStruct.period);
14641464
statsUV.pushKV("elapsed", statsStruct.elapsed);
14651465
statsUV.pushKV("count", statsStruct.count);
@@ -1483,7 +1483,7 @@ static void SoftForkDescPushBack(const CBlockIndex* blockindex, const std::unord
14831483
UniValue rv(UniValue::VOBJ);
14841484
rv.pushKV("type", "bip9");
14851485
if (ThresholdState::ACTIVE == next_state) {
1486-
rv.pushKV("height", g_versionbitscache.StateSinceHeight(blockindex, chainman.GetConsensus(), id));
1486+
rv.pushKV("height", chainman.m_versionbitscache.StateSinceHeight(blockindex, chainman.GetConsensus(), id));
14871487
}
14881488
rv.pushKV("active", ThresholdState::ACTIVE == next_state);
14891489
rv.pushKV("bip9", bip9);

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: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct TestChainDATSetup : public TestChainSetup
3434

3535
void signal(int num_blocks, bool expected_lockin)
3636
{
37-
const auto& consensus_params = Params().GetConsensus();
37+
const auto& chainman = *Assert(m_node.chainman);
3838
CScript coinbasePubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
3939
// Mine non-signalling blocks
4040
gArgs.ForceSetArg("-blockversion", "536870912");
@@ -50,21 +50,27 @@ 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(chainman.m_versionbitscache.State(chainman.ActiveChain().Tip(), chainman.GetConsensus(),
54+
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(chainman.m_versionbitscache.State(chainman.ActiveChain().Tip(), chainman.GetConsensus(),
58+
deployment_id),
59+
ThresholdState::STARTED);
5660
}
5761
}
5862

5963
void test(int activation_index, bool check_activation_at_min)
6064
{
61-
const auto& consensus_params = Params().GetConsensus();
65+
const auto& chainman = *Assert(m_node.chainman);
6266
CScript coinbasePubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
6367

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(chainman.m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
72+
chainman.GetConsensus(), deployment_id),
73+
ThresholdState::DEFINED);
6874
}
6975

7076
CreateAndProcessBlock({}, coinbasePubKey);
@@ -73,11 +79,18 @@ 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(chainman.m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
83+
chainman.GetConsensus(), deployment_id),
84+
ThresholdState::STARTED);
85+
BOOST_CHECK_EQUAL(chainman.m_versionbitscache
86+
.Statistics(m_node.chainman->ActiveChain().Tip(), chainman.GetConsensus(), deployment_id)
87+
.threshold,
88+
threshold(0));
7889
// Next block should be signaling by default
79-
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
80-
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
90+
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node,
91+
m_node.mempool.get(), chainman.GetParams())
92+
.CreateNewBlock(coinbasePubKey);
93+
const uint32_t bitmask = ((uint32_t)1) << chainman.GetConsensus().vDeployments[deployment_id].bit;
8194
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Tip()->nVersion & bitmask, 0);
8295
BOOST_CHECK_EQUAL(pblocktemplate->block.nVersion & bitmask, bitmask);
8396
}
@@ -90,17 +103,25 @@ struct TestChainDATSetup : public TestChainSetup
90103
// Still STARTED but with a (potentially) new threshold
91104
LOCK(cs_main);
92105
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);
106+
BOOST_CHECK_EQUAL(chainman.m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
107+
chainman.GetConsensus(), deployment_id),
108+
ThresholdState::STARTED);
109+
const auto vbts = chainman.m_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(),
110+
chainman.GetConsensus(), deployment_id);
95111
BOOST_CHECK_EQUAL(vbts.threshold, threshold(i + 1));
96112
BOOST_CHECK(vbts.threshold <= th_start);
97113
BOOST_CHECK(vbts.threshold >= th_end);
98114
}
99115
}
100116
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);
117+
BOOST_CHECK_EQUAL(chainman.m_versionbitscache
118+
.Statistics(m_node.chainman->ActiveChain().Tip(), chainman.GetConsensus(), deployment_id)
119+
.threshold,
120+
th_end);
102121
} else {
103-
BOOST_CHECK(g_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id).threshold > th_end);
122+
BOOST_CHECK(chainman.m_versionbitscache
123+
.Statistics(m_node.chainman->ActiveChain().Tip(), chainman.GetConsensus(), deployment_id)
124+
.threshold > th_end);
104125
}
105126

106127
// activate
@@ -110,7 +131,9 @@ struct TestChainDATSetup : public TestChainSetup
110131
}
111132
{
112133
LOCK(cs_main);
113-
BOOST_CHECK_EQUAL(g_versionbitscache.State(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id), ThresholdState::ACTIVE);
134+
BOOST_CHECK_EQUAL(chainman.m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
135+
chainman.GetConsensus(), deployment_id),
136+
ThresholdState::ACTIVE);
114137
}
115138

116139
}

0 commit comments

Comments
 (0)