Skip to content

Commit 215360b

Browse files
authored
Define and implement hard diff removal hardfork (#41)
* Bump to version 5.1.42 & setup BIP9 deployment for hard diff removal * CalculateNextWorkRequired: Implement hard diff removal hardfork * CalculateDifficultyDelta: Fix comment and adjust difficulty faster when the block time outside of 15 ~ 60 minutes range * CTestNetParams: Adjust DEPLOYMENT_HARD_DIFF_REMOVAL conditions * getblockchaininfo: Show hard_diff_removal status
1 parent 31766ce commit 215360b

File tree

7 files changed

+106
-17
lines changed

7 files changed

+106
-17
lines changed

configure.ac

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
AC_PREREQ([2.69])
2-
define(_CLIENT_VERSION_MAJOR, 5)
3-
define(_CLIENT_VERSION_MINOR, 0)
4-
define(_CLIENT_VERSION_BUILD, 70 )
2+
define(_CLIENT_VERSION_MAJOR, 5)
3+
define(_CLIENT_VERSION_MINOR, 1)
4+
define(_CLIENT_VERSION_BUILD, 42)
55
define(_CLIENT_VERSION_RC, 0)
66
define(_CLIENT_VERSION_IS_RELEASE, true )
77
define(_COPYRIGHT_YEAR, 2025)

src/chainparams.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ class CMainParams : public CChainParams {
129129
consensus.nDeadpoolAnnounceValidity = 672;
130130
consensus.nDeadpoolAnnounceMinBurn = 1000000; // 0.01 COIN
131131

132+
// Hard diff removal hardfork
133+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].bit = 26;
134+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nStartTime = 1743465600LL; // 2025-04-01
135+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nTimeout = 1775001600LL; // 2026-04-01
136+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].min_activation_height = 160000; // no delay
137+
132138
/**
133139
* The message start string is designed to be unlikely to occur in normal data.
134140
* The characters are rarely used upper ASCII, not valid as UTF-8, and produce
@@ -232,6 +238,12 @@ class CTestNetParams : public CChainParams {
232238
consensus.nDeadpoolAnnounceValidity = 100;
233239
consensus.nDeadpoolAnnounceMinBurn = 1000000; // 0.01 COIN
234240

241+
// Hard diff removal hardfork
242+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].bit = 26;
243+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nStartTime = 0;
244+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
245+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].min_activation_height = (4 * consensus.nMinerConfirmationWindow);
246+
235247
//Number of rounds for gHash to generate random Ws around which to search for semiprimes.
236248
consensus.hashRounds = 1;
237249

@@ -389,6 +401,12 @@ class SigNetParams : public CChainParams {
389401
consensus.nDeadpoolAnnounceValidity = 100;
390402
consensus.nDeadpoolAnnounceMinBurn = 1000000; // 0.01 COIN
391403

404+
// Hard diff removal hardfork
405+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].bit = 26;
406+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nStartTime = 1743465600LL; // 2025-04-01
407+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nTimeout = 1775001600LL; // 2026-04-01
408+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].min_activation_height = 160000; // no delay
409+
392410
vFixedSeeds.clear();
393411
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
394412
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
@@ -467,6 +485,12 @@ class CRegTestParams : public CChainParams {
467485
consensus.nDeadpoolAnnounceValidity = 100;
468486
consensus.nDeadpoolAnnounceMinBurn = 1000000; // 0.01 COIN
469487

488+
// Hard diff removal hardfork
489+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].bit = 26;
490+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nStartTime = 1743465600LL; // 2025-04-01
491+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].nTimeout = 1775001600LL; // 2026-04-01
492+
consensus.vDeployments[Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL].min_activation_height = 160000; // no delay
493+
470494
UpdateActivationParametersFromArgs(args);
471495

472496
vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.

src/consensus/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_
2929
enum DeploymentPos : uint16_t {
3030
DEPLOYMENT_TESTDUMMY,
3131
DEPLOYMENT_DEADPOOL,
32+
DEPLOYMENT_HARD_DIFF_REMOVAL,
3233
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
3334
MAX_VERSION_BITS_DEPLOYMENTS
3435
};

src/deploymentinfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_B
1515
/*.name =*/ "deadpool",
1616
/*.gbt_force =*/ true,
1717
},
18+
{
19+
/*.name =*/ "hard_diff_removal",
20+
/*.gbt_force =*/ true,
21+
},
1822
};
1923

2024
std::string DeploymentName(Consensus::BuriedDeployment dep)

src/pow.cpp

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
//Fancy popcount implementation
2929
#include <libpopcnt.h>
3030

31+
// DeploymentActiveAfter
32+
#include <deploymentstatus.h>
33+
3134
uint16_t GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock, const Consensus::Params& params)
3235
{
3336
assert(pindexLast != nullptr);
@@ -62,6 +65,73 @@ uint16_t GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader*
6265
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
6366
}
6467

68+
int32_t CalculateDifficultyDelta(const int32_t nBits, const double nPeriodTimeProportionConsumed, const bool isHardDiffRemoved) {
69+
if (!isHardDiffRemoved) {
70+
// Original difficulty adjustment algorithm
71+
72+
//Note for mainnet:
73+
//If it takes more than 1 minute over the target blocktime, reduce difficulty.
74+
if (nPeriodTimeProportionConsumed > 1.0333f)
75+
return -1;
76+
77+
//Note for mainnet:
78+
//To increase difficulty the network must be able to move the blocktime
79+
//3 minutes under target blocktime. This is to avoid the difficulty becoming
80+
//too much work for the network to handle. Based on heuristics.
81+
if (nPeriodTimeProportionConsumed < 0.90f)
82+
return 1;
83+
} else {
84+
// Difficulty adjustment algorithm that skips over odd aka. hard diffs (2025)
85+
86+
// If block time is too long, decrease to the previous even diff
87+
if (nPeriodTimeProportionConsumed > 1.0333f) {
88+
int32_t nRetarget = 0;
89+
if (nBits % 2 == 0) {
90+
// Even diff to even diff
91+
nRetarget = -2;
92+
} else {
93+
// Odd diff to even diff
94+
nRetarget = -1;
95+
}
96+
97+
// If block time is way too long (>60 min on mainnet), decrease by 4 or 3 instead of by 2 or 1
98+
if (nPeriodTimeProportionConsumed > 2.0f) {
99+
nRetarget -= 2;
100+
}
101+
102+
return nRetarget;
103+
}
104+
105+
// If block time is too short, increase to the next even diff
106+
if (nPeriodTimeProportionConsumed < 0.90f) {
107+
int32_t nRetarget = 0;
108+
if (nBits % 2 == 0) {
109+
// Even diff to even diff
110+
nRetarget = 2;
111+
} else {
112+
// Odd diff to even diff
113+
nRetarget = 1;
114+
}
115+
116+
// If block time is way too short (<15 min on mainnet), increase by 4 or 3 instead of by 2 or 1
117+
if (nPeriodTimeProportionConsumed < 0.5f) {
118+
nRetarget += 2;
119+
}
120+
121+
return nRetarget;
122+
}
123+
124+
// The block time is just right. See if we should move away from an odd diff
125+
if (nBits % 2 == 0) {
126+
// Already even diff. Don't change difficulty
127+
return 0;
128+
} else {
129+
// Currently odd diff. Decrease diff by 1 to reach an even difficulty
130+
return -1;
131+
}
132+
}
133+
}
134+
65135
uint16_t CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
66136
{
67137
if (params.fPowNoRetargeting)
@@ -70,22 +140,10 @@ uint16_t CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirst
70140
// Compute constants
71141
const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
72142
const double nPeriodTimeProportionConsumed = (double)nActualTimespan / (double)params.nPowTargetTimespan;
143+
const bool isHardDiffRemoved = DeploymentActiveAfter(pindexLast, params, Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL);
73144

74145
//Variable to set difficulty delta
75-
int32_t nRetarget = 0;
76-
77-
//Note for mainnet:
78-
//If it takes more than 1 minute over the target blocktime, reduce difficulty.
79-
if (nPeriodTimeProportionConsumed > 1.0333f)
80-
nRetarget = -1;
81-
82-
//Note for mainnet:
83-
//To increase difficulty the network must be able to move the blocktime
84-
//3 minutes under target blocktime. This is to avoid the difficulty becoming
85-
//too much work for the network to handle. Based on heuristics.
86-
if (nPeriodTimeProportionConsumed < 0.90f)
87-
nRetarget = 1;
88-
146+
int32_t nRetarget = CalculateDifficultyDelta(pindexLast->nBits, nPeriodTimeProportionConsumed, isHardDiffRemoved);
89147

90148
return (int32_t)pindexLast->nBits + nRetarget;
91149
}

src/pow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CBlockIndex;
1616
class uint256;
1717

1818
uint16_t GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&);
19+
int32_t CalculateDifficultyDelta(const int32_t nBits, const double nPeriodTimeProportionConsumed, const bool isHardDiffRemoved);
1920
uint16_t CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
2021

2122
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */

src/rpc/blockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@ RPCHelpMan getblockchaininfo()
15011501
SoftForkDescPushBack(tip, softforks, consensusParams, Consensus::DEPLOYMENT_TESTDUMMY);
15021502
SoftForkDescPushBack(tip, softforks, consensusParams, Consensus::DEPLOYMENT_TAPROOT);
15031503
SoftForkDescPushBack(tip, softforks, consensusParams, Consensus::DEPLOYMENT_DEADPOOL);
1504+
SoftForkDescPushBack(tip, softforks, consensusParams, Consensus::DEPLOYMENT_HARD_DIFF_REMOVAL);
15041505
obj.pushKV("softforks", softforks);
15051506

15061507
obj.pushKV("warnings", GetWarnings(false).original);

0 commit comments

Comments
 (0)