2019-04-12 06:36:43 +02:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2018-04-10 06:49:20 +02:00
|
|
|
// Copyright (c) 2018, The Loki Project
|
2015-09-01 10:40:33 +02:00
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
|
2019-09-13 07:46:15 +02:00
|
|
|
#include "common/loki.h"
|
2017-02-25 17:41:35 +01:00
|
|
|
#include "cryptonote_basic/cryptonote_basic.h"
|
2015-09-01 10:40:33 +02:00
|
|
|
#include "blockchain_db/blockchain_db.h"
|
|
|
|
#include "hardfork.h"
|
|
|
|
|
2018-04-10 06:49:20 +02:00
|
|
|
#undef LOKI_DEFAULT_LOG_CATEGORY
|
|
|
|
#define LOKI_DEFAULT_LOG_CATEGORY "hardfork"
|
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 17:34:23 +01:00
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
using namespace cryptonote;
|
|
|
|
|
2015-10-21 20:18:00 +02: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-23 15:04:33 +01:00
|
|
|
static uint8_t get_block_version(const cryptonote::block &b)
|
|
|
|
{
|
|
|
|
return b.major_version;
|
|
|
|
}
|
|
|
|
|
2019-09-13 07:46:15 +02:00
|
|
|
// TODO(loki): Re-evaluate Hardfork as a class. Originally designed to
|
|
|
|
// handle voting, hardforks are now locked in, maybe we just need helper
|
|
|
|
// functions on the hardcoded table instead of hiding everything behind
|
|
|
|
// a class.
|
|
|
|
|
|
|
|
// version 7 from the start of the blockchain, inhereted from Monero mainnet
|
|
|
|
static constexpr HardFork::Params mainnet_hard_forks[] =
|
|
|
|
{
|
2019-10-09 03:54:16 +02:00
|
|
|
{ network_version_7, 1, 0, 1503046577 },
|
|
|
|
{ network_version_8, 64324, 0, 1533006000 },
|
|
|
|
{ network_version_9_service_nodes, 101250, 0, 1537444800 },
|
|
|
|
{ network_version_10_bulletproofs, 161849, 0, 1544743800 }, // 2018-12-13 23:30UTC
|
|
|
|
{ network_version_11_infinite_staking, 234767, 0, 1554170400 }, // 2019-03-26 13:00AEDT
|
|
|
|
{ network_version_12_checkpointing, 321467, 0, 1563940800 }, // 2019-07-24 14:00AEDT
|
|
|
|
{ network_version_13_enforce_checkpoints, 385824, 0, 1571850000 }, // 2019-10-23 19:00AEDT
|
2020-01-21 07:16:08 +01:00
|
|
|
{ network_version_14_blink, 442333, 0, 1578528000 }, // 2020-01-09 00:00UTC
|
2020-03-17 03:01:03 +01:00
|
|
|
{ network_version_15_lns, 496969, 0, 1585105200 }, // 2020-03-25 14:00AEDT (03:00UTC)
|
2020-10-01 03:36:25 +02:00
|
|
|
{ network_version_16_pulse, 638900, 0, 1602199800 }, // 2020-10-09 10:30AEDT (23:30UTC, 10-08)
|
|
|
|
{ network_version_17, 638900 + BLOCKS_EXPECTED_IN_DAYS(180), 0, 1602199800 + 180*24*60*60 }, // HF16 + 180 days
|
2019-09-13 07:46:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr HardFork::Params testnet_hard_forks[] =
|
|
|
|
{
|
2019-09-13 08:31:58 +02:00
|
|
|
{ network_version_7, 1, 0, 1533631121 },
|
|
|
|
{ network_version_8, 2, 0, 1533631122 },
|
|
|
|
{ network_version_9_service_nodes, 3, 0, 1533631123 },
|
|
|
|
{ network_version_10_bulletproofs, 4, 0, 1542681077 },
|
|
|
|
{ network_version_11_infinite_staking, 5, 0, 1551223964 },
|
2020-03-04 00:22:20 +01:00
|
|
|
{ network_version_12_checkpointing, 75471, 0, 1561608000 }, // 2019-06-28 14:00AEDT
|
|
|
|
{ network_version_13_enforce_checkpoints, 127028, 0, 1568440800 }, // 2019-09-13 16:00AEDT
|
|
|
|
{ network_version_14_blink, 174630, 0, 1575075600 }, // 2019-11-30 07:00UTC
|
2020-03-11 07:27:35 +01:00
|
|
|
{ network_version_15_lns, 244777, 0, 1583940000 }, // 2020-03-11 15:20UTC
|
2020-09-19 00:11:26 +02:00
|
|
|
{ network_version_16_pulse, 382222, 0, 1600468200 }, // 2020-09-18 22:30UTC
|
|
|
|
{ network_version_17, 382222 + BLOCKS_EXPECTED_IN_DAYS(180), 0, 1600468200 + 180*24*60*60 }, // HF16 + 180 days
|
2019-09-13 07:46:15 +02:00
|
|
|
};
|
|
|
|
|
2020-08-11 23:41:54 +02:00
|
|
|
static constexpr HardFork::Params devnet_hard_forks[] =
|
2019-09-13 07:46:15 +02:00
|
|
|
{
|
2020-09-11 20:19:15 +02:00
|
|
|
{ network_version_7, 1, 0, 1599848400 },
|
2020-09-18 21:39:50 +02:00
|
|
|
{ network_version_16_pulse, 2, 0, 1599848400 }, // 2020-09-11 18:20 UTC
|
2019-09-13 07:46:15 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 08:08:37 +02:00
|
|
|
uint64_t HardFork::get_hardcoded_hard_fork_height(network_type nettype, cryptonote::network_version version)
|
|
|
|
{
|
|
|
|
uint64_t result = INVALID_HF_VERSION_HEIGHT;
|
|
|
|
for (const auto &record : cryptonote::HardFork::get_hardcoded_hard_forks(nettype))
|
|
|
|
{
|
|
|
|
if (record.version >= version)
|
|
|
|
{
|
|
|
|
result = record.height;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-13 07:46:15 +02:00
|
|
|
HardFork::ParamsIterator HardFork::get_hardcoded_hard_forks(network_type nettype)
|
|
|
|
{
|
|
|
|
if (nettype == MAINNET) return {mainnet_hard_forks, std::end(mainnet_hard_forks)};
|
|
|
|
else if (nettype == TESTNET) return {testnet_hard_forks, std::end(testnet_hard_forks)};
|
2020-08-11 23:41:54 +02:00
|
|
|
else if (nettype == DEVNET) return {devnet_hard_forks, std::end(devnet_hard_forks)};
|
2019-09-13 07:46:15 +02:00
|
|
|
return {nullptr, nullptr};
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:32:26 +02:00
|
|
|
HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, time_t forked_time, time_t update_time, uint64_t window_size, uint8_t default_threshold_percent):
|
2015-09-20 19:41:38 +02:00
|
|
|
db(db),
|
2015-09-01 10:40:33 +02:00
|
|
|
original_version(original_version),
|
|
|
|
forked_time(forked_time),
|
|
|
|
update_time(update_time),
|
2015-09-20 19:41:38 +02:00
|
|
|
window_size(window_size),
|
2018-10-29 17:15:22 +01:00
|
|
|
default_threshold_percent(default_threshold_percent),
|
|
|
|
current_fork_index(0)
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
2015-09-26 14:25:22 +02:00
|
|
|
if (window_size == 0)
|
2020-08-15 16:26:03 +02:00
|
|
|
throw std::logic_error{"window_size needs to be strictly positive"};
|
2015-11-08 14:04:41 +01:00
|
|
|
if (default_threshold_percent > 100)
|
2020-08-15 16:26:03 +02:00
|
|
|
throw std::logic_error{"default_threshold_percent needs to be between 0 and 100"};
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 02:48:47 +02:00
|
|
|
void HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-01 10:40:33 +02:00
|
|
|
|
|
|
|
// add in order
|
|
|
|
if (version == 0)
|
2020-08-12 02:48:47 +02:00
|
|
|
throw std::runtime_error{"Cannot add a hard fork with HF version 0"};
|
2015-09-01 10:40:33 +02:00
|
|
|
if (!heights.empty()) {
|
2020-08-12 02:48:47 +02:00
|
|
|
const auto& [v, h, _thresh, t] = heights.back();
|
|
|
|
if (version <= v)
|
|
|
|
throw std::runtime_error{"Cannot add hard fork: version(" + std::to_string(version) + ") must be > previous HF version(" + std::to_string(v) + ")"};
|
|
|
|
if (height <= h)
|
|
|
|
throw std::runtime_error{"Cannot add hard fork: height(" + std::to_string(height) + ") must be > previous HF height(" + std::to_string(h) + ")"};
|
|
|
|
if (time < t)
|
|
|
|
throw std::runtime_error{"Cannot add hard fork: timestamp(" + std::to_string(time) + ") must be >= previous HF timestamp(" + std::to_string(t) + ")"};
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
2015-11-08 14:04:41 +01:00
|
|
|
if (threshold > 100)
|
2020-08-12 02:48:47 +02:00
|
|
|
throw std::runtime_error{"Cannot add hard fork: invalid threshold (" + std::to_string(threshold) + ")"};
|
|
|
|
|
2019-09-13 07:46:15 +02:00
|
|
|
heights.push_back({version, height, threshold, time});
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 02:48:47 +02:00
|
|
|
void HardFork::add_fork(uint8_t version, uint64_t height, time_t time)
|
2015-11-08 14:04:41 +01:00
|
|
|
{
|
2020-08-12 02:48:47 +02:00
|
|
|
add_fork(version, height, default_threshold_percent, time);
|
2015-11-08 14:04:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
uint8_t HardFork::get_effective_version(uint8_t voting_version) const
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
|
|
|
if (!heights.empty()) {
|
|
|
|
uint8_t max_version = heights.back().version;
|
2015-11-24 21:46:10 +01:00
|
|
|
if (voting_version > max_version)
|
|
|
|
voting_version = max_version;
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
2015-11-24 21:46:10 +01:00
|
|
|
return voting_version;
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
bool HardFork::do_check(uint8_t block_version, uint8_t voting_version) const
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
2016-01-15 12:34:22 +01:00
|
|
|
return block_version == heights[current_fork_index].version
|
2015-11-24 21:46:10 +01:00
|
|
|
&& voting_version >= heights[current_fork_index].version;
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HardFork::check(const cryptonote::block &block) const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-11-24 21:46:10 +01:00
|
|
|
return do_check(::get_block_version(block), ::get_block_vote(block));
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:41:14 +01:00
|
|
|
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
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2017-01-14 23:41:14 +01:00
|
|
|
return do_check_for_height(::get_block_version(block), ::get_block_vote(block), height);
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
bool HardFork::add(uint8_t block_version, uint8_t voting_version, uint64_t height)
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-01 10:40:33 +02:00
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
if (!do_check(block_version, voting_version))
|
2015-09-01 10:40:33 +02:00
|
|
|
return false;
|
|
|
|
|
2015-09-26 14:25:22 +02:00
|
|
|
db.set_hard_fork_version(height, heights[current_fork_index].version);
|
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
voting_version = get_effective_version(voting_version);
|
2015-09-01 10:40:33 +02:00
|
|
|
|
2015-09-20 19:41:38 +02:00
|
|
|
while (versions.size() >= window_size) {
|
2015-09-01 10:40:33 +02:00
|
|
|
const uint8_t old_version = versions.front();
|
2015-10-04 13:37:59 +02:00
|
|
|
assert(last_versions[old_version] >= 1);
|
2015-09-01 10:40:33 +02:00
|
|
|
last_versions[old_version]--;
|
|
|
|
versions.pop_front();
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:46:10 +01:00
|
|
|
last_versions[voting_version]++;
|
|
|
|
versions.push_back(voting_version);
|
2015-09-01 10:40:33 +02:00
|
|
|
|
2015-09-26 14:25:22 +02:00
|
|
|
uint8_t voted = get_voted_fork_index(height + 1);
|
2015-09-01 10:40:33 +02:00
|
|
|
if (voted > current_fork_index) {
|
|
|
|
current_fork_index = voted;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:22:57 +02:00
|
|
|
bool HardFork::add(const cryptonote::block &block, uint64_t height)
|
|
|
|
{
|
2015-11-24 21:46:10 +01:00
|
|
|
return add(::get_block_version(block), ::get_block_vote(block), height);
|
2015-09-26 17:22:57 +02:00
|
|
|
}
|
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
void HardFork::init()
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2016-02-08 21:58:06 +01:00
|
|
|
|
|
|
|
// add a placeholder for the default version, to avoid special cases
|
|
|
|
if (heights.empty())
|
2019-09-13 07:46:15 +02:00
|
|
|
heights.push_back({original_version, 0, 0, 0});
|
2016-02-08 21:58:06 +01:00
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
versions.clear();
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
last_versions[n] = 0;
|
|
|
|
current_fork_index = 0;
|
2015-09-20 19:41:38 +02:00
|
|
|
|
|
|
|
// restore state from DB
|
|
|
|
uint64_t height = db.height();
|
|
|
|
if (height > window_size)
|
2016-07-25 23:41:49 +02:00
|
|
|
height -= window_size - 1;
|
2015-09-20 19:41:38 +02:00
|
|
|
else
|
|
|
|
height = 1;
|
|
|
|
|
2019-04-14 12:33:22 +02:00
|
|
|
rescan_from_chain_height(height);
|
|
|
|
MDEBUG("init done");
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 17:22:57 +02:00
|
|
|
uint8_t HardFork::get_block_version(uint64_t height) const
|
|
|
|
{
|
|
|
|
const cryptonote::block &block = db.get_block_from_height(height);
|
2015-11-23 15:04:33 +01:00
|
|
|
return ::get_block_version(block);
|
2015-09-26 17:22:57 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 19:41:38 +02:00
|
|
|
bool HardFork::reorganize_from_block_height(uint64_t height)
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-20 19:41:38 +02:00
|
|
|
if (height >= db.height())
|
2015-09-01 10:40:33 +02:00
|
|
|
return false;
|
2015-09-20 19:41:38 +02:00
|
|
|
|
2017-03-03 17:50:53 +01:00
|
|
|
bool stop_batch = db.batch_start();
|
2015-09-26 17:22:57 +02:00
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
versions.clear();
|
|
|
|
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
last_versions[n] = 0;
|
2015-09-26 14:25:22 +02:00
|
|
|
const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0;
|
2015-09-20 19:41:38 +02:00
|
|
|
const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height);
|
2015-12-26 14:17:00 +01:00
|
|
|
while (current_fork_index > 0 && heights[current_fork_index].version > start_version) {
|
2015-09-20 19:41:38 +02:00
|
|
|
--current_fork_index;
|
|
|
|
}
|
2015-09-01 10:40:33 +02:00
|
|
|
for (uint64_t h = rescan_height; h <= height; ++h) {
|
2015-09-20 19:41:38 +02:00
|
|
|
cryptonote::block b = db.get_block_from_height(h);
|
2015-10-21 20:18:00 +02:00
|
|
|
const uint8_t v = get_effective_version(get_block_vote(b));
|
2015-09-01 10:40:33 +02:00
|
|
|
last_versions[v]++;
|
|
|
|
versions.push_back(v);
|
|
|
|
}
|
2015-09-26 14:25:22 +02:00
|
|
|
|
|
|
|
uint8_t voted = get_voted_fork_index(height + 1);
|
|
|
|
if (voted > current_fork_index) {
|
|
|
|
current_fork_index = voted;
|
|
|
|
}
|
|
|
|
|
2015-09-20 19:41:38 +02:00
|
|
|
const uint64_t bc_height = db.height();
|
2015-09-01 10:40:33 +02:00
|
|
|
for (uint64_t h = height + 1; h < bc_height; ++h) {
|
2015-11-24 21:46:10 +01:00
|
|
|
add(db.get_block_from_height(h), h);
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:50:53 +01:00
|
|
|
if (stop_batch)
|
|
|
|
db.batch_stop();
|
2015-09-26 17:22:57 +02:00
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-20 19:41:38 +02:00
|
|
|
bool HardFork::reorganize_from_chain_height(uint64_t height)
|
2015-09-01 10:40:33 +02:00
|
|
|
{
|
|
|
|
if (height == 0)
|
|
|
|
return false;
|
2015-09-20 19:41:38 +02:00
|
|
|
return reorganize_from_block_height(height - 1);
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 20:38:44 +02:00
|
|
|
bool HardFork::rescan_from_block_height(uint64_t height)
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2019-04-05 11:28:30 +02:00
|
|
|
db_rtxn_guard rtxn_guard(&db);
|
|
|
|
if (height >= db.height())
|
2015-10-16 20:38:44 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
versions.clear();
|
|
|
|
|
|
|
|
for (size_t n = 0; n < 256; ++n)
|
|
|
|
last_versions[n] = 0;
|
2016-07-25 23:41:49 +02:00
|
|
|
for (uint64_t h = height; h < db.height(); ++h) {
|
2015-10-16 20:38:44 +02:00
|
|
|
cryptonote::block b = db.get_block_from_height(h);
|
2015-10-21 20:18:00 +02:00
|
|
|
const uint8_t v = get_effective_version(get_block_vote(b));
|
2015-10-16 20:38:44 +02:00
|
|
|
last_versions[v]++;
|
|
|
|
versions.push_back(v);
|
|
|
|
}
|
|
|
|
|
2015-11-23 22:12:55 +01:00
|
|
|
uint8_t lastv = db.get_hard_fork_version(db.height() - 1);
|
2015-10-16 20:38:44 +02:00
|
|
|
current_fork_index = 0;
|
|
|
|
while (current_fork_index + 1 < heights.size() && heights[current_fork_index].version != lastv)
|
|
|
|
++current_fork_index;
|
2016-07-25 23:41:49 +02:00
|
|
|
|
|
|
|
uint8_t voted = get_voted_fork_index(db.height());
|
|
|
|
if (voted > current_fork_index) {
|
|
|
|
current_fork_index = voted;
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:38:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HardFork::rescan_from_chain_height(uint64_t height)
|
|
|
|
{
|
|
|
|
if (height == 0)
|
|
|
|
return false;
|
|
|
|
return rescan_from_block_height(height - 1);
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:26:46 +01:00
|
|
|
void HardFork::on_block_popped(uint64_t nblocks)
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_THROW_MES(nblocks > 0, "nblocks must be greater than 0");
|
|
|
|
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2019-01-01 19:26:46 +01:00
|
|
|
|
|
|
|
const uint64_t new_chain_height = db.height();
|
|
|
|
const uint64_t old_chain_height = new_chain_height + nblocks;
|
|
|
|
uint8_t version;
|
2019-04-20 01:55:04 +02:00
|
|
|
for (uint64_t height = old_chain_height - 1; height >= new_chain_height; --height)
|
2019-01-01 19:26:46 +01:00
|
|
|
{
|
2019-03-12 00:35:54 +01:00
|
|
|
version = versions.back();
|
|
|
|
last_versions[version]--;
|
2019-01-01 19:26:46 +01:00
|
|
|
versions.pop_back();
|
|
|
|
version = db.get_hard_fork_version(height);
|
|
|
|
versions.push_front(version);
|
2019-03-12 00:35:54 +01:00
|
|
|
last_versions[version]++;
|
2019-01-01 19:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// does not take voting into account
|
|
|
|
for (current_fork_index = heights.size() - 1; current_fork_index > 0; --current_fork_index)
|
2019-04-20 01:55:04 +02:00
|
|
|
if (new_chain_height >= heights[current_fork_index].height)
|
2019-01-01 19:26:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-01 10:40:33 +02:00
|
|
|
int HardFork::get_voted_fork_index(uint64_t height) const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-19 17:34:29 +02:00
|
|
|
uint32_t accumulated_votes = 0;
|
2019-02-27 16:56:49 +01:00
|
|
|
for (int n = heights.size() - 1; n >= 0; --n) {
|
2015-09-01 10:40:33 +02:00
|
|
|
uint8_t v = heights[n].version;
|
|
|
|
accumulated_votes += last_versions[v];
|
2015-11-08 14:04:41 +01:00
|
|
|
uint32_t threshold = (window_size * heights[n].threshold + 99) / 100;
|
|
|
|
if (height >= heights[n].height && accumulated_votes >= threshold) {
|
2015-09-01 10:40:33 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return current_fork_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
HardFork::State HardFork::get_state(time_t t) const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-01 10:40:33 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2016-02-01 21:06:17 +01:00
|
|
|
if (height > db.height()) {
|
2015-09-20 19:41:38 +02:00
|
|
|
assert(false);
|
2019-11-25 03:50:22 +01:00
|
|
|
return INVALID_HF_VERSION;
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
2016-02-01 21:06:17 +01:00
|
|
|
if (height == db.height()) {
|
|
|
|
return get_current_version();
|
|
|
|
}
|
2015-09-20 19:41:38 +02:00
|
|
|
return db.get_hard_fork_version(height);
|
2015-09-01 10:40:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t HardFork::get_current_version() const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-01 10:40:33 +02:00
|
|
|
return heights[current_fork_index].version;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t HardFork::get_ideal_version() const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-01 10:40:33 +02:00
|
|
|
return heights.back().version;
|
|
|
|
}
|
|
|
|
|
2015-11-09 22:22:32 +01:00
|
|
|
uint8_t HardFork::get_ideal_version(uint64_t height) const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-11-09 22:22:32 +01:00
|
|
|
for (unsigned int n = heights.size() - 1; n > 0; --n) {
|
|
|
|
if (height >= heights[n].height) {
|
|
|
|
return heights[n].version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return original_version;
|
|
|
|
}
|
|
|
|
|
2015-12-19 15:52:30 +01:00
|
|
|
uint64_t HardFork::get_earliest_ideal_height_for_version(uint8_t version) const
|
|
|
|
{
|
2018-03-20 01:52:16 +01:00
|
|
|
uint64_t height = std::numeric_limits<uint64_t>::max();
|
|
|
|
for (auto i = heights.rbegin(); i != heights.rend(); ++i) {
|
|
|
|
if (i->version >= version) {
|
|
|
|
height = i->height;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2015-12-19 15:52:30 +01:00
|
|
|
}
|
2018-03-20 01:52:16 +01:00
|
|
|
return height;
|
2015-12-19 15:52:30 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 20:19:25 +02:00
|
|
|
uint8_t HardFork::get_next_version() const
|
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2016-08-12 20:19:25 +02:00
|
|
|
uint64_t height = db.height();
|
2018-03-20 03:09:58 +01:00
|
|
|
for (auto i = heights.rbegin(); i != heights.rend(); ++i) {
|
|
|
|
if (height >= i->height) {
|
|
|
|
return (i == heights.rbegin() ? i : (i - 1))->version;
|
2016-08-12 20:19:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return original_version;
|
|
|
|
}
|
|
|
|
|
2015-12-19 15:52:30 +01:00
|
|
|
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
|
2015-09-19 17:34:29 +02:00
|
|
|
{
|
Purge epee::critical_crap and CRITICAL_CRAP
This purges epee::critical_region/epee::critical_section and the awful
CRITICAL_REGION_LOCAL and CRITICAL_REGION_LOCAL1 and
CRITICAL_REGION_BEGIN1 and all that crap from epee code.
This wrapper class around a mutex is just painful, macro-infested
indirection that accomplishes nothing (and, worse, forces all using code
to use a std::recursive_mutex even when a different mutex type is more
appropriate).
This commit purges it, replacing the "critical_section" mutex wrappers
with either std::mutex, std::recursive_mutex, or std::shared_mutex as
appropriate. I kept anything that looked uncertain as a
recursive_mutex, simple cases that obviously don't recurse as
std::mutex, and simple cases with reader/writing mechanics as a
shared_mutex.
Ideally all the recursive_mutexes should be eliminated because a
recursive_mutex is almost always a design flaw where someone has let the
locking code get impossibly tangled, but that requires a lot more time
to properly trace down all the ways the mutexes are used.
Other notable changes:
- There was one NIH promise/future-like class here that was used in
example one place in p2p/net_node; I replaced it with a
std::promise/future.
- moved the mutex for LMDB resizing into LMDB itself; having it in the
abstract base class is bad design, and also made it impossible to make a
moveable base class (which gets used for the fake db classes in the test
code).
2020-06-22 21:40:31 +02:00
|
|
|
std::unique_lock l{lock};
|
2015-09-19 17:34:29 +02:00
|
|
|
|
|
|
|
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];
|
2016-01-30 23:13:20 +01:00
|
|
|
threshold = (window * heights[current_fork_index].threshold + 99) / 100;
|
2015-12-27 09:44:18 +01:00
|
|
|
//assert((votes >= threshold) == enabled);
|
2015-12-19 15:52:30 +01:00
|
|
|
earliest_height = get_earliest_ideal_height_for_version(version);
|
2015-09-19 17:34:29 +02:00
|
|
|
voting = heights.back().version;
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|