8#include <chainparams.h>
13#include <test/util/random.h>
14#include <test/util/setup_common.h>
16#include <boost/test/unit_test.hpp>
20BOOST_FIXTURE_TEST_SUITE(aserti32d_tests, BasicTestingSetup)
25 block.
pprev = pindexPrev;
26 block.
nHeight = pindexPrev->nHeight + 1;
27 block.
nTime = pindexPrev->nTime + nTimeInterval;
36 return (nBits & 0xff'ff'ff) * pow(256, (nBits >> 24) - 3);
40 const uint32_t finalBits,
42 const int64_t nHeightDiff =
44 const int64_t nTimeDiff =
46 const uint32_t initialBits = pindexAnchorBlock->
nBits;
53 double(nTimeDiff - (nHeightDiff + 1) * 600) / double(2 * 24 * 3600);
54 double dTarget = dInitialPow * pow(2, dExponent);
56 return (dFinalPow - dTarget) / dTarget;
62 std::vector<CBlockIndex> blocks(3000 + 2 * 24 * 3600);
67 uint32_t initialBits = currentPow.
GetCompact();
68 double dMaxErr = 0.0001166792656486;
72 blocks[0].nHeight = 0;
73 blocks[0].nTime = 1269211443;
76 blocks[0].nBits = 0x0dedbeef;
93 &blocks[1])) < dMaxErr);
103 &blocks[1])) < dMaxErr);
109 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 172800, nBits);
118 &blocks[1])) < dMaxErr);
121 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 172800, nBits);
127 &blocks[1])) < dMaxErr);
130 for (; i < 150; i++) {
141 for (
size_t j = 0; j < 10; i++, j++) {
155 &blocks[1])) < dMaxErr);
158 &blocks[i - 2])) < dMaxErr);
164 &blocks[1])) < dMaxErr);
167 &blocks[i - 2])) < dMaxErr);
177 &blocks[1])) < dMaxErr);
180 &blocks[i - 2])) < dMaxErr);
186 &blocks[1])) < dMaxErr);
189 &blocks[i - 2])) < dMaxErr);
194 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
199 &blocks[1])) < dMaxErr);
202 &blocks[i - 2])) < dMaxErr);
207 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
211 &blocks[i - 1], nBits, &blocks[1])) < dMaxErr);
218 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
223 &blocks[1])) < dMaxErr);
226 &blocks[i - 2])) < dMaxErr);
234 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
239 &blocks[1])) < dMaxErr);
242 &blocks[i - 2])) < dMaxErr);
244 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
249 &blocks[1])) < dMaxErr);
252 &blocks[i - 2])) < dMaxErr);
273 uint32_t nBitsRingBuffer[8];
275 blocks[i] =
GetBlockIndex(&blocks[i - 1], -2 * 24 * 3600 - 30, nBits);
276 for (
size_t j = 0; j < 4 * 24 * 3600 + 660; j++) {
294 dMaxStep = std::max(dMaxStep, dStep);
298 nBitsRingBuffer[j % 8] = nBits;
303 dMin = std::min(dMin, dErr);
304 dMax = std::max(dMax, dErr);
305 dRelMin = std::min(dRelMin, dRelErr);
306 dRelMax = std::max(dRelMax, dRelErr);
308 fabs(dErr) < dMaxErr,
310 "solveTime: %d\tStep size: %.8f%%\tdErr: %.8f%%\tnBits: %0x\n",
311 int64_t(blocks[i].nTime) - blocks[i - 1].nTime, dStep * 100,
314 fabs(dRelErr) < dMaxErr,
315 strprintf(
"solveTime: %d\tStep size: %.8f%%\tdRelErr: "
316 "%.8f%%\tnBits: %0x\n",
317 int64_t(blocks[i].nTime) - blocks[i - 1].nTime,
318 dStep * 100, dRelErr * 100, nBits));
321 "Min error: %16.14f%%\tMax error: %16.14f%%\tMax step: %16.14f%%\n",
322 dMin * 100, dMax * 100, dMaxStep * 100);
324 dMin < -0.0001013168981059 && dMin > -0.0001013168981060 &&
325 dMax > 0.0001166792656485 && dMax < 0.0001166792656486,
327 failMsg =
strprintf(
"Min relError: %16.14f%%\tMax relError: %16.14f%%\n",
328 dRelMin * 100, dRelMax * 100);
330 dRelMin < -0.0001013168981059 && dRelMin > -0.0001013168981060 &&
331 dRelMax > 0.0001166792656485 && dRelMax < 0.0001166792656486,
335 for (
size_t j = 0; j < 100; i++, j++) {
354 const int64_t targetSpacing,
355 const int64_t timeDiff,
356 const int64_t heightDiff,
358 const uint32_t expectednBits) {
365 "exp nBits= 0x%08x\n",
366 refTarget.
ToString(), targetSpacing, timeDiff, heightDiff,
367 expectedTarget.
ToString(), expectednBits);
386 static const int64_t parent_time_diff = 600;
391 parent_time_diff + 600 , ++height, powLimit, nHalfLife);
396 parent_time_diff + 600 + 300, ++height,
397 powLimit, nHalfLife);
404 parent_time_diff + 600 + 300 + 900, ++height,
405 powLimit, nHalfLife);
411 prevTarget = nextTarget;
414 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
418 prevTarget = nextTarget;
421 parent_time_diff + 288 * 0, 288, powLimit, nHalfLife);
426 uint32_t powLimit_nBits = powLimit.
GetCompact();
428 for (
size_t k = 0; k < 3; k++) {
429 prevTarget = nextTarget;
431 parent_time_diff + 288 * 1200, 288,
432 powLimit, nHalfLife);
439 prevTarget = nextTarget;
442 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
452 parent_time_diff + 512 * 144 * 600, 0, powLimit,
461 2 * (256 - 33) * 144, powLimit, nHalfLife);
470 int64_t targetSpacing;
474 uint32_t expectednBits;
479 "00000000ffb1ffffffffffffffffffffffffffffffffffffffffffffffffffff"};
481 "000000008000000000000000000fffffffffffffffffffffffffffffffffffff"};
486 const std::vector<calc_params> calculate_args = {
491 {powLimit, 600, 0, 2 * 144, powLimit >> 1, 0x1c7fffff},
492 {powLimit, 600, 0, 4 * 144, powLimit >> 2, 0x1c3fffff},
493 {powLimit >> 1, 600, 0, 2 * 144, powLimit >> 2, 0x1c3fffff},
494 {powLimit >> 2, 600, 0, 2 * 144, powLimit >> 3, 0x1c1fffff},
495 {powLimit >> 3, 600, 0, 2 * 144, powLimit >> 4, 0x1c0fffff},
496 {powLimit, 600, 0, 2 * (256 - 34) * 144, 3, 0x01030000},
497 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 119, 3, 0x01030000},
498 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 120, 2, 0x01020000},
499 {powLimit, 600, 0, 2 * (256 - 33) * 144 - 1, 2, 0x01020000},
501 {powLimit, 600, 0, 2 * (256 - 33) * 144, 1, 0x01010000},
503 {powLimit, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
504 {1, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
505 {powLimit, 600, 2 * (512 - 32) * 144, 0, powLimit, powLimit_nBits},
506 {1, 600, (512 - 64) * 144 * 600, 0, powLimit, powLimit_nBits},
508 {powLimit, 600, 300, 1, SINGLE_300_TARGET, 0x1d00ffb1},
510 {FUNNY_REF_TARGET, 600, 600 * 2 * 33 * 144, 0, powLimit,
513 {1, 600, 600 * 2 * 256 * 144, 0, powLimit, powLimit_nBits},
515 {1, 600, 600 * 2 * 224 * 144 - 1, 0,
arith_uint256(0xffff8) << 204,
519 for (
auto &v : calculate_args) {
521 parent_time_diff + v.timeDiff, v.heightDiff,
522 powLimit, nHalfLife);
526 parent_time_diff + v.timeDiff, v.heightDiff,
527 v.expectedTarget, v.expectednBits) +
528 strprintf(
"nextTarget= %s\nnext nBits= 0x%08x\n",
530 BOOST_CHECK_MESSAGE(nextTarget == v.expectedTarget &&
531 next_nBits == v.expectednBits,
539 int daaHeight,
int axionHeight)
541 BOOST_REQUIRE_GT(axionHeight, daaHeight);
555 const auto mainChainParams =
557 const int asertActivationHeight = 4000;
559 asertActivationHeight);
566 uint32_t initialBits = 0x1802a842;
570 std::vector<CBlockIndex> blocks(10000);
574 blocks[0].nHeight = 0;
575 blocks[0].nTime = 1269211443;
576 blocks[0].nBits = initialBits;
582 for (
int i = 1; i < asertActivationHeight - 145; i++) {
583 BOOST_REQUIRE(bidx <
int(blocks.size()));
584 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 600, initialBits);
589 for (
int i = 0; i < 145; i++) {
590 BOOST_REQUIRE(bidx <
int(blocks.size()));
591 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 500, initialBits);
594 CBlockIndex *pindexPreActivation = &blocks[bidx - 1];
646 uint32_t anchorBits2 = 0x180210fe;
655 uint32_t anchorBits3 = 0x18034567;
665 GetBlockIndex(&indexActivation3, -86400 * 90 + 2 * 600, anchorBits3);
667 &blkHeaderDummy, chainParams),
671 &blkHeaderDummy, chainParams),
689 for (
int i = 1; i < 1000; i++) {
690 BOOST_REQUIRE(bidx <
int(blocks.size()));
694 GetBlockIndex(pindexChain2, InsecureRandRange(1200), nextBits);
695 pindexChain2 = &blocks[bidx++];
699 for (
CBlockIndex *pindex = pindexChain2; pindex != &indexActivation2;
700 pindex = pindex->
pprev) {
707BOOST_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 std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
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