Skip to content

Commit 95d5f97

Browse files
committed
merge bitcoin#24595: move g_versionbitscache global to ChainstateManager
includes: - d603f1d - eca22c7 - bb5c24b
1 parent 14a77f6 commit 95d5f97

11 files changed

+101
-77
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,20 +10,17 @@
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 */
1714
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep)
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 */
@@ -33,10 +30,10 @@ inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params
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/evo/specialtxman.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ bool CSpecialTxProcessor::RebuildListFromBlock(const CBlock& block, gsl::not_nul
216216

217217
newList.DecreaseScores();
218218

219-
const bool isMNRewardReallocation{DeploymentActiveAfter(pindexPrev, m_consensus_params, Consensus::DEPLOYMENT_MN_RR)};
220-
const bool is_v24_deployed{DeploymentActiveAfter(pindexPrev, m_consensus_params, Consensus::DEPLOYMENT_V24)};
219+
const bool isMNRewardReallocation{
220+
DeploymentActiveAfter(pindexPrev, m_chainman.GetConsensus(), Consensus::DEPLOYMENT_MN_RR)};
221+
const bool is_v24_deployed{DeploymentActiveAfter(pindexPrev, m_chainman, Consensus::DEPLOYMENT_V24)};
221222

222223
// we skip the coinbase
223224
for (int i = 1; i < (int)block.vtx.size(); i++) {

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
@@ -1428,7 +1428,7 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, const std:
14281428
if (!DeploymentEnabled(chainman, id)) return;
14291429

14301430
UniValue bip9(UniValue::VOBJ);
1431-
const ThresholdState thresholdState = g_versionbitscache.State(active_chain_tip, chainman.GetConsensus(), id);
1431+
const ThresholdState thresholdState = chainman.m_versionbitscache.State(active_chain_tip, chainman.GetConsensus(), id);
14321432
switch (thresholdState) {
14331433
case ThresholdState::DEFINED: bip9.pushKV("status", "defined"); break;
14341434
case ThresholdState::STARTED: bip9.pushKV("status", "started"); break;
@@ -1447,11 +1447,11 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, const std:
14471447
if (auto it = signals.find(chainman.GetConsensus().vDeployments[id].bit); it != signals.end()) {
14481448
bip9.pushKV("ehf_height", it->second);
14491449
}
1450-
int64_t since_height = g_versionbitscache.StateSinceHeight(active_chain_tip, chainman.GetConsensus(), id);
1450+
int64_t since_height = chainman.m_versionbitscache.StateSinceHeight(active_chain_tip, chainman.GetConsensus(), id);
14511451
bip9.pushKV("since", since_height);
14521452
if (has_signal) {
14531453
UniValue statsUV(UniValue::VOBJ);
1454-
BIP9Stats statsStruct = g_versionbitscache.Statistics(active_chain_tip, chainman.GetConsensus(), id);
1454+
BIP9Stats statsStruct = chainman.m_versionbitscache.Statistics(active_chain_tip, chainman.GetConsensus(), id);
14551455
statsUV.pushKV("period", statsStruct.period);
14561456
statsUV.pushKV("elapsed", statsStruct.elapsed);
14571457
statsUV.pushKV("count", statsStruct.count);

src/rpc/mining.cpp

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

src/test/evo_deterministicmns_tests.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void FuncV19Activation(TestChainSetup& setup)
286286
auto& chainman = *Assert(setup.m_node.chainman.get());
287287
auto& dmnman = *Assert(setup.m_node.dmnman);
288288

289-
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
289+
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
290290

291291
// create
292292
auto utxos = BuildSimpleUtxoMap(setup.m_coinbase_txns);
@@ -303,7 +303,7 @@ void FuncV19Activation(TestChainSetup& setup)
303303

304304
auto block = std::make_shared<CBlock>(setup.CreateBlock({tx_reg}, coinbase_pk, chainman.ActiveChainstate()));
305305
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
306-
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
306+
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
307307
++nHeight;
308308
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight);
309309
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
@@ -321,7 +321,7 @@ void FuncV19Activation(TestChainSetup& setup)
321321

322322
block = std::make_shared<CBlock>(setup.CreateBlock({tx_upreg}, coinbase_pk, chainman.ActiveChainstate()));
323323
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
324-
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
324+
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
325325
++nHeight;
326326
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight);
327327
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
@@ -341,7 +341,7 @@ void FuncV19Activation(TestChainSetup& setup)
341341
BOOST_REQUIRE(SignSignature(signing_provider, CTransaction(tx_reg), tx_spend, 0, SIGHASH_ALL));
342342
block = std::make_shared<CBlock>(setup.CreateBlock({tx_spend}, coinbase_pk, chainman.ActiveChainstate()));
343343
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
344-
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
344+
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
345345
++nHeight;
346346
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight);
347347
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
@@ -353,7 +353,7 @@ void FuncV19Activation(TestChainSetup& setup)
353353

354354
// mine another block so that it's not the last one before V19
355355
setup.CreateAndProcessBlock({}, coinbase_pk);
356-
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
356+
BOOST_REQUIRE(!DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
357357
++nHeight;
358358
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight);
359359
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
@@ -365,7 +365,7 @@ void FuncV19Activation(TestChainSetup& setup)
365365

366366
// this block should activate V19
367367
setup.CreateAndProcessBlock({}, coinbase_pk);
368-
BOOST_REQUIRE(DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
368+
BOOST_REQUIRE(DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
369369
++nHeight;
370370
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight);
371371
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
@@ -387,7 +387,7 @@ void FuncV19Activation(TestChainSetup& setup)
387387
{
388388
setup.CreateAndProcessBlock({}, coinbase_pk);
389389
BOOST_REQUIRE(
390-
DeploymentActiveAfter(chainman.ActiveChain().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_V19));
390+
DeploymentActiveAfter(chainman.ActiveChain().Tip(), chainman.GetConsensus(), Consensus::DEPLOYMENT_V19));
391391
BOOST_CHECK_EQUAL(chainman.ActiveChain().Height(), nHeight + 1 + i);
392392
dmnman.UpdatedBlockTip(chainman.ActiveChain().Tip());
393393
dmnman.DoMaintenance();
@@ -965,10 +965,9 @@ struct TestChainV19Setup : public TestChainV19BeforeActivationSetup {
965965
for (int i = 0; i < 5; ++i) {
966966
CreateAndProcessBlock({}, coinbase_pk);
967967
}
968-
bool v19_just_activated{DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), Params().GetConsensus(),
969-
Consensus::DEPLOYMENT_V19) &&
970-
!DeploymentActiveAt(*m_node.chainman->ActiveChain().Tip(), Params().GetConsensus(),
971-
Consensus::DEPLOYMENT_V19)};
968+
bool v19_just_activated{
969+
DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), m_node.chainman->GetConsensus(), Consensus::DEPLOYMENT_V19) &&
970+
!DeploymentActiveAt(*m_node.chainman->ActiveChain().Tip(), m_node.chainman->GetConsensus(), Consensus::DEPLOYMENT_V19)};
972971
assert(v19_just_activated);
973972
}
974973
};
@@ -977,7 +976,7 @@ struct TestChainV19Setup : public TestChainV19BeforeActivationSetup {
977976
TestChainV19BeforeActivationSetup::TestChainV19BeforeActivationSetup() :
978977
TestChainSetup(494, CBaseChainParams::REGTEST, {"-testactivationheight=v19@500", "-testactivationheight=v20@500", "-testactivationheight=mn_rr@500"})
979978
{
980-
bool v19_active{DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), Params().GetConsensus(),
979+
bool v19_active{DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), m_node.chainman->GetConsensus(),
981980
Consensus::DEPLOYMENT_V19)};
982981
assert(!v19_active);
983982
}

0 commit comments

Comments
 (0)