Skip to content

Commit 4bd8921

Browse files
ajtownssipa
authored andcommitted
Unit tests for always-active versionbits.
1 parent d07ee77 commit 4bd8921

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/test/versionbits_tests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class TestConditionChecker : public AbstractThresholdConditionChecker
3232
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, paramsDummy, cache); }
3333
};
3434

35+
class TestAlwaysActiveConditionChecker : public TestConditionChecker
36+
{
37+
public:
38+
int64_t BeginTime(const Consensus::Params& params) const override { return Consensus::BIP9Deployment::ALWAYS_ACTIVE; }
39+
};
40+
3541
#define CHECKERS 6
3642

3743
class VersionBitsTester
@@ -43,6 +49,8 @@ class VersionBitsTester
4349
// The first one performs all checks, the second only 50%, the third only 25%, etc...
4450
// This is to test whether lack of cached information leads to the same results.
4551
TestConditionChecker checker[CHECKERS];
52+
// Another 6 that assume always active activation
53+
TestAlwaysActiveConditionChecker checker_always[CHECKERS];
4654

4755
// Test counter (to identify failures)
4856
int num;
@@ -56,6 +64,7 @@ class VersionBitsTester
5664
}
5765
for (unsigned int i = 0; i < CHECKERS; i++) {
5866
checker[i] = TestConditionChecker();
67+
checker_always[i] = TestAlwaysActiveConditionChecker();
5968
}
6069
vpblock.clear();
6170
return *this;
@@ -82,6 +91,7 @@ class VersionBitsTester
8291
for (int i = 0; i < CHECKERS; i++) {
8392
if (InsecureRandBits(i) == 0) {
8493
BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num));
94+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == 0, strprintf("Test %i for StateSinceHeight (always active)", num));
8595
}
8696
}
8797
num++;
@@ -92,6 +102,7 @@ class VersionBitsTester
92102
for (int i = 0; i < CHECKERS; i++) {
93103
if (InsecureRandBits(i) == 0) {
94104
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_DEFINED, strprintf("Test %i for DEFINED", num));
105+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
95106
}
96107
}
97108
num++;
@@ -102,6 +113,7 @@ class VersionBitsTester
102113
for (int i = 0; i < CHECKERS; i++) {
103114
if (InsecureRandBits(i) == 0) {
104115
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_STARTED, strprintf("Test %i for STARTED", num));
116+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
105117
}
106118
}
107119
num++;
@@ -112,6 +124,7 @@ class VersionBitsTester
112124
for (int i = 0; i < CHECKERS; i++) {
113125
if (InsecureRandBits(i) == 0) {
114126
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_LOCKED_IN, strprintf("Test %i for LOCKED_IN", num));
127+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
115128
}
116129
}
117130
num++;
@@ -122,6 +135,7 @@ class VersionBitsTester
122135
for (int i = 0; i < CHECKERS; i++) {
123136
if (InsecureRandBits(i) == 0) {
124137
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE", num));
138+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
125139
}
126140
}
127141
num++;
@@ -132,6 +146,7 @@ class VersionBitsTester
132146
for (int i = 0; i < CHECKERS; i++) {
133147
if (InsecureRandBits(i) == 0) {
134148
BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_FAILED, strprintf("Test %i for FAILED", num));
149+
BOOST_CHECK_MESSAGE(checker_always[i].GetStateFor(vpblock.empty() ? nullptr : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE (always active)", num));
135150
}
136151
}
137152
num++;

0 commit comments

Comments
 (0)