33 #include "gtest/gtest.h" 38 #include "blockchain_db/testdb.h" 42 #define BLOCKS_PER_YEAR 525960 43 #define SECONDS_PER_YEAR 31557600 48 class TestDB:
public cryptonote::BaseTestDB {
51 virtual void add_block(
const block& blk
61 virtual void remove_block()
override {
blocks.pop_back(); }
66 if (versions.size() <=
height)
71 return versions.at(
height);
76 std::deque<uint8_t> versions;
111 db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0,
crypto::hash());
117 db.add_block(mkblock(1, 1), 0, 0, 0, 0, 0,
crypto::hash());
124 db.add_block(mkblock(2, 1), 0, 0, 0, 0, 0,
crypto::hash());
137 for (
uint64_t h = 0; h <= 10; ++h) {
138 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0,
crypto::hash());
160 TEST(check_for_height, Success)
172 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0,
crypto::hash());
176 for (
uint64_t h = 5; h <= 10; ++h) {
179 db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0,
crypto::hash());
196 db.add_block(mkblock(hf, h, 1), 0, 0, 0, 0, 0,
crypto::hash());
202 db.add_block(mkblock(hf, h, 2), 0, 0, 0, 0, 0,
crypto::hash());
206 for (
uint64_t h = 10; h <= 15; ++h) {
208 db.add_block(mkblock(hf, h, 4), 0, 0, 0, 0, 0,
crypto::hash());
249 db.add_block(mkblock(hf, h, 9), 0, 0, 0, 0, 0,
crypto::hash());
271 for (
int n = 1 ; n < 10; ++n)
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());
287 for (
int history = 1; history <= 12; ++history) {
289 HardFork hf(db, 1, 0, 1, 1, history, 100);
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 };
301 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0,
crypto::hash());
305 for (
uint64_t rh = 0; rh < 20; ++rh) {
307 for (
int hh = 0; hh < 20; ++hh) {
308 uint8_t version = hh >= history ? block_versions[hh - history] : 1;
318 HardFork hf(db, 1, 0, 1, 1, 4, 100);
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 };
332 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0,
crypto::hash());
336 for (
uint64_t rh = 0; rh < 16; ++rh) {
338 for (
int hh = 0; hh < 16; ++hh) {
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 };
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);
358 for (
int hh = 0; hh < 15; ++hh) {
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);
392 TEST(voting, different_thresholds)
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 };
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);
414 for (
uint64_t h = 0; h <
sizeof(expected_versions) /
sizeof(expected_versions[0]); ++h) {
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 };
439 for (
uint64_t h = 0; h <
sizeof(block_versions) /
sizeof(block_versions[0]); ++h) {
445 ASSERT_EQ(std::min<uint64_t>(h, 4), votes);
449 ASSERT_EQ(std::min<uint64_t>(h <= 3 ? 0 : h - 3, 4), votes);
453 ASSERT_EQ(std::min<uint64_t>(h <= 8 ? 0 : h - 8, 4), votes);
457 ASSERT_EQ(std::min<uint64_t>(h <= 14 ? 0 : h - 14, 4), votes);
460 ASSERT_EQ(std::min<uint64_t>(h, 4), window);
464 db.add_block(mkblock(hf, h, block_versions[h]), 0, 0, 0, 0, 0,
crypto::hash());
524 #define ADD(v, h, a) \ 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)); \ 530 #define ADD_TRUE(v, h) ADD(v, h, TRUE) 531 #define ADD_FALSE(v, h) ADD(v, h, FALSE) 584 TEST(
get, earliest_ideal_height)
uint8_t get_current_version() const
returns the current version
uint8_t get_next_version() const
returns the next version
static const time_t DEFAULT_FORKED_TIME
void init()
initialize the object
static const time_t DEFAULT_UPDATE_TIME
#define ASSERT_FALSE(condition)
Holds cryptonote related classes and helpers.
#define ASSERT_EQ(val1, val2)
uint8_t get(uint64_t height) const
returns the hard fork version for the given block height
bool add(const cryptonote::block &block, uint64_t height)
add a new block
unsigned __int64 uint64_t
bool check_for_height(const cryptonote::block &block, uint64_t height) const
same as check, but for a particular height, rather than the top
version
Supported socks variants.
uint8_t get_ideal_version() const
returns the latest "ideal" version
bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
returns information about current voting state
boost::multiprecision::uint128_t difficulty_type
#define ASSERT_TRUE(condition)
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const
returns the earliest block a given version may activate
State get_state(time_t t) const
returns current state at the given time
bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
add a new hardfork height
bool reorganize_from_block_height(uint64_t height)
called when the blockchain is reorganized