Git Product home page Git Product logo

Comments (1)

zawy12 avatar zawy12 commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.