Skip to content

Commit 3ba9283

Browse files
ajtownsSjors
andcommitted
tests: more helpful errors for failing versionbits tests
Co-authored-by: Sjors Provoost <[email protected]>
1 parent 831675c commit 3ba9283

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

src/test/versionbits_tests.cpp

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
1515
static int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; }
1616

17+
static const std::string StateName(ThresholdState state)
18+
{
19+
switch (state) {
20+
case ThresholdState::DEFINED: return "DEFINED";
21+
case ThresholdState::STARTED: return "STARTED";
22+
case ThresholdState::LOCKED_IN: return "LOCKED_IN";
23+
case ThresholdState::ACTIVE: return "ACTIVE";
24+
case ThresholdState::FAILED: return "FAILED";
25+
} // no default case, so the compiler can warn about missing cases
26+
return "";
27+
}
28+
1729
static const Consensus::Params paramsDummy = Consensus::Params();
1830

1931
class TestConditionChecker : public AbstractThresholdConditionChecker
@@ -98,60 +110,28 @@ class VersionBitsTester
98110
return *this;
99111
}
100112

101-
VersionBitsTester& TestDefined() {
102-
for (int i = 0; i < CHECKERS; i++) {
103-
if (InsecureRandBits(i) == 0) {
104-
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::DEFINED, strprintf("Test %i for DEFINED", num));
105-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
106-
}
107-
}
108-
num++;
109-
return *this;
110-
}
111-
112-
VersionBitsTester& TestStarted() {
113-
for (int i = 0; i < CHECKERS; i++) {
114-
if (InsecureRandBits(i) == 0) {
115-
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::STARTED, strprintf("Test %i for STARTED", num));
116-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
117-
}
118-
}
119-
num++;
120-
return *this;
121-
}
122-
123-
VersionBitsTester& TestLockedIn() {
113+
VersionBitsTester& TestState(ThresholdState exp) {
124114
for (int i = 0; i < CHECKERS; i++) {
125115
if (InsecureRandBits(i) == 0) {
126-
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::LOCKED_IN, strprintf("Test %i for LOCKED_IN", num));
127-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
116+
const CBlockIndex* pindex = vpblock.empty() ? nullptr : vpblock.back();
117+
ThresholdState got = checker[i].GetStateFor(pindex);
118+
ThresholdState got_always = checker_always[i].GetStateFor(pindex);
119+
// nHeight of the next block. If vpblock is empty, the next (ie first)
120+
// block should be the genesis block with nHeight == 0.
121+
int height = pindex == nullptr ? 0 : pindex->nHeight + 1;
122+
BOOST_CHECK_MESSAGE(got == exp, strprintf("Test %i for %s height %d (got %s)", num, StateName(exp), height, StateName(got)));
123+
BOOST_CHECK_MESSAGE(got_always == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE height %d (got %s; always active case)", num, height, StateName(got_always)));
128124
}
129125
}
130126
num++;
131127
return *this;
132128
}
133129

134-
VersionBitsTester& TestActive() {
135-
for (int i = 0; i < CHECKERS; i++) {
136-
if (InsecureRandBits(i) == 0) {
137-
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE", num));
138-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
139-
}
140-
}
141-
num++;
142-
return *this;
143-
}
144-
145-
VersionBitsTester& TestFailed() {
146-
for (int i = 0; i < CHECKERS; i++) {
147-
if (InsecureRandBits(i) == 0) {
148-
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::FAILED, strprintf("Test %i for FAILED", num));
149-
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
150-
}
151-
}
152-
num++;
153-
return *this;
154-
}
130+
VersionBitsTester& TestDefined() { return TestState(ThresholdState::DEFINED); }
131+
VersionBitsTester& TestStarted() { return TestState(ThresholdState::STARTED); }
132+
VersionBitsTester& TestLockedIn() { return TestState(ThresholdState::LOCKED_IN); }
133+
VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); }
134+
VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); }
155135

156136
CBlockIndex * Tip() { return vpblock.size() ? vpblock.back() : nullptr; }
157137
};

0 commit comments

Comments
 (0)