Merge pull request #252 from wowario/testnet

refactor difficulty for testnet
This commit is contained in:
jw 2020-05-11 07:35:01 -07:00 committed by GitHub
commit 12a2a4084b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 29 deletions

View file

@ -364,10 +364,44 @@ namespace cryptonote {
// LWMA-1 difficulty algorithm
// Copyright (c) 2017-2019 Zawy, MIT License
// https://github.com/zawy12/difficulty-algorithms/issues/3
difficulty_type next_difficulty_v5(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT, uint64_t FORK_HEIGHT, uint64_t difficulty_guess) {
difficulty_type next_difficulty_v5(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT) {
assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= N+1 );
if (HEIGHT >= FORK_HEIGHT && HEIGHT < FORK_HEIGHT + N) { return difficulty_guess; }
if (HEIGHT >= 81769 && HEIGHT < 81769 + N) { return 10000000; }
assert(timestamps.size() == N+1);
uint64_t L(0), next_D, i, this_timestamp(0), previous_timestamp(0), avg_D;
previous_timestamp = timestamps[0]-T;
for ( i = 1; i <= N; i++) {
// Safely prevent out-of-sequence timestamps
if ( timestamps[i] > previous_timestamp ) { this_timestamp = timestamps[i]; }
else { this_timestamp = previous_timestamp+1; }
L += i*std::min(6*T ,this_timestamp - previous_timestamp);
previous_timestamp = this_timestamp;
}
if (L < N*N*T/20 ) { L = N*N*T/20; }
avg_D = static_cast<uint64_t>(( cumulative_difficulties[N] - cumulative_difficulties[0] )/ N);
// Prevent round off error for small D and overflow for large D.
if (avg_D > 2000000*N*N*T) {
next_D = (avg_D/(200*L))*(N*(N+1)*T*99);
}
else { next_D = (avg_D*N*(N+1)*T*99)/(200*L); }
// Make all insignificant digits zero for easy reading.
i = 1000000000;
while (i > 1) {
if ( next_D > i*100 ) { next_D = ((next_D+i/2)/i)*i; break; }
else { i /= 10; }
}
return next_D;
}
difficulty_type next_difficulty_test(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT) {
assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= N+1 );
if (HEIGHT < N) { return 1337; }
assert(timestamps.size() == N+1);
uint64_t L(0), next_D, i, this_timestamp(0), previous_timestamp(0), avg_D;

View file

@ -61,7 +61,8 @@ namespace cryptonote
difficulty_type next_difficulty_v2(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds);
difficulty_type next_difficulty_v3(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties);
difficulty_type next_difficulty_v4(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t height);
difficulty_type next_difficulty_v5(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT, uint64_t FORK_HEIGHT, uint64_t difficulty_guess);
difficulty_type next_difficulty_v5(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT);
difficulty_type next_difficulty_test(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT);
std::string hex(difficulty_type v);
}

View file

@ -89,8 +89,6 @@
#define DIFFICULTY_BLOCKS_COUNT_V3 DIFFICULTY_WINDOW_V3 + 1 // added +1 to make N=N
#define DIFFICULTY_BLOCKS_COUNT_V2 DIFFICULTY_WINDOW_V2 + 1 // added +1 to make N=N
#define DIFFICULTY_BLOCKS_COUNT DIFFICULTY_WINDOW + DIFFICULTY_LAG
#define DIFFICULTY_FORK_HEIGHT 81769 // ~14 February 2019
#define DIFFICULTY_RESET 10000000 // 10 million
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1 DIFFICULTY_TARGET_V1 * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS
#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V2 DIFFICULTY_TARGET_V2 * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS

View file

@ -887,21 +887,25 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
uint64_t T = DIFFICULTY_TARGET_V2;
uint64_t N = DIFFICULTY_WINDOW_V3;
uint64_t HEIGHT = m_db->height();
uint64_t FORK_HEIGHT = DIFFICULTY_FORK_HEIGHT;
uint64_t difficulty_guess = DIFFICULTY_RESET;
difficulty_type diff = next_difficulty(timestamps, difficulties, target);
if (version >= 11) {
diff = next_difficulty_v5(timestamps, difficulties, T, N, HEIGHT, FORK_HEIGHT, difficulty_guess);
} else if (version == 10) {
diff = next_difficulty_v4(timestamps, difficulties, height);
} else if (version == 9) {
diff = next_difficulty_v3(timestamps, difficulties);
} else if (version == 8) {
diff = next_difficulty_v2(timestamps, difficulties, target);
} else {
diff = next_difficulty(timestamps, difficulties, target);
if (m_nettype == MAINNET) {
if (version >= 11) {
diff = next_difficulty_v5(timestamps, difficulties, T, N, HEIGHT);
} else if (version == 10) {
diff = next_difficulty_v4(timestamps, difficulties, height);
} else if (version == 9) {
diff = next_difficulty_v3(timestamps, difficulties);
} else if (version == 8) {
diff = next_difficulty_v2(timestamps, difficulties, target);
} else {
diff = next_difficulty(timestamps, difficulties, target);
}
}
if (m_nettype == TESTNET) {
diff = next_difficulty_test(timestamps, difficulties, T, N, HEIGHT);
}
CRITICAL_REGION_LOCAL1(m_difficulty_lock);
@ -1147,20 +1151,24 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
uint64_t T = DIFFICULTY_TARGET_V2;
uint64_t N = DIFFICULTY_WINDOW_V3;
uint64_t HEIGHT = m_db->height();
uint64_t FORK_HEIGHT = DIFFICULTY_FORK_HEIGHT;
uint64_t difficulty_guess = DIFFICULTY_RESET;
// calculate the difficulty target for the block and return it
if (version >= 11) {
return next_difficulty_v5(timestamps, cumulative_difficulties, T, N, HEIGHT, FORK_HEIGHT, difficulty_guess);
} else if (version == 10) {
return next_difficulty_v4(timestamps, cumulative_difficulties, height);
} else if (version == 9) {
return next_difficulty_v3(timestamps, cumulative_difficulties);
} else if (version == 8) {
return next_difficulty_v2(timestamps, cumulative_difficulties, target);
} else {
return next_difficulty(timestamps, cumulative_difficulties, target);
if (m_nettype == MAINNET) {
if (version >= 11) {
return next_difficulty_v5(timestamps, cumulative_difficulties, T, N, HEIGHT);
} else if (version == 10) {
return next_difficulty_v4(timestamps, cumulative_difficulties, height);
} else if (version == 9) {
return next_difficulty_v3(timestamps, cumulative_difficulties);
} else if (version == 8) {
return next_difficulty_v2(timestamps, cumulative_difficulties, target);
} else {
return next_difficulty(timestamps, cumulative_difficulties, target);
}
}
if (m_nettype == TESTNET) {
return next_difficulty_test(timestamps, cumulative_difficulties, T, N, HEIGHT);
}
}
//------------------------------------------------------------------