Skip to content

Commit 70176bf

Browse files
authored
Merge pull request #2 from luke-jr/Sjors/2021/03/bip9_tests
Sjors/2021/03/bip9 tests
2 parents 95ea54b + 0c471a5 commit 70176bf

File tree

1 file changed

+41
-45
lines changed

1 file changed

+41
-45
lines changed

src/test/versionbits_tests.cpp

+41-45
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
@@ -38,6 +50,13 @@ class TestAlwaysActiveConditionChecker : public TestConditionChecker
3850
int64_t BeginTime(const Consensus::Params& params) const override { return Consensus::BIP9Deployment::ALWAYS_ACTIVE; }
3951
};
4052

53+
class TestNeverActiveConditionChecker : public TestConditionChecker
54+
{
55+
public:
56+
int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
57+
int64_t EndTime(const Consensus::Params& params) const override { return 1230768000; }
58+
};
59+
4160
#define CHECKERS 6
4261

4362
class VersionBitsTester
@@ -51,6 +70,8 @@ class VersionBitsTester
5170
TestConditionChecker checker[CHECKERS];
5271
// Another 6 that assume always active activation
5372
TestAlwaysActiveConditionChecker checker_always[CHECKERS];
73+
// Another 6 that assume never active activation
74+
TestNeverActiveConditionChecker checker_never[CHECKERS];
5475

5576
// Test counter (to identify failures)
5677
int num;
@@ -65,6 +86,7 @@ class VersionBitsTester
6586
for (unsigned int i = 0; i < CHECKERS; i++) {
6687
checker[i] = TestConditionChecker();
6788
checker_always[i] = TestAlwaysActiveConditionChecker();
89+
checker_never[i] = TestNeverActiveConditionChecker();
6890
}
6991
vpblock.clear();
7092
return *this;
@@ -92,66 +114,40 @@ class VersionBitsTester
92114
if (InsecureRandBits(i) == 0) {
93115
BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num));
94116
BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == 0, strprintf("Test %i for StateSinceHeight (always active)", num));
95-
}
96-
}
97-
num++;
98-
return *this;
99-
}
100117

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));
118+
// 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+
BOOST_CHECK_MESSAGE(never_height == 0 || never_height == checker_never[i].Period(paramsDummy), strprintf("Test %i for StateSinceHeight (never active)", num));
106121
}
107122
}
108123
num++;
109124
return *this;
110125
}
111126

112-
VersionBitsTester& TestStarted() {
127+
VersionBitsTester& TestState(ThresholdState exp) {
113128
for (int i = 0; i < CHECKERS; i++) {
114129
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));
130+
const CBlockIndex* pindex = vpblock.empty() ? nullptr : vpblock.back();
131+
ThresholdState got = checker[i].GetStateFor(pindex);
132+
ThresholdState got_always = checker_always[i].GetStateFor(pindex);
133+
ThresholdState got_never = checker_never[i].GetStateFor(pindex);
134+
// nHeight of the next block. If vpblock is empty, the next (ie first)
135+
// block should be the genesis block with nHeight == 0.
136+
int height = pindex == nullptr ? 0 : pindex->nHeight + 1;
137+
BOOST_CHECK_MESSAGE(got == exp, strprintf("Test %i for %s height %d (got %s)", num, StateName(exp), height, StateName(got)));
138+
BOOST_CHECK_MESSAGE(got_always == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE height %d (got %s; always active case)", num, height, StateName(got_always)));
139+
BOOST_CHECK_MESSAGE(got_never == ThresholdState::DEFINED|| got_never == ThresholdState::FAILED, strprintf("Test %i for DEFINED/FAILED height %d (got %s; never active case)", num, height, StateName(got_never)));
117140
}
118141
}
119142
num++;
120143
return *this;
121144
}
122145

123-
VersionBitsTester& TestLockedIn() {
124-
for (int i = 0; i < CHECKERS; i++) {
125-
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));
128-
}
129-
}
130-
num++;
131-
return *this;
132-
}
133-
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-
}
146+
VersionBitsTester& TestDefined() { return TestState(ThresholdState::DEFINED); }
147+
VersionBitsTester& TestStarted() { return TestState(ThresholdState::STARTED); }
148+
VersionBitsTester& TestLockedIn() { return TestState(ThresholdState::LOCKED_IN); }
149+
VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); }
150+
VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); }
155151

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

0 commit comments

Comments
 (0)