-
Notifications
You must be signed in to change notification settings - Fork 26
Dynamic LWMA
zawy12 edited this page Jun 3, 2018
·
18 revisions
This has not been tested on any coins yet. Always check this page for changes before doing a commit or merge.
Do not use this algorithm. This is still in testing. Using this may result in bad delays.
The history of this algorithm is here.
Discuss this algorithm here.
I am working to improve this by making it a continuous function based on CDF of gamma distribution.
Make sure you have the following to prevent exploits.
- Cryptonote (Monero/Forknote/Bytecoin/Karbo/Sumo/Masari) clones must change:
#define DIFFICULTY_WINDOW_V2 = 60 + 1;
#define CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V2 = 3*DIFFICULTY_TARGET;
#define BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 11;
- Bitcoin clones reduce 7200 in the following to 3x your target solvetime
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200;
or maybe it is here:
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60);
Send any changes to me for validation before testnet. I may assign a version number to it if it passes testnet (send me 200 timestamps and difficulties for 2nd validation). Old versions can be seen in this wiki's history.
Version control numbers are first 2 bytes of SHA256. 1. 2.
// config file:
const size_t DIFFICULTY_WINDOW_V2 = 60 + 1;
const uint64_t CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V2 = 3*DIFFICULTY_TARGET;
const size_t BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 11;
// Declarations
uint64_t T = DIFFICULTY_TARGET; // target solvetime
uint64_t FTL = CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT;
uint64_t maxTS(0), i, L(0);
// Clone timestamps and CD vectors
vector<T> TS(timestamps);
vector<T> CD(cumulativeDifficulties);
// If POW change will cause delays, try this:
// if (height < fork_height + 61 ) { return reasonable_difficulty; }
// D-LWMA difficulty algorithm
// Copyright (c) 2018 Zawy
// Parent version was 0000
// See requirements before using this code:
// https://github.com/zawy12/difficulty-algorithms/wiki/Dynamic-LWMA
M=20; N=60;
// In case it's a new coin, give away 1st 6 blocks
if (timestamps.size() < 6 ) { return 1; }
else if ( timestamps.size() < N+1 ) { N = timestamps.size() - 1; }
else { timestamps.resize(N+1); cumulative_difficulties.resize((N+1); }
// Do Dynamic method only if FTL is not dangerously large & it's not a new coin
if (FTL <= 3*T && TS.size() > M+1) {
// Use Kyuupichan's (?) method to safely prevent negative solvetimes.
maxTS = TS[0];
for (i = 1; i <= N; i++) {
if (TS[i] > maxTS ) { maxTS = TS[i]; }
else { TS[i] = maxTS; }
}
// If a trigger occurred within last 6+5 blocks do fast LWMA
for ( i = N-6; i <= N; i++ ) {
if ( TS[i] - TS[i-5]) < 1.2*T ) {
if ( i == N ) { return 1.25*(CD[N]-CD[N-1]; } else { N = M; }
}
}
}
// Do LWMA with the N selected above
for ( i = 1; i <= N; i++) { L += (TS[i] - TS[i-1]) * i ; }
next_D = (CD[N] - CD[0]) * T*(N+1)*0.991*0.5/L;
return static_cast<uint64_t>(next_D);
// next_Target = sumTargets * L *2 / 0.998 / T / (N+1) / N / N; See Bitcoin Gold code.
// ======= End SHA256 with this line. =======
Credits:
// Preliminary algo: Dgenr8
// Support: Karbo, Masari, Sumo
// Error discovery: IPBC, BTC Gold, BTC Candy, Stellite
// Exploit discovery: gabetron
Here is the expected improvement over LWMA:
