Comments (1)
Code from other coins for my future reference:
Niobio
difficulty_type Currency::nextDifficultyV5(std::vector<uint64_t> timestamps, std::vector<difficulty_type> cumulativeDifficulties) const {
// Slipstream EMA difficulty algorithm
// Copyright (c) 2018 Zawy
// EMA & LWMA math by Jacob Eliosoff and Tom Harding.
// https://github.com/zawy12/difficulty-algorithms/issues/27
// Cryptonote-like coins must see the link above for additional changes.
// After startup, timestamps & cumulativeDifficulties vectors should be size N+1.
double N = CryptoNote::parameters::DIFFICULTY_WINDOW_V4 - 1;
double T = m_difficultyTarget;
double FTL = static_cast<int64_t>(CryptoNote::parameters::CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V5);
double next_D, ST, D, tSMA, sumD, sumST;
if (timestamps.size() >= (N + 1)) {
// After startup, the following should be the norm.
timestamps.resize(N + 1); cumulativeDifficulties.resize(N + 1);
// For new coins height < N+1, give away first 4 blocks or use smaller N
} else if (timestamps.size() >= 4) {
N = timestamps.size() - 1;
} else {
return 100;
}
// Calculate fast EMA using most recent 2 blocks.
// +6xT prevents D dropping too far after attack to prevent on-off attack oscillations.
// -FTL prevents maliciously raising D. ST=solvetime.
ST = std::max(-FTL, std::min(double(timestamps[N] - timestamps[N - 1]), 6 * T));
D = cumulativeDifficulties[N] - cumulativeDifficulties[N - 1];
next_D = roundOffProtection(D * 9 * T / (8 * T + ST * 1.058));
// Calculate a tempered SMA.
sumD = cumulativeDifficulties[N] - cumulativeDifficulties[0];
sumST = timestamps[N] - timestamps[0];
tSMA = roundOffProtection(sumD * 2 * T / (N * T + sumST));
// Do slow EMA if fast EMA is outside +/- 14% from tSMA.
if (next_D > (tSMA * 1.14) || next_D < (tSMA * 0.877)) {
next_D = roundOffProtection(D * 28 * T / (27 * T + ST * 1.02));
}
return static_cast<uint64_t>(0.9935 * next_D);
}
Wownero
// Slipstream EMA difficulty algorithm
// Copyright (c) 2018 Zawy
// EMA math by Jacob Eliosoff and Tom Harding.
// https://github.com/zawy12/difficulty-algorithms/issues/27
// Cryptonote-like coins must see the link above for additional changes.
// Round Off Protection. D must normally be > 100 and always < 10 T.
// This is used because some devs believe double may pose a round-off risk.
double ROP(double RR) {
if( ceil(RR + 0.01) > ceil(RR) ) { RR = ceil(RR + 0.03); }
return RR;
}
difficulty_type next_difficulty_v3(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties) {
// After startup, timestamps & cumulative_difficulties vectors should be size N+1.
double N = DIFFICULTY_WINDOW_V2;
double T = DIFFICULTY_TARGET_V2;
double FTL = static_cast<int64_t>(CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V3); // < 3xT
double next_D, ST, D, tSMA, sumD, sumST;
// For new coins height < N+1, give away first 4 blocks or use smaller N
if (timestamps.size() < 4) { return 100; }
else if (timestamps.size() < N+1) { N = timestamps.size() - 1; }
// After startup, the following should be the norm.
else { timestamps.resize(N+1); cumulative_difficulties.resize(N+1); }
// Calculate fast EMA using most recent 2 blocks.
// +6xT prevents D dropping too far after attack to prevent on-off attack oscillations.
// -FTL prevents maliciously raising D. ST=solvetime.
ST = std::max(-FTL, std::min(double(timestamps[N] - timestamps[N-1]), 6*T));
// Most recent solvetime applies to previous difficulty, not the most recent one.
D = cumulative_difficulties[N] - cumulative_difficulties[N-1];
next_D = ROP(D*9/(8+ST/T/0.945));
// Calculate a tempered SMA. Don't shift the difficulties back 1 as in EMA.
sumD = cumulative_difficulties[N] - cumulative_difficulties[0];
sumST = timestamps[N] - timestamps[0];
tSMA = ROP(sumD/(0.5*N+0.5*sumST/T));
// Do slow EMA if fast EMA is outside +/- 14% from tSMA.
if (next_D > tSMA*1.14 or next_D < tSMA/1.14) {
next_D = ROP( D*28/(27+ST/T/0.98));
}
return static_cast<uint64_t>(0.9935*next_D);
}
}
Technohacker
difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
// After startup, timestamps & cumulative_difficulties vectors should be size N+1.
double N = DIFFICULTY_WINDOW;
double T = target_seconds;
double FTL = static_cast<int64_t>(CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT);
double next_D, ST, D, tSMA, sumD, sumST;
// For new coins height < N+1, give away first 4 blocks or use smaller N
if (timestamps.size() < 4) { return 100; }
else if (timestamps.size() < N+1) { N = timestamps.size() - 1; }
// After startup, the following should be the norm.
else { timestamps.resize(N+1); cumulative_difficulties.resize(N+1); }
// Calculate fast EMA using most recent 2 blocks.
// +6xT prevents D dropping too far after attack to prevent on-off attack oscillations.
// -FTL prevents maliciously raising D. ST=solvetime.
ST = std::max(-FTL, std::min(double(timestamps[N] - timestamps[N-1]), 6*T));
// Most recent solvetime applies to previous difficulty, not the most recent one.
D = cumulative_difficulties[N-1] - cumulative_difficulties[N-2];
next_D = ROP(D*9/(8+ST/T/0.945));
// Calculate a tempered SMA. Don't shift the difficulties back 1 as in EMA.
sumD = cumulative_difficulties[N] - cumulative_difficulties[0];
sumST = timestamps[N] - timestamps[0];
tSMA = ROP(sumD/(0.5*N+0.5*sumST/T));
// Do slow EMA if fast EMA is outside +/- 14% from tSMA.
if (next_D > tSMA*1.14 or next_D < tSMA/1.14) {
next_D = ROP( D*28/(27+ST/T/0.98));
}
return static_cast<uint64_t>(0.9935*next_D);
}
from difficulty-algorithms.
Related Issues (20)
- Detecting & Empowering Dedicated Miners In 51% Attacks
- Self-Hashing Transactions to Generate Coin
- Smooth Coin-Emission Formula
- Correcting Chain Work
- Importing Chain Work with Notarizations HOT 2
- New Selfish Mining Attack?
- ASERT / NEFDA is the perfect DA? HOT 22
- EMA for BCH (part 2) HOT 159
- Confirmation time, long averaging windows, a history
- Quantifying the Consensus Trilemma
- Verge Attack, February 15, 2021 HOT 1
- Multishield's geometric mean to get chain work in Multi-POW coins
- Easier-to-remember Seed Words HOT 3
- Installation HOT 3
- WTEMA / ASERT as an RC filter
- what is latest solution for Lwma? HOT 6
- Difficulty algorithm for interesting hashes instead of lowest target
- Maybe you can help me with go Implemetation this version of lwma HOT 1
- Best Difficulty Algorithm, Timestamp Rules, Selfish Mining, Selecting N
- Estimating Chain Work Based on Lowest Hashes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from difficulty-algorithms.