Skip to content

Commit 2a3ca83

Browse files
committed
merge bitcoin#24595: move g_versionbitscache global to ChainstateManager
includes: - d603f1d - eca22c7 - bb5c24b
1 parent a813d64 commit 2a3ca83

12 files changed

+113
-91
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/evo/specialtxman.cpp

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

218218
newList.DecreaseScores();
219219

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

223223
// we skip the coinbase
224224
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/block_reward_reallocation_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
210210
// next block should be signaling by default
211211
LOCK(cs_main);
212212
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
213-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
213+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
214214
dmnman.UpdatedBlockTip(tip);
215215
BOOST_REQUIRE(dmnman.GetListAtChainTip().HasMN(tx.GetHash()));
216216
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
@@ -226,7 +226,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
226226
{
227227
LOCK(cs_main);
228228
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
229-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
229+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
230230
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
231231
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, block_subsidy, isV20Active);
232232
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
@@ -243,20 +243,20 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
243243
}
244244
LOCK(cs_main);
245245
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
246-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
246+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
247247
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
248248
const CAmount masternode_payment = GetMasternodePayment(tip->nHeight, block_subsidy, isV20Active);
249249
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
250250
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
251251
}
252252
}
253-
BOOST_CHECK(DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), consensus_params, Consensus::DEPLOYMENT_V20));
253+
BOOST_CHECK(DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), *m_node.chainman, Consensus::DEPLOYMENT_V20));
254254
// Allocation of block subsidy is 60% MN, 20% miners and 20% treasury
255255
{
256256
// Reward split should reach ~75/25 after reallocation is done
257257
LOCK(cs_main);
258258
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
259-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
259+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
260260
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
261261
const CAmount block_subsidy_sb = GetSuperblockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
262262
CAmount block_subsidy_potential = block_subsidy + block_subsidy_sb;
@@ -270,7 +270,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
270270
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment);
271271
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, 106300596); // 0.75
272272
}
273-
BOOST_CHECK(!DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), consensus_params, Consensus::DEPLOYMENT_MN_RR));
273+
BOOST_CHECK(!DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), *m_node.chainman, Consensus::DEPLOYMENT_MN_RR));
274274

275275
// Reward split should stay ~75/25 after reallocation is done,
276276
// check 10 next superblocks
@@ -280,8 +280,8 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
280280
}
281281
LOCK(cs_main);
282282
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
283-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
284-
const bool isMNRewardReallocated{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_MN_RR)};
283+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
284+
const bool isMNRewardReallocated{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_MN_RR)};
285285
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
286286
CAmount masternode_payment = GetMasternodePayment(tip->nHeight, block_subsidy, isV20Active);
287287
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
@@ -295,12 +295,12 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
295295
BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[payment_index].nValue, masternode_payment);
296296
}
297297

298-
BOOST_CHECK(DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), consensus_params, Consensus::DEPLOYMENT_MN_RR));
298+
BOOST_CHECK(DeploymentActiveAfter(m_node.chainman->ActiveChain().Tip(), *m_node.chainman, Consensus::DEPLOYMENT_MN_RR));
299299
{ // At this moment Masternode reward should be reallocated to platform
300300
// Allocation of block subsidy is 60% MN, 20% miners and 20% treasury
301301
LOCK(cs_main);
302302
const CBlockIndex* const tip{m_node.chainman->ActiveChain().Tip()};
303-
const bool isV20Active{DeploymentActiveAfter(tip, consensus_params, Consensus::DEPLOYMENT_V20)};
303+
const bool isV20Active{DeploymentActiveAfter(tip, *m_node.chainman, Consensus::DEPLOYMENT_V20)};
304304
const CAmount block_subsidy = GetBlockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
305305
const CAmount block_subsidy_sb = GetSuperblockSubsidyInner(tip->nBits, tip->nHeight, consensus_params, isV20Active);
306306
CAmount masternode_payment = GetMasternodePayment(tip->nHeight, block_subsidy, isV20Active);

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)