Changeset View
Changeset View
Standalone View
Standalone View
src/pow/test/aserti32d_tests.cpp
Show First 20 Lines • Show All 526 Lines • ▼ Show 20 Lines | for (auto &v : calculate_args) { | ||||
strprintf("nextTarget= %s\nnext nBits= 0x%08x\n", | strprintf("nextTarget= %s\nnext nBits= 0x%08x\n", | ||||
nextTarget.ToString(), next_nBits); | nextTarget.ToString(), next_nBits); | ||||
BOOST_CHECK_MESSAGE(nextTarget == v.expectedTarget && | BOOST_CHECK_MESSAGE(nextTarget == v.expectedTarget && | ||||
next_nBits == v.expectednBits, | next_nBits == v.expectednBits, | ||||
failMsg); | failMsg); | ||||
} | } | ||||
} | } | ||||
class ChainParamsWithDAAActivation : public CChainParams { | class ChainParamsWithCustomActivation : public CChainParams { | ||||
public: | public: | ||||
ChainParamsWithDAAActivation(const CChainParams &chainParams, int daaHeight) | ChainParamsWithCustomActivation(const CChainParams &chainParams, | ||||
int daaHeight, int axionHeight) | |||||
: CChainParams(chainParams) { | : CChainParams(chainParams) { | ||||
BOOST_REQUIRE_GT(axionHeight, daaHeight); | |||||
consensus.daaHeight = daaHeight; | consensus.daaHeight = daaHeight; | ||||
consensus.axionHeight = axionHeight; | |||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Test transition of cw144 to ASERT algorithm, which involves the selection | * Test transition of cw144 to ASERT algorithm, which involves the selection | ||||
* of an anchor block. | * of an anchor block. | ||||
*/ | */ | ||||
BOOST_AUTO_TEST_CASE(asert_activation_anchor_test) { | BOOST_AUTO_TEST_CASE(asert_activation_anchor_test) { | ||||
// Make a custom chain params based on mainnet, activating the cw144 DAA | // Make a custom chain params based on mainnet, activating the cw144 DAA | ||||
// at a lower height than usual, so we don't need to waste time making a | // at a lower height than usual, so we don't need to waste time making a | ||||
// 504000-long chain. | // 504000-long chain. | ||||
const auto mainChainParams = CreateChainParams(CBaseChainParams::MAIN); | const auto mainChainParams = CreateChainParams(CBaseChainParams::MAIN); | ||||
const ChainParamsWithDAAActivation chainParams(*mainChainParams, 2016); | const int asertActivationHeight = 4000; | ||||
const ChainParamsWithCustomActivation chainParams(*mainChainParams, 2016, | |||||
asertActivationHeight); | |||||
const Consensus::Params ¶ms = chainParams.GetConsensus(); | const Consensus::Params ¶ms = chainParams.GetConsensus(); | ||||
const int64_t activationTime = | |||||
gArgs.GetArg("-axionactivationtime", params.axionActivationTime); | |||||
CBlockHeader blkHeaderDummy; | CBlockHeader blkHeaderDummy; | ||||
// an arbitrary compact target for our chain (based on BCH chain ~ Aug 10 | // an arbitrary compact target for our chain (based on BCH chain ~ Aug 10 | ||||
// 2020). | // 2020). | ||||
uint32_t initialBits = 0x1802a842; | uint32_t initialBits = 0x1802a842; | ||||
// Block store for anonymous blocks; needs to be big enough to fit all | // Block store for anonymous blocks; needs to be big enough to fit all | ||||
// generated blocks in this test. | // generated blocks in this test. | ||||
std::vector<CBlockIndex> blocks(10000); | std::vector<CBlockIndex> blocks(10000); | ||||
int bidx = 1; | int bidx = 1; | ||||
// Genesis block. | // Genesis block. | ||||
blocks[0].nHeight = 0; | blocks[0].nHeight = 0; | ||||
blocks[0].nTime = 1269211443; | blocks[0].nTime = 1269211443; | ||||
blocks[0].nBits = initialBits; | blocks[0].nBits = initialBits; | ||||
blocks[0].nChainWork = GetBlockProof(blocks[0]); | blocks[0].nChainWork = GetBlockProof(blocks[0]); | ||||
// Pile up a random number of blocks to establish some history of random | // Pile up a random number of blocks to establish some history of random | ||||
// height. cw144 DAA requires us to have height at least 2016, dunno why | // height. cw144 DAA requires us to have height at least 2016, dunno why | ||||
// that much. | // that much. Keep going up to 145 blocks prior to ASERT activation. | ||||
const int initialBlockCount = 2000 + int(InsecureRandRange(1000)); | for (int i = 1; i < asertActivationHeight - 145; i++) { | ||||
for (int i = 1; i < initialBlockCount; i++) { | |||||
blocks[bidx] = GetBlockIndex(&blocks[bidx - 1], 600, initialBits); | |||||
bidx++; | |||||
BOOST_REQUIRE(bidx < int(blocks.size())); | BOOST_REQUIRE(bidx < int(blocks.size())); | ||||
} | |||||
// Start making blocks prior to activation. First, make a block about 1 day | |||||
// before activation. Then put down 145 more blocks with 500 second | |||||
// solvetime each, such that the MTP on the final block is 1 second short of | |||||
// activationTime. | |||||
{ | |||||
blocks[bidx] = GetBlockIndex(&blocks[bidx - 1], 600, initialBits); | blocks[bidx] = GetBlockIndex(&blocks[bidx - 1], 600, initialBits); | ||||
blocks[bidx].nTime = activationTime - 140 * 500 - 1; | |||||
bidx++; | bidx++; | ||||
} | } | ||||
// Then put down 145 more blocks with 500 second solvetime each, such that | |||||
// the final block is the one prior to activation. | |||||
for (int i = 0; i < 145; i++) { | for (int i = 0; i < 145; i++) { | ||||
BOOST_REQUIRE(bidx < int(blocks.size())); | BOOST_REQUIRE(bidx < int(blocks.size())); | ||||
blocks[bidx] = GetBlockIndex(&blocks[bidx - 1], 500, initialBits); | blocks[bidx] = GetBlockIndex(&blocks[bidx - 1], 500, initialBits); | ||||
bidx++; | bidx++; | ||||
} | } | ||||
CBlockIndex *pindexPreActivation = &blocks[bidx - 1]; | CBlockIndex *pindexPreActivation = &blocks[bidx - 1]; | ||||
BOOST_CHECK_EQUAL(pindexPreActivation->nTime, activationTime + 5 * 500 - 1); | BOOST_CHECK_EQUAL(pindexPreActivation->nHeight, asertActivationHeight - 1); | ||||
BOOST_CHECK_EQUAL(pindexPreActivation->GetMedianTimePast(), | |||||
activationTime - 1); | |||||
BOOST_CHECK(IsDAAEnabled(params, pindexPreActivation)); | BOOST_CHECK(IsDAAEnabled(params, pindexPreActivation)); | ||||
// If we consult DAA, then it uses cw144 which returns a significantly lower | // If we consult DAA, then it uses cw144 which returns a significantly lower | ||||
// target because we have been mining too fast by a ratio 600/500 for a | // target because we have been mining too fast by a ratio 600/500 for a | ||||
// whole day. | // whole day. | ||||
BOOST_CHECK(!IsAxionEnabled(params, pindexPreActivation)); | BOOST_CHECK(!IsAxionEnabled(params, pindexPreActivation)); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL( | ||||
GetNextWorkRequired(pindexPreActivation, &blkHeaderDummy, chainParams), | GetNextWorkRequired(pindexPreActivation, &blkHeaderDummy, chainParams), | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | BOOST_CHECK_EQUAL(GetNextWorkRequired(&indexActivation3_return, | ||||
&blkHeaderDummy, chainParams), | &blkHeaderDummy, chainParams), | ||||
anchorBits3); | anchorBits3); | ||||
BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation3); | BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation3); | ||||
// Make an activation with MTP == activation exactly. This is a backwards | // Make an activation with MTP == activation exactly. This is a backwards | ||||
// timestamp jump so the resulting target is 1.2% lower. | // timestamp jump so the resulting target is 1.2% lower. | ||||
CBlockIndex indexActivation4 = | CBlockIndex indexActivation4 = | ||||
GetBlockIndex(pindexPreActivation, 0, 0x18011111); | GetBlockIndex(pindexPreActivation, 0, 0x18011111); | ||||
indexActivation4.nTime = activationTime; | indexActivation4.nTime = pindexPreActivation->GetMedianTimePast(); | ||||
BOOST_CHECK_EQUAL(indexActivation4.GetMedianTimePast(), activationTime); | |||||
BOOST_CHECK(IsAxionEnabled(params, &indexActivation4)); | BOOST_CHECK(IsAxionEnabled(params, &indexActivation4)); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL( | ||||
GetNextWorkRequired(&indexActivation4, &blkHeaderDummy, chainParams), | GetNextWorkRequired(&indexActivation4, &blkHeaderDummy, chainParams), | ||||
0x18010db3); | 0x18010db3); | ||||
BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation4); | BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation4); | ||||
// Finally create a random chain on top of our second activation, using | // Finally create a random chain on top of our second activation, using | ||||
// ASERT targets all the way. Erase cache so that this will do a fresh | // ASERT targets all the way. Erase cache so that this will do a fresh | ||||
Show All 25 Lines |