Skip to content

Commit 0cfd6c6

Browse files
committed
[refactor] versionbits: make VersionBitsCache a full class
Moves the VersionBits* functions to be methods of the cache class, and makes the cache and its lock private to the class.
1 parent 8ee3e0b commit 0cfd6c6

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

src/deploymentstatus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus
2323
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep)
2424
{
2525
assert(Consensus::ValidDeployment(dep));
26-
return ThresholdState::ACTIVE == VersionBitsState(pindexPrev, params, dep, g_versionbitscache);
26+
return ThresholdState::ACTIVE == g_versionbitscache.State(pindexPrev, params, dep);
2727
}
2828

2929
/** Determine if a deployment is active for this block */

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, UniValue&
13721372
if (consensusParams.vDeployments[id].nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE) return;
13731373

13741374
UniValue bip9(UniValue::VOBJ);
1375-
const ThresholdState thresholdState = VersionBitsState(active_chain_tip, consensusParams, id, g_versionbitscache);
1375+
const ThresholdState thresholdState = g_versionbitscache.State(active_chain_tip, consensusParams, id);
13761376
switch (thresholdState) {
13771377
case ThresholdState::DEFINED: bip9.pushKV("status", "defined"); break;
13781378
case ThresholdState::STARTED: bip9.pushKV("status", "started"); break;
@@ -1386,12 +1386,12 @@ static void SoftForkDescPushBack(const CBlockIndex* active_chain_tip, UniValue&
13861386
}
13871387
bip9.pushKV("start_time", consensusParams.vDeployments[id].nStartTime);
13881388
bip9.pushKV("timeout", consensusParams.vDeployments[id].nTimeout);
1389-
int64_t since_height = VersionBitsStateSinceHeight(active_chain_tip, consensusParams, id, g_versionbitscache);
1389+
int64_t since_height = g_versionbitscache.StateSinceHeight(active_chain_tip, consensusParams, id);
13901390
bip9.pushKV("since", since_height);
13911391
if (ThresholdState::STARTED == thresholdState)
13921392
{
13931393
UniValue statsUV(UniValue::VOBJ);
1394-
BIP9Stats statsStruct = VersionBitsStatistics(active_chain_tip, consensusParams, id);
1394+
BIP9Stats statsStruct = g_versionbitscache.Statistics(active_chain_tip, consensusParams, id);
13951395
statsUV.pushKV("period", statsStruct.period);
13961396
statsUV.pushKV("threshold", statsStruct.threshold);
13971397
statsUV.pushKV("elapsed", statsStruct.elapsed);

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,15 +841,15 @@ static RPCHelpMan getblocktemplate()
841841
UniValue vbavailable(UniValue::VOBJ);
842842
for (int j = 0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
843843
Consensus::DeploymentPos pos = Consensus::DeploymentPos(j);
844-
ThresholdState state = VersionBitsState(pindexPrev, consensusParams, pos, g_versionbitscache);
844+
ThresholdState state = g_versionbitscache.State(pindexPrev, consensusParams, pos);
845845
switch (state) {
846846
case ThresholdState::DEFINED:
847847
case ThresholdState::FAILED:
848848
// Not exposed to GBT at all
849849
break;
850850
case ThresholdState::LOCKED_IN:
851851
// Ensure bit is set in block version
852-
pblock->nVersion |= VersionBitsMask(consensusParams, pos);
852+
pblock->nVersion |= g_versionbitscache.Mask(consensusParams, pos);
853853
// FALL THROUGH to get vbavailable set...
854854
case ThresholdState::STARTED:
855855
{
@@ -858,7 +858,7 @@ static RPCHelpMan getblocktemplate()
858858
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
859859
if (!vbinfo.gbt_force) {
860860
// If the client doesn't support this, don't indicate it in the [default] version
861-
pblock->nVersion &= ~VersionBitsMask(consensusParams, pos);
861+
pblock->nVersion &= ~g_versionbitscache.Mask(consensusParams, pos);
862862
}
863863
}
864864
break;

src/test/versionbits_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
288288
// Check min_activation_height is on a retarget boundary
289289
BOOST_REQUIRE_EQUAL(min_activation_height % params.nMinerConfirmationWindow, 0U);
290290

291-
const uint32_t bitmask{VersionBitsMask(params, dep)};
291+
const uint32_t bitmask{g_versionbitscache.Mask(params, dep)};
292292
BOOST_CHECK_EQUAL(bitmask, uint32_t{1} << bit);
293293

294294
// In the first chain, test that the bit is set by CBV until it has failed.
@@ -426,7 +426,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
426426
// not take precedence over STARTED/LOCKED_IN. So all softforks on
427427
// the same bit might overlap, even when non-overlapping start-end
428428
// times are picked.
429-
const uint32_t dep_mask{VersionBitsMask(chainParams->GetConsensus(), dep)};
429+
const uint32_t dep_mask{g_versionbitscache.Mask(chainParams->GetConsensus(), dep)};
430430
BOOST_CHECK(!(chain_all_vbits & dep_mask));
431431
chain_all_vbits |= dep_mask;
432432
check_computeblockversion(chainParams->GetConsensus(), dep);

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,9 +1611,9 @@ int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Para
16111611
int32_t nVersion = VERSIONBITS_TOP_BITS;
16121612

16131613
for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
1614-
ThresholdState state = VersionBitsState(pindexPrev, params, static_cast<Consensus::DeploymentPos>(i), g_versionbitscache);
1614+
ThresholdState state = g_versionbitscache.State(pindexPrev, params, static_cast<Consensus::DeploymentPos>(i));
16151615
if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) {
1616-
nVersion |= VersionBitsMask(params, static_cast<Consensus::DeploymentPos>(i));
1616+
nVersion |= g_versionbitscache.Mask(params, static_cast<Consensus::DeploymentPos>(i));
16171617
}
16181618
}
16191619

src/versionbits.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,32 +190,32 @@ class VersionBitsConditionChecker : public AbstractThresholdConditionChecker {
190190

191191
} // namespace
192192

193-
ThresholdState VersionBitsState(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache)
193+
ThresholdState VersionBitsCache::State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
194194
{
195-
LOCK(cache.mutex);
196-
return VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, cache.caches[pos]);
195+
LOCK(m_mutex);
196+
return VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, m_caches[pos]);
197197
}
198198

199-
BIP9Stats VersionBitsStatistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
199+
BIP9Stats VersionBitsCache::Statistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
200200
{
201201
return VersionBitsConditionChecker(pos).GetStateStatisticsFor(pindexPrev, params);
202202
}
203203

204-
int VersionBitsStateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache)
204+
int VersionBitsCache::StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
205205
{
206-
LOCK(cache.mutex);
207-
return VersionBitsConditionChecker(pos).GetStateSinceHeightFor(pindexPrev, params, cache.caches[pos]);
206+
LOCK(m_mutex);
207+
return VersionBitsConditionChecker(pos).GetStateSinceHeightFor(pindexPrev, params, m_caches[pos]);
208208
}
209209

210-
uint32_t VersionBitsMask(const Consensus::Params& params, Consensus::DeploymentPos pos)
210+
uint32_t VersionBitsCache::Mask(const Consensus::Params& params, Consensus::DeploymentPos pos)
211211
{
212212
return VersionBitsConditionChecker(pos).Mask(params);
213213
}
214214

215215
void VersionBitsCache::Clear()
216216
{
217-
LOCK(mutex);
217+
LOCK(m_mutex);
218218
for (unsigned int d = 0; d < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; d++) {
219-
caches[d].clear();
219+
m_caches[d].clear();
220220
}
221221
}

src/versionbits.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,27 @@ class AbstractThresholdConditionChecker {
7373
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
7474
};
7575

76-
/** BIP 9 allows multiple softforks to be deployed in parallel. We cache per-period state for every one of them
77-
* keyed by the bit position used to signal support. */
78-
struct VersionBitsCache
76+
/** BIP 9 allows multiple softforks to be deployed in parallel. We cache
77+
* per-period state for every one of them. */
78+
class VersionBitsCache
7979
{
80-
Mutex mutex;
81-
ThresholdConditionCache caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] GUARDED_BY(mutex);
80+
private:
81+
Mutex m_mutex;
82+
ThresholdConditionCache m_caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] GUARDED_BY(m_mutex);
83+
84+
public:
85+
/** Get the numerical statistics for a given deployment for the signalling period that includes the block after pindexPrev. */
86+
static BIP9Stats Statistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
87+
88+
static uint32_t Mask(const Consensus::Params& params, Consensus::DeploymentPos pos);
89+
90+
/** Get the BIP9 state for a given deployment for the block after pindexPrev. */
91+
ThresholdState State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
92+
93+
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
94+
int StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
8295

8396
void Clear();
8497
};
8598

86-
/** Get the BIP9 state for a given deployment for the block after pindexPrev. */
87-
ThresholdState VersionBitsState(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache);
88-
/** Get the numerical statistics for a given deployment for the signalling period that includes the block after pindexPrev. */
89-
BIP9Stats VersionBitsStatistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos);
90-
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
91-
int VersionBitsStateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache);
92-
uint32_t VersionBitsMask(const Consensus::Params& params, Consensus::DeploymentPos pos);
93-
9499
#endif // BITCOIN_VERSIONBITS_H

0 commit comments

Comments
 (0)