Skip to content

Commit

Permalink
Optimization of difficulty adjustment strategy
Browse files Browse the repository at this point in the history
Optimization of difficulty adjustment strategy
  • Loading branch information
bitcoincandyofficial committed Apr 12, 2018
1 parent 523dd1e commit f26e521
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 63 deletions.
14 changes: 14 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
#include <vector>
#include <string.h>

/**
* Maximum amount of time that a block timestamp is allowed to exceed the
* current network-adjusted time before the block will be accepted.
*/
static const int64_t BCH_MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60; //T<600 should not use it

/**
* Timestamp window used as a grace period by code that compares external
* timestamps (such as timestamps passed to RPCs, or wallet key creation times)
* to block timestamps. This should be set at least as high as
* BCH_MAX_FUTURE_BLOCK_TIME.
*/
static const int64_t TIMESTAMP_WINDOW = BCH_MAX_FUTURE_BLOCK_TIME;

class CBlockFileInfo {
public:
//!< number of blocks stored in file
Expand Down
56 changes: 34 additions & 22 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "base58.h"
#include <boost/assign/list_of.hpp>
#include <limits>

#include "chainparamsseeds.h"

Expand Down Expand Up @@ -121,12 +122,14 @@ class CMainParams : public CChainParams {
consensus.powLimit = uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.powLimitLegacy = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

//based on https://github.com/BTCGPU/BTCGPU/issues/78
consensus.nPowAveragingWindow = 30;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nPowAveragingWindow);
consensus.nPowMaxAdjustDown = 32;
consensus.nPowMaxAdjustUp = 16;
consensus.nDigishieldAveragingWindow = 30;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nDigishieldAveragingWindow);
consensus.nDigishieldMaxAdjustDown = 32;
consensus.nDigishieldMaxAdjustUp = 16;

consensus.nZawyLwmaAveragingWindow = 60;
consensus.CDYMaxFutureBlockTime = 7 * 2 * 60; //

// two weeks
consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60;
consensus.nPowTargetSpacing = 10 * 60;
Expand Down Expand Up @@ -172,6 +175,11 @@ class CMainParams : public CChainParams {

// hard fork-----add by hmc
consensus.cdyHeight = 512666;



consensus.CDYZawyLWMAHeight = 573123;

consensus.BitcoinPostforkBlock = uint256S("0000000000000000007b746068bd08ba4089f97636690e9dc758774e7db21f17"); // 512666 block hash
consensus.BitcoinPostforkTime = 1515799972;

Expand Down Expand Up @@ -303,14 +311,15 @@ class CTestNetParams : public CChainParams {
consensus.antiReplayOpReturnSunsetHeight = 1250000;
consensus.antiReplayOpReturnCommitment = GetAntiReplayCommitment();
consensus.powLimit = uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.powLimitLegacy = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.powLimitLegacy = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

//based on https://github.com/BTCGPU/BTCGPU/issues/78
consensus.nPowAveragingWindow = 30;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nPowAveragingWindow);
consensus.nPowMaxAdjustDown = 32;
consensus.nPowMaxAdjustUp = 16;
consensus.nDigishieldAveragingWindow= 30;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nDigishieldAveragingWindow);
consensus.nDigishieldMaxAdjustDown = 32;
consensus.nDigishieldMaxAdjustUp = 16;

consensus.nZawyLwmaAveragingWindow = 60;
consensus.CDYMaxFutureBlockTime = 7 * 2 * 60; // 14 mins
// two weeks
consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60;
consensus.nPowTargetSpacing = 10 * 60;
Expand Down Expand Up @@ -355,6 +364,8 @@ class CTestNetParams : public CChainParams {

// hard fork-----add by hmc
consensus.cdyHeight = 201601;

consensus.CDYZawyLWMAHeight = 201681;
consensus.BitcoinPostforkBlock = uint256S("00000000d16d6c2aecc7436eea0c54a53741fee9abf265606aa465d6fd3f3d8a"); // block 201601
consensus.BitcoinPostforkTime = 1393815074;

Expand All @@ -379,7 +390,7 @@ class CTestNetParams : public CChainParams {

// use different default
nDefaultPort = 18367;
nBitcoinDefaultPort = 18333;
nBitcoinDefaultPort = 18333;
nPruneAfterHeight = 1000;
const size_t N = 200, K = 9;
BOOST_STATIC_ASSERT(equihash_parameters_acceptable(N, K));
Expand All @@ -399,9 +410,7 @@ class CTestNetParams : public CChainParams {
vFixedSeeds.clear();
vSeeds.clear();
// nodes with support for servicebits filtering should be at the top
// Bitcoin ABC seeder
vSeeds.push_back(CDNSSeedData("bitcoincandy.one", "testnet-seed.bitcoincandy.one", true));

base58Prefixes[PUBKEY_ADDRESS] = std::vector<uint8_t>(1, 111);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<uint8_t>(1, 196);
base58Prefixes[SECRET_KEY] = std::vector<uint8_t>(1, 239);
Expand Down Expand Up @@ -457,14 +466,15 @@ class CRegTestParams : public CChainParams {
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.powLimitLegacy = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

//based on https://github.com/BTCGPU/BTCGPU/issues/78
consensus.nPowAveragingWindow = 30;
//assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nPowAveragingWindow);
consensus.nPowMaxAdjustDown = 32;
consensus.nPowMaxAdjustUp = 16;
consensus.nDigishieldAveragingWindow = 30;
consensus.nDigishieldMaxAdjustDown = 32;
consensus.nDigishieldMaxAdjustUp = 16;

// two weeks
consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60;
consensus.nZawyLwmaAveragingWindow = 45;
consensus.nZawyLwmaAjustedWeight = 13632;
consensus.CDYMaxFutureBlockTime = 7 * 10 * 60; // 70 mins

consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
consensus.nPowTargetSpacingCDY = 2 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
Expand Down Expand Up @@ -497,7 +507,9 @@ class CRegTestParams : public CChainParams {

// Nov, 13 hard fork is always on on regtest.
consensus.daaHeight = 2250;


consensus.CDYZawyLWMAHeight = -1;

diskMagic[0] = 0xfa;
diskMagic[1] = 0xbf;
diskMagic[2] = 0xb5;
Expand Down
21 changes: 15 additions & 6 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ struct Params {
int uahfHeight;
/** Block height at which CDHF kicks in --add by hmc*/
int cdyHeight;
/** Block height at which Zawy's LWMA difficulty algorithm becomes active */
int CDYZawyLWMAHeight;
/** Limit BITCOIN_MAX_FUTURE_BLOCK_TIME **/
int64_t CDYMaxFutureBlockTime;

/** Block height at which the new DAA becomes active */
/** The first post-fork block of Bitcoin blockchain. **/
uint256 BitcoinPostforkBlock;
Expand Down Expand Up @@ -83,14 +88,18 @@ struct Params {
int64_t nPowTargetTimespanLegacy;
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespanLegacy / nPowTargetSpacing; }
//Zcash logic for diff adjustment
int64_t nPowAveragingWindow;
int64_t nPowMaxAdjustDown;
int64_t nPowMaxAdjustUp;
int64_t AveragingWindowTimespan() const { return nPowAveragingWindow * nPowTargetSpacingCDY; }
int64_t MinActualTimespan() const { return (AveragingWindowTimespan() * (100 - nPowMaxAdjustUp )) / 100; }
int64_t MaxActualTimespan() const { return (AveragingWindowTimespan() * (100 + nPowMaxAdjustDown)) / 100; }
int64_t nDigishieldAveragingWindow;
int64_t nDigishieldMaxAdjustDown;
int64_t nDigishieldMaxAdjustUp;
int64_t DigishieldAveragingWindowTimespan() const { return nDigishieldAveragingWindow * nPowTargetSpacingCDY; }
int64_t DigishieldMinActualTimespan() const { return (DigishieldAveragingWindowTimespan() * (100 - nDigishieldMaxAdjustUp )) / 100; }
int64_t DigishieldMaxActualTimespan() const { return (DigishieldAveragingWindowTimespan() * (100 + nDigishieldMaxAdjustDown)) / 100; }
uint256 nMinimumChainWork;
uint256 defaultAssumeValid;

// Params for Zawy's LWMA difficulty adjustment algorithm. (Used by testnet and regtest)
int64_t nZawyLwmaAveragingWindow; // N
int64_t nZawyLwmaAjustedWeight; // k = (N+1)/2 * 0.9989^(500/N) * T
};
} // namespace Consensus

Expand Down
Loading

0 comments on commit f26e521

Please sign in to comment.