Debian Codegraph @

tests/unit_tests/hardfork.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <algorithm>
32 #include "gtest/gtest.h"
33
34 #include "blockchain_db/blockchain_db.h"
35 #include "cryptonote_basic/cryptonote_format_utils.h"
36 #include "cryptonote_basic/hardfork.h"
37 #include "blockchain_db/testdb.h"
38
39 using namespace cryptonote;
40
41 #define BLOCKS_PER_YEAR 525960
42 #define SECONDS_PER_YEAR 31557600
43
44 namespace
45 {
46
47 class TestDB: public cryptonote::BaseTestDB {
48 public:
49 virtual uint64_t height() const override { return blocks.size(); }
50 virtual void add_block( const block& blk
51 , size_t block_weight
52 , uint64_t long_term_block_weight
53 , const difficulty_type& cumulative_difficulty
54 , const uint64_t& coins_generated
55 , uint64_t num_rct_outs
56 , const crypto::hash& blk_hash
57 ) override {
58 blocks.push_back(blk);
59 }
60 virtual void remove_block() override { blocks.pop_back(); }
61 virtual block get_block_from_height(const uint64_t& height) const override {
62 return blocks.at(height);
63 }
64 virtual void set_hard_fork_version(uint64_t height, uint8_t version) override {
65 if (versions.size() <= height)
66 versions.resize(height+1);
67 versions[height] = version;
68 }
69 virtual uint8_t get_hard_fork_version(uint64_t height) const override {
70 return versions.at(height);
71 }
72
73 private:
74 std::vector<block> blocks;
75 std::deque<uint8_t> versions;
76 };
77
78 }
79
80 static cryptonote::block mkblock(uint8_t version, uint8_t vote)
81 {
82 cryptonote::block b;
83 b.major_version = version;
84 b.minor_version = vote;
85 return b;
86 }
87
88 static cryptonote::block mkblock(const HardFork &hf, uint64_t height, uint8_t vote)
89 {
90 cryptonote::block b;
91 b.major_version = hf.get(height);
92 b.minor_version = vote;
93 return b;
94 }
95
96 TEST(major, Only)
97 {
98 TestDB db;
99 HardFork hf(db, 1, 0, 0, 0, 1, 0); // no voting
100
101 // v h t
102 ASSERT_TRUE(hf.add_fork(1, 0, 0));
103 ASSERT_TRUE(hf.add_fork(2, 2, 1));
104 hf.init();
105
106 // block height 0, only version 1 is accepted
107 ASSERT_FALSE(hf.add(mkblock(0, 2), 0));
108 ASSERT_FALSE(hf.add(mkblock(2, 2), 0));
109 ASSERT_TRUE(hf.add(mkblock(1, 2), 0));
110 db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0, crypto::hash());
111
112 // block height 1, only version 1 is accepted
113 ASSERT_FALSE(hf.add(mkblock(0, 2), 1));
114 ASSERT_FALSE(hf.add(mkblock(2, 2), 1));
115 ASSERT_TRUE(hf.add(mkblock(1, 2), 1));
116 db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0, crypto::hash());
117
118 // block height 2, only version 2 is accepted
119 ASSERT_FALSE(hf.add(mkblock(0, 2), 2));
120 ASSERT_FALSE(hf.add(mkblock(1, 2), 2));
121 ASSERT_FALSE(hf.add(mkblock(3, 2), 2));
122 ASSERT_TRUE(hf.add(mkblock(2, 2), 2));
123 db.add_block(mkblock(2, 1), 0, 0, 0, 0, 0, crypto::hash());
124 }
125
126 TEST(empty_hardforks, Success)
127 {
128 TestDB db;
129 HardFork hf(db);
130
131 ASSERT_TRUE(hf.add_fork(1, 0, 0));
132 hf.init();
133 ASSERT_TRUE(hf.get_state(time(NULL)) == HardFork::Ready);
134 ASSERT_TRUE(hf.get_state(time(NULL) + 3600*24*400) == HardFork::Ready);
135
136 for (uint64_t h = 0; h <= 10; ++h) {
137 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
138 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
139 }
140 ASSERT_EQ(hf.get(0), 1);
141 ASSERT_EQ(hf.get(1), 1);
142 ASSERT_EQ(hf.get(10), 1);
143 }
144
145 TEST(ordering, Success)
146 {
147 TestDB db;
148 HardFork hf(db);
149
150 ASSERT_TRUE(hf.add_fork(2, 2, 1));
151 ASSERT_FALSE(hf.add_fork(3, 3, 1));
152 ASSERT_FALSE(hf.add_fork(3, 2, 2));
153 ASSERT_FALSE(hf.add_fork(2, 3, 2));
154 ASSERT_TRUE(hf.add_fork(3, 10, 2));
155 ASSERT_TRUE(hf.add_fork(4, 20, 3));
156 ASSERT_FALSE(hf.add_fork(5, 5, 4));
157 }
158
159 TEST(check_for_height, Success)
160 {
161 TestDB db;
162 HardFork hf(db, 1, 0, 0, 0, 1, 0); // no voting
163
164 ASSERT_TRUE(hf.add_fork(1, 0, 0));
165 ASSERT_TRUE(hf.add_fork(2, 5, 1));
166 hf.init();
167
168 for (uint64_t h = 0; h <= 4; ++h) {
169 ASSERT_TRUE(hf.check_for_height(mkblock(1, 1), h));
170 ASSERT_FALSE(hf.check_for_height(mkblock(2, 2), h)); // block version is too high
171 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
172 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
173 }
174
175 for (uint64_t h = 5; h <= 10; ++h) {
176 ASSERT_FALSE(hf.check_for_height(mkblock(1, 1), h)); // block version is too low
177 ASSERT_TRUE(hf.check_for_height(mkblock(2, 2), h));
178 db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0, crypto::hash());
179 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
180 }
181 }
182
183 TEST(get, next_version)
184 {
185 TestDB db;
186 HardFork hf(db);
187
188 ASSERT_TRUE(hf.add_fork(1, 0, 0));
189 ASSERT_TRUE(hf.add_fork(2, 5, 1));
190 ASSERT_TRUE(hf.add_fork(4, 10, 2));
191 hf.init();
192
193 for (uint64_t h = 0; h <= 4; ++h) {
194 ASSERT_EQ(2, hf.get_next_version());
195 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
196 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
197 }
198
199 for (uint64_t h = 5; h <= 9; ++h) {
200 ASSERT_EQ(4, hf.get_next_version());
201 db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0, crypto::hash());
202 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
203 }
204
205 for (uint64_t h = 10; h <= 15; ++h) {
206 ASSERT_EQ(4, hf.get_next_version());
207 db.add_block(mkblock(hf, h, 4), 0, 0, 0, 0, 0, crypto::hash());
208 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
209 }
210 }
211
212 TEST(states, Success)
213 {
214 TestDB db;
215 HardFork hf(db);
216
217 ASSERT_TRUE(hf.add_fork(1, 0, 0));
218 ASSERT_TRUE(hf.add_fork(2, BLOCKS_PER_YEAR, SECONDS_PER_YEAR));
219
220 ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
221 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
222 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
223 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::UpdateNeeded);
224 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::LikelyForked);
225
226 ASSERT_TRUE(hf.add_fork(3, BLOCKS_PER_YEAR * 5, SECONDS_PER_YEAR * 5));
227
228 ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
229 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
230 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
231 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::Ready);
232 ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::Ready);
233 }
234
235 TEST(steps_asap, Success)
236 {
237 TestDB db;
238 HardFork hf(db, 1,0,1,1,1);
239
240 // v h t
241 ASSERT_TRUE(hf.add_fork(1, 0, 0));
242 ASSERT_TRUE(hf.add_fork(4, 2, 1));
243 ASSERT_TRUE(hf.add_fork(7, 4, 2));
244 ASSERT_TRUE(hf.add_fork(9, 6, 3));
245 hf.init();
246
247 for (uint64_t h = 0; h < 10; ++h) {
248 db.add_block(mkblock(hf, h, 9), 0, 0, 0, 0, 0, crypto::hash());
249 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
250 }
251
252 ASSERT_EQ(hf.get(0), 1);
253 ASSERT_EQ(hf.get(1), 1);
254 ASSERT_EQ(hf.get(2), 4);
255 ASSERT_EQ(hf.get(3), 4);
256 ASSERT_EQ(hf.get(4), 7);
257 ASSERT_EQ(hf.get(5), 7);
258 ASSERT_EQ(hf.get(6), 9);
259 ASSERT_EQ(hf.get(7), 9);
260 ASSERT_EQ(hf.get(8), 9);
261 ASSERT_EQ(hf.get(9), 9);
262 }
263
264 TEST(steps_1, Success)
265 {
266 TestDB db;
267 HardFork hf(db, 1,0,1,1,1);
268
269 ASSERT_TRUE(hf.add_fork(1, 0, 0));
270 for (int n = 1 ; n < 10; ++n)
271 ASSERT_TRUE(hf.add_fork(n+1, n, n));
272 hf.init();
273
274 for (uint64_t h = 0 ; h < 10; ++h) {
275 db.add_block(mkblock(hf, h, h+1), 0, 0, 0, 0, 0, crypto::hash());
276 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
277 }
278
279 for (uint64_t h = 0; h < 10; ++h) {
280 ASSERT_EQ(hf.get(h), std::max(1,(int)h));
281 }
282 }
283
284 TEST(reorganize, Same)
285 {
286 for (int history = 1; history <= 12; ++history) {
287 TestDB db;
288 HardFork hf(db, 1, 0, 1, 1, history, 100);
289
290 // v h t
291 ASSERT_TRUE(hf.add_fork(1, 0, 0));
292 ASSERT_TRUE(hf.add_fork(4, 2, 1));
293 ASSERT_TRUE(hf.add_fork(7, 4, 2));
294 ASSERT_TRUE(hf.add_fork(9, 6, 3));
295 hf.init();
296
297 // index 0 1 2 3 4 5 6 7 8 9
298 static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
299 for (uint64_t h = 0; h < 20; ++h) {
300 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
301 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
302 }
303
304 for (uint64_t rh = 0; rh < 20; ++rh) {
305 hf.reorganize_from_block_height(rh);
306 for (int hh = 0; hh < 20; ++hh) {
307 uint8_t version = hh >= history ? block_versions[hh - history] : 1;
308 ASSERT_EQ(hf.get(hh), version);
309 }
310 }
311 }
312 }
313
314 TEST(reorganize, Changed)
315 {
316 TestDB db;
317 HardFork hf(db, 1, 0, 1, 1, 4, 100);
318
319 // v h t
320 ASSERT_TRUE(hf.add_fork(1, 0, 0));
321 ASSERT_TRUE(hf.add_fork(4, 2, 1));
322 ASSERT_TRUE(hf.add_fork(7, 4, 2));
323 ASSERT_TRUE(hf.add_fork(9, 6, 3));
324 hf.init();
325
326 // fork 4 7 9
327 // index 0 1 2 3 4 5 6 7 8 9
328 static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
329 static const uint8_t expected_versions[] = { 1, 1, 1, 1, 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9 };
330 for (uint64_t h = 0; h < 16; ++h) {
331 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
332 ASSERT_TRUE (hf.add(db.get_block_from_height(h), h));
333 }
334
335 for (uint64_t rh = 0; rh < 16; ++rh) {
336 hf.reorganize_from_block_height(rh);
337 for (int hh = 0; hh < 16; ++hh) {
338 ASSERT_EQ(hf.get(hh), expected_versions[hh]);
339 }
340 }
341
342 // delay a bit for 9, and go back to 1 to check it stays at 9
343 static const uint8_t block_versions_new[] = { 1, 1, 4, 4, 7, 7, 4, 7, 7, 7, 9, 9, 9, 9, 9, 1 };
344 static const uint8_t expected_versions_new[] = { 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 7, 7, 7, 9, 9 };
345 for (uint64_t h = 3; h < 16; ++h) {
346 db.remove_block();
347 }
348 ASSERT_EQ(db.height(), 3);
349 hf.reorganize_from_block_height(2);
350 for (uint64_t h = 3; h < 16; ++h) {
351 db.add_block(mkblock(hf, h, block_versions_new[h]), 0, 0, 0, 0, 0, crypto::hash());
352 bool ret = hf.add(db.get_block_from_height(h), h);
353 ASSERT_EQ (ret, h < 15);
354 }
355 db.remove_block(); // last block added to the blockchain, but not hf
356 ASSERT_EQ(db.height(), 15);
357 for (int hh = 0; hh < 15; ++hh) {
358 ASSERT_EQ(hf.get(hh), expected_versions_new[hh]);
359 }
360 }
361
362 TEST(voting, threshold)
363 {
364 for (int threshold = 87; threshold <= 88; ++threshold) {
365 TestDB db;
366 HardFork hf(db, 1, 0, 1, 1, 8, threshold);
367
368 // v h t
369 ASSERT_TRUE(hf.add_fork(1, 0, 0));
370 ASSERT_TRUE(hf.add_fork(2, 2, 1));
371 hf.init();
372
373 for (uint64_t h = 0; h <= 8; ++h) {
374 uint8_t v = 1 + !!(h % 8);
375 db.add_block(mkblock(hf, h, v), 0, 0, 0, 0, 0, crypto::hash());
376 bool ret = hf.add(db.get_block_from_height(h), h);
377 if (h >= 8 && threshold == 87) {
378 // for threshold 87, we reach the treshold at height 7, so from height 8, hard fork to version 2, but 8 tries to add 1
379 ASSERT_FALSE(ret);
380 }
381 else {
382 // for threshold 88, we never reach the threshold
383 ASSERT_TRUE(ret);
384 uint8_t expected = threshold == 88 ? 1 : h < 8 ? 1 : 2;
385 ASSERT_EQ(hf.get(h), expected);
386 }
387 }
388 }
389 }
390
391 TEST(voting, different_thresholds)
392 {
393 for (int threshold = 87; threshold <= 88; ++threshold) {
394 TestDB db;
395 HardFork hf(db, 1, 0, 1, 1, 4, 50); // window size 4
396
397 // v h t
398 ASSERT_TRUE(hf.add_fork(1, 0, 0));
399 ASSERT_TRUE(hf.add_fork(2, 5, 0, 1)); // asap
400 ASSERT_TRUE(hf.add_fork(3, 10, 100, 2)); // all votes
401 ASSERT_TRUE(hf.add_fork(4, 15, 3)); // default 50% votes
402 hf.init();
403
404 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
405 static const uint8_t block_versions[] = { 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4 };
406 static const uint8_t expected_versions[] = { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 };
407
408 for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) {
409 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
410 bool ret = hf.add(db.get_block_from_height(h), h);
411 ASSERT_EQ(ret, true);
412 }
413 for (uint64_t h = 0; h < sizeof(expected_versions) / sizeof(expected_versions[0]); ++h) {
414 ASSERT_EQ(hf.get(h), expected_versions[h]);
415 }
416 }
417 }
418
419 TEST(voting, info)
420 {
421 TestDB db;
422 HardFork hf(db, 1, 0, 1, 1, 4, 50); // window size 4, default threshold 50%
423
424 // v h ts
425 ASSERT_TRUE(hf.add_fork(1, 0, 0));
426 // v h thr ts
427 ASSERT_TRUE(hf.add_fork(2, 5, 0, 1)); // asap
428 ASSERT_TRUE(hf.add_fork(3, 10, 100, 2)); // all votes
429 // v h ts
430 ASSERT_TRUE(hf.add_fork(4, 15, 3)); // default 50% votes
431 hf.init();
432
433 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
434 static const uint8_t block_versions[] = { 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4 };
435 static const uint8_t expected_thresholds[] = { 0, 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 2, 2, 2, 2 };
436
437 for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) {
438 uint32_t window, votes, threshold;
439 uint64_t earliest_height;
440 uint8_t voting;
441
442 ASSERT_TRUE(hf.get_voting_info(1, window, votes, threshold, earliest_height, voting));
443 ASSERT_EQ(std::min<uint64_t>(h, 4), votes);
444 ASSERT_EQ(0, earliest_height);
445
446 ASSERT_EQ(hf.get_current_version() >= 2, hf.get_voting_info(2, window, votes, threshold, earliest_height, voting));
447 ASSERT_EQ(std::min<uint64_t>(h <= 3 ? 0 : h - 3, 4), votes);
448 ASSERT_EQ(5, earliest_height);
449
450 ASSERT_EQ(hf.get_current_version() >= 3, hf.get_voting_info(3, window, votes, threshold, earliest_height, voting));
451 ASSERT_EQ(std::min<uint64_t>(h <= 8 ? 0 : h - 8, 4), votes);
452 ASSERT_EQ(10, earliest_height);
453
454 ASSERT_EQ(hf.get_current_version() == 4, hf.get_voting_info(4, window, votes, threshold, earliest_height, voting));
455 ASSERT_EQ(std::min<uint64_t>(h <= 14 ? 0 : h - 14, 4), votes);
456 ASSERT_EQ(15, earliest_height);
457
458 ASSERT_EQ(std::min<uint64_t>(h, 4), window);
459 ASSERT_EQ(expected_thresholds[h], threshold);
460 ASSERT_EQ(4, voting);
461
462 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
463 ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
464 }
465 }
466
467 TEST(new_blocks, denied)
468 {
469 TestDB db;
470 HardFork hf(db, 1, 0, 1, 1, 4, 50);
471
472 // v h t
473 ASSERT_TRUE(hf.add_fork(1, 0, 0));
474 ASSERT_TRUE(hf.add_fork(2, 2, 1));
475 hf.init();
476
477 ASSERT_TRUE(hf.add(mkblock(1, 1), 0));
478 ASSERT_TRUE(hf.add(mkblock(1, 1), 1));
479 ASSERT_TRUE(hf.add(mkblock(1, 1), 2));
480 ASSERT_TRUE(hf.add(mkblock(1, 2), 3));
481 ASSERT_TRUE(hf.add(mkblock(1, 1), 4));
482 ASSERT_TRUE(hf.add(mkblock(1, 1), 5));
483 ASSERT_TRUE(hf.add(mkblock(1, 1), 6));
484 ASSERT_TRUE(hf.add(mkblock(1, 2), 7));
485 ASSERT_TRUE(hf.add(mkblock(1, 2), 8)); // we reach 50% of the last 4
486 ASSERT_FALSE(hf.add(mkblock(2, 1), 9)); // so this one can't get added
487 ASSERT_TRUE(hf.add(mkblock(2, 2), 9));
488 }
489
490 TEST(new_version, early)
491 {
492 TestDB db;
493 HardFork hf(db, 1, 0, 1, 1, 4, 50);
494
495 // v h t
496 ASSERT_TRUE(hf.add_fork(1, 0, 0));
497 ASSERT_TRUE(hf.add_fork(2, 4, 1));
498 hf.init();
499
500 ASSERT_TRUE(hf.add(mkblock(1, 2), 0));
501 ASSERT_TRUE(hf.add(mkblock(1, 2), 1)); // we have enough votes already
502 ASSERT_TRUE(hf.add(mkblock(1, 2), 2));
503 ASSERT_TRUE(hf.add(mkblock(1, 1), 3)); // we accept a previous version because we did not switch, even with all the votes
504 ASSERT_TRUE(hf.add(mkblock(2, 2), 4)); // but have to wait for the declared height anyway
505 ASSERT_TRUE(hf.add(mkblock(2, 2), 5));
506 ASSERT_FALSE(hf.add(mkblock(2, 1), 6)); // we don't accept 1 anymore
507 ASSERT_TRUE(hf.add(mkblock(2, 2), 7)); // but we do accept 2
508 }
509
510 TEST(reorganize, changed)
511 {
512 TestDB db;
513 HardFork hf(db, 1, 0, 1, 1, 4, 50);
514
515 // v h t
516 ASSERT_TRUE(hf.add_fork(1, 0, 0));
517 ASSERT_TRUE(hf.add_fork(2, 2, 1));
518 ASSERT_TRUE(hf.add_fork(3, 5, 2));
519 ASSERT_TRUE(hf.add_fork(4, 555, 222));
520 hf.init();
521
522 #define ADD(v, h, a) \
523 do { \
524 cryptonote::block b = mkblock(hf, h, v); \
525 db.add_block(b, 0, 0, 0, 0, 0, crypto::hash()); \
526 ASSERT_##a(hf.add(b, h)); \
527 } while(0)
528 #define ADD_TRUE(v, h) ADD(v, h, TRUE)
529 #define ADD_FALSE(v, h) ADD(v, h, FALSE)
530
531 ADD_TRUE(1, 0);
532 ADD_TRUE(1, 1);
533 ADD_TRUE(2, 2);
534 ADD_TRUE(2, 3); // switch to 2 here
535 ADD_TRUE(2, 4);
536 ADD_TRUE(2, 5);
537 ADD_TRUE(2, 6);
538 ASSERT_EQ(hf.get_current_version(), 2);
539 ADD_TRUE(3, 7);
540 ADD_TRUE(4, 8);
541 ADD_TRUE(4, 9);
542 ASSERT_EQ(hf.get_current_version(), 3);
543
544 // pop a few blocks and check current version goes back down
545 db.remove_block();
546 hf.reorganize_from_block_height(8);
547 ASSERT_EQ(hf.get_current_version(), 3);
548 db.remove_block();
549 hf.reorganize_from_block_height(7);
550 ASSERT_EQ(hf.get_current_version(), 2);
551 db.remove_block();
552 ASSERT_EQ(hf.get_current_version(), 2);
553
554 // add blocks again, but remaining at 2
555 ADD_TRUE(2, 7);
556 ADD_TRUE(2, 8);
557 ADD_TRUE(2, 9);
558 ASSERT_EQ(hf.get_current_version(), 2); // we did not bump to 3 this time
559 }
560
561 TEST(get, higher)
562 {
563 TestDB db;
564 HardFork hf(db, 1, 0, 1, 1, 4, 50);
565
566 // v h t
567 ASSERT_TRUE(hf.add_fork(1, 0, 0));
568 ASSERT_TRUE(hf.add_fork(2, 2, 1));
569 ASSERT_TRUE(hf.add_fork(3, 5, 2));
570 hf.init();
571
572 ASSERT_EQ(hf.get_ideal_version(0), 1);
573 ASSERT_EQ(hf.get_ideal_version(1), 1);
574 ASSERT_EQ(hf.get_ideal_version(2), 2);
575 ASSERT_EQ(hf.get_ideal_version(3), 2);
576 ASSERT_EQ(hf.get_ideal_version(4), 2);
577 ASSERT_EQ(hf.get_ideal_version(5), 3);
578 ASSERT_EQ(hf.get_ideal_version(6), 3);
579 ASSERT_EQ(hf.get_ideal_version(7), 3);
580 }
581
582 TEST(get, earliest_ideal_height)
583 {
584 TestDB db;
585 HardFork hf(db, 1, 0, 1, 1, 4, 50);
586
587 // v h t
588 ASSERT_TRUE(hf.add_fork(1, 0, 0));
589 ASSERT_TRUE(hf.add_fork(2, 2, 1));
590 ASSERT_TRUE(hf.add_fork(5, 5, 2));
591 ASSERT_TRUE(hf.add_fork(6, 10, 3));
592 ASSERT_TRUE(hf.add_fork(9, 15, 4));
593 hf.init();
594
595 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(1), 0);
596 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(2), 2);
597 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(3), 5);
598 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(4), 5);
599 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(5), 5);
600 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(6), 10);
601 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(7), 15);
602 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(8), 15);
603 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(9), 15);
604 ASSERT_EQ(hf.get_earliest_ideal_height_for_version(10), std::numeric_limits<uint64_t>::max());
605 }
606