13 #include <test/util/setup_common.h>
15 #include <boost/test/unit_test.hpp>
24 block.
pprev = pindexPrev;
25 block.
nHeight = pindexPrev->nHeight + 1;
26 block.
nTime = pindexPrev->nTime + nTimeInterval;
35 return (nBits & 0xff'ff'ff) * pow(256, (nBits >> 24) - 3);
39 const uint32_t finalBits,
41 const int64_t nHeightDiff =
43 const int64_t nTimeDiff =
45 const uint32_t initialBits = pindexAnchorBlock->
nBits;
52 double(nTimeDiff - (nHeightDiff + 1) * 600) / double(2 * 24 * 3600);
53 double dTarget = dInitialPow * pow(2, dExponent);
55 return (dFinalPow - dTarget) / dTarget;
61 std::vector<CBlockIndex> blocks(3000 + 2 * 24 * 3600);
66 uint32_t initialBits = currentPow.
GetCompact();
67 double dMaxErr = 0.0001166792656486;
71 blocks[0].nHeight = 0;
72 blocks[0].nTime = 1269211443;
75 blocks[0].nBits = 0x0dedbeef;
92 &blocks[1])) < dMaxErr);
102 &blocks[1])) < dMaxErr);
108 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 172800, nBits);
117 &blocks[1])) < dMaxErr);
120 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 172800, nBits);
126 &blocks[1])) < dMaxErr);
129 for (; i < 150; i++) {
140 for (
size_t j = 0; j < 10; i++, j++) {
154 &blocks[1])) < dMaxErr);
157 &blocks[i - 2])) < dMaxErr);
163 &blocks[1])) < dMaxErr);
166 &blocks[i - 2])) < dMaxErr);
176 &blocks[1])) < dMaxErr);
179 &blocks[i - 2])) < dMaxErr);
185 &blocks[1])) < dMaxErr);
188 &blocks[i - 2])) < dMaxErr);
193 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
198 &blocks[1])) < dMaxErr);
201 &blocks[i - 2])) < dMaxErr);
206 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
210 &blocks[i - 1], nBits, &blocks[1])) < dMaxErr);
217 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 - 2 * 24 * 3600, nBits);
222 &blocks[1])) < dMaxErr);
225 &blocks[i - 2])) < dMaxErr);
233 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
238 &blocks[1])) < dMaxErr);
241 &blocks[i - 2])) < dMaxErr);
243 blocks[i] =
GetBlockIndex(&blocks[i - 1], 600 + 2 * 24 * 3600, nBits);
248 &blocks[1])) < dMaxErr);
251 &blocks[i - 2])) < dMaxErr);
272 uint32_t nBitsRingBuffer[8];
274 blocks[i] =
GetBlockIndex(&blocks[i - 1], -2 * 24 * 3600 - 30, nBits);
275 for (
size_t j = 0; j < 4 * 24 * 3600 + 660; j++) {
293 dMaxStep = std::max(dMaxStep, dStep);
297 nBitsRingBuffer[j % 8] = nBits;
302 dMin = std::min(dMin, dErr);
303 dMax = std::max(dMax, dErr);
304 dRelMin = std::min(dRelMin, dRelErr);
305 dRelMax = std::max(dRelMax, dRelErr);
307 fabs(dErr) < dMaxErr,
309 "solveTime: %d\tStep size: %.8f%%\tdErr: %.8f%%\tnBits: %0x\n",
310 int64_t(blocks[i].nTime) - blocks[i - 1].nTime, dStep * 100,
313 fabs(dRelErr) < dMaxErr,
314 strprintf(
"solveTime: %d\tStep size: %.8f%%\tdRelErr: "
315 "%.8f%%\tnBits: %0x\n",
316 int64_t(blocks[i].nTime) - blocks[i - 1].nTime,
317 dStep * 100, dRelErr * 100, nBits));
320 "Min error: %16.14f%%\tMax error: %16.14f%%\tMax step: %16.14f%%\n",
321 dMin * 100, dMax * 100, dMaxStep * 100);
323 dMin < -0.0001013168981059 && dMin > -0.0001013168981060 &&
324 dMax > 0.0001166792656485 && dMax < 0.0001166792656486,
326 failMsg =
strprintf(
"Min relError: %16.14f%%\tMax relError: %16.14f%%\n",
327 dRelMin * 100, dRelMax * 100);
329 dRelMin < -0.0001013168981059 && dRelMin > -0.0001013168981060 &&
330 dRelMax > 0.0001166792656485 && dRelMax < 0.0001166792656486,
334 for (
size_t j = 0; j < 100; i++, j++) {
353 const int64_t targetSpacing,
354 const int64_t timeDiff,
355 const int64_t heightDiff,
357 const uint32_t expectednBits) {
364 "exp nBits= 0x%08x\n",
365 refTarget.
ToString(), targetSpacing, timeDiff, heightDiff,
366 expectedTarget.
ToString(), expectednBits);
385 static const int64_t parent_time_diff = 600;
390 parent_time_diff + 600 , ++height, powLimit, nHalfLife);
395 parent_time_diff + 600 + 300, ++height,
396 powLimit, nHalfLife);
403 parent_time_diff + 600 + 300 + 900, ++height,
404 powLimit, nHalfLife);
410 prevTarget = nextTarget;
413 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
417 prevTarget = nextTarget;
420 parent_time_diff + 288 * 0, 288, powLimit, nHalfLife);
425 uint32_t powLimit_nBits = powLimit.
GetCompact();
427 for (
size_t k = 0; k < 3; k++) {
428 prevTarget = nextTarget;
430 parent_time_diff + 288 * 1200, 288,
431 powLimit, nHalfLife);
438 prevTarget = nextTarget;
441 parent_time_diff + 288 * 1200, 288, powLimit, nHalfLife);
451 parent_time_diff + 512 * 144 * 600, 0, powLimit,
460 2 * (256 - 33) * 144, powLimit, nHalfLife);
469 int64_t targetSpacing;
473 uint32_t expectednBits;
478 "00000000ffb1ffffffffffffffffffffffffffffffffffffffffffffffffffff"};
480 "000000008000000000000000000fffffffffffffffffffffffffffffffffffff"};
485 const std::vector<calc_params> calculate_args = {
490 {powLimit, 600, 0, 2 * 144, powLimit >> 1, 0x1c7fffff},
491 {powLimit, 600, 0, 4 * 144, powLimit >> 2, 0x1c3fffff},
492 {powLimit >> 1, 600, 0, 2 * 144, powLimit >> 2, 0x1c3fffff},
493 {powLimit >> 2, 600, 0, 2 * 144, powLimit >> 3, 0x1c1fffff},
494 {powLimit >> 3, 600, 0, 2 * 144, powLimit >> 4, 0x1c0fffff},
495 {powLimit, 600, 0, 2 * (256 - 34) * 144, 3, 0x01030000},
496 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 119, 3, 0x01030000},
497 {powLimit, 600, 0, 2 * (256 - 34) * 144 + 120, 2, 0x01020000},
498 {powLimit, 600, 0, 2 * (256 - 33) * 144 - 1, 2, 0x01020000},
500 {powLimit, 600, 0, 2 * (256 - 33) * 144, 1, 0x01010000},
502 {powLimit, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
503 {1, 600, 0, 2 * (256 - 32) * 144, 1, 0x01010000},
504 {powLimit, 600, 2 * (512 - 32) * 144, 0, powLimit, powLimit_nBits},
505 {1, 600, (512 - 64) * 144 * 600, 0, powLimit, powLimit_nBits},
507 {powLimit, 600, 300, 1, SINGLE_300_TARGET, 0x1d00ffb1},
509 {FUNNY_REF_TARGET, 600, 600 * 2 * 33 * 144, 0, powLimit,
512 {1, 600, 600 * 2 * 256 * 144, 0, powLimit, powLimit_nBits},
514 {1, 600, 600 * 2 * 224 * 144 - 1, 0,
arith_uint256(0xffff8) << 204,
518 for (
auto &v : calculate_args) {
520 parent_time_diff + v.timeDiff, v.heightDiff,
521 powLimit, nHalfLife);
525 parent_time_diff + v.timeDiff, v.heightDiff,
526 v.expectedTarget, v.expectednBits) +
527 strprintf(
"nextTarget= %s\nnext nBits= 0x%08x\n",
529 BOOST_CHECK_MESSAGE(nextTarget == v.expectedTarget &&
530 next_nBits == v.expectednBits,
538 int daaHeight,
int axionHeight)
540 BOOST_REQUIRE_GT(axionHeight, daaHeight);
555 const int asertActivationHeight = 4000;
557 asertActivationHeight);
564 uint32_t initialBits = 0x1802a842;
568 std::vector<CBlockIndex> blocks(10000);
572 blocks[0].nHeight = 0;
573 blocks[0].nTime = 1269211443;
574 blocks[0].nBits = initialBits;
580 for (
int i = 1; i < asertActivationHeight - 145; i++) {
581 BOOST_REQUIRE(bidx <
int(blocks.size()));
582 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 600, initialBits);
587 for (
int i = 0; i < 145; i++) {
588 BOOST_REQUIRE(bidx <
int(blocks.size()));
589 blocks[bidx] =
GetBlockIndex(&blocks[bidx - 1], 500, initialBits);
592 CBlockIndex *pindexPreActivation = &blocks[bidx - 1];
644 uint32_t anchorBits2 = 0x180210fe;
653 uint32_t anchorBits3 = 0x18034567;
663 GetBlockIndex(&indexActivation3, -86400 * 90 + 2 * 600, anchorBits3);
665 &blkHeaderDummy, chainParams),
669 &blkHeaderDummy, chainParams),
687 for (
int i = 1; i < 1000; i++) {
688 BOOST_REQUIRE(bidx <
int(blocks.size()));
692 GetBlockIndex(pindexChain2, InsecureRandRange(1200), nextBits);
693 pindexChain2 = &blocks[bidx++];
697 for (
CBlockIndex *pindex = pindexChain2; pindex != &indexActivation2;
698 pindex = pindex->
pprev) {
static bool IsAxionEnabled(const Consensus::Params ¶ms, int32_t nHeight)
static bool IsDAAEnabled(const Consensus::Params ¶ms, int 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< CChainParams > CreateChainParams(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.
Consensus::Params consensus
const Consensus::Params & GetConsensus() const
ChainParamsWithCustomActivation(const CChainParams &chainParams, int daaHeight, int axionHeight)
const CChainParams & GetChainParams() const override
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_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
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