Skip to content

Commit 37b9b67

Browse files
committed
versionbits: Simplify VersionBitsCache API
Replaces State() (which returned ACTIVE/STARTED/etc) with IsActiveAfter() which just returns a bool, as this was all State was actually used for. Drops Mask(), which was only used in tests and can be replaced with `1<<bit`, and also drops StateSinceHeight() and Statistics(), which are now only used internally for Info().
1 parent 1198e7d commit 37b9b67

File tree

4 files changed

+18
-41
lines changed

4 files changed

+18
-41
lines changed

src/deploymentstatus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus
2020
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep, VersionBitsCache& versionbitscache)
2121
{
2222
assert(Consensus::ValidDeployment(dep));
23-
return ThresholdState::ACTIVE == versionbitscache.State(pindexPrev, params, dep);
23+
return versionbitscache.IsActiveAfter(pindexPrev, params, dep);
2424
}
2525

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

src/test/versionbits_tests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,6 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
297297
// Check min_activation_height is on a retarget boundary
298298
BOOST_REQUIRE_EQUAL(min_activation_height % period, 0U);
299299

300-
const uint32_t bitmask{versionbitscache.Mask(params, dep)};
301-
BOOST_CHECK_EQUAL(bitmask, uint32_t{1} << bit);
302-
303300
// In the first chain, test that the bit is set by CBV until it has failed.
304301
// In the second chain, test the bit is set by CBV while STARTED and
305302
// LOCKED-IN, and then no longer set while ACTIVE.
@@ -438,7 +435,7 @@ BOOST_FIXTURE_TEST_CASE(versionbits_computeblockversion, BlockVersionTest)
438435
// not take precedence over STARTED/LOCKED_IN. So all softforks on
439436
// the same bit might overlap, even when non-overlapping start-end
440437
// times are picked.
441-
const uint32_t dep_mask{vbcache.Mask(chainParams->GetConsensus(), dep)};
438+
const uint32_t dep_mask{uint32_t{1} << chainParams->GetConsensus().vDeployments[dep].bit};
442439
BOOST_CHECK(!(chain_all_vbits & dep_mask));
443440
chain_all_vbits |= dep_mask;
444441
check_computeblockversion(vbcache, chainParams->GetConsensus(), dep);

src/versionbits.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,23 @@ BIP9Info VersionBitsCache::Info(const CBlockIndex& block_index, const Consensus:
222222
{
223223
BIP9Info result;
224224

225-
const auto current_state = State(block_index.pprev, params, id);
226-
result.current_state = StateName(current_state);
227-
result.since = StateSinceHeight(block_index.pprev, params, id);
225+
VersionBitsConditionChecker checker(params, id);
226+
227+
ThresholdState current_state, next_state;
228+
229+
{
230+
LOCK(m_mutex);
231+
current_state = checker.GetStateFor(block_index.pprev, m_caches[id]);
232+
next_state = checker.GetStateFor(&block_index, m_caches[id]);
233+
result.since = checker.GetStateSinceHeightFor(block_index.pprev, m_caches[id]);
234+
}
228235

229-
const auto next_state = State(&block_index, params, id);
236+
result.current_state = StateName(current_state);
230237
result.next_state = StateName(next_state);
231238

232239
const bool has_signal = (STARTED == current_state || LOCKED_IN == current_state);
233240
if (has_signal) {
234-
result.stats.emplace(Statistics(&block_index, params, id, &result.signalling_blocks));
241+
result.stats.emplace(checker.GetStateStatisticsFor(&block_index, &result.signalling_blocks));
235242
if (LOCKED_IN == current_state) {
236243
result.stats->threshold = 0;
237244
result.stats->possible = false;
@@ -278,26 +285,10 @@ BIP9GBTStatus VersionBitsCache::GBTStatus(const CBlockIndex& block_index, const
278285
return result;
279286
}
280287

281-
ThresholdState VersionBitsCache::State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
282-
{
283-
LOCK(m_mutex);
284-
return VersionBitsConditionChecker(params, pos).GetStateFor(pindexPrev, m_caches[pos]);
285-
}
286-
287-
BIP9Stats VersionBitsCache::Statistics(const CBlockIndex* pindex, const Consensus::Params& params, Consensus::DeploymentPos pos, std::vector<bool>* signalling_blocks)
288-
{
289-
return VersionBitsConditionChecker(params, pos).GetStateStatisticsFor(pindex, signalling_blocks);
290-
}
291-
292-
int VersionBitsCache::StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
288+
bool VersionBitsCache::IsActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
293289
{
294290
LOCK(m_mutex);
295-
return VersionBitsConditionChecker(params, pos).GetStateSinceHeightFor(pindexPrev, m_caches[pos]);
296-
}
297-
298-
uint32_t VersionBitsCache::Mask(const Consensus::Params& params, Consensus::DeploymentPos pos)
299-
{
300-
return VersionBitsConditionChecker(params, pos).Mask();
291+
return ThresholdState::ACTIVE == VersionBitsConditionChecker(params, pos).GetStateFor(pindexPrev, m_caches[pos]);
301292
}
302293

303294
static int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, std::array<ThresholdConditionCache, Consensus::MAX_VERSION_BITS_DEPLOYMENTS>& caches)

src/versionbits.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,14 @@ class VersionBitsCache
109109
std::array<ThresholdConditionCache,Consensus::MAX_VERSION_BITS_DEPLOYMENTS> m_caches GUARDED_BY(m_mutex);
110110

111111
public:
112-
/** Get the numerical statistics for a given deployment for the signalling period that includes pindex.
113-
* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled
114-
*/
115-
static BIP9Stats Statistics(const CBlockIndex* pindex, const Consensus::Params& params, Consensus::DeploymentPos pos, std::vector<bool>* signalling_blocks = nullptr);
116-
117-
static uint32_t Mask(const Consensus::Params& params, Consensus::DeploymentPos pos);
118-
119112
BIP9Info Info(const CBlockIndex& block_index, const Consensus::Params& params, Consensus::DeploymentPos id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
120113

121114
BIP9GBTStatus GBTStatus(const CBlockIndex& block_index, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
122115

123116
/** Get the BIP9 state for a given deployment for the block after pindexPrev. */
124-
ThresholdState State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
125-
126-
/** Get the block height at which the BIP9 deployment switched into the state for the block after pindexPrev. */
127-
int StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
117+
bool IsActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
128118

129-
/** Determine what nVersion a new block should use
130-
*/
119+
/** Determine what nVersion a new block should use */
131120
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
132121

133122
/** Check for unknown activations

0 commit comments

Comments
 (0)