|
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
|
@@ -98,60 +110,28 @@ class VersionBitsTester
|
98 | 110 | return *this;
|
99 | 111 | }
|
100 | 112 |
|
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) { |
124 | 114 | for (int i = 0; i < CHECKERS; i++) {
|
125 | 115 | 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))); |
128 | 124 | }
|
129 | 125 | }
|
130 | 126 | num++;
|
131 | 127 | return *this;
|
132 | 128 | }
|
133 | 129 |
|
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); } |
155 | 135 |
|
156 | 136 | CBlockIndex * Tip() { return vpblock.size() ? vpblock.back() : nullptr; }
|
157 | 137 | };
|
|
0 commit comments