2015-09-01 08:40:33 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include "cryptonote_core/cryptonote_basic.h"
|
|
|
|
#include "blockchain_db/blockchain_db.h"
|
|
|
|
#include "hardfork.h"
|
|
|
|
|
|
|
|
using namespace cryptonote;
|
|
|
|
|
2015-10-21 18:18:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:04:41 +00:00
|
|
|
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):
|
2015-09-20 17:41:38 +00:00
|
|
|
db(db),
|
2015-09-01 08:40:33 +00:00
|
|
|
original_version(original_version),
|
2015-09-26 15:22:57 +00:00
|
|
|
original_version_till_height(original_version_till_height),
|
2015-09-01 08:40:33 +00:00
|
|
|
forked_time(forked_time),
|
|
|
|
update_time(update_time),
|
2015-09-20 17:41:38 +00:00
|
|
|
window_size(window_size),
|
2015-11-08 13:04:41 +00:00
|
|
|
default_threshold_percent(default_threshold_percent)
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
2015-09-26 12:25:22 +00:00
|
|
|
if (window_size == 0)
|
|
|
|
throw "window_size needs to be strictly positive";
|
2015-11-08 13:04:41 +00:00
|
|
|
if (default_threshold_percent > 100)
|
|
|
|
throw "default_threshold_percent needs to be between 0 and 100";
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 13:04:41 +00:00
|
|
|
bool HardFork::add(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2015-11-08 13:04:41 +00:00
|
|
|
if (threshold > 100)
|
|
|
|
return false;
|
|
|
|
heights.push_back(Params(version, height, threshold, time));
|
2015-09-01 08:40:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:04:41 +00:00
|
|
|
bool HardFork::add(uint8_t version, uint64_t height, time_t time)
|
|
|
|
{
|
|
|
|
return add(version, height, default_threshold_percent, time);
|
|
|
|
}
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
uint8_t HardFork::get_effective_version(uint8_t version) const
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
|
|
|
if (!heights.empty()) {
|
|
|
|
uint8_t max_version = heights.back().version;
|
|
|
|
if (version > max_version)
|
|
|
|
version = max_version;
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
bool HardFork::do_check(uint8_t version) const
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
2015-09-26 15:22:57 +00:00
|
|
|
return version >= heights[current_fork_index].version;
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HardFork::check(const cryptonote::block &block) const
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
2015-10-21 18:18:00 +00:00
|
|
|
return do_check(get_block_vote(block));
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
bool HardFork::add(uint8_t block_version, uint64_t height)
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
if (!do_check(block_version))
|
2015-09-01 08:40:33 +00:00
|
|
|
return false;
|
|
|
|
|
2015-09-26 12:25:22 +00:00
|
|
|
db.set_hard_fork_version(height, heights[current_fork_index].version);
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
const uint8_t version = get_effective_version(block_version);
|
2015-09-01 08:40:33 +00:00
|
|
|
|
2015-09-20 17:41:38 +00:00
|
|
|
while (versions.size() >= window_size) {
|
2015-09-01 08:40:33 +00:00
|
|
|
const uint8_t old_version = versions.front();
|
2015-10-04 11:37:59 +00:00
|
|
|
assert(last_versions[old_version] >= 1);
|
2015-09-01 08:40:33 +00:00
|
|
|
last_versions[old_version]--;
|
|
|
|
versions.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
last_versions[version]++;
|
|
|
|
versions.push_back(version);
|
|
|
|
|
2015-09-26 12:25:22 +00:00
|
|
|
uint8_t voted = get_voted_fork_index(height + 1);
|
2015-09-01 08:40:33 +00:00
|
|
|
if (voted > current_fork_index) {
|
|
|
|
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
|
2015-09-26 12:25:22 +00:00
|
|
|
// we reached the vote threshold with this block, next one will be forked
|
|
|
|
db.set_hard_fork_starting_height(v, height + 1);
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
current_fork_index = voted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
bool HardFork::add(const cryptonote::block &block, uint64_t height)
|
|
|
|
{
|
2015-10-21 18:18:00 +00:00
|
|
|
return add(get_block_vote(block), height);
|
2015-09-26 15:22:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 08:40:33 +00:00
|
|
|
void HardFork::init()
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
|
|
|
versions.clear();
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
last_versions[n] = 0;
|
|
|
|
current_fork_index = 0;
|
2015-09-20 17:41:38 +00:00
|
|
|
|
|
|
|
// restore state from DB
|
|
|
|
uint64_t height = db.height();
|
|
|
|
if (height > window_size)
|
|
|
|
height -= window_size;
|
|
|
|
else
|
|
|
|
height = 1;
|
|
|
|
|
|
|
|
bool populate = db.get_hard_fork_starting_height(original_version) == std::numeric_limits<uint64_t>::max();
|
|
|
|
if (populate) {
|
|
|
|
LOG_PRINT_L0("The DB has no hard fork info, reparsing from start");
|
|
|
|
height = 1;
|
|
|
|
}
|
|
|
|
LOG_PRINT_L1("reorganizing from " << height);
|
|
|
|
if (populate) {
|
2015-10-16 18:38:44 +00:00
|
|
|
reorganize_from_chain_height(height);
|
2015-09-20 17:41:38 +00:00
|
|
|
// reorg will not touch the genesis block, use this as a flag for populating done
|
|
|
|
db.set_hard_fork_version(0, original_version);
|
|
|
|
db.set_hard_fork_starting_height(original_version, 0);
|
|
|
|
}
|
2015-10-16 18:38:44 +00:00
|
|
|
else {
|
|
|
|
rescan_from_chain_height(height);
|
|
|
|
}
|
2015-09-20 17:41:38 +00:00
|
|
|
LOG_PRINT_L1("reorganization done");
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 15:22:57 +00:00
|
|
|
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);
|
2015-10-21 18:18:00 +00:00
|
|
|
return get_block_vote(block);
|
2015-09-26 15:22:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 17:41:38 +00:00
|
|
|
bool HardFork::reorganize_from_block_height(uint64_t height)
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
2015-09-20 17:41:38 +00:00
|
|
|
if (height >= db.height())
|
2015-09-01 08:40:33 +00:00
|
|
|
return false;
|
2015-09-20 17:41:38 +00:00
|
|
|
|
2015-10-16 18:39:59 +00:00
|
|
|
db.set_batch_transactions(true);
|
|
|
|
db.batch_start();
|
2015-09-26 15:22:57 +00:00
|
|
|
|
2015-09-01 08:40:33 +00:00
|
|
|
versions.clear();
|
|
|
|
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
last_versions[n] = 0;
|
2015-09-26 12:25:22 +00:00
|
|
|
const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
|
2015-09-20 17:41:38 +00:00
|
|
|
const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height);
|
|
|
|
while (heights[current_fork_index].version > start_version) {
|
|
|
|
db.set_hard_fork_starting_height(heights[current_fork_index].version, std::numeric_limits<uint64_t>::max());
|
|
|
|
--current_fork_index;
|
|
|
|
}
|
2015-09-01 08:40:33 +00:00
|
|
|
for (uint64_t h = rescan_height; h <= height; ++h) {
|
2015-09-20 17:41:38 +00:00
|
|
|
cryptonote::block b = db.get_block_from_height(h);
|
2015-10-21 18:18:00 +00:00
|
|
|
const uint8_t v = get_effective_version(get_block_vote(b));
|
2015-09-01 08:40:33 +00:00
|
|
|
last_versions[v]++;
|
|
|
|
versions.push_back(v);
|
|
|
|
}
|
2015-09-26 12:25:22 +00:00
|
|
|
|
|
|
|
uint8_t voted = get_voted_fork_index(height + 1);
|
|
|
|
if (voted > current_fork_index) {
|
|
|
|
for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) {
|
|
|
|
// we reached the vote threshold with this block, next one will be forked
|
|
|
|
db.set_hard_fork_starting_height(v, height + 1);
|
|
|
|
}
|
|
|
|
current_fork_index = voted;
|
|
|
|
}
|
|
|
|
|
2015-09-20 17:41:38 +00:00
|
|
|
const uint64_t bc_height = db.height();
|
2015-09-01 08:40:33 +00:00
|
|
|
for (uint64_t h = height + 1; h < bc_height; ++h) {
|
2015-09-26 15:22:57 +00:00
|
|
|
add(get_block_version(h), h);
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 18:39:59 +00:00
|
|
|
db.batch_stop();
|
2015-09-26 15:22:57 +00:00
|
|
|
|
2015-09-01 08:40:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-20 17:41:38 +00:00
|
|
|
bool HardFork::reorganize_from_chain_height(uint64_t height)
|
2015-09-01 08:40:33 +00:00
|
|
|
{
|
|
|
|
if (height == 0)
|
|
|
|
return false;
|
2015-09-20 17:41:38 +00:00
|
|
|
return reorganize_from_block_height(height - 1);
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 18:38:44 +00:00
|
|
|
bool HardFork::rescan_from_block_height(uint64_t height)
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
|
|
|
if (height >= db.height())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (uint64_t h = rescan_height; h <= height; ++h) {
|
|
|
|
cryptonote::block b = db.get_block_from_height(h);
|
2015-10-21 18:18:00 +00:00
|
|
|
const uint8_t v = get_effective_version(get_block_vote(b));
|
2015-10-16 18:38:44 +00:00
|
|
|
last_versions[v]++;
|
|
|
|
versions.push_back(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t lastv = db.get_hard_fork_version(height);
|
|
|
|
current_fork_index = 0;
|
|
|
|
while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
|
|
|
|
++current_fork_index;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HardFork::rescan_from_chain_height(uint64_t height)
|
|
|
|
{
|
|
|
|
if (height == 0)
|
|
|
|
return false;
|
|
|
|
return rescan_from_block_height(height - 1);
|
|
|
|
}
|
|
|
|
|
2015-09-01 08:40:33 +00:00
|
|
|
int HardFork::get_voted_fork_index(uint64_t height) const
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
2015-09-19 15:34:29 +00:00
|
|
|
uint32_t accumulated_votes = 0;
|
2015-09-01 08:40:33 +00:00
|
|
|
for (unsigned int n = heights.size() - 1; n > current_fork_index; --n) {
|
|
|
|
uint8_t v = heights[n].version;
|
|
|
|
accumulated_votes += last_versions[v];
|
2015-11-08 13:04:41 +00:00
|
|
|
uint32_t threshold = (window_size * heights[n].threshold + 99) / 100;
|
|
|
|
if (height >= heights[n].height && accumulated_votes >= threshold) {
|
2015-09-01 08:40:33 +00:00
|
|
|
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);
|
2015-09-26 13:06:00 +00:00
|
|
|
if (height >= db.height()) {
|
2015-09-20 17:41:38 +00:00
|
|
|
assert(false);
|
|
|
|
return 255;
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
2015-09-20 17:41:38 +00:00
|
|
|
return db.get_hard_fork_version(height);
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t HardFork::get_start_height(uint8_t version) const
|
|
|
|
{
|
|
|
|
CRITICAL_REGION_LOCAL(lock);
|
2015-09-20 17:41:38 +00:00
|
|
|
return db.get_hard_fork_starting_height(version);
|
2015-09-01 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:22:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-19 15:34:29 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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];
|
2015-11-08 13:04:41 +00:00
|
|
|
threshold = (window * heights[current_version].threshold + 99) / 100;
|
2015-09-19 15:34:29 +00:00
|
|
|
assert((votes >= threshold) == enabled);
|
|
|
|
voting = heights.back().version;
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|