diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp index 96460c90..b7b7e1a0 100644 --- a/src/cryptonote_basic/hardfork.cpp +++ b/src/cryptonote_basic/hardfork.cpp @@ -39,16 +39,6 @@ using namespace cryptonote; -static uint8_t get_block_vote(const cryptonote::block &b) -{ - return b.minor_version; -} - -static uint8_t get_block_version(const cryptonote::block &b) -{ - return b.major_version; -} - HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, uint64_t window_size, uint8_t default_threshold_percent): db(db), original_version(original_version), @@ -62,7 +52,18 @@ HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint6 throw "default_threshold_percent needs to be between 0 and 100"; } -bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold) +uint8_t HardFork::get_block_vote(const cryptonote::block *b) const +{ + return b->minor_version; +} + +uint8_t HardFork::get_block_version(const cryptonote::block &b) const +{ + return b.major_version; +} + + +bool HardFork::add_fork(uint8_t const& version, uint64_t const& height, uint8_t const& threshold) { CRITICAL_REGION_LOCAL(lock); @@ -81,7 +82,7 @@ bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold) return true; } -bool HardFork::add_fork(uint8_t version, uint64_t height) +bool HardFork::add_fork(uint8_t const& version, uint64_t const& height) { return add_fork(version, height, default_threshold_percent); } @@ -89,14 +90,14 @@ bool HardFork::add_fork(uint8_t version, uint64_t height) uint8_t HardFork::get_effective_version(uint8_t voting_version) const { if (!heights.empty()) { - uint8_t max_version = heights.back().version; + uint8_t const max_version = heights.back().version; if (voting_version > max_version) voting_version = max_version; } return voting_version; } -bool HardFork::do_check(uint8_t block_version, uint8_t voting_version) const +bool HardFork::do_check(uint8_t const& block_version, uint8_t voting_version) const { return block_version == heights[current_fork_index].version && voting_version >= heights[current_fork_index].version; @@ -105,23 +106,23 @@ bool HardFork::do_check(uint8_t block_version, uint8_t voting_version) const bool HardFork::check(const cryptonote::block &block) const { CRITICAL_REGION_LOCAL(lock); - return do_check(::get_block_version(block), ::get_block_vote(block)); + return do_check(get_block_version(block), get_block_vote(&block)); } -bool HardFork::do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const +bool HardFork::do_check_for_height(uint8_t const& block_version, uint8_t voting_version, uint64_t const& height) const { int fork_index = get_voted_fork_index(height); return block_version == heights[fork_index].version && voting_version >= heights[fork_index].version; } -bool HardFork::check_for_height(const cryptonote::block &block, uint64_t height) const +bool HardFork::check_for_height(const cryptonote::block &block, uint64_t const& height) const { CRITICAL_REGION_LOCAL(lock); - return do_check_for_height(::get_block_version(block), ::get_block_vote(block), height); + return do_check_for_height(get_block_version(block), get_block_vote(&block), height); } -bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t height) +bool HardFork::add(uint8_t const& block_version, uint8_t voting_version, uint64_t const& height) { CRITICAL_REGION_LOCAL(lock); @@ -133,7 +134,7 @@ bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t heigh voting_version = get_effective_version(voting_version); while (versions.size() >= window_size) { - const uint8_t old_version = versions.front(); + uint8_t const old_version = versions.front(); assert(last_versions[old_version] >= 1); last_versions[old_version]--; versions.pop_front(); @@ -150,9 +151,9 @@ bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t heigh return true; } -bool HardFork::add(const cryptonote::block &block, uint64_t height) +bool HardFork::add(const cryptonote::block &block, uint64_t const& height) { - return add(::get_block_version(block), ::get_block_vote(block), height); + return add(get_block_version(block), get_block_vote(&block), height); } void HardFork::init() @@ -164,7 +165,7 @@ void HardFork::init() heights.push_back(Params(original_version, 0, 0)); versions.clear(); - for (size_t n = 0; n < 256; ++n) + for (size_t n = 0; n < 255; ++n) last_versions[n] = 0; current_fork_index = 0; @@ -172,8 +173,6 @@ void HardFork::init() uint64_t height = db.height(); if (height > window_size) height -= window_size - 1; - else - height = 1; bool populate = false; try @@ -197,16 +196,16 @@ void HardFork::init() MDEBUG("reorganization done"); } -uint8_t HardFork::get_block_version(uint64_t height) const +uint8_t HardFork::get_block_version(uint64_t const& height) const { if (height <= original_version_till_height) return original_version; const cryptonote::block &block = db.get_block_from_height(height); - return ::get_block_version(block); + return get_block_version(block); } -bool HardFork::reorganize_from_block_height(uint64_t height) +bool HardFork::reorganize_from_block_height(uint64_t const& height) { CRITICAL_REGION_LOCAL(lock); if (height >= db.height()) @@ -217,7 +216,7 @@ bool HardFork::reorganize_from_block_height(uint64_t height) versions.clear(); - for (size_t n = 0; n < 256; ++n) + for (size_t n = 0; n < 255; ++n) last_versions[n] = 0; const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0; const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height); @@ -226,7 +225,7 @@ bool HardFork::reorganize_from_block_height(uint64_t height) } for (uint64_t h = rescan_height; h <= height; ++h) { cryptonote::block b = db.get_block_from_height(h); - const uint8_t v = get_effective_version(get_block_vote(b)); + const uint8_t v = get_effective_version(get_block_version(b)); last_versions[v]++; versions.push_back(v); } @@ -247,14 +246,14 @@ bool HardFork::reorganize_from_block_height(uint64_t height) return true; } -bool HardFork::reorganize_from_chain_height(uint64_t height) +bool HardFork::reorganize_from_chain_height(uint64_t const& height) { if (height == 0) return false; return reorganize_from_block_height(height - 1); } -bool HardFork::rescan_from_block_height(uint64_t height) +bool HardFork::rescan_from_block_height(uint64_t const& height) { CRITICAL_REGION_LOCAL(lock); db.block_txn_start(true); @@ -265,11 +264,11 @@ bool HardFork::rescan_from_block_height(uint64_t height) versions.clear(); - for (size_t n = 0; n < 256; ++n) + for (size_t n = 0; n < 255; ++n) last_versions[n] = 0; for (uint64_t h = height; h < db.height(); ++h) { cryptonote::block b = db.get_block_from_height(h); - const uint8_t v = get_effective_version(get_block_vote(b)); + const uint8_t v = get_effective_version(get_block_version(b)); last_versions[v]++; versions.push_back(v); } @@ -289,14 +288,14 @@ bool HardFork::rescan_from_block_height(uint64_t height) return true; } -bool HardFork::rescan_from_chain_height(uint64_t height) +bool HardFork::rescan_from_chain_height(uint64_t const& height) { if (height == 0) return false; return rescan_from_block_height(height - 1); } -int HardFork::get_voted_fork_index(uint64_t height) const +int HardFork::get_voted_fork_index(uint64_t const& height) const { CRITICAL_REGION_LOCAL(lock); uint32_t accumulated_votes = 0; @@ -311,7 +310,7 @@ int HardFork::get_voted_fork_index(uint64_t height) const return current_fork_index; } -HardFork::State HardFork::get_state(uint64_t height) const +HardFork::State HardFork::get_state(uint64_t const& height) const { CRITICAL_REGION_LOCAL(lock); @@ -332,7 +331,7 @@ HardFork::State HardFork::get_state() const return get_state(db.height()); } -uint8_t HardFork::get(uint64_t height) const +uint8_t HardFork::get(uint64_t const& height) const { CRITICAL_REGION_LOCAL(lock); if (height > db.height()) { @@ -357,7 +356,7 @@ uint8_t HardFork::get_ideal_version() const return heights.back().version; } -uint8_t HardFork::get_ideal_version(uint64_t height) const +uint8_t HardFork::get_ideal_version(uint64_t const& height) const { CRITICAL_REGION_LOCAL(lock); for (unsigned int n = heights.size() - 1; n > 0; --n) { @@ -384,7 +383,7 @@ uint64_t HardFork::get_earliest_ideal_height_for_version(uint8_t version) const uint8_t HardFork::get_next_version() const { CRITICAL_REGION_LOCAL(lock); - uint64_t height = db.height(); + uint64_t const& height = db.height(); for (auto i = heights.rbegin(); i != heights.rend(); ++i) { if (height >= i->height) { return (i == heights.rbegin() ? i : (i - 1))->version; @@ -393,7 +392,7 @@ uint8_t HardFork::get_next_version() const return original_version; } -bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const +bool HardFork::get_voting_info(uint8_t const& version, uint32_t& window, uint32_t& votes, uint32_t& threshold, uint64_t& earliest_height, uint8_t& voting) const { CRITICAL_REGION_LOCAL(lock); @@ -401,7 +400,7 @@ bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &vote const bool enabled = current_version >= version; window = versions.size(); votes = 0; - for (size_t n = version; n < 256; ++n) + for (size_t n = version; n < 255; ++n) votes += last_versions[n]; threshold = (window * heights[current_fork_index].threshold + 99) / 100; //assert((votes >= threshold) == enabled); diff --git a/src/cryptonote_basic/hardfork.h b/src/cryptonote_basic/hardfork.h index 09de59c8..da72a14c 100644 --- a/src/cryptonote_basic/hardfork.h +++ b/src/cryptonote_basic/hardfork.h @@ -56,6 +56,8 @@ namespace cryptonote * @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, uint64_t window_size = DEFAULT_WINDOW_SIZE, uint8_t default_threshold_percent = DEFAULT_THRESHOLD_PERCENT); + uint8_t get_block_vote(cryptonote::block const* b) const; + uint8_t get_block_version(cryptonote::block const& b) const; /** * @brief add a new hardfork height @@ -66,7 +68,7 @@ namespace cryptonote * @param height The height the hardfork takes effect * @param threshold The threshold of votes needed for this fork (0-100) */ - bool add_fork(uint8_t version, uint64_t height, uint8_t threshold); + bool add_fork(uint8_t const& version, uint64_t const& height, uint8_t const& threshold); /** * @brief add a new hardfork height @@ -76,7 +78,7 @@ namespace cryptonote * @param version the major block version for the fork * @param height The height the hardfork takes effect */ - bool add_fork(uint8_t version, uint64_t height); + bool add_fork(uint8_t const& version, uint64_t const& height); /** * @brief initialize the object @@ -118,7 +120,7 @@ namespace cryptonote * @param block the new block * @param height which height to check for */ - bool check_for_height(const cryptonote::block &block, uint64_t height) const; + bool check_for_height(const cryptonote::block &block, uint64_t const& height) const; /** * @brief add a new block @@ -127,7 +129,7 @@ namespace cryptonote * * @param block the new block */ - bool add(const cryptonote::block &block, uint64_t height); + bool add(const cryptonote::block &block, uint64_t const& height); /** * @brief called when the blockchain is reorganized @@ -140,18 +142,10 @@ namespace cryptonote * @param blockchain the blockchain * @param height of the last block kept from the previous blockchain */ - bool reorganize_from_block_height(uint64_t height); - bool reorganize_from_chain_height(uint64_t height); + bool reorganize_from_block_height(uint64_t const& height); + bool reorganize_from_chain_height(uint64_t const& height); - /** - * @brief returns current state at the given height - * - * Based on the approximate height of the last known hard fork, - * estimate whether we need to update, or if we're way behind - * - * @param t the time to consider - */ - State get_state(uint64_t height) const; + State get_state(uint64_t const& height) const; State get_state() const; /** @@ -159,7 +153,7 @@ namespace cryptonote * * @param height height of the block to check */ - uint8_t get(uint64_t height) const; + uint8_t get(uint64_t const& height) const; /** * @brief returns the latest "ideal" version @@ -173,7 +167,7 @@ namespace cryptonote * * @param height height of the block to check */ - uint8_t get_ideal_version(uint64_t height) const; + uint8_t get_ideal_version(uint64_t const& height) const; /** * @brief returns the next version @@ -207,7 +201,7 @@ namespace cryptonote * @param threshold number of votes needed to switch to next version * @param earliest_height earliest height at which the version can take effect */ - bool get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const; + bool get_voting_info(uint8_t const& version, uint32_t& window, uint32_t& votes, uint32_t& threshold, uint64_t& earliest_height, uint8_t& voting) const; /** * @brief returns the size of the voting window in blocks @@ -216,15 +210,15 @@ namespace cryptonote private: - uint8_t get_block_version(uint64_t height) const; - bool do_check(uint8_t block_version, uint8_t voting_version) const; - bool do_check_for_height(uint8_t block_version, uint8_t voting_version, uint64_t height) const; - int get_voted_fork_index(uint64_t height) const; + uint8_t get_block_version(uint64_t const& height) const; + bool do_check(uint8_t const& block_version, uint8_t voting_version) const; + bool do_check_for_height(uint8_t const& block_version, uint8_t voting_version, uint64_t const& height) const; + int get_voted_fork_index(uint64_t const& height) const; uint8_t get_effective_version(uint8_t voting_version) const; - bool add(uint8_t block_version, uint8_t voting_version, uint64_t height); + bool add(uint8_t const& block_version, uint8_t voting_version, uint64_t const& height); - bool rescan_from_block_height(uint64_t height); - bool rescan_from_chain_height(uint64_t height); + bool rescan_from_block_height(uint64_t const& height); + bool rescan_from_chain_height(uint64_t const& height); private: @@ -240,12 +234,12 @@ namespace cryptonote uint8_t version; uint8_t threshold; uint64_t height; - Params(uint8_t version, uint64_t height, uint8_t threshold): version(version), threshold(threshold), height(height) {} + Params(uint8_t const& version, uint64_t const& height, uint8_t const& threshold): version(version), threshold(threshold), height(height) {} }; 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 */ + unsigned int last_versions[255]; /* count of the block versions in the last N blocks */ uint32_t current_fork_index; mutable epee::critical_section lock;