mirror of
https://codeberg.org/anoncontributorxmr/monero.git
synced 2024-11-30 03:03:17 +00:00
New hardfork class
This keeps track of voting via block version, in order to decide when to enable a particular fork's code.
This commit is contained in:
parent
bed9a44e56
commit
62b1f74116
5 changed files with 871 additions and 3 deletions
|
@ -37,7 +37,8 @@ set(cryptonote_core_sources
|
||||||
cryptonote_format_utils.cpp
|
cryptonote_format_utils.cpp
|
||||||
difficulty.cpp
|
difficulty.cpp
|
||||||
miner.cpp
|
miner.cpp
|
||||||
tx_pool.cpp)
|
tx_pool.cpp
|
||||||
|
hardfork.cpp)
|
||||||
|
|
||||||
set(cryptonote_core_headers)
|
set(cryptonote_core_headers)
|
||||||
|
|
||||||
|
@ -60,7 +61,8 @@ set(cryptonote_core_private_headers
|
||||||
miner.h
|
miner.h
|
||||||
tx_extra.h
|
tx_extra.h
|
||||||
tx_pool.h
|
tx_pool.h
|
||||||
verification_context.h)
|
verification_context.h
|
||||||
|
hardfork.h)
|
||||||
|
|
||||||
if(PER_BLOCK_CHECKPOINT)
|
if(PER_BLOCK_CHECKPOINT)
|
||||||
set(Blocks "blocks")
|
set(Blocks "blocks")
|
||||||
|
|
266
src/cryptonote_core/hardfork.cpp
Normal file
266
src/cryptonote_core/hardfork.cpp
Normal file
|
@ -0,0 +1,266 @@
|
||||||
|
// Copyright (c) 2015, 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.
|
||||||
|
//
|
||||||
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "cryptonote_core/cryptonote_basic.h"
|
||||||
|
#include "blockchain_db/blockchain_db.h"
|
||||||
|
#include "hardfork.h"
|
||||||
|
|
||||||
|
using namespace cryptonote;
|
||||||
|
|
||||||
|
HardFork::HardFork(uint8_t original_version, time_t forked_time, time_t update_time, uint64_t max_history, int threshold_percent, uint64_t checkpoint_period):
|
||||||
|
original_version(original_version),
|
||||||
|
forked_time(forked_time),
|
||||||
|
update_time(update_time),
|
||||||
|
max_history(max_history),
|
||||||
|
threshold_percent(threshold_percent),
|
||||||
|
checkpoint_period(checkpoint_period)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::add(uint8_t version, uint64_t height, 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;
|
||||||
|
}
|
||||||
|
heights.push_back({version: version, height: height, time: time});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t HardFork::get_effective_version(const cryptonote::block &block) const
|
||||||
|
{
|
||||||
|
uint8_t version = block.major_version;
|
||||||
|
if (!heights.empty()) {
|
||||||
|
uint8_t max_version = heights.back().version;
|
||||||
|
if (version > max_version)
|
||||||
|
version = max_version;
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::do_check(const cryptonote::block &block) const
|
||||||
|
{
|
||||||
|
return block.major_version >= heights[current_fork_index].version;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::check(const cryptonote::block &block) const
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
return do_check(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::add(const cryptonote::block &block, uint64_t height)
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
|
||||||
|
if (!do_check(block))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const uint8_t version = get_effective_version(block);
|
||||||
|
|
||||||
|
while (versions.size() >= max_history) {
|
||||||
|
const uint8_t old_version = versions.front();
|
||||||
|
last_versions[old_version]--;
|
||||||
|
assert(last_versions[old_version] >= 0);
|
||||||
|
versions.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
last_versions[version]++;
|
||||||
|
versions.push_back(version);
|
||||||
|
|
||||||
|
uint8_t voted = get_voted_fork_index(height);
|
||||||
|
if (voted > current_fork_index) {
|
||||||
|
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
|
||||||
|
starting[v] = height;
|
||||||
|
}
|
||||||
|
current_fork_index = voted;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (height % checkpoint_period == 0)
|
||||||
|
checkpoints.push_back(std::make_pair(height, current_fork_index));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HardFork::init()
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
versions.clear();
|
||||||
|
for (size_t n = 0; n < 256; ++n)
|
||||||
|
last_versions[n] = 0;
|
||||||
|
for (size_t n = 0; n < 256; ++n)
|
||||||
|
starting[n] = std::numeric_limits<uint64_t>::max();
|
||||||
|
add(original_version, 0, 0);
|
||||||
|
for (size_t n = 0; n <= original_version; ++n)
|
||||||
|
starting[n] = 0;
|
||||||
|
checkpoints.clear();
|
||||||
|
current_fork_index = 0;
|
||||||
|
vote_threshold = (unsigned int)ceilf(max_history * threshold_percent / 100.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::reorganize_from_block_height(const cryptonote::BlockchainDB *db, uint64_t height)
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
if (!db || height >= db->height())
|
||||||
|
return false;
|
||||||
|
while (!checkpoints.empty() && checkpoints.back().first > height)
|
||||||
|
checkpoints.pop_back();
|
||||||
|
versions.clear();
|
||||||
|
|
||||||
|
int v;
|
||||||
|
for (v = 255; v >= 0; --v) {
|
||||||
|
if (starting[v] <= height)
|
||||||
|
break;
|
||||||
|
if (starting[v] != std::numeric_limits<uint64_t>::max()) {
|
||||||
|
starting[v] = std::numeric_limits<uint64_t>::max();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (current_fork_index = 0; current_fork_index < heights.size(); ++current_fork_index) {
|
||||||
|
if (heights[current_fork_index].version == v)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (size_t n = 0; n < 256; ++n)
|
||||||
|
last_versions[n] = 0;
|
||||||
|
const uint64_t rescan_height = height >= (max_history - 1) ? height - (max_history - 1) : 0;
|
||||||
|
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(b);
|
||||||
|
last_versions[v]++;
|
||||||
|
versions.push_back(v);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HardFork::reorganize_from_chain_height(const cryptonote::BlockchainDB *db, uint64_t height)
|
||||||
|
{
|
||||||
|
if (height == 0)
|
||||||
|
return false;
|
||||||
|
return reorganize_from_block_height(db, height - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int HardFork::get_voted_fork_index(uint64_t height) const
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
unsigned int 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];
|
||||||
|
if (height >= heights[n].height && accumulated_votes >= vote_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);
|
||||||
|
for (size_t n = 1; n < 256; ++n) {
|
||||||
|
if (starting[n] > height)
|
||||||
|
return n - 1;
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
return 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t HardFork::get_start_height(uint8_t version) const
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
return starting[version];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class archive_t>
|
||||||
|
void HardFork::serialize(archive_t & ar, const unsigned int version)
|
||||||
|
{
|
||||||
|
CRITICAL_REGION_LOCAL(lock);
|
||||||
|
ar & forked_time;
|
||||||
|
ar & update_time;
|
||||||
|
ar & max_history;
|
||||||
|
ar & threshold_percent;
|
||||||
|
ar & original_version;
|
||||||
|
ar & heights;
|
||||||
|
ar & last_versions;
|
||||||
|
ar & starting;
|
||||||
|
ar & current_fork_index;
|
||||||
|
ar & vote_threshold;
|
||||||
|
ar & checkpoint_period;
|
||||||
|
ar & checkpoints;
|
||||||
|
}
|
205
src/cryptonote_core/hardfork.h
Normal file
205
src/cryptonote_core/hardfork.h
Normal file
|
@ -0,0 +1,205 @@
|
||||||
|
// Copyright (c) 2015, 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.
|
||||||
|
//
|
||||||
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <boost/serialization/serialization.hpp>
|
||||||
|
#include <boost/serialization/version.hpp>
|
||||||
|
#include "syncobj.h"
|
||||||
|
#include "cryptonote_core/cryptonote_basic.h"
|
||||||
|
|
||||||
|
namespace cryptonote
|
||||||
|
{
|
||||||
|
class BlockchainDB;
|
||||||
|
|
||||||
|
class HardFork
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef enum {
|
||||||
|
LikelyForked,
|
||||||
|
UpdateNeeded,
|
||||||
|
Ready,
|
||||||
|
} State;
|
||||||
|
|
||||||
|
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_MAX_HISTORY = 50; // supermajority window check length
|
||||||
|
static const int DEFAULT_THRESHOLD_PERCENT = 80;
|
||||||
|
static const uint64_t DEFAULT_CHECKPOINT_PERIOD = 1024; // mark a checkpoint every that many blocks
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 max_history the size of the window in blocks to consider for version voting
|
||||||
|
* @param threshold_percent the size of the majority in percents
|
||||||
|
*/
|
||||||
|
HardFork(uint8_t original_version = 1, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t max_history = DEFAULT_MAX_HISTORY, int threshold_percent = DEFAULT_THRESHOLD_PERCENT, uint64_t checkpoint_period = DEFAULT_CHECKPOINT_PERIOD);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 time Approximate time of the hardfork (seconds since epoch)
|
||||||
|
*/
|
||||||
|
bool add(uint8_t version, uint64_t height, time_t time);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 rescan the blockchain versions past the
|
||||||
|
* last checkpoint, potentially causing a large number of DB
|
||||||
|
* operations.
|
||||||
|
*/
|
||||||
|
bool check(const cryptonote::block &block) 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(const cryptonote::BlockchainDB *db, uint64_t height);
|
||||||
|
bool reorganize_from_chain_height(const cryptonote::BlockchainDB *db, 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 height of the first block on the fork with th given version
|
||||||
|
*
|
||||||
|
* @param version version of the fork to query the starting block for
|
||||||
|
*/
|
||||||
|
uint64_t get_start_height(uint8_t version) 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 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;
|
||||||
|
|
||||||
|
template<class archive_t>
|
||||||
|
void serialize(archive_t & ar, const unsigned int version);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void init();
|
||||||
|
bool do_check(const cryptonote::block &block) const;
|
||||||
|
int get_voted_fork_index(uint64_t height) const;
|
||||||
|
uint8_t get_effective_version(const cryptonote::block &block) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
time_t forked_time;
|
||||||
|
time_t update_time;
|
||||||
|
uint64_t max_history;
|
||||||
|
int threshold_percent;
|
||||||
|
|
||||||
|
uint8_t original_version;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t version;
|
||||||
|
uint64_t height;
|
||||||
|
time_t time;
|
||||||
|
} Params;
|
||||||
|
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 lsat N blocks */
|
||||||
|
uint64_t starting[256]; /* block height at which each fork starts */
|
||||||
|
unsigned int current_fork_index;
|
||||||
|
unsigned int vote_threshold;
|
||||||
|
|
||||||
|
uint64_t checkpoint_period;
|
||||||
|
std::vector<std::pair<uint64_t, int>> checkpoints;
|
||||||
|
|
||||||
|
mutable epee::critical_section lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace cryptonote
|
||||||
|
|
||||||
|
BOOST_CLASS_VERSION(cryptonote::HardFork, 1)
|
|
@ -46,7 +46,8 @@ set(unit_tests_sources
|
||||||
slow_memmem.cpp
|
slow_memmem.cpp
|
||||||
test_format_utils.cpp
|
test_format_utils.cpp
|
||||||
test_peerlist.cpp
|
test_peerlist.cpp
|
||||||
test_protocol_pack.cpp)
|
test_protocol_pack.cpp
|
||||||
|
hardfork.cpp)
|
||||||
|
|
||||||
set(unit_tests_headers
|
set(unit_tests_headers
|
||||||
unit_tests_utils.h)
|
unit_tests_utils.h)
|
||||||
|
|
394
tests/unit_tests/hardfork.cpp
Normal file
394
tests/unit_tests/hardfork.cpp
Normal file
|
@ -0,0 +1,394 @@
|
||||||
|
// Copyright (c) 2014-2015, 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.
|
||||||
|
//
|
||||||
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "blockchain_db/lmdb/db_lmdb.h"
|
||||||
|
#include "cryptonote_core/hardfork.h"
|
||||||
|
|
||||||
|
using namespace cryptonote;
|
||||||
|
|
||||||
|
#define BLOCKS_PER_YEAR 525960
|
||||||
|
#define SECONDS_PER_YEAR 31557600
|
||||||
|
|
||||||
|
static cryptonote::block mkblock(uint8_t version)
|
||||||
|
{
|
||||||
|
cryptonote::block b;
|
||||||
|
b.major_version = version;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(empty_hardforks, Success)
|
||||||
|
{
|
||||||
|
HardFork hf;
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.get_state(time(NULL)) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(time(NULL) + 3600*24*400) == HardFork::Ready);
|
||||||
|
|
||||||
|
ASSERT_EQ(hf.get(0), 1);
|
||||||
|
ASSERT_EQ(hf.get(1), 1);
|
||||||
|
ASSERT_EQ(hf.get(100000000), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ordering, Success)
|
||||||
|
{
|
||||||
|
HardFork hf;
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.add(2, 2, 1));
|
||||||
|
ASSERT_FALSE(hf.add(3, 3, 1));
|
||||||
|
ASSERT_FALSE(hf.add(3, 2, 2));
|
||||||
|
ASSERT_FALSE(hf.add(2, 3, 2));
|
||||||
|
ASSERT_TRUE(hf.add(3, 10, 2));
|
||||||
|
ASSERT_TRUE(hf.add(4, 20, 3));
|
||||||
|
ASSERT_FALSE(hf.add(5, 5, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(states, Success)
|
||||||
|
{
|
||||||
|
HardFork hf;
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.add(2, BLOCKS_PER_YEAR, SECONDS_PER_YEAR));
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::UpdateNeeded);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::LikelyForked);
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.add(3, BLOCKS_PER_YEAR * 5, SECONDS_PER_YEAR * 5));
|
||||||
|
|
||||||
|
ASSERT_TRUE(hf.get_state(0) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR / 2) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_UPDATE_TIME / 2) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + (HardFork::DEFAULT_UPDATE_TIME + HardFork::DEFAULT_FORKED_TIME) / 2) == HardFork::Ready);
|
||||||
|
ASSERT_TRUE(hf.get_state(SECONDS_PER_YEAR + HardFork::DEFAULT_FORKED_TIME * 2) == HardFork::Ready);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(steps_asap, Success)
|
||||||
|
{
|
||||||
|
HardFork hf(1,1,1,1);
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(4, 2, 1));
|
||||||
|
ASSERT_TRUE(hf.add(7, 4, 2));
|
||||||
|
ASSERT_TRUE(hf.add(9, 6, 3));
|
||||||
|
|
||||||
|
for (uint64_t h = 0; h < 10; ++h)
|
||||||
|
hf.add(mkblock(10), h);
|
||||||
|
|
||||||
|
ASSERT_EQ(hf.get(0), 1);
|
||||||
|
ASSERT_EQ(hf.get(1), 1);
|
||||||
|
ASSERT_EQ(hf.get(2), 4);
|
||||||
|
ASSERT_EQ(hf.get(3), 4);
|
||||||
|
ASSERT_EQ(hf.get(4), 7);
|
||||||
|
ASSERT_EQ(hf.get(5), 7);
|
||||||
|
ASSERT_EQ(hf.get(6), 9);
|
||||||
|
ASSERT_EQ(hf.get(7), 9);
|
||||||
|
ASSERT_EQ(hf.get(8), 9);
|
||||||
|
ASSERT_EQ(hf.get(9), 9);
|
||||||
|
ASSERT_EQ(hf.get(100000), 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(steps_1, Success)
|
||||||
|
{
|
||||||
|
HardFork hf(1,1,1,1);
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
for (int n = 1 ; n < 10; ++n)
|
||||||
|
ASSERT_TRUE(hf.add(n+1, n, n));
|
||||||
|
|
||||||
|
for (uint64_t h = 0; h < 10; ++h) {
|
||||||
|
hf.add(mkblock(h+1), h);
|
||||||
|
ASSERT_EQ(hf.get(h), h+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestDB: public BlockchainDB {
|
||||||
|
public:
|
||||||
|
virtual void open(const std::string& filename, const int db_flags = 0) {}
|
||||||
|
virtual void close() {}
|
||||||
|
virtual void sync() {}
|
||||||
|
virtual void reset() {}
|
||||||
|
virtual std::vector<std::string> get_filenames() const { return std::vector<std::string>(); }
|
||||||
|
virtual std::string get_db_name() const { return std::string(); }
|
||||||
|
virtual bool lock() { return true; }
|
||||||
|
virtual void unlock() { }
|
||||||
|
virtual void batch_start(uint64_t batch_num_blocks=0) {}
|
||||||
|
virtual void batch_stop() {}
|
||||||
|
virtual void set_batch_transactions(bool) {}
|
||||||
|
virtual bool block_exists(const crypto::hash& h) const { return false; }
|
||||||
|
virtual block get_block(const crypto::hash& h) const { return block(); }
|
||||||
|
virtual uint64_t get_block_height(const crypto::hash& h) const { return 0; }
|
||||||
|
virtual block_header get_block_header(const crypto::hash& h) const { return block_header(); }
|
||||||
|
virtual uint64_t get_block_timestamp(const uint64_t& height) const { return 0; }
|
||||||
|
virtual uint64_t get_top_block_timestamp() const { return 0; }
|
||||||
|
virtual size_t get_block_size(const uint64_t& height) const { return 128; }
|
||||||
|
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const { return 10; }
|
||||||
|
virtual difficulty_type get_block_difficulty(const uint64_t& height) const { return 0; }
|
||||||
|
virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const { return 10000000000; }
|
||||||
|
virtual crypto::hash get_block_hash_from_height(const uint64_t& height) const { return crypto::hash(); }
|
||||||
|
virtual std::vector<block> get_blocks_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<block>(); }
|
||||||
|
virtual std::vector<crypto::hash> get_hashes_range(const uint64_t& h1, const uint64_t& h2) const { return std::vector<crypto::hash>(); }
|
||||||
|
virtual crypto::hash top_block_hash() const { return crypto::hash(); }
|
||||||
|
virtual block get_top_block() const { return block(); }
|
||||||
|
virtual uint64_t height() const { return blocks.size(); }
|
||||||
|
virtual bool tx_exists(const crypto::hash& h) const { return false; }
|
||||||
|
virtual uint64_t get_tx_unlock_time(const crypto::hash& h) const { return 0; }
|
||||||
|
virtual transaction get_tx(const crypto::hash& h) const { return transaction(); }
|
||||||
|
virtual uint64_t get_tx_count() const { return 0; }
|
||||||
|
virtual std::vector<transaction> get_tx_list(const std::vector<crypto::hash>& hlist) const { return std::vector<transaction>(); }
|
||||||
|
virtual uint64_t get_tx_block_height(const crypto::hash& h) const { return 0; }
|
||||||
|
virtual uint64_t get_num_outputs(const uint64_t& amount) const { return 1; }
|
||||||
|
virtual output_data_t get_output_key(const uint64_t& amount, const uint64_t& index) { return output_data_t(); }
|
||||||
|
virtual output_data_t get_output_key(const uint64_t& global_index) const { return output_data_t(); }
|
||||||
|
virtual tx_out get_output(const crypto::hash& h, const uint64_t& index) const { return tx_out(); }
|
||||||
|
virtual tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const { return tx_out_index(); }
|
||||||
|
virtual tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) { return tx_out_index(); }
|
||||||
|
virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<tx_out_index> &indices) {}
|
||||||
|
virtual void get_output_key(const uint64_t &amount, const std::vector<uint64_t> &offsets, std::vector<output_data_t> &outputs) {}
|
||||||
|
virtual bool can_thread_bulk_indices() const { return false; }
|
||||||
|
virtual std::vector<uint64_t> get_tx_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); }
|
||||||
|
virtual std::vector<uint64_t> get_tx_amount_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); }
|
||||||
|
virtual bool has_key_image(const crypto::key_image& img) const { return false; }
|
||||||
|
virtual void remove_block() { blocks.pop_back(); }
|
||||||
|
virtual void add_transaction_data(const crypto::hash& blk_hash, const transaction& tx, const crypto::hash& tx_hash) {}
|
||||||
|
virtual void remove_transaction_data(const crypto::hash& tx_hash, const transaction& tx) {}
|
||||||
|
virtual void add_output(const crypto::hash& tx_hash, const tx_out& tx_output, const uint64_t& local_index, const uint64_t unlock_time) {}
|
||||||
|
virtual void remove_output(const tx_out& tx_output) {}
|
||||||
|
virtual void add_spent_key(const crypto::key_image& k_image) {}
|
||||||
|
virtual void remove_spent_key(const crypto::key_image& k_image) {}
|
||||||
|
|
||||||
|
virtual void add_block( const block& blk
|
||||||
|
, const size_t& block_size
|
||||||
|
, const difficulty_type& cumulative_difficulty
|
||||||
|
, const uint64_t& coins_generated
|
||||||
|
, const crypto::hash& blk_hash
|
||||||
|
) {
|
||||||
|
blocks.push_back(blk);
|
||||||
|
}
|
||||||
|
virtual block get_block_from_height(const uint64_t& height) const {
|
||||||
|
return blocks[height];
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<block> blocks;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(reorganize, Same)
|
||||||
|
{
|
||||||
|
for (int history = 1; history <= 12; ++history) {
|
||||||
|
for (uint64_t checkpoint_period = 1; checkpoint_period <= 16; checkpoint_period++) {
|
||||||
|
HardFork hf(1, 1, 1, history, 100, checkpoint_period);
|
||||||
|
TestDB db;
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(4, 2, 1));
|
||||||
|
ASSERT_TRUE(hf.add(7, 4, 2));
|
||||||
|
ASSERT_TRUE(hf.add(9, 6, 3));
|
||||||
|
|
||||||
|
// index 0 1 2 3 4 5 6 7 8 9
|
||||||
|
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 };
|
||||||
|
for (uint64_t h = 0; h < 20; ++h) {
|
||||||
|
db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash());
|
||||||
|
hf.add(db.get_block_from_height(h), h);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint64_t rh = 0; rh < 20; ++rh) {
|
||||||
|
hf.reorganize_from_block_height(&db, rh);
|
||||||
|
for (int hh = 0; hh < 20; ++hh) {
|
||||||
|
uint8_t version = hh >= (history-1) ? block_versions[hh - (history-1)] : 1;
|
||||||
|
ASSERT_EQ(hf.get(hh), version);
|
||||||
|
}
|
||||||
|
ASSERT_EQ(hf.get(100000), 9);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(reorganize, Changed)
|
||||||
|
{
|
||||||
|
int history = 4;
|
||||||
|
for (uint64_t checkpoint_period = 1; checkpoint_period <= 16; checkpoint_period++) {
|
||||||
|
HardFork hf(1, 1, 1, 4, 100, checkpoint_period);
|
||||||
|
TestDB db;
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(4, 2, 1));
|
||||||
|
ASSERT_TRUE(hf.add(7, 4, 2));
|
||||||
|
ASSERT_TRUE(hf.add(9, 6, 3));
|
||||||
|
|
||||||
|
// index 0 1 2 3 4 5 6 7 8 9
|
||||||
|
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 };
|
||||||
|
for (uint64_t h = 0; h < 20; ++h) {
|
||||||
|
db.add_block(mkblock(block_versions[h]), 0, 0, 0, crypto::hash());
|
||||||
|
hf.add(db.get_block_from_height(h), h);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint64_t rh = 0; rh < 20; ++rh) {
|
||||||
|
hf.reorganize_from_block_height(&db, rh);
|
||||||
|
for (int hh = 0; hh < 20; ++hh) {
|
||||||
|
uint8_t version = hh >= (history-1) ? block_versions[hh - (history-1)] : 1;
|
||||||
|
ASSERT_EQ(hf.get(hh), version);
|
||||||
|
}
|
||||||
|
ASSERT_EQ(hf.get(100000), 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delay a bit for 9, and go back to 1 to check it stays at 9
|
||||||
|
static const uint8_t block_versions_new[] = { 1, 1, 4, 4, 7, 7, 4, 7, 7, 7, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1 };
|
||||||
|
static const uint8_t expected_versions_new[] = { 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9 };
|
||||||
|
for (uint64_t h = 3; h < 20; ++h) {
|
||||||
|
db.remove_block();
|
||||||
|
}
|
||||||
|
ASSERT_EQ(db.height(), 3);
|
||||||
|
hf.reorganize_from_block_height(&db, 2);
|
||||||
|
for (uint64_t h = 3; h < 20; ++h) {
|
||||||
|
db.add_block(mkblock(block_versions_new[h]), 0, 0, 0, crypto::hash());
|
||||||
|
hf.add(db.get_block_from_height(h), h);
|
||||||
|
}
|
||||||
|
for (int hh = 0; hh < 20; ++hh) {
|
||||||
|
ASSERT_EQ(hf.get(hh), expected_versions_new[hh]);
|
||||||
|
}
|
||||||
|
ASSERT_EQ(hf.get(100000), 9);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(voting, threshold)
|
||||||
|
{
|
||||||
|
for (int threshold = 87; threshold <= 88; ++threshold) {
|
||||||
|
HardFork hf(1, 1, 1, 8, threshold, 10);
|
||||||
|
TestDB db;
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(2, 2, 1));
|
||||||
|
|
||||||
|
for (uint64_t h = 0; h < 10; ++h) {
|
||||||
|
uint8_t v = 1 + !!(h % 8);
|
||||||
|
hf.add(mkblock(v), h);
|
||||||
|
uint8_t expected = threshold == 88 ? 1 : h < 7 ? 1 : 2;
|
||||||
|
ASSERT_EQ(hf.get(h), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(new_blocks, denied)
|
||||||
|
{
|
||||||
|
HardFork hf(1, 1, 1, 4, 50, 10);
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(2, 2, 1));
|
||||||
|
|
||||||
|
ASSERT_FALSE(hf.add(mkblock(0), 0));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 0));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 1));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 2));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 3));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 4));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 5));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 6));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 7));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 8)); // we reach 50% of the last 4
|
||||||
|
ASSERT_FALSE(hf.add(mkblock(1), 9)); // so this one can't get added
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 10));
|
||||||
|
|
||||||
|
ASSERT_EQ(hf.get_start_height(2), 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(new_version, early)
|
||||||
|
{
|
||||||
|
HardFork hf(1, 1, 1, 4, 50, 10);
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(2, 4, 1));
|
||||||
|
|
||||||
|
ASSERT_FALSE(hf.add(mkblock(0), 0));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 0));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 1)); // we have enough votes already
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 2));
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(1), 3)); // we accept a previous version because we did not switch, even with all the votes
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 4)); // but have to wait for the declared height anyway
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 5));
|
||||||
|
ASSERT_FALSE(hf.add(mkblock(1), 6)); // we don't accept 1 anymore
|
||||||
|
ASSERT_TRUE(hf.add(mkblock(2), 7)); // but we do accept 2
|
||||||
|
|
||||||
|
ASSERT_EQ(hf.get_start_height(2), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(reorganize, changed)
|
||||||
|
{
|
||||||
|
HardFork hf(1, 1, 1, 4, 50, 10);
|
||||||
|
TestDB db;
|
||||||
|
|
||||||
|
// v h t
|
||||||
|
ASSERT_TRUE(hf.add(2, 2, 1));
|
||||||
|
ASSERT_TRUE(hf.add(3, 5, 2));
|
||||||
|
|
||||||
|
#define ADD(v, h, a) \
|
||||||
|
do { \
|
||||||
|
cryptonote::block b = mkblock(v); \
|
||||||
|
db.add_block(b, 0, 0, 0, crypto::hash()); \
|
||||||
|
ASSERT_##a(hf.add(b, h)); \
|
||||||
|
} while(0)
|
||||||
|
#define ADD_TRUE(v, h) ADD(v, h, TRUE)
|
||||||
|
#define ADD_FALSE(v, h) ADD(v, h, FALSE)
|
||||||
|
|
||||||
|
ADD_FALSE(0, 0);
|
||||||
|
ADD_TRUE(1, 0);
|
||||||
|
ADD_TRUE(1, 1);
|
||||||
|
ADD_TRUE(2, 2);
|
||||||
|
ADD_TRUE(2, 3); // switch to 2 here
|
||||||
|
ADD_TRUE(2, 4);
|
||||||
|
ADD_TRUE(2, 5);
|
||||||
|
ADD_TRUE(2, 6);
|
||||||
|
ASSERT_EQ(hf.get_current_version(), 2);
|
||||||
|
ADD_TRUE(3, 7);
|
||||||
|
ADD_TRUE(4, 8);
|
||||||
|
ADD_TRUE(4, 9);
|
||||||
|
ASSERT_EQ(hf.get_start_height(2), 3);
|
||||||
|
ASSERT_EQ(hf.get_start_height(3), 8);
|
||||||
|
ASSERT_EQ(hf.get_current_version(), 3);
|
||||||
|
|
||||||
|
// pop a few blocks and check current version goes back down
|
||||||
|
db.remove_block();
|
||||||
|
hf.reorganize_from_block_height(&db, 8);
|
||||||
|
ASSERT_EQ(hf.get_current_version(), 3);
|
||||||
|
db.remove_block();
|
||||||
|
hf.reorganize_from_block_height(&db, 7);
|
||||||
|
ASSERT_EQ(hf.get_current_version(), 2);
|
||||||
|
db.remove_block();
|
||||||
|
ASSERT_EQ(hf.get_current_version(), 2);
|
||||||
|
|
||||||
|
// add blocks again, but remaining at 2
|
||||||
|
ADD_TRUE(2, 7);
|
||||||
|
ADD_TRUE(2, 8);
|
||||||
|
ADD_TRUE(2, 9);
|
||||||
|
ASSERT_EQ(hf.get_start_height(2), 3); // unchanged
|
||||||
|
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<uint64_t>::max()); // not yet
|
||||||
|
}
|
Loading…
Reference in a new issue