diff --git a/src/pow/aserti32d.h b/src/pow/aserti32d.h --- a/src/pow/aserti32d.h +++ b/src/pow/aserti32d.h @@ -39,4 +39,6 @@ */ void ResetASERTAnchorBlockCache() noexcept; +const CBlockIndex *GetASERTAnchorBlockCache() noexcept; + #endif // BITCOIN_POW_ASERTI32D_H diff --git a/src/pow/aserti32d.cpp b/src/pow/aserti32d.cpp --- a/src/pow/aserti32d.cpp +++ b/src/pow/aserti32d.cpp @@ -16,6 +16,10 @@ cachedAnchor = nullptr; } +const CBlockIndex *GetASERTAnchorBlockCache() noexcept { + return cachedAnchor.load(); +} + /** * Returns a pointer to the anchor block used for ASERT. * As anchor we use the first block for which IsAxionEnabled() returns true. diff --git a/src/pow/test/aserti32d_tests.cpp b/src/pow/test/aserti32d_tests.cpp --- a/src/pow/test/aserti32d_tests.cpp +++ b/src/pow/test/aserti32d_tests.cpp @@ -508,6 +508,11 @@ // confuses any attempt to detect overflow by inspecting result {FUNNY_REF_TARGET, 600, 600 * 2 * 33 * 144, 0, powLimit, powLimit_nBits}, + // overflow to exactly 2^256 + {1, 600, 600 * 2 * 256 * 144, 0, powLimit, powLimit_nBits}, + // just under powlimit (not clamped) yet over powlimit_nbits + {1, 600, 600 * 2 * 224 * 144 - 1, 0, arith_uint256(0xffff8) << 204, + powLimit_nBits}, }; for (auto &v : calculate_args) { @@ -604,6 +609,9 @@ GetNextWorkRequired(pindexPreActivation, &blkHeaderDummy, chainParams), 0x180236e1); + // ASERT has never run yet, so cache is unpopulated. + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), nullptr); + /** * Now we'll try adding on blocks to activate ASERT. The activation block * is going to be our anchor block. We will make several distinct anchor @@ -620,6 +628,7 @@ GetNextWorkRequired(&indexActivation0, &blkHeaderDummy, chainParams), 0x180236e1); // second call will have used anchor cache, shouldn't change anything + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation0); BOOST_CHECK_EQUAL( GetNextWorkRequired(&indexActivation0, &blkHeaderDummy, chainParams), 0x180236e1); @@ -638,6 +647,7 @@ GetNextWorkRequired(&indexActivation1, &blkHeaderDummy, chainParams), 0x180232fd); // second call will have used anchor cache, shouldn't change anything + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation1); BOOST_CHECK_EQUAL( GetNextWorkRequired(&indexActivation1, &blkHeaderDummy, chainParams), 0x180232fd); @@ -646,6 +656,7 @@ BOOST_CHECK_EQUAL( GetNextWorkRequired(&indexActivation1, &blkHeaderDummy, chainParams), 0x180232fd); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation1); // Try activation with expected solvetime, which will keep target the same. uint32_t anchorBits2 = 0x180210fe; @@ -655,6 +666,7 @@ BOOST_CHECK_EQUAL( GetNextWorkRequired(&indexActivation2, &blkHeaderDummy, chainParams), anchorBits2); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation2); // Try a three-month solvetime which will cause us to hit powLimit. uint32_t anchorBits3 = 0x18034567; @@ -675,6 +687,7 @@ BOOST_CHECK_EQUAL(GetNextWorkRequired(&indexActivation3_return, &blkHeaderDummy, chainParams), anchorBits3); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation3); // Make an activation with MTP == activation exactly. This is a backwards // timestamp jump so the resulting target is 1.2% lower. @@ -686,6 +699,7 @@ BOOST_CHECK_EQUAL( GetNextWorkRequired(&indexActivation4, &blkHeaderDummy, chainParams), 0x18010db3); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation4); // 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 @@ -697,6 +711,7 @@ ResetASERTAnchorBlockCache(); uint32_t nextBits = GetNextWorkRequired(pindexChain2, &blkHeaderDummy, chainParams); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation2); blocks[bidx] = GetBlockIndex(pindexChain2, InsecureRandRange(1200), nextBits); pindexChain2 = &blocks[bidx++]; @@ -708,6 +723,7 @@ uint32_t nextBits = GetNextWorkRequired(pindex->pprev, &blkHeaderDummy, chainParams); BOOST_CHECK_EQUAL(nextBits, pindex->nBits); + BOOST_CHECK_EQUAL(GetASERTAnchorBlockCache(), &indexActivation2); } }