Electroneum
hardfork.cpp File Reference
#include <algorithm>
#include "gtest/gtest.h"
#include "blockchain_db/blockchain_db.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "cryptonote_basic/hardfork.h"
#include "blockchain_db/testdb.h"
Include dependency graph for hardfork.cpp:

Go to the source code of this file.

Macros

#define BLOCKS_PER_YEAR   525960
 
#define SECONDS_PER_YEAR   31557600
 
#define ADD(v, h, a)
 
#define ADD_TRUE(v, h)   ADD(v, h, TRUE)
 
#define ADD_FALSE(v, h)   ADD(v, h, FALSE)
 

Functions

 TEST (major, Only)
 
 TEST (empty_hardforks, Success)
 
 TEST (ordering, Success)
 
 TEST (check_for_height, Success)
 
 TEST (get, next_version)
 
 TEST (states, Success)
 
 TEST (steps_asap, Success)
 
 TEST (steps_1, Success)
 
 TEST (reorganize, Same)
 
 TEST (reorganize, Changed)
 
 TEST (voting, threshold)
 
 TEST (voting, different_thresholds)
 
 TEST (voting, info)
 
 TEST (new_blocks, denied)
 
 TEST (new_version, early)
 
 TEST (reorganize, changed)
 
 TEST (get, higher)
 
 TEST (get, earliest_ideal_height)
 

Macro Definition Documentation

◆ ADD

#define ADD (   v,
  h,
  a 
)
Value:
do { \
cryptonote::block b = mkblock(hf, h, v); \
db.add_block(b, 0, 0, 0, 0, 0, crypto::hash()); \
ASSERT_##a(hf.add(b, h)); \
} while(0)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
POD_CLASS hash
Definition: hash.h:50

◆ ADD_FALSE

#define ADD_FALSE (   v,
 
)    ADD(v, h, FALSE)

◆ ADD_TRUE

#define ADD_TRUE (   v,
 
)    ADD(v, h, TRUE)

◆ BLOCKS_PER_YEAR

#define BLOCKS_PER_YEAR   525960

Definition at line 42 of file hardfork.cpp.

◆ SECONDS_PER_YEAR

#define SECONDS_PER_YEAR   31557600

Definition at line 43 of file hardfork.cpp.

Function Documentation

◆ TEST() [1/18]

TEST ( major  ,
Only   
)

Definition at line 97 of file hardfork.cpp.

98 {
99  TestDB db;
100  HardFork hf(db, 1, 0, 0, 0, 1, 0); // no voting
101 
102  // v h t
103  ASSERT_TRUE(hf.add_fork(1, 0, 0));
104  ASSERT_TRUE(hf.add_fork(2, 2, 1));
105  hf.init();
106 
107  // block height 0, only version 1 is accepted
108  ASSERT_FALSE(hf.add(mkblock(0, 2), 0));
109  ASSERT_FALSE(hf.add(mkblock(2, 2), 0));
110  ASSERT_TRUE(hf.add(mkblock(1, 2), 0));
111  db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0, crypto::hash());
112 
113  // block height 1, only version 1 is accepted
114  ASSERT_FALSE(hf.add(mkblock(0, 2), 1));
115  ASSERT_FALSE(hf.add(mkblock(2, 2), 1));
116  ASSERT_TRUE(hf.add(mkblock(1, 2), 1));
117  db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0, crypto::hash());
118 
119  // block height 2, only version 2 is accepted
120  ASSERT_FALSE(hf.add(mkblock(0, 2), 2));
121  ASSERT_FALSE(hf.add(mkblock(1, 2), 2));
122  ASSERT_FALSE(hf.add(mkblock(3, 2), 2));
123  ASSERT_TRUE(hf.add(mkblock(2, 2), 2));
124  db.add_block(mkblock(2, 1), 0, 0, 0, 0, 0, crypto::hash());
125 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [2/18]

TEST ( empty_hardforks  ,
Success   
)

Definition at line 127 of file hardfork.cpp.

128 {
129  TestDB db;
130  HardFork hf(db);
131 
132  ASSERT_TRUE(hf.add_fork(1, 0, 0));
133  hf.init();
134  ASSERT_TRUE(hf.get_state(time(NULL)) == HardFork::Ready);
135  ASSERT_TRUE(hf.get_state(time(NULL) + 3600*24*400) == HardFork::Ready);
136 
137  for (uint64_t h = 0; h <= 10; ++h) {
138  db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
139  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
140  }
141  ASSERT_EQ(hf.get(0), 1);
142  ASSERT_EQ(hf.get(1), 1);
143  ASSERT_EQ(hf.get(10), 1);
144 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
time_t time
Definition: blockchain.cpp:93
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [3/18]

TEST ( ordering  ,
Success   
)

Definition at line 146 of file hardfork.cpp.

147 {
148  TestDB db;
149  HardFork hf(db);
150 
151  ASSERT_TRUE(hf.add_fork(2, 2, 1));
152  ASSERT_FALSE(hf.add_fork(3, 3, 1));
153  ASSERT_FALSE(hf.add_fork(3, 2, 2));
154  ASSERT_FALSE(hf.add_fork(2, 3, 2));
155  ASSERT_TRUE(hf.add_fork(3, 10, 2));
156  ASSERT_TRUE(hf.add_fork(4, 20, 3));
157  ASSERT_FALSE(hf.add_fork(5, 5, 4));
158 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [4/18]

TEST ( check_for_height  ,
Success   
)

Definition at line 160 of file hardfork.cpp.

161 {
162  TestDB db;
163  HardFork hf(db, 1, 0, 0, 0, 1, 0); // no voting
164 
165  ASSERT_TRUE(hf.add_fork(1, 0, 0));
166  ASSERT_TRUE(hf.add_fork(2, 5, 1));
167  hf.init();
168 
169  for (uint64_t h = 0; h <= 4; ++h) {
170  ASSERT_TRUE(hf.check_for_height(mkblock(1, 1), h));
171  ASSERT_FALSE(hf.check_for_height(mkblock(2, 2), h)); // block version is too high
172  db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
173  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
174  }
175 
176  for (uint64_t h = 5; h <= 10; ++h) {
177  ASSERT_FALSE(hf.check_for_height(mkblock(1, 1), h)); // block version is too low
178  ASSERT_TRUE(hf.check_for_height(mkblock(2, 2), h));
179  db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0, crypto::hash());
180  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
181  }
182 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [5/18]

TEST ( get  ,
next_version   
)

Definition at line 184 of file hardfork.cpp.

185 {
186  TestDB db;
187  HardFork hf(db);
188 
189  ASSERT_TRUE(hf.add_fork(1, 0, 0));
190  ASSERT_TRUE(hf.add_fork(2, 5, 1));
191  ASSERT_TRUE(hf.add_fork(4, 10, 2));
192  hf.init();
193 
194  for (uint64_t h = 0; h <= 4; ++h) {
195  ASSERT_EQ(2, hf.get_next_version());
196  db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0, crypto::hash());
197  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
198  }
199 
200  for (uint64_t h = 5; h <= 9; ++h) {
201  ASSERT_EQ(4, hf.get_next_version());
202  db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0, crypto::hash());
203  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
204  }
205 
206  for (uint64_t h = 10; h <= 15; ++h) {
207  ASSERT_EQ(4, hf.get_next_version());
208  db.add_block(mkblock(hf, h, 4), 0, 0, 0, 0, 0, crypto::hash());
209  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
210  }
211 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [6/18]

TEST ( states  ,
Success   
)

Definition at line 213 of file hardfork.cpp.

214 {
215  TestDB db;
216  HardFork hf(db);
217 
218  ASSERT_TRUE(hf.add_fork(1, 0, 0));
219  ASSERT_TRUE(hf.add_fork(2, BLOCKS_PER_YEAR, SECONDS_PER_YEAR));
220 
221  ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
222  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
223  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
224  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::UpdateNeeded);
225  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::LikelyForked);
226 
227  ASSERT_TRUE(hf.add_fork(3, BLOCKS_PER_YEAR * 5, SECONDS_PER_YEAR * 5));
228 
229  ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
230  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
231  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
232  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::Ready);
233  ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::Ready);
234 }
#define BLOCKS_PER_YEAR
Definition: hardfork.cpp:42
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define SECONDS_PER_YEAR
Definition: hardfork.cpp:43
Here is the call graph for this function:

◆ TEST() [7/18]

TEST ( steps_asap  ,
Success   
)

Definition at line 236 of file hardfork.cpp.

237 {
238  TestDB db;
239  HardFork hf(db, 1,0,1,1,1);
240 
241  // v h t
242  ASSERT_TRUE(hf.add_fork(1, 0, 0));
243  ASSERT_TRUE(hf.add_fork(4, 2, 1));
244  ASSERT_TRUE(hf.add_fork(7, 4, 2));
245  ASSERT_TRUE(hf.add_fork(9, 6, 3));
246  hf.init();
247 
248  for (uint64_t h = 0; h < 10; ++h) {
249  db.add_block(mkblock(hf, h, 9), 0, 0, 0, 0, 0, crypto::hash());
250  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
251  }
252 
253  ASSERT_EQ(hf.get(0), 1);
254  ASSERT_EQ(hf.get(1), 1);
255  ASSERT_EQ(hf.get(2), 4);
256  ASSERT_EQ(hf.get(3), 4);
257  ASSERT_EQ(hf.get(4), 7);
258  ASSERT_EQ(hf.get(5), 7);
259  ASSERT_EQ(hf.get(6), 9);
260  ASSERT_EQ(hf.get(7), 9);
261  ASSERT_EQ(hf.get(8), 9);
262  ASSERT_EQ(hf.get(9), 9);
263 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [8/18]

TEST ( steps_1  ,
Success   
)

Definition at line 265 of file hardfork.cpp.

266 {
267  TestDB db;
268  HardFork hf(db, 1,0,1,1,1);
269 
270  ASSERT_TRUE(hf.add_fork(1, 0, 0));
271  for (int n = 1 ; n < 10; ++n)
272  ASSERT_TRUE(hf.add_fork(n+1, n, n));
273  hf.init();
274 
275  for (uint64_t h = 0 ; h < 10; ++h) {
276  db.add_block(mkblock(hf, h, h+1), 0, 0, 0, 0, 0, crypto::hash());
277  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
278  }
279 
280  for (uint64_t h = 0; h < 10; ++h) {
281  ASSERT_EQ(hf.get(h), std::max(1,(int)h));
282  }
283 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [9/18]

TEST ( reorganize  ,
Same   
)

Definition at line 285 of file hardfork.cpp.

286 {
287  for (int history = 1; history <= 12; ++history) {
288  TestDB db;
289  HardFork hf(db, 1, 0, 1, 1, history, 100);
290 
291  // v h t
292  ASSERT_TRUE(hf.add_fork(1, 0, 0));
293  ASSERT_TRUE(hf.add_fork(4, 2, 1));
294  ASSERT_TRUE(hf.add_fork(7, 4, 2));
295  ASSERT_TRUE(hf.add_fork(9, 6, 3));
296  hf.init();
297 
298  // index 0 1 2 3 4 5 6 7 8 9
299  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 };
300  for (uint64_t h = 0; h < 20; ++h) {
301  db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
302  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
303  }
304 
305  for (uint64_t rh = 0; rh < 20; ++rh) {
306  hf.reorganize_from_block_height(rh);
307  for (int hh = 0; hh < 20; ++hh) {
308  uint8_t version = hh >= history ? block_versions[hh - history] : 1;
309  ASSERT_EQ(hf.get(hh), version);
310  }
311  }
312  }
313 }
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
version
Supported socks variants.
Definition: socks.h:57
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [10/18]

TEST ( reorganize  ,
Changed   
)

Definition at line 315 of file hardfork.cpp.

316 {
317  TestDB db;
318  HardFork hf(db, 1, 0, 1, 1, 4, 100);
319 
320  // v h t
321  ASSERT_TRUE(hf.add_fork(1, 0, 0));
322  ASSERT_TRUE(hf.add_fork(4, 2, 1));
323  ASSERT_TRUE(hf.add_fork(7, 4, 2));
324  ASSERT_TRUE(hf.add_fork(9, 6, 3));
325  hf.init();
326 
327  // fork 4 7 9
328  // index 0 1 2 3 4 5 6 7 8 9
329  static const uint8_t block_versions[] = { 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
330  static const uint8_t expected_versions[] = { 1, 1, 1, 1, 1, 1, 4, 4, 7, 7, 9, 9, 9, 9, 9, 9 };
331  for (uint64_t h = 0; h < 16; ++h) {
332  db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
333  ASSERT_TRUE (hf.add(db.get_block_from_height(h), h));
334  }
335 
336  for (uint64_t rh = 0; rh < 16; ++rh) {
337  hf.reorganize_from_block_height(rh);
338  for (int hh = 0; hh < 16; ++hh) {
339  ASSERT_EQ(hf.get(hh), expected_versions[hh]);
340  }
341  }
342 
343  // delay a bit for 9, and go back to 1 to check it stays at 9
344  static const uint8_t block_versions_new[] = { 1, 1, 4, 4, 7, 7, 4, 7, 7, 7, 9, 9, 9, 9, 9, 1 };
345  static const uint8_t expected_versions_new[] = { 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 7, 7, 7, 9, 9 };
346  for (uint64_t h = 3; h < 16; ++h) {
347  db.remove_block();
348  }
349  ASSERT_EQ(db.height(), 3);
350  hf.reorganize_from_block_height(2);
351  for (uint64_t h = 3; h < 16; ++h) {
352  db.add_block(mkblock(hf, h, block_versions_new[h]), 0, 0, 0, 0, 0, crypto::hash());
353  bool ret = hf.add(db.get_block_from_height(h), h);
354  ASSERT_EQ (ret, h < 15);
355  }
356  db.remove_block(); // last block added to the blockchain, but not hf
357  ASSERT_EQ(db.height(), 15);
358  for (int hh = 0; hh < 15; ++hh) {
359  ASSERT_EQ(hf.get(hh), expected_versions_new[hh]);
360  }
361 }
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ TEST() [11/18]

TEST ( voting  ,
threshold   
)

Definition at line 363 of file hardfork.cpp.

364 {
365  for (int threshold = 87; threshold <= 88; ++threshold) {
366  TestDB db;
367  HardFork hf(db, 1, 0, 1, 1, 8, threshold);
368 
369  // v h t
370  ASSERT_TRUE(hf.add_fork(1, 0, 0));
371  ASSERT_TRUE(hf.add_fork(2, 2, 1));
372  hf.init();
373 
374  for (uint64_t h = 0; h <= 8; ++h) {
375  uint8_t v = 1 + !!(h % 8);
376  db.add_block(mkblock(hf, h, v), 0, 0, 0, 0, 0, crypto::hash());
377  bool ret = hf.add(db.get_block_from_height(h), h);
378  if (h >= 8 && threshold == 87) {
379  // 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
380  ASSERT_FALSE(ret);
381  }
382  else {
383  // for threshold 88, we never reach the threshold
384  ASSERT_TRUE(ret);
385  uint8_t expected = threshold == 88 ? 1 : h < 8 ? 1 : 2;
386  ASSERT_EQ(hf.get(h), expected);
387  }
388  }
389  }
390 }
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
uint8_t threshold
Definition: blockchain.cpp:92
Here is the call graph for this function:

◆ TEST() [12/18]

TEST ( voting  ,
different_thresholds   
)

Definition at line 392 of file hardfork.cpp.

393 {
394  for (int threshold = 87; threshold <= 88; ++threshold) {
395  TestDB db;
396  HardFork hf(db, 1, 0, 1, 1, 4, 50); // window size 4
397 
398  // v h t
399  ASSERT_TRUE(hf.add_fork(1, 0, 0));
400  ASSERT_TRUE(hf.add_fork(2, 5, 0, 1)); // asap
401  ASSERT_TRUE(hf.add_fork(3, 10, 100, 2)); // all votes
402  ASSERT_TRUE(hf.add_fork(4, 15, 3)); // default 50% votes
403  hf.init();
404 
405  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
406  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 };
407  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 };
408 
409  for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) {
410  db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
411  bool ret = hf.add(db.get_block_from_height(h), h);
412  ASSERT_EQ(ret, true);
413  }
414  for (uint64_t h = 0; h < sizeof(expected_versions) / sizeof(expected_versions[0]); ++h) {
415  ASSERT_EQ(hf.get(h), expected_versions[h]);
416  }
417  }
418 }
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
uint8_t threshold
Definition: blockchain.cpp:92
Here is the call graph for this function:

◆ TEST() [13/18]

TEST ( voting  ,
info   
)

Definition at line 420 of file hardfork.cpp.

421 {
422  TestDB db;
423  HardFork hf(db, 1, 0, 1, 1, 4, 50); // window size 4, default threshold 50%
424 
425  // v h ts
426  ASSERT_TRUE(hf.add_fork(1, 0, 0));
427  // v h thr ts
428  ASSERT_TRUE(hf.add_fork(2, 5, 0, 1)); // asap
429  ASSERT_TRUE(hf.add_fork(3, 10, 100, 2)); // all votes
430  // v h ts
431  ASSERT_TRUE(hf.add_fork(4, 15, 3)); // default 50% votes
432  hf.init();
433 
434  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
435  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 };
436  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 };
437  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 };
438 
439  for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) {
440  uint32_t window, votes, threshold;
441  uint64_t earliest_height;
442  uint8_t voting;
443 
444  ASSERT_TRUE(hf.get_voting_info(1, window, votes, threshold, earliest_height, voting));
445  ASSERT_EQ(std::min<uint64_t>(h, 4), votes);
446  ASSERT_EQ(0, earliest_height);
447 
448  ASSERT_EQ(hf.get_current_version() >= 2, hf.get_voting_info(2, window, votes, threshold, earliest_height, voting));
449  ASSERT_EQ(std::min<uint64_t>(h <= 3 ? 0 : h - 3, 4), votes);
450  ASSERT_EQ(5, earliest_height);
451 
452  ASSERT_EQ(hf.get_current_version() >= 3, hf.get_voting_info(3, window, votes, threshold, earliest_height, voting));
453  ASSERT_EQ(std::min<uint64_t>(h <= 8 ? 0 : h - 8, 4), votes);
454  ASSERT_EQ(10, earliest_height);
455 
456  ASSERT_EQ(hf.get_current_version() == 4, hf.get_voting_info(4, window, votes, threshold, earliest_height, voting));
457  ASSERT_EQ(std::min<uint64_t>(h <= 14 ? 0 : h - 14, 4), votes);
458  ASSERT_EQ(15, earliest_height);
459 
460  ASSERT_EQ(std::min<uint64_t>(h, 4), window);
461  ASSERT_EQ(expected_thresholds[h], threshold);
462  ASSERT_EQ(4, voting);
463 
464  db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0, crypto::hash());
465  ASSERT_TRUE(hf.add(db.get_block_from_height(h), h));
466  }
467 }
unsigned char uint8_t
Definition: stdint.h:124
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
POD_CLASS hash
Definition: hash.h:50
uint8_t threshold
Definition: blockchain.cpp:92
Here is the call graph for this function:

◆ TEST() [14/18]

TEST ( new_blocks  ,
denied   
)

Definition at line 469 of file hardfork.cpp.

470 {
471  TestDB db;
472  HardFork hf(db, 1, 0, 1, 1, 4, 50);
473 
474  // v h t
475  ASSERT_TRUE(hf.add_fork(1, 0, 0));
476  ASSERT_TRUE(hf.add_fork(2, 2, 1));
477  hf.init();
478 
479  ASSERT_TRUE(hf.add(mkblock(1, 1), 0));
480  ASSERT_TRUE(hf.add(mkblock(1, 1), 1));
481  ASSERT_TRUE(hf.add(mkblock(1, 1), 2));
482  ASSERT_TRUE(hf.add(mkblock(1, 2), 3));
483  ASSERT_TRUE(hf.add(mkblock(1, 1), 4));
484  ASSERT_TRUE(hf.add(mkblock(1, 1), 5));
485  ASSERT_TRUE(hf.add(mkblock(1, 1), 6));
486  ASSERT_TRUE(hf.add(mkblock(1, 2), 7));
487  ASSERT_TRUE(hf.add(mkblock(1, 2), 8)); // we reach 50% of the last 4
488  ASSERT_FALSE(hf.add(mkblock(2, 1), 9)); // so this one can't get added
489  ASSERT_TRUE(hf.add(mkblock(2, 2), 9));
490 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [15/18]

TEST ( new_version  ,
early   
)

Definition at line 492 of file hardfork.cpp.

493 {
494  TestDB db;
495  HardFork hf(db, 1, 0, 1, 1, 4, 50);
496 
497  // v h t
498  ASSERT_TRUE(hf.add_fork(1, 0, 0));
499  ASSERT_TRUE(hf.add_fork(2, 4, 1));
500  hf.init();
501 
502  ASSERT_TRUE(hf.add(mkblock(1, 2), 0));
503  ASSERT_TRUE(hf.add(mkblock(1, 2), 1)); // we have enough votes already
504  ASSERT_TRUE(hf.add(mkblock(1, 2), 2));
505  ASSERT_TRUE(hf.add(mkblock(1, 1), 3)); // we accept a previous version because we did not switch, even with all the votes
506  ASSERT_TRUE(hf.add(mkblock(2, 2), 4)); // but have to wait for the declared height anyway
507  ASSERT_TRUE(hf.add(mkblock(2, 2), 5));
508  ASSERT_FALSE(hf.add(mkblock(2, 1), 6)); // we don't accept 1 anymore
509  ASSERT_TRUE(hf.add(mkblock(2, 2), 7)); // but we do accept 2
510 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [16/18]

TEST ( reorganize  ,
changed   
)

Definition at line 512 of file hardfork.cpp.

513 {
514  TestDB db;
515  HardFork hf(db, 1, 0, 1, 1, 4, 50);
516 
517  // v h t
518  ASSERT_TRUE(hf.add_fork(1, 0, 0));
519  ASSERT_TRUE(hf.add_fork(2, 2, 1));
520  ASSERT_TRUE(hf.add_fork(3, 5, 2));
521  ASSERT_TRUE(hf.add_fork(4, 555, 222));
522  hf.init();
523 
524 #define ADD(v, h, a) \
525  do { \
526  cryptonote::block b = mkblock(hf, h, v); \
527  db.add_block(b, 0, 0, 0, 0, 0, crypto::hash()); \
528  ASSERT_##a(hf.add(b, h)); \
529  } while(0)
530 #define ADD_TRUE(v, h) ADD(v, h, TRUE)
531 #define ADD_FALSE(v, h) ADD(v, h, FALSE)
532 
533  ADD_TRUE(1, 0);
534  ADD_TRUE(1, 1);
535  ADD_TRUE(2, 2);
536  ADD_TRUE(2, 3); // switch to 2 here
537  ADD_TRUE(2, 4);
538  ADD_TRUE(2, 5);
539  ADD_TRUE(2, 6);
540  ASSERT_EQ(hf.get_current_version(), 2);
541  ADD_TRUE(3, 7);
542  ADD_TRUE(4, 8);
543  ADD_TRUE(4, 9);
544  ASSERT_EQ(hf.get_current_version(), 3);
545 
546  // pop a few blocks and check current version goes back down
547  db.remove_block();
548  hf.reorganize_from_block_height(8);
549  ASSERT_EQ(hf.get_current_version(), 3);
550  db.remove_block();
551  hf.reorganize_from_block_height(7);
552  ASSERT_EQ(hf.get_current_version(), 2);
553  db.remove_block();
554  ASSERT_EQ(hf.get_current_version(), 2);
555 
556  // add blocks again, but remaining at 2
557  ADD_TRUE(2, 7);
558  ADD_TRUE(2, 8);
559  ADD_TRUE(2, 9);
560  ASSERT_EQ(hf.get_current_version(), 2); // we did not bump to 3 this time
561 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define ADD_TRUE(v, h)
Here is the call graph for this function:

◆ TEST() [17/18]

TEST ( get  ,
higher   
)

Definition at line 563 of file hardfork.cpp.

564 {
565  TestDB db;
566  HardFork hf(db, 1, 0, 1, 1, 4, 50);
567 
568  // v h t
569  ASSERT_TRUE(hf.add_fork(1, 0, 0));
570  ASSERT_TRUE(hf.add_fork(2, 2, 1));
571  ASSERT_TRUE(hf.add_fork(3, 5, 2));
572  hf.init();
573 
574  ASSERT_EQ(hf.get_ideal_version(0), 1);
575  ASSERT_EQ(hf.get_ideal_version(1), 1);
576  ASSERT_EQ(hf.get_ideal_version(2), 2);
577  ASSERT_EQ(hf.get_ideal_version(3), 2);
578  ASSERT_EQ(hf.get_ideal_version(4), 2);
579  ASSERT_EQ(hf.get_ideal_version(5), 3);
580  ASSERT_EQ(hf.get_ideal_version(6), 3);
581  ASSERT_EQ(hf.get_ideal_version(7), 3);
582 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [18/18]

TEST ( get  ,
earliest_ideal_height   
)

Definition at line 584 of file hardfork.cpp.

585 {
586  TestDB db;
587  HardFork hf(db, 1, 0, 1, 1, 4, 50);
588 
589  // v h t
590  ASSERT_TRUE(hf.add_fork(1, 0, 0));
591  ASSERT_TRUE(hf.add_fork(2, 2, 1));
592  ASSERT_TRUE(hf.add_fork(5, 5, 2));
593  ASSERT_TRUE(hf.add_fork(6, 10, 3));
594  ASSERT_TRUE(hf.add_fork(9, 15, 4));
595  hf.init();
596 
597  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(1), 0);
598  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(2), 2);
599  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(3), 5);
600  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(4), 5);
601  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(5), 5);
602  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(6), 10);
603  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(7), 15);
604  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(8), 15);
605  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(9), 15);
606  ASSERT_EQ(hf.get_earliest_ideal_height_for_version(10), std::numeric_limits<uint64_t>::max());
607 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function: