8#include <chainparams.h>
14#include <test/util/random.h>
15#include <test/util/setup_common.h>
17#include <boost/test/unit_test.hpp>
21BOOST_FIXTURE_TEST_SUITE(aserti32d_tests, BasicTestingSetup)
26 block.
pprev = pindexPrev;
27 block.
nHeight = pindexPrev->nHeight + 1;
28 block.
nTime = pindexPrev->nTime + nTimeInterval;
37 return (nBits & 0xff'ff'ff) * pow(256, (nBits >> 24) - 3);
41 const uint32_t finalBits,
43 const int64_t nHeightDiff =
45 const int64_t nTimeDiff =
47 const uint32_t initialBits = pindexAnchorBlock->
nBits;
54 double(nTimeDiff - (nHeightDiff + 1) * 600) / double(2 * 24 * 3600);
55 double dTarget = dInitialPow * pow(2, dExponent);
57 return (dFinalPow - dTarget) / dTarget;
63 std::vector<CBlockIndex> blocks(3000 + 2 * 24 * 3600);
68 uint32_t initialBits = currentPow.
GetCompact();
69 double dMaxErr = 0.0001166792656486;
73 blocks[0].nHeight = 0;
74 blocks[0].nTime = 1269211443;
77 blocks[0].nBits = 0x0dedbeef;
94 &blocks[1])) < dMaxErr);
104 &blocks[1])) < dMaxErr);
110 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 172800, nBits);
119 &blocks[1])) < dMaxErr);
122 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 172800, nBits);
128 &blocks[1])) < dMaxErr);
131 for (; i < 150; i++) {
142 for (
size_t j = 0; j < 10; i++, j++) {
156 &blocks[1])) < dMaxErr);
159 &blocks[i - 2])) < dMaxErr);
165 &blocks[1])) < dMaxErr);
168 &blocks[i - 2])) < dMaxErr);
178 &blocks[1])) < dMaxErr);
181 &blocks[i - 2])) < dMaxErr);
187 &blocks[1])) < dMaxErr);
190 &blocks[i - 2])) < dMaxErr);
195 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
200 &blocks[1])) < dMaxErr);
203 &blocks[i - 2])) < dMaxErr);
208 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
212 &blocks[i - 1], nBits, &blocks[1])) < dMaxErr);
219 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
224 &blocks[1])) < dMaxErr);
227 &blocks[i - 2])) < dMaxErr);
235 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
240 &blocks[1])) < dMaxErr);
243 &blocks[i - 2])) < dMaxErr);
245 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
250 &blocks[1])) < dMaxErr);
253 &blocks[i - 2])) < dMaxErr);
274 uint32_t nBitsRingBuffer[8];
276 blocks[i] =
GetBlockIndex(&blocks[i - 1], -2 * 24 * 3600 - 30, nBits);
277 for (
size_t j = 0; j < 4 * 24 * 3600 + 660; j++) {
295 dMaxStep = std::max(dMaxStep, dStep);
299 nBitsRingBuffer[j % 8] = nBits;
304 dMin = std::min(dMin, dErr);
305 dMax = std::max(dMax, dErr);
306 dRelMin = std::min(dRelMin, dRelErr);
307 dRelMax = std::max(dRelMax, dRelErr);
309 fabs(dErr) < dMaxErr,
311 "solveTime: %d\tStep size: %.8f%%\tdErr: %.8f%%\tnBits: %0x\n",
312 int64_t(blocks[i].nTime) - blocks[i - 1].nTime, dStep * 100,
315 fabs(dRelErr) < dMaxErr,
316 strprintf(
"solveTime: %d\tStep size: %.8f%%\tdRelErr: "
317 "%.8f%%\tnBits: %0x\n",
318 int64_t(blocks[i].nTime) - blocks[i - 1].nTime,
319 dStep * 100, dRelErr * 100, nBits));
322 "Min error: %16.14f%%\tMax error: %16.14f%%\tMax step: %16.14f%%\n",
323 dMin * 100, dMax * 100, dMaxStep * 100);
325 dMin < -0.0001013168981059 && dMin > -0.0001013168981060 &&
326 dMax > 0.0001166792656485 && dMax < 0.0001166792656486,
328 failMsg =
strprintf(
"Min relError: %16.14f%%\tMax relError: %16.14f%%\n",
329 dRelMin * 100, dRelMax * 100);
331 dRelMin < -0.0001013168981059 && dRelMin > -0.0001013168981060 &&
332 dRelMax > 0.0001166792656485 && dRelMax < 0.0001166792656486,
336 for (
size_t j = 0; j < 100; i++, j++) {
355 const int64_t targetSpacing,
356 const int64_t timeDiff,
357 const int64_t heightDiff,
359 const uint32_t expectednBits) {
366 "exp nBits= 0x%08x\n",
367 refTarget.
ToString(), targetSpacing, timeDiff, heightDiff,
368 expectedTarget.
ToString(), expectednBits);
387 static const int64_t parent_time_diff = 600;
392 parent_time_diff + 600 , ++height, powLimit, nHalfLife);
397 parent_time_diff + 600 + 300, ++height,
398 powLimit, nHalfLife);
405 parent_time_diff + 600 + 300 + 900, ++height,
406 powLimit, nHalfLife);
412 prevTarget = nextTarget;
415 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
419 prevTarget = nextTarget;
422 parent_time_diff + 288 * 0, 288, powLimit, nHalfLife);
427 uint32_t powLimit_nBits = powLimit.
GetCompact();
429 for (
size_t k = 0; k < 3; k++) {
430 prevTarget = nextTarget;
432 parent_time_diff + 288 * 1200, 288,
433 powLimit, nHalfLife);
440 prevTarget = nextTarget;
443 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
453 parent_time_diff + 512 * 144 * 600, 0, powLimit,
462 2 * (256 - 33) * 144, powLimit, nHalfLife);
471 int64_t targetSpacing;
475 uint32_t expectednBits;
480 "00000000ffb1ffffffffffffffffffffffffffffffffffffffffffffffffffff"};
482 "000000008000000000000000000fffffffffffffffffffffffffffffffffffff"};
487 const std::vector<calc_params> calculate_args = {
492 {powLimit, 600, 0, 2 * 144, powLimit >> 1, 0x1c7fffff},
493 {powLimit, 600, 0, 4 * 144, powLimit >> 2, 0x1c3fffff},
494 {powLimit >> 1, 600, 0, 2 * 144, powLimit >> 2, 0x1c3fffff},
495 {powLimit >> 2, 600, 0, 2 * 144, powLimit >> 3, 0x1c1fffff},
496 {powLimit >> 3, 600, 0, 2 * 144, powLimit >> 4, 0x1c0fffff},
497 {powLimit, 600, 0, 2 * (256 - 34) * 144, 3, 0x01030000},
498 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 119, 3, 0x01030000},
499 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 120, 2, 0x01020000},
500 {powLimit, 600, 0, 2 * (256 - 33) * 144 - 1, 2, 0x01020000},
502 {powLimit, 600, 0, 2 * (256 - 33) * 144, 1, 0x01010000},
504 {powLimit, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
505 {1, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
506 {powLimit, 600, 2 * (512 - 32) * 144, 0, powLimit, powLimit_nBits},
507 {1, 600, (512 - 64) * 144 * 600, 0, powLimit, powLimit_nBits},
509 {powLimit, 600, 300, 1, SINGLE_300_TARGET, 0x1d00ffb1},
511 {FUNNY_REF_TARGET, 600, 600 * 2 * 33 * 144, 0, powLimit,
514 {1, 600, 600 * 2 * 256 * 144, 0, powLimit, powLimit_nBits},
516 {1, 600, 600 * 2 * 224 * 144 - 1, 0,
arith_uint256(0xffff8) << 204,
520 for (
auto &v : calculate_args) {
522 parent_time_diff + v.timeDiff, v.heightDiff,
523 powLimit, nHalfLife);
527 parent_time_diff + v.timeDiff, v.heightDiff,
528 v.expectedTarget, v.expectednBits) +
529 strprintf(
"nextTarget= %s\nnext nBits= 0x%08x\n",
531 BOOST_CHECK_MESSAGE(nextTarget == v.expectedTarget &&
532 next_nBits == v.expectednBits,
540 int daaHeight,
int axionHeight)
542 BOOST_REQUIRE_GT(axionHeight, daaHeight);
556 const auto mainChainParams =
558 const int asertActivationHeight = 4000;
560 asertActivationHeight);
567 uint32_t initialBits = 0x1802a842;
571 std::vector<CBlockIndex> blocks(10000);
575 blocks[0].nHeight = 0;
576 blocks[0].nTime = 1269211443;
577 blocks[0].nBits = initialBits;
583 for (
int i = 1; i < asertActivationHeight - 145; i++) {
584 BOOST_REQUIRE(bidx <
int(blocks.size()));
585 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 600, initialBits);
590 for (
int i = 0; i < 145; i++) {
591 BOOST_REQUIRE(bidx <
int(blocks.size()));
592 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 500, initialBits);
595 CBlockIndex *pindexPreActivation = &blocks[bidx - 1];
647 uint32_t anchorBits2 = 0x180210fe;
656 uint32_t anchorBits3 = 0x18034567;
666 GetBlockIndex(&indexActivation3, -86400 * 90 + 2 * 600, anchorBits3);
668 &blkHeaderDummy, chainParams),
672 &blkHeaderDummy, chainParams),
690 for (
int i = 1; i < 1000; i++) {
691 BOOST_REQUIRE(bidx <
int(blocks.size()));
695 GetBlockIndex(pindexChain2, InsecureRandRange(1200), nextBits);
696 pindexChain2 = &blocks[bidx++];
700 for (
CBlockIndex *pindex = pindexChain2; pindex != &indexActivation2;
701 pindex = pindex->
pprev) {
708BOOST_AUTO_TEST_SUITE_END()
bool IsDAAEnabled(const Consensus::Params ¶ms, int nHeight)
static bool IsAxionEnabled(const Consensus::Params ¶ms, int32_t nHeight)
arith_uint256 UintToArith256(const uint256 &a)
arith_uint256 CalculateASERT(const arith_uint256 &refTarget, const int64_t nPowTargetSpacing, const int64_t nTimeDiff, const int64_t nHeightDiff, const arith_uint256 &powLimit, const int64_t nHalfLife) noexcept
uint32_t GetNextASERTWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params ¶ms) noexcept
static double GetASERTApproximationError(const CBlockIndex *pindexPrev, const uint32_t finalBits, const CBlockIndex *pindexAnchorBlock)
BOOST_AUTO_TEST_CASE(asert_difficulty_test)
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval, uint32_t nBits)
static std::string StrPrintCalcArgs(const arith_uint256 refTarget, const int64_t targetSpacing, const int64_t timeDiff, const int64_t heightDiff, const arith_uint256 expectedTarget, const uint32_t expectednBits)
static double TargetFromBits(const uint32_t nBits)
arith_uint256 GetBlockProof(const CBlockIndex &block)
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
std::string ChainTypeToString(ChainType chain)
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
void BuildSkip()
Build the skiplist pointer for this entry.
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
int64_t GetBlockTime() const
int64_t GetMedianTimePast() const
int nHeight
height of the entry in the chain. The genesis block has height 0
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const Consensus::Params & GetConsensus() const
Consensus::Params consensus
ChainParamsWithCustomActivation(const CChainParams &chainParams, int daaHeight, int axionHeight)
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
std::string ToString() const
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Parameters that influence chain consensus.
int axionHeight
Block height at which the axion activation becomes active.
int daaHeight
Block height at which the new DAA becomes active.
uint256 powLimit
Proof of work parameters.
int64_t nPowTargetSpacing