From 4187e569d8f944aeaa16141a853d33c8e162d11e Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 8 Nov 2015 13:04:41 +0000 Subject: [PATCH 1/2] hardfork: allow per-fork voting thresholds And setup the first fork to not vote --- src/cryptonote_core/blockchain.cpp | 14 ++++++++------ src/cryptonote_core/hardfork.cpp | 25 ++++++++++++++++--------- src/cryptonote_core/hardfork.h | 24 ++++++++++++++++++------ tests/unit_tests/hardfork.cpp | 28 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 21 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index d115d3bd5..49443e0e2 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -72,23 +72,25 @@ DISABLE_VS_WARNINGS(4267) static const struct { uint8_t version; uint64_t height; + uint8_t threshold; time_t time; } mainnet_hard_forks[] = { // version 1 from the start of the blockchain - { 1, 1, 1341378000 }, + { 1, 1, 0, 1341378000 }, - // version 2 can start from block 1009827, setup on the 20th of september - { 2, 1009827, 1442763710 }, + // version 2 can start from block 1009827, setup on the 20th of september. No vote. + { 2, 1009827, 0, 1442763710 }, }; static const uint64_t mainnet_hard_fork_version_1_till = 750000; static const struct { uint8_t version; uint64_t height; + uint8_t threshold; time_t time; } testnet_hard_forks[] = { // version 1 from the start of the blockchain - { 1, 1, 1341378000 }, + { 1, 1, 0, 1341378000 }, }; static const uint64_t testnet_hard_fork_version_1_till = 540000; @@ -252,13 +254,13 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet) if (testnet) { m_hardfork = new HardFork(*db, 1, testnet_hard_fork_version_1_till); for (size_t n = 0; n < sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]); ++n) - m_hardfork->add(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].time); + m_hardfork->add(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].threshold, testnet_hard_forks[n].time); } else { m_hardfork = new HardFork(*db, 1, mainnet_hard_fork_version_1_till); for (size_t n = 0; n < sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]); ++n) - m_hardfork->add(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].time); + m_hardfork->add(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].threshold, mainnet_hard_forks[n].time); } m_hardfork->init(); diff --git a/src/cryptonote_core/hardfork.cpp b/src/cryptonote_core/hardfork.cpp index 6ecaff056..45d623ca9 100644 --- a/src/cryptonote_core/hardfork.cpp +++ b/src/cryptonote_core/hardfork.cpp @@ -46,22 +46,22 @@ static uint8_t get_block_vote(const cryptonote::block &b) return b.minor_version; } -HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent): +HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, uint8_t default_threshold_percent): db(db), original_version(original_version), original_version_till_height(original_version_till_height), forked_time(forked_time), update_time(update_time), window_size(window_size), - threshold_percent(threshold_percent) + default_threshold_percent(default_threshold_percent) { if (window_size == 0) throw "window_size needs to be strictly positive"; - if (threshold_percent > 100) - throw "threshold_percent needs to be between 0 and 100"; + if (default_threshold_percent > 100) + throw "default_threshold_percent needs to be between 0 and 100"; } -bool HardFork::add(uint8_t version, uint64_t height, time_t time) +bool HardFork::add(uint8_t version, uint64_t height, uint8_t threshold, time_t time) { CRITICAL_REGION_LOCAL(lock); @@ -76,10 +76,17 @@ bool HardFork::add(uint8_t version, uint64_t height, time_t time) if (time <= heights.back().time) return false; } - heights.push_back(Params(version, height, time)); + if (threshold > 100) + return false; + heights.push_back(Params(version, height, threshold, time)); return true; } +bool HardFork::add(uint8_t version, uint64_t height, time_t time) +{ + return add(version, height, default_threshold_percent, time); +} + uint8_t HardFork::get_effective_version(uint8_t version) const { if (!heights.empty()) { @@ -146,7 +153,6 @@ void HardFork::init() for (size_t n = 0; n < 256; ++n) last_versions[n] = 0; current_fork_index = 0; - vote_threshold = (uint32_t)ceilf(window_size * threshold_percent / 100.0f); // restore state from DB uint64_t height = db.height(); @@ -274,7 +280,8 @@ int HardFork::get_voted_fork_index(uint64_t height) const for (unsigned int n = heights.size() - 1; n > current_fork_index; --n) { uint8_t v = heights[n].version; accumulated_votes += last_versions[v]; - if (height >= heights[n].height && accumulated_votes >= vote_threshold) { + uint32_t threshold = (window_size * heights[n].threshold + 99) / 100; + if (height >= heights[n].height && accumulated_votes >= threshold) { return n; } } @@ -340,7 +347,7 @@ bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &vote votes = 0; for (size_t n = version; n < 256; ++n) votes += last_versions[n]; - threshold = vote_threshold; + threshold = (window * heights[current_version].threshold + 99) / 100; assert((votes >= threshold) == enabled); voting = heights.back().version; return enabled; diff --git a/src/cryptonote_core/hardfork.h b/src/cryptonote_core/hardfork.h index 4b2c20258..f44d20f8e 100644 --- a/src/cryptonote_core/hardfork.h +++ b/src/cryptonote_core/hardfork.h @@ -48,7 +48,7 @@ namespace cryptonote static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2; static const uint64_t DEFAULT_WINDOW_SIZE = 10080; // supermajority window check length - a week - static const int DEFAULT_THRESHOLD_PERCENT = 80; + static const uint8_t DEFAULT_THRESHOLD_PERCENT = 80; /** * @brief creates a new HardFork object @@ -57,9 +57,21 @@ namespace cryptonote * @param forked_time the time in seconds before thinking we're forked * @param update_time the time in seconds before thinking we need to update * @param window_size the size of the window in blocks to consider for version voting - * @param threshold_percent the size of the majority in percents + * @param default_threshold_percent the size of the majority in percents */ - HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, uint64_t original_version_till_height = DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT); + HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, uint64_t original_version_till_height = DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, uint8_t default_threshold_percent = DEFAULT_THRESHOLD_PERCENT); + + /** + * @brief add a new hardfork height + * + * returns true if no error, false otherwise + * + * @param version the major block version for the fork + * @param height The height the hardfork takes effect + * @param threshold The threshold of votes needed for this fork (0-100) + * @param time Approximate time of the hardfork (seconds since epoch) + */ + bool add(uint8_t version, uint64_t height, uint8_t threshold, time_t time); /** * @brief add a new hardfork height @@ -199,23 +211,23 @@ namespace cryptonote time_t forked_time; time_t update_time; uint64_t window_size; - int threshold_percent; + uint8_t default_threshold_percent; uint8_t original_version; uint64_t original_version_till_height; struct Params { uint8_t version; + uint8_t threshold; uint64_t height; time_t time; - Params(uint8_t version, uint64_t height, time_t time): version(version), height(height), time(time) {} + Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {} }; std::vector heights; std::deque versions; /* rolling window of the last N blocks' versions */ unsigned int last_versions[256]; /* count of the block versions in the last N blocks */ uint32_t current_fork_index; - uint32_t vote_threshold; mutable epee::critical_section lock; }; diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index f7e4f27e9..e7387db1f 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -352,6 +352,34 @@ TEST(voting, threshold) } } +TEST(voting, different_thresholds) +{ + for (int threshold = 87; threshold <= 88; ++threshold) { + TestDB db; + HardFork hf(db, 1, 0, 1, 1, 4, 50); // window size 4 + + // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); + ASSERT_TRUE(hf.add(2, 5, 0, 1)); // asap + ASSERT_TRUE(hf.add(3, 10, 100, 2)); // all votes + ASSERT_TRUE(hf.add(4, 15, 3)); // default 50% votes + hf.init(); + + // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 + 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 }; + 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 }; + + for (uint64_t h = 0; h < sizeof(block_versions) / sizeof(block_versions[0]); ++h) { + db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash()); + bool ret = hf.add(db.get_block_from_height(h), h); + ASSERT_EQ(ret, true); + } + for (uint64_t h = 0; h < sizeof(expected_versions) / sizeof(expected_versions[0]); ++h) { + ASSERT_EQ(hf.get(h), expected_versions[h]); + } + } +} + TEST(new_blocks, denied) { TestDB db; From 2f254ff5999f1b814a91c33f982cb2fc0844ec66 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 9 Nov 2015 21:22:32 +0000 Subject: [PATCH 2/2] hardfork: add a get_ideal_version(uint64_t) function It returns the ideal version for a given height, which is based on the minimum height for a fork, disregarding votes --- src/cryptonote_core/hardfork.cpp | 11 +++++++++++ src/cryptonote_core/hardfork.h | 7 +++++++ tests/unit_tests/hardfork.cpp | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/cryptonote_core/hardfork.cpp b/src/cryptonote_core/hardfork.cpp index 45d623ca9..edc2f33a9 100644 --- a/src/cryptonote_core/hardfork.cpp +++ b/src/cryptonote_core/hardfork.cpp @@ -337,6 +337,17 @@ uint8_t HardFork::get_ideal_version() const return heights.back().version; } +uint8_t HardFork::get_ideal_version(uint64_t height) const +{ + CRITICAL_REGION_LOCAL(lock); + for (unsigned int n = heights.size() - 1; n > 0; --n) { + if (height >= heights[n].height) { + return heights[n].version; + } + } + return original_version; +} + bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint8_t &voting) const { CRITICAL_REGION_LOCAL(lock); diff --git a/src/cryptonote_core/hardfork.h b/src/cryptonote_core/hardfork.h index f44d20f8e..6800749da 100644 --- a/src/cryptonote_core/hardfork.h +++ b/src/cryptonote_core/hardfork.h @@ -167,6 +167,13 @@ namespace cryptonote */ uint8_t get_ideal_version() const; + /** + * @brief returns the "ideal" version for a given height + * + * @param height height of the block to check + */ + uint8_t get_ideal_version(uint64_t height) const; + /** * @brief returns the current version * diff --git a/tests/unit_tests/hardfork.cpp b/tests/unit_tests/hardfork.cpp index e7387db1f..1f6ef5bf0 100644 --- a/tests/unit_tests/hardfork.cpp +++ b/tests/unit_tests/hardfork.cpp @@ -480,3 +480,25 @@ TEST(reorganize, changed) ASSERT_EQ(hf.get_current_version(), 2); // we did not bump to 3 this time ASSERT_EQ(hf.get_start_height(3), std::numeric_limits::max()); // not yet } + +TEST(get, higher) +{ + TestDB db; + HardFork hf(db, 1, 0, 1, 1, 4, 50); + + // v h t + ASSERT_TRUE(hf.add(1, 0, 0)); + ASSERT_TRUE(hf.add(2, 2, 1)); + ASSERT_TRUE(hf.add(3, 5, 2)); + hf.init(); + + ASSERT_EQ(hf.get_ideal_version(0), 1); + ASSERT_EQ(hf.get_ideal_version(1), 1); + ASSERT_EQ(hf.get_ideal_version(2), 2); + ASSERT_EQ(hf.get_ideal_version(3), 2); + ASSERT_EQ(hf.get_ideal_version(4), 2); + ASSERT_EQ(hf.get_ideal_version(5), 3); + ASSERT_EQ(hf.get_ideal_version(6), 3); + ASSERT_EQ(hf.get_ideal_version(7), 3); +} +