14
14
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
15
15
static int32_t TestTime (int nHeight) { return 1415926536 + 600 * nHeight; }
16
16
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
+
17
29
static const Consensus::Params paramsDummy = Consensus::Params();
18
30
19
31
class TestConditionChecker : public AbstractThresholdConditionChecker
@@ -38,6 +50,13 @@ class TestAlwaysActiveConditionChecker : public TestConditionChecker
38
50
int64_t BeginTime (const Consensus::Params& params) const override { return Consensus::BIP9Deployment::ALWAYS_ACTIVE; }
39
51
};
40
52
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
+
41
60
#define CHECKERS 6
42
61
43
62
class VersionBitsTester
@@ -51,6 +70,8 @@ class VersionBitsTester
51
70
TestConditionChecker checker[CHECKERS];
52
71
// Another 6 that assume always active activation
53
72
TestAlwaysActiveConditionChecker checker_always[CHECKERS];
73
+ // Another 6 that assume never active activation
74
+ TestNeverActiveConditionChecker checker_never[CHECKERS];
54
75
55
76
// Test counter (to identify failures)
56
77
int num;
@@ -65,6 +86,7 @@ class VersionBitsTester
65
86
for (unsigned int i = 0 ; i < CHECKERS; i++) {
66
87
checker[i] = TestConditionChecker ();
67
88
checker_always[i] = TestAlwaysActiveConditionChecker ();
89
+ checker_never[i] = TestNeverActiveConditionChecker ();
68
90
}
69
91
vpblock.clear ();
70
92
return *this ;
@@ -92,66 +114,40 @@ class VersionBitsTester
92
114
if (InsecureRandBits (i) == 0 ) {
93
115
BOOST_CHECK_MESSAGE (checker[i].GetStateSinceHeightFor (vpblock.empty () ? nullptr : vpblock.back ()) == height, strprintf (" Test %i for StateSinceHeight" , num));
94
116
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
- }
100
117
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));
106
121
}
107
122
}
108
123
num++;
109
124
return *this ;
110
125
}
111
126
112
- VersionBitsTester& TestStarted ( ) {
127
+ VersionBitsTester& TestState (ThresholdState exp ) {
113
128
for (int i = 0 ; i < CHECKERS; i++) {
114
129
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)));
117
140
}
118
141
}
119
142
num++;
120
143
return *this ;
121
144
}
122
145
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); }
155
151
156
152
CBlockIndex * Tip () { return vpblock.size () ? vpblock.back () : nullptr ; }
157
153
};
0 commit comments