Add bounded emergency difficulty adjustment
Some checks failed
build / Cross-Mac aarch64 (push) Failing after 4s
build / ARM v8 (push) Successful in 7m58s
build / ARM v7 (push) Successful in 7m25s
build / i686 Linux (push) Successful in 6m15s
build / i686 Win (push) Successful in 6m54s
build / RISCV 64bit (push) Successful in 12m40s
build / x86_64 Linux (push) Has been cancelled
build / x86_64 Freebsd (push) Has been cancelled
build / Win64 (push) Has been cancelled
build / Cross-Mac x86_64 (push) Has been cancelled
Some checks failed
build / Cross-Mac aarch64 (push) Failing after 4s
build / ARM v8 (push) Successful in 7m58s
build / ARM v7 (push) Successful in 7m25s
build / i686 Linux (push) Successful in 6m15s
build / i686 Win (push) Successful in 6m54s
build / RISCV 64bit (push) Successful in 12m40s
build / x86_64 Linux (push) Has been cancelled
build / x86_64 Freebsd (push) Has been cancelled
build / Win64 (push) Has been cancelled
build / Cross-Mac x86_64 (push) Has been cancelled
This commit is contained in:
@@ -253,6 +253,7 @@
|
||||
#define HF_VERSION_AUDIT2_PAUSE 9
|
||||
#define HF_VERSION_CARROT 10
|
||||
#define HF_VERSION_ENABLE_TOKENS 11
|
||||
#define HF_VERSION_EMERGENCY_DIFFICULTY 12
|
||||
|
||||
#define HF_VERSION_REQUIRE_VIEW_TAGS 255
|
||||
#define HF_VERSION_ENABLE_CONVERT 255
|
||||
@@ -286,6 +287,11 @@
|
||||
#define PRICING_RECORD_VALID_BLOCKS 10
|
||||
#define PRICING_RECORD_VALID_TIME_DIFF_FROM_BLOCK 120 // seconds
|
||||
|
||||
// Emergency difficulty adjustment after an abnormally slow block.
|
||||
// Applied as a bounded cap on top of the regular difficulty algorithm.
|
||||
#define DIFFICULTY_EDA_THRESHOLD 6
|
||||
#define DIFFICULTY_EDA_MAX_DROP_PERCENT 75
|
||||
|
||||
//The limit is enough for the mandatory transaction content with 16 outputs (547 bytes),
|
||||
//a custom tag (1 byte) and up to 32 bytes of custom data for each recipient.
|
||||
// (1+32) + (1+1+16*32) + (1+16*32) = 1060
|
||||
|
||||
@@ -77,6 +77,42 @@
|
||||
|
||||
using namespace crypto;
|
||||
|
||||
namespace
|
||||
{
|
||||
cryptonote::difficulty_type apply_emergency_difficulty_cap(
|
||||
cryptonote::difficulty_type calculated_difficulty,
|
||||
const std::vector<uint64_t> ×tamps,
|
||||
const std::vector<cryptonote::difficulty_type> &cumulative_difficulties,
|
||||
size_t target_seconds)
|
||||
{
|
||||
if (timestamps.size() < 2 || cumulative_difficulties.size() < 2)
|
||||
return calculated_difficulty;
|
||||
|
||||
const uint64_t last_ts = timestamps[timestamps.size() - 1];
|
||||
const uint64_t prev_ts = timestamps[timestamps.size() - 2];
|
||||
const uint64_t solve_time = last_ts > prev_ts ? (last_ts - prev_ts) : 1;
|
||||
if (solve_time <= DIFFICULTY_EDA_THRESHOLD * target_seconds)
|
||||
return calculated_difficulty;
|
||||
|
||||
cryptonote::difficulty_type last_difficulty =
|
||||
cumulative_difficulties[cumulative_difficulties.size() - 1] -
|
||||
cumulative_difficulties[cumulative_difficulties.size() - 2];
|
||||
cryptonote::difficulty_type adjusted = (last_difficulty * target_seconds) / solve_time;
|
||||
if (adjusted < 1)
|
||||
adjusted = 1;
|
||||
|
||||
cryptonote::difficulty_type bounded_adjusted =
|
||||
(last_difficulty * (100 - DIFFICULTY_EDA_MAX_DROP_PERCENT)) / 100;
|
||||
if (bounded_adjusted < 1)
|
||||
bounded_adjusted = 1;
|
||||
|
||||
if (adjusted < bounded_adjusted)
|
||||
adjusted = bounded_adjusted;
|
||||
|
||||
return adjusted < calculated_difficulty ? adjusted : calculated_difficulty;
|
||||
}
|
||||
}
|
||||
|
||||
//#include "serialization/json_archive.h"
|
||||
|
||||
/* TODO:
|
||||
@@ -885,6 +921,7 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
|
||||
++height;
|
||||
|
||||
uint8_t version = get_current_hard_fork_version();
|
||||
const uint8_t next_version = m_hardfork->get_ideal_version(height);
|
||||
size_t difficulty_blocks_count;
|
||||
if (version == 1) {
|
||||
difficulty_blocks_count = DIFFICULTY_BLOCKS_COUNT;
|
||||
@@ -944,6 +981,8 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
|
||||
} else {
|
||||
diff = next_difficulty_v2(timestamps, difficulties, target);
|
||||
}
|
||||
if (next_version >= HF_VERSION_EMERGENCY_DIFFICULTY)
|
||||
diff = apply_emergency_difficulty_cap(diff, timestamps, difficulties, target);
|
||||
|
||||
CRITICAL_REGION_LOCAL1(m_difficulty_lock);
|
||||
m_difficulty_for_next_block_top_hash = top_hash;
|
||||
@@ -1271,6 +1310,7 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
|
||||
std::vector<uint64_t> timestamps;
|
||||
std::vector<difficulty_type> cumulative_difficulties;
|
||||
uint8_t version = get_current_hard_fork_version();
|
||||
const uint8_t next_version = get_ideal_hard_fork_version(bei.height);
|
||||
size_t difficulty_blocks_count;
|
||||
if (version == 1) {
|
||||
difficulty_blocks_count = DIFFICULTY_BLOCKS_COUNT;
|
||||
@@ -1335,7 +1375,10 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
|
||||
if (version == 1) {
|
||||
return next_difficulty(timestamps, cumulative_difficulties, target);
|
||||
} else {
|
||||
return next_difficulty_v2(timestamps, cumulative_difficulties, target);
|
||||
difficulty_type diff = next_difficulty_v2(timestamps, cumulative_difficulties, target);
|
||||
if (next_version >= HF_VERSION_EMERGENCY_DIFFICULTY)
|
||||
diff = apply_emergency_difficulty_cap(diff, timestamps, cumulative_difficulties, target);
|
||||
return diff;
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
|
||||
@@ -36,6 +36,7 @@ const hardfork_t mainnet_hard_forks[] = {
|
||||
{ 1, 0, 0, 1341378000 },
|
||||
{10, 1, 0, 1341378120 },
|
||||
{11, 2, 0, 1341378240 },
|
||||
{12, 3, 0, 1341378360 },
|
||||
};
|
||||
const size_t num_mainnet_hard_forks = sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]);
|
||||
const uint64_t mainnet_hard_fork_version_1_till = 0;
|
||||
@@ -45,6 +46,7 @@ const hardfork_t testnet_hard_forks[] = {
|
||||
{ 1, 0, 0, 1341378000 },
|
||||
{10, 1, 0, 1341378120 },
|
||||
{11, 2, 0, 1341378240 },
|
||||
{12, 3, 0, 1341378360 },
|
||||
};
|
||||
const size_t num_testnet_hard_forks = sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]);
|
||||
const uint64_t testnet_hard_fork_version_1_till = 0;
|
||||
@@ -54,5 +56,6 @@ const hardfork_t stagenet_hard_forks[] = {
|
||||
{ 1, 0, 0, 1341378000 },
|
||||
{10, 1, 0, 1341378120 },
|
||||
{11, 2, 0, 1341378240 },
|
||||
{12, 3, 0, 1341378360 },
|
||||
};
|
||||
const size_t num_stagenet_hard_forks = sizeof(stagenet_hard_forks) / sizeof(stagenet_hard_forks[0]);
|
||||
|
||||
Reference in New Issue
Block a user