diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-02-25 16:41:35 +0000 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-02-25 16:41:35 +0000 |
commit | 12adb4a3f3ce7b3a9263ba187589a086f1b2fa54 (patch) | |
tree | 6036997b784f86b5496a0275871b77b13474f18d /src/cryptonote_basic | |
parent | Merge pull request #1798 (diff) | |
download | monero-12adb4a3f3ce7b3a9263ba187589a086f1b2fa54.tar.xz |
core: move hardfork back to cryptonote_core
should fix a cross dependency betewen cryptonote_basic and
blockchain_db
Diffstat (limited to 'src/cryptonote_basic')
-rw-r--r-- | src/cryptonote_basic/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/cryptonote_basic/hardfork.cpp | 416 | ||||
-rw-r--r-- | src/cryptonote_basic/hardfork.h | 265 |
3 files changed, 2 insertions, 685 deletions
diff --git a/src/cryptonote_basic/CMakeLists.txt b/src/cryptonote_basic/CMakeLists.txt index 9df157a9e..db478c690 100644 --- a/src/cryptonote_basic/CMakeLists.txt +++ b/src/cryptonote_basic/CMakeLists.txt @@ -32,8 +32,7 @@ set(cryptonote_basic_sources cryptonote_basic_impl.cpp cryptonote_format_utils.cpp difficulty.cpp - miner.cpp - hardfork.cpp) + miner.cpp) set(cryptonote_basic_headers) @@ -50,8 +49,7 @@ set(cryptonote_basic_private_headers difficulty.h miner.h tx_extra.h - verification_context.h - hardfork.h) + verification_context.h) monero_private_headers(cryptonote_basic ${crypto_private_headers}) diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp deleted file mode 100644 index 24d6655f0..000000000 --- a/src/cryptonote_basic/hardfork.cpp +++ /dev/null @@ -1,416 +0,0 @@ -// Copyright (c) 2014-2017, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#include <algorithm> -#include <cstdio> - -#include "cryptonote_basic.h" -#include "blockchain_db/blockchain_db.h" -#include "hardfork.h" - -#undef MONERO_DEFAULT_LOG_CATEGORY -#define MONERO_DEFAULT_LOG_CATEGORY "hardfork" - -using namespace cryptonote; - -static uint8_t get_block_vote(const cryptonote::block &b) -{ - // Pre-hardfork blocks have a minor version hardcoded to 0. - // For the purposes of voting, we consider 0 to refer to - // version number 1, which is what all blocks from the genesis - // block are. It makes things simpler. - if (b.minor_version == 0) - return 1; - 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, 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), - default_threshold_percent(default_threshold_percent) -{ - if (window_size == 0) - throw "window_size needs to be strictly positive"; - if (default_threshold_percent > 100) - throw "default_threshold_percent needs to be between 0 and 100"; -} - -bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time) -{ - CRITICAL_REGION_LOCAL(lock); - - // add in order - if (version == 0) - return false; - if (!heights.empty()) { - if (version <= heights.back().version) - return false; - if (height <= heights.back().height) - return false; - if (time <= heights.back().time) - return false; - } - if (threshold > 100) - return false; - heights.push_back(Params(version, height, threshold, time)); - return true; -} - -bool HardFork::add_fork(uint8_t version, uint64_t height, time_t time) -{ - return add_fork(version, height, default_threshold_percent, time); -} - -uint8_t HardFork::get_effective_version(uint8_t voting_version) const -{ - if (!heights.empty()) { - uint8_t 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 -{ - return block_version == heights[current_fork_index].version - && voting_version >= heights[current_fork_index].version; -} - -bool HardFork::check(const cryptonote::block &block) const -{ - CRITICAL_REGION_LOCAL(lock); - 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 -{ - 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 -{ - CRITICAL_REGION_LOCAL(lock); - 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) -{ - CRITICAL_REGION_LOCAL(lock); - - if (!do_check(block_version, voting_version)) - return false; - - db.set_hard_fork_version(height, heights[current_fork_index].version); - - voting_version = get_effective_version(voting_version); - - while (versions.size() >= window_size) { - const uint8_t old_version = versions.front(); - assert(last_versions[old_version] >= 1); - last_versions[old_version]--; - versions.pop_front(); - } - - last_versions[voting_version]++; - versions.push_back(voting_version); - - uint8_t voted = get_voted_fork_index(height + 1); - if (voted > current_fork_index) { - current_fork_index = voted; - } - - return true; -} - -bool HardFork::add(const cryptonote::block &block, uint64_t height) -{ - return add(::get_block_version(block), ::get_block_vote(block), height); -} - -void HardFork::init() -{ - CRITICAL_REGION_LOCAL(lock); - - // add a placeholder for the default version, to avoid special cases - if (heights.empty()) - heights.push_back(Params(original_version, 0, 0, 0)); - - versions.clear(); - for (size_t n = 0; n < 256; ++n) - last_versions[n] = 0; - current_fork_index = 0; - - // restore state from DB - uint64_t height = db.height(); - if (height > window_size) - height -= window_size - 1; - else - height = 1; - - bool populate = false; - try - { - db.get_hard_fork_version(0); - } - catch (...) { populate = true; } - if (populate) { - MINFO("The DB has no hard fork info, reparsing from start"); - height = 1; - } - MDEBUG("reorganizing from " << height); - if (populate) { - reorganize_from_chain_height(height); - // reorg will not touch the genesis block, use this as a flag for populating done - db.set_hard_fork_version(0, original_version); - } - else { - rescan_from_chain_height(height); - } - MDEBUG("reorganization done"); -} - -uint8_t HardFork::get_block_version(uint64_t 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); -} - -bool HardFork::reorganize_from_block_height(uint64_t height) -{ - CRITICAL_REGION_LOCAL(lock); - if (height >= db.height()) - return false; - - db.set_batch_transactions(true); - db.batch_start(); - - versions.clear(); - - for (size_t n = 0; n < 256; ++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); - while (current_fork_index > 0 && heights[current_fork_index].version > start_version) { - --current_fork_index; - } - 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)); - last_versions[v]++; - versions.push_back(v); - } - - uint8_t voted = get_voted_fork_index(height + 1); - if (voted > current_fork_index) { - current_fork_index = voted; - } - - const uint64_t bc_height = db.height(); - for (uint64_t h = height + 1; h < bc_height; ++h) { - add(db.get_block_from_height(h), h); - } - - db.batch_stop(); - - return true; -} - -bool HardFork::reorganize_from_chain_height(uint64_t height) -{ - if (height == 0) - return false; - return reorganize_from_block_height(height - 1); -} - -bool HardFork::rescan_from_block_height(uint64_t height) -{ - CRITICAL_REGION_LOCAL(lock); - db.block_txn_start(true); - if (height >= db.height()) { - db.block_txn_stop(); - return false; - } - - versions.clear(); - - for (size_t n = 0; n < 256; ++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)); - last_versions[v]++; - versions.push_back(v); - } - - uint8_t lastv = db.get_hard_fork_version(db.height() - 1); - current_fork_index = 0; - while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv) - ++current_fork_index; - - uint8_t voted = get_voted_fork_index(db.height()); - if (voted > current_fork_index) { - current_fork_index = voted; - } - - db.block_txn_stop(); - - return true; -} - -bool HardFork::rescan_from_chain_height(uint64_t height) -{ - if (height == 0) - return false; - return rescan_from_block_height(height - 1); -} - -int HardFork::get_voted_fork_index(uint64_t height) const -{ - CRITICAL_REGION_LOCAL(lock); - uint32_t accumulated_votes = 0; - for (unsigned int n = heights.size() - 1; n > current_fork_index; --n) { - uint8_t v = heights[n].version; - accumulated_votes += last_versions[v]; - uint32_t threshold = (window_size * heights[n].threshold + 99) / 100; - if (height >= heights[n].height && accumulated_votes >= threshold) { - return n; - } - } - return current_fork_index; -} - -HardFork::State HardFork::get_state(time_t t) const -{ - CRITICAL_REGION_LOCAL(lock); - - // no hard forks setup yet - if (heights.size() <= 1) - return Ready; - - time_t t_last_fork = heights.back().time; - if (t >= t_last_fork + forked_time) - return LikelyForked; - if (t >= t_last_fork + update_time) - return UpdateNeeded; - return Ready; -} - -HardFork::State HardFork::get_state() const -{ - return get_state(time(NULL)); -} - -uint8_t HardFork::get(uint64_t height) const -{ - CRITICAL_REGION_LOCAL(lock); - if (height > db.height()) { - assert(false); - return 255; - } - if (height == db.height()) { - return get_current_version(); - } - return db.get_hard_fork_version(height); -} - -uint8_t HardFork::get_current_version() const -{ - CRITICAL_REGION_LOCAL(lock); - return heights[current_fork_index].version; -} - -uint8_t HardFork::get_ideal_version() const -{ - CRITICAL_REGION_LOCAL(lock); - 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; -} - -uint64_t HardFork::get_earliest_ideal_height_for_version(uint8_t version) const -{ - for (unsigned int n = heights.size() - 1; n > 0; --n) { - if (heights[n].version <= version) - return heights[n].height; - } - return 0; -} - -uint8_t HardFork::get_next_version() const -{ - CRITICAL_REGION_LOCAL(lock); - uint64_t height = db.height(); - for (unsigned int n = heights.size() - 1; n > 0; --n) { - if (height >= heights[n].height) { - return heights[n < heights.size() - 1 ? n + 1 : n].version; - } - } - 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 -{ - CRITICAL_REGION_LOCAL(lock); - - const uint8_t current_version = heights[current_fork_index].version; - const bool enabled = current_version >= version; - window = versions.size(); - votes = 0; - for (size_t n = version; n < 256; ++n) - votes += last_versions[n]; - threshold = (window * heights[current_fork_index].threshold + 99) / 100; - //assert((votes >= threshold) == enabled); - earliest_height = get_earliest_ideal_height_for_version(version); - voting = heights.back().version; - return enabled; -} - diff --git a/src/cryptonote_basic/hardfork.h b/src/cryptonote_basic/hardfork.h deleted file mode 100644 index 11241e657..000000000 --- a/src/cryptonote_basic/hardfork.h +++ /dev/null @@ -1,265 +0,0 @@ -// Copyright (c) 2014-2017, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#pragma once - -#include "syncobj.h" -#include "cryptonote_basic.h" - -namespace cryptonote -{ - class BlockchainDB; - - class HardFork - { - public: - typedef enum { - LikelyForked, - UpdateNeeded, - Ready, - } State; - - static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT = 0; // <= actual height - 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 uint8_t DEFAULT_THRESHOLD_PERCENT = 80; - - /** - * @brief creates a new HardFork object - * - * @param original_version the block version for blocks 0 through to the first fork - * @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 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, 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_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time); - - /** - * @brief add a new hardfork height - * - * returns true if no error, false otherwise - * - * @param version the major block version for the fork - * @param voting_version the minor block version for the fork, used for voting - * @param height The height the hardfork takes effect - * @param time Approximate time of the hardfork (seconds since epoch) - */ - bool add_fork(uint8_t version, uint64_t height, time_t time); - - /** - * @brief initialize the object - * - * Must be done after adding all the required hardforks via add above - */ - void init(); - - /** - * @brief check whether a new block would be accepted - * - * returns true if the block is accepted, false otherwise - * - * @param block the new block - * - * This check is made by add. It is exposed publicly to allow - * the caller to inexpensively check whether a block would be - * accepted or rejected by its version number. Indeed, if this - * check could only be done as part of add, the caller would - * either have to add the block to the blockchain first, then - * call add, then have to pop the block from the blockchain if - * its version did not satisfy the hard fork requirements, or - * call add first, then, if the hard fork requirements are met, - * add the block to the blockchain, upon which a failure (the - * block being invalid, double spending, etc) would cause the - * hardfork object to reorganize. - */ - bool check(const cryptonote::block &block) const; - - /** - * @brief same as check, but for a particular height, rather than the top - * - * NOTE: this does not play well with voting, and relies on voting to be - * disabled (that is, forks happen on the scheduled date, whether or not - * enough blocks have voted for the fork). - * - * returns true if no error, false otherwise - * - * @param block the new block - * @param height which height to check for - */ - bool check_for_height(const cryptonote::block &block, uint64_t height) const; - - /** - * @brief add a new block - * - * returns true if no error, false otherwise - * - * @param block the new block - */ - bool add(const cryptonote::block &block, uint64_t height); - - /** - * @brief called when the blockchain is reorganized - * - * This will rescan the blockchain to determine which hard forks - * have been triggered - * - * returns true if no error, false otherwise - * - * @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); - - /** - * @brief returns current state at the given time - * - * Based on the approximate time 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(time_t t) const; - State get_state() const; - - /** - * @brief returns the hard fork version for the given block height - * - * @param height height of the block to check - */ - uint8_t get(uint64_t height) const; - - /** - * @brief returns the latest "ideal" version - * - * This is the latest version that's been scheduled - */ - 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 next version - * - * This is the version which will we fork to next - */ - uint8_t get_next_version() const; - - /** - * @brief returns the current version - * - * This is the latest version that's past its trigger date and had enough votes - * at one point in the past. - */ - uint8_t get_current_version() const; - - /** - * @brief returns the earliest block a given version may activate - */ - uint64_t get_earliest_ideal_height_for_version(uint8_t version) const; - - /** - * @brief returns information about current voting state - * - * returns true if the given version is enabled (ie, the current version - * is at least the passed version), false otherwise - * - * @param version the version to check voting for - * @param window the number of blocks considered in voting - * @param votes number of votes for next version - * @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; - - /** - * @brief returns the size of the voting window in blocks - */ - uint64_t get_window_size() const { return window_size; } - - 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_effective_version(uint8_t voting_version) const; - bool add(uint8_t block_version, uint8_t voting_version, uint64_t height); - - bool rescan_from_block_height(uint64_t height); - bool rescan_from_chain_height(uint64_t height); - - private: - - BlockchainDB &db; - - time_t forked_time; - time_t update_time; - uint64_t window_size; - 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, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {} - }; - std::vector<Params> heights; - - std::deque<uint8_t> 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; - - mutable epee::critical_section lock; - }; - -} // namespace cryptonote - |