feat: 2 types of timespan for ASERT

This commit is contained in:
nandhh 2024-07-19 09:23:44 +08:00
parent dfa35b361e
commit be8a325252
3 changed files with 47 additions and 22 deletions

View File

@ -78,6 +78,8 @@ public:
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day consensus.nPowTargetTimespan = 24 * 60 * 60; // one day
consensus.nPowTargetSpacing = 30; consensus.nPowTargetSpacing = 30;
consensus.nPowTargetSpacingLegacy = 45;
consensus.nPowTargetSpacingAuxPow = 90;
consensus.fPowAllowMinDifficultyBlocks = false; consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false; consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 2592; // 90% of 2880 consensus.nRuleChangeActivationThreshold = 2592; // 90% of 2880
@ -102,16 +104,15 @@ public:
consensus.nMinimumChainWork = uint256S(""); consensus.nMinimumChainWork = uint256S("");
consensus.defaultAssumeValid = uint256S(""); consensus.defaultAssumeValid = uint256S("");
consensus.nAuxpowChainId = 0x0003; consensus.nAuxpowChainId = 0x2024;
consensus.nAuxpowStartHeight = 3;
consensus.fStrictChainId = true; consensus.fStrictChainId = true;
// Anchor params: Note that the block after this height *must* also be checkpointed below. // Anchor params: Note that the block after this height *must* also be checkpointed below.
consensus.asertAnchorParams = Consensus::Params::ASERTAnchor{ consensus.asertAnchorParams = Consensus::Params::ASERTAnchor{
1, // anchor block height 1, // anchor block height
0x1804dafe, // anchor block nBits 0x1d00ffff, // anchor block nBits
0x1804dafe, // anchor block nBits for auxpow 0x1d00ffff, // anchor block nBits for auxpow
1605447844, // anchor block previous block timestamp 1721369687, // anchor block previous block timestamp
}; };
/** /**
@ -160,7 +161,7 @@ public:
checkpointData = { checkpointData = {
{ {
{0, uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")}, {0, uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")},
{1, uint256S("00000000067e53da31878d29e76cc124468892658f1c763ab047c0c1fcfc73f5")}, {1, uint256S("000000008fbbbe27a3e408afd8178e22de95253b52f7fc4372cb6e3cd5aa0ebc")},
} }
}; };
@ -199,6 +200,8 @@ public:
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day consensus.nPowTargetTimespan = 24 * 60 * 60; // one day
consensus.nPowTargetSpacing = 30; consensus.nPowTargetSpacing = 30;
consensus.nPowTargetSpacingLegacy = 45;
consensus.nPowTargetSpacingAuxPow = 90;
consensus.fPowAllowMinDifficultyBlocks = true; consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = false; consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 2160; // 75% for testchains consensus.nRuleChangeActivationThreshold = 2160; // 75% for testchains
@ -223,8 +226,7 @@ public:
consensus.nMinimumChainWork = uint256S(""); consensus.nMinimumChainWork = uint256S("");
consensus.defaultAssumeValid = uint256S(""); consensus.defaultAssumeValid = uint256S("");
consensus.nAuxpowStartHeight = 3; consensus.nAuxpowChainId = 0x2024;
consensus.nAuxpowChainId = 0x0003;
consensus.fStrictChainId = false; consensus.fStrictChainId = false;
// Anchor params: Note that the block after this height *must* also be checkpointed below. // Anchor params: Note that the block after this height *must* also be checkpointed below.
@ -232,7 +234,7 @@ public:
1, // anchor block height 1, // anchor block height
0x1d00ffff, // anchor block nBits 0x1d00ffff, // anchor block nBits
0x1d00ffff, // anchor block nBits for auxpow 0x1d00ffff, // anchor block nBits for auxpow
1605445400, // anchor block previous block timestamp 1721369687, // anchor block previous block timestamp
}; };
pchMessageStart[0] = 0x0b; pchMessageStart[0] = 0x0b;
@ -346,6 +348,8 @@ public:
consensus.SegwitHeight = 1; consensus.SegwitHeight = 1;
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day consensus.nPowTargetTimespan = 24 * 60 * 60; // one day
consensus.nPowTargetSpacing = 30; consensus.nPowTargetSpacing = 30;
consensus.nPowTargetSpacingLegacy = 45;
consensus.nPowTargetSpacingAuxPow = 90;
consensus.fPowAllowMinDifficultyBlocks = false; consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false; consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 2592; // 90% of 2880 consensus.nRuleChangeActivationThreshold = 2592; // 90% of 2880
@ -369,8 +373,7 @@ public:
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT; consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay
consensus.nAuxpowStartHeight = 3; consensus.nAuxpowChainId = 0x2024;
consensus.nAuxpowChainId = 0x0003;
consensus.fStrictChainId = true; consensus.fStrictChainId = true;
// Anchor params: Note that the block after this height *must* also be checkpointed below. // Anchor params: Note that the block after this height *must* also be checkpointed below.
@ -378,7 +381,7 @@ public:
1, // anchor block height 1, // anchor block height
0x1d00ffff, // anchor block nBits 0x1d00ffff, // anchor block nBits
0x1d00ffff, // anchor block nBits for auxpow 0x1d00ffff, // anchor block nBits for auxpow
1605445400, // anchor block previous block timestamp 1721369687, // anchor block previous block timestamp
}; };
@ -435,6 +438,8 @@ public:
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day consensus.nPowTargetTimespan = 24 * 60 * 60; // one day
consensus.nPowTargetSpacing = 30; consensus.nPowTargetSpacing = 30;
consensus.nPowTargetSpacingLegacy = 45;
consensus.nPowTargetSpacingAuxPow = 90;
consensus.fPowAllowMinDifficultyBlocks = true; consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true; consensus.fPowNoRetargeting = true;
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
@ -458,8 +463,7 @@ public:
consensus.nMinimumChainWork = uint256{}; consensus.nMinimumChainWork = uint256{};
consensus.defaultAssumeValid = uint256{}; consensus.defaultAssumeValid = uint256{};
consensus.nAuxpowStartHeight = 3; consensus.nAuxpowChainId = 0x2024;
consensus.nAuxpowChainId = 0x0003;
consensus.fStrictChainId = true; consensus.fStrictChainId = true;
// RegTest has no hard-coded anchor block // RegTest has no hard-coded anchor block

View File

@ -109,6 +109,8 @@ struct Params {
bool fPowAllowMinDifficultyBlocks; bool fPowAllowMinDifficultyBlocks;
bool fPowNoRetargeting; bool fPowNoRetargeting;
int64_t nPowTargetSpacing; int64_t nPowTargetSpacing;
int64_t nPowTargetSpacingLegacy;
int64_t nPowTargetSpacingAuxPow;
int64_t nASERTHalfLife; int64_t nASERTHalfLife;
int64_t nPowTargetTimespan; int64_t nPowTargetTimespan;
std::chrono::seconds PowTargetSpacing() const std::chrono::seconds PowTargetSpacing() const
@ -124,7 +126,7 @@ struct Params {
/** Used by the ASERT DAA activated */ /** Used by the ASERT DAA activated */
struct ASERTAnchor { struct ASERTAnchor {
int nHeight; int nHeight;
uint32_t nBits; uint32_t nBitsLegacy;
uint32_t nBitsAuxPow; uint32_t nBitsAuxPow;
int64_t nBlockTime; int64_t nBlockTime;
}; };
@ -158,7 +160,6 @@ struct Params {
/** Auxpow parameters */ /** Auxpow parameters */
int32_t nAuxpowChainId; int32_t nAuxpowChainId;
int nAuxpowStartHeight;
bool fStrictChainId; bool fStrictChainId;
}; };

View File

@ -39,7 +39,13 @@ static uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev,
// as per the formula the timestamp of block M-1 must be used if the anchor is M. // as per the formula the timestamp of block M-1 must be used if the anchor is M.
assert(pindexPrev->pprev != nullptr); assert(pindexPrev->pprev != nullptr);
const arith_uint256 refBlockTarget = arith_uint256().SetCompact(anchorParams.nBits); uint32_t anchornBits = anchorParams.nBitsLegacy;
int64_t nPowTargetSpacing = params.nPowTargetSpacingLegacy;
if (pblock->IsAuxpow()) {
anchornBits = anchorParams.nBitsAuxPow;
nPowTargetSpacing = params.nPowTargetSpacingAuxPow;
}
const arith_uint256 refBlockTarget = arith_uint256().SetCompact(anchornBits);
// Time difference is from anchor block's timestamp // Time difference is from anchor block's timestamp
const int64_t nTimeDiff = pindexPrev->GetBlockTime() - anchorParams.nBlockTime; const int64_t nTimeDiff = pindexPrev->GetBlockTime() - anchorParams.nBlockTime;
@ -49,7 +55,7 @@ static uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev,
// Do the actual target adaptation calculation in separate // Do the actual target adaptation calculation in separate
// CalculateASERT() function // CalculateASERT() function
arith_uint256 nextTarget = CalculateASERT(refBlockTarget, arith_uint256 nextTarget = CalculateASERT(refBlockTarget,
params.nPowTargetSpacing, nPowTargetSpacing,
nTimeDiff, nTimeDiff,
nHeightDiff, nHeightDiff,
powLimit, powLimit,
@ -158,7 +164,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
assert(params.asertAnchorParams.nHeight > 0); assert(params.asertAnchorParams.nHeight > 0);
if (pindexLast->nHeight <= params.asertAnchorParams.nHeight) { if (pindexLast->nHeight <= params.asertAnchorParams.nHeight) {
return params.asertAnchorParams.nBits; return params.asertAnchorParams.nBitsLegacy;
} }
// Special difficulty rule for testnet // Special difficulty rule for testnet
@ -166,11 +172,25 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
// mining of a min-difficulty block. // mining of a min-difficulty block.
if (params.fPowAllowMinDifficultyBlocks && if (params.fPowAllowMinDifficultyBlocks &&
(pblock->GetBlockTime() > (pblock->GetBlockTime() >
pindexLast->GetBlockTime() + 2 * params.nPowTargetSpacing)) { pindexLast->GetBlockTime() + 2 * params.nPowTargetSpacingAuxPow)) {
return UintToArith256(params.powLimit).GetCompact(); return UintToArith256(params.powLimit).GetCompact();
} }
return GetNextASERTWorkRequired(pindexLast, pblock, params); // Slow path: walk back until we find the first same version block
const CBlockIndex *pindexPrev = pindexLast;
while (pindexPrev->GetPureHeader().IsAuxpow() != pblock->IsAuxpow()) {
if (pindexPrev->nHeight <= params.asertAnchorParams.nHeight) {
if (pblock->IsAuxpow()) {
return params.asertAnchorParams.nBitsAuxPow;
} else {
return params.asertAnchorParams.nBitsLegacy;
}
}
pindexPrev = pindexPrev->pprev;
}
return GetNextASERTWorkRequired(pindexPrev, pblock, params);
} }
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)