Skip to content

Commit 9e6b65f

Browse files
committed
tests: clean up versionbits test
Simplify the versionbits unit test slightly to make the next set of changes a little easier to follow.
1 parent 5932744 commit 9e6b65f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/test/versionbits_tests.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class VersionBitsTester
100100
while (vpblock.size() < height) {
101101
CBlockIndex* pindex = new CBlockIndex();
102102
pindex->nHeight = vpblock.size();
103-
pindex->pprev = vpblock.size() > 0 ? vpblock.back() : nullptr;
103+
pindex->pprev = Tip();
104104
pindex->nTime = nTime;
105105
pindex->nVersion = nVersion;
106106
pindex->BuildSkip();
@@ -110,13 +110,14 @@ class VersionBitsTester
110110
}
111111

112112
VersionBitsTester& TestStateSinceHeight(int height) {
113+
const CBlockIndex* tip = Tip();
113114
for (int i = 0; i < CHECKERS; i++) {
114115
if (InsecureRandBits(i) == 0) {
115-
BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num));
116-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == 0, strprintf("Test %i for StateSinceHeight (always active)", num));
116+
BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(tip) == height, strprintf("Test %i for StateSinceHeight", num));
117+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (always active)", num));
117118

118119
// never active may go from DEFINED -> FAILED at the first period
119-
const auto never_height = checker_never[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back());
120+
const auto never_height = checker_never[i].GetStateSinceHeightFor(tip);
120121
BOOST_CHECK_MESSAGE(never_height == 0 || never_height == checker_never[i].Period(paramsDummy), strprintf("Test %i for StateSinceHeight (never active)", num));
121122
}
122123
}
@@ -125,9 +126,9 @@ class VersionBitsTester
125126
}
126127

127128
VersionBitsTester& TestState(ThresholdState exp) {
129+
const CBlockIndex* pindex = Tip();
128130
for (int i = 0; i < CHECKERS; i++) {
129131
if (InsecureRandBits(i) == 0) {
130-
const CBlockIndex* pindex = vpblock.empty() ? nullptr : vpblock.back();
131132
ThresholdState got = checker[i].GetStateFor(pindex);
132133
ThresholdState got_always = checker_always[i].GetStateFor(pindex);
133134
ThresholdState got_never = checker_never[i].GetStateFor(pindex);
@@ -149,7 +150,7 @@ class VersionBitsTester
149150
VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); }
150151
VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); }
151152

152-
CBlockIndex * Tip() { return vpblock.size() ? vpblock.back() : nullptr; }
153+
CBlockIndex* Tip() { return vpblock.empty() ? nullptr : vpblock.back(); }
153154
};
154155

155156
BOOST_FIXTURE_TEST_SUITE(versionbits_tests, TestingSetup)
@@ -217,7 +218,10 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
217218
.Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000)
218219
.Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000);
219220
}
221+
}
220222

223+
BOOST_AUTO_TEST_CASE(versionbits_sanity)
224+
{
221225
// Sanity checks of version bit deployments
222226
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
223227
const Consensus::Params &mainnetParams = chainParams->GetConsensus();
@@ -271,7 +275,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
271275

272276
int64_t nTime = nStartTime;
273277

274-
CBlockIndex *lastBlock = nullptr;
278+
const CBlockIndex *lastBlock = nullptr;
275279

276280
// Before MedianTimePast of the chain has crossed nStartTime, the bit
277281
// should not be set.

0 commit comments

Comments
 (0)