|
1 |
| -// Copyright (c) 2014-2015 The Bitcoin Core developers |
| 1 | +// Copyright (c) 2014-2016 The Bitcoin Core developers |
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
@@ -30,6 +30,7 @@ class TestConditionChecker : public AbstractThresholdConditionChecker
|
30 | 30 | bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const { return (pindex->nVersion & 0x100); }
|
31 | 31 |
|
32 | 32 | ThresholdState GetStateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, paramsDummy, cache); }
|
| 33 | + int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, paramsDummy, cache); } |
33 | 34 | };
|
34 | 35 |
|
35 | 36 | #define CHECKERS 6
|
@@ -78,6 +79,16 @@ class VersionBitsTester
|
78 | 79 | return *this;
|
79 | 80 | }
|
80 | 81 |
|
| 82 | + VersionBitsTester& TestStateSinceHeight(int height) { |
| 83 | + for (int i = 0; i < CHECKERS; i++) { |
| 84 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 85 | + BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? NULL : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num)); |
| 86 | + } |
| 87 | + } |
| 88 | + num++; |
| 89 | + return *this; |
| 90 | + } |
| 91 | + |
81 | 92 | VersionBitsTester& TestDefined() {
|
82 | 93 | for (int i = 0; i < CHECKERS; i++) {
|
83 | 94 | if ((insecure_rand() & ((1 << i) - 1)) == 0) {
|
@@ -137,53 +148,64 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
|
137 | 148 | {
|
138 | 149 | for (int i = 0; i < 64; i++) {
|
139 | 150 | // DEFINED -> FAILED
|
140 |
| - VersionBitsTester().TestDefined() |
141 |
| - .Mine(1, TestTime(1), 0x100).TestDefined() |
142 |
| - .Mine(11, TestTime(11), 0x100).TestDefined() |
143 |
| - .Mine(989, TestTime(989), 0x100).TestDefined() |
144 |
| - .Mine(999, TestTime(20000), 0x100).TestDefined() |
145 |
| - .Mine(1000, TestTime(20000), 0x100).TestFailed() |
146 |
| - .Mine(1999, TestTime(30001), 0x100).TestFailed() |
147 |
| - .Mine(2000, TestTime(30002), 0x100).TestFailed() |
148 |
| - .Mine(2001, TestTime(30003), 0x100).TestFailed() |
149 |
| - .Mine(2999, TestTime(30004), 0x100).TestFailed() |
150 |
| - .Mine(3000, TestTime(30005), 0x100).TestFailed() |
| 151 | + VersionBitsTester().TestDefined().TestStateSinceHeight(0) |
| 152 | + .Mine(1, TestTime(1), 0x100).TestDefined().TestStateSinceHeight(0) |
| 153 | + .Mine(11, TestTime(11), 0x100).TestDefined().TestStateSinceHeight(0) |
| 154 | + .Mine(989, TestTime(989), 0x100).TestDefined().TestStateSinceHeight(0) |
| 155 | + .Mine(999, TestTime(20000), 0x100).TestDefined().TestStateSinceHeight(0) |
| 156 | + .Mine(1000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(1000) |
| 157 | + .Mine(1999, TestTime(30001), 0x100).TestFailed().TestStateSinceHeight(1000) |
| 158 | + .Mine(2000, TestTime(30002), 0x100).TestFailed().TestStateSinceHeight(1000) |
| 159 | + .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(1000) |
| 160 | + .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(1000) |
| 161 | + .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(1000) |
151 | 162 |
|
152 | 163 | // DEFINED -> STARTED -> FAILED
|
153 |
| - .Reset().TestDefined() |
154 |
| - .Mine(1, TestTime(1), 0).TestDefined() |
155 |
| - .Mine(1000, TestTime(10000) - 1, 0x100).TestDefined() // One second more and it would be defined |
156 |
| - .Mine(2000, TestTime(10000), 0x100).TestStarted() // So that's what happens the next period |
157 |
| - .Mine(2051, TestTime(10010), 0).TestStarted() // 51 old blocks |
158 |
| - .Mine(2950, TestTime(10020), 0x100).TestStarted() // 899 new blocks |
159 |
| - .Mine(3000, TestTime(20000), 0).TestFailed() // 50 old blocks (so 899 out of the past 1000) |
160 |
| - .Mine(4000, TestTime(20010), 0x100).TestFailed() |
| 164 | + .Reset().TestDefined().TestStateSinceHeight(0) |
| 165 | + .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) |
| 166 | + .Mine(1000, TestTime(10000) - 1, 0x100).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined |
| 167 | + .Mine(2000, TestTime(10000), 0x100).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period |
| 168 | + .Mine(2051, TestTime(10010), 0).TestStarted().TestStateSinceHeight(2000) // 51 old blocks |
| 169 | + .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 899 new blocks |
| 170 | + .Mine(3000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(3000) // 50 old blocks (so 899 out of the past 1000) |
| 171 | + .Mine(4000, TestTime(20010), 0x100).TestFailed().TestStateSinceHeight(3000) |
161 | 172 |
|
162 | 173 | // DEFINED -> STARTED -> FAILED while threshold reached
|
163 |
| - .Reset().TestDefined() |
164 |
| - .Mine(1, TestTime(1), 0).TestDefined() |
165 |
| - .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined() // One second more and it would be defined |
166 |
| - .Mine(2000, TestTime(10000), 0x101).TestStarted() // So that's what happens the next period |
167 |
| - .Mine(2999, TestTime(30000), 0x100).TestStarted() // 999 new blocks |
168 |
| - .Mine(3000, TestTime(30000), 0x100).TestFailed() // 1 new block (so 1000 out of the past 1000 are new) |
169 |
| - .Mine(3999, TestTime(30001), 0).TestFailed() |
170 |
| - .Mine(4000, TestTime(30002), 0).TestFailed() |
171 |
| - .Mine(14333, TestTime(30003), 0).TestFailed() |
172 |
| - .Mine(24000, TestTime(40000), 0).TestFailed() |
| 174 | + .Reset().TestDefined().TestStateSinceHeight(0) |
| 175 | + .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) |
| 176 | + .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined |
| 177 | + .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period |
| 178 | + .Mine(2999, TestTime(30000), 0x100).TestStarted().TestStateSinceHeight(2000) // 999 new blocks |
| 179 | + .Mine(3000, TestTime(30000), 0x100).TestFailed().TestStateSinceHeight(3000) // 1 new block (so 1000 out of the past 1000 are new) |
| 180 | + .Mine(3999, TestTime(30001), 0).TestFailed().TestStateSinceHeight(3000) |
| 181 | + .Mine(4000, TestTime(30002), 0).TestFailed().TestStateSinceHeight(3000) |
| 182 | + .Mine(14333, TestTime(30003), 0).TestFailed().TestStateSinceHeight(3000) |
| 183 | + .Mine(24000, TestTime(40000), 0).TestFailed().TestStateSinceHeight(3000) |
173 | 184 |
|
174 | 185 | // DEFINED -> STARTED -> LOCKEDIN at the last minute -> ACTIVE
|
175 | 186 | .Reset().TestDefined()
|
176 |
| - .Mine(1, TestTime(1), 0).TestDefined() |
177 |
| - .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined() // One second more and it would be defined |
178 |
| - .Mine(2000, TestTime(10000), 0x101).TestStarted() // So that's what happens the next period |
179 |
| - .Mine(2050, TestTime(10010), 0x200).TestStarted() // 50 old blocks |
180 |
| - .Mine(2950, TestTime(10020), 0x100).TestStarted() // 900 new blocks |
181 |
| - .Mine(2999, TestTime(19999), 0x200).TestStarted() // 49 old blocks |
182 |
| - .Mine(3000, TestTime(29999), 0x200).TestLockedIn() // 1 old block (so 900 out of the past 1000) |
183 |
| - .Mine(3999, TestTime(30001), 0).TestLockedIn() |
184 |
| - .Mine(4000, TestTime(30002), 0).TestActive() |
185 |
| - .Mine(14333, TestTime(30003), 0).TestActive() |
186 |
| - .Mine(24000, TestTime(40000), 0).TestActive(); |
| 187 | + .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) |
| 188 | + .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined |
| 189 | + .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period |
| 190 | + .Mine(2050, TestTime(10010), 0x200).TestStarted().TestStateSinceHeight(2000) // 50 old blocks |
| 191 | + .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 900 new blocks |
| 192 | + .Mine(2999, TestTime(19999), 0x200).TestStarted().TestStateSinceHeight(2000) // 49 old blocks |
| 193 | + .Mine(3000, TestTime(29999), 0x200).TestLockedIn().TestStateSinceHeight(3000) // 1 old block (so 900 out of the past 1000) |
| 194 | + .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000) |
| 195 | + .Mine(4000, TestTime(30002), 0).TestActive().TestStateSinceHeight(4000) |
| 196 | + .Mine(14333, TestTime(30003), 0).TestActive().TestStateSinceHeight(4000) |
| 197 | + .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000) |
| 198 | + |
| 199 | + // DEFINED multiple periods -> STARTED multiple periods -> FAILED |
| 200 | + .Reset().TestDefined().TestStateSinceHeight(0) |
| 201 | + .Mine(999, TestTime(999), 0).TestDefined().TestStateSinceHeight(0) |
| 202 | + .Mine(1000, TestTime(1000), 0).TestDefined().TestStateSinceHeight(0) |
| 203 | + .Mine(2000, TestTime(2000), 0).TestDefined().TestStateSinceHeight(0) |
| 204 | + .Mine(3000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) |
| 205 | + .Mine(4000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) |
| 206 | + .Mine(5000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) |
| 207 | + .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000) |
| 208 | + .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000); |
187 | 209 | }
|
188 | 210 |
|
189 | 211 | // Sanity checks of version bit deployments
|
|
0 commit comments