Changeset View
Changeset View
Standalone View
Standalone View
src/test/rtt_tests.cpp
| // Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| #include <policy/block/rtt.h> | #include <policy/block/rtt.h> | ||||
| #include <arith_uint256.h> | #include <arith_uint256.h> | ||||
| #include <avalanche/processor.h> | |||||
| #include <blockindex.h> | |||||
| #include <chainparams.h> | #include <chainparams.h> | ||||
| #include <common/args.h> | |||||
| #include <pow/pow.h> | |||||
| #include <random.h> | |||||
| #include <util/time.h> | #include <util/time.h> | ||||
| #include <validation.h> | |||||
| #include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
| #include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
| BOOST_FIXTURE_TEST_SUITE(rtt_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(rtt_tests, BasicTestingSetup) | ||||
| BOOST_AUTO_TEST_CASE(get_next_rtt_work_required) { | BOOST_AUTO_TEST_CASE(get_next_rtt_work_required) { | ||||
| ▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(get_next_rtt_work_required) { | ||||
| // difficulty than the pow limit | // difficulty than the pow limit | ||||
| for (int64_t t : {431, 600, 1000, 3600, 24 * 60 * 60, 365 * 24 * 60 * 60}) { | for (int64_t t : {431, 600, 1000, 3600, 24 * 60 * 60, 365 * 24 * 60 * 60}) { | ||||
| auto minWork = GetNextRTTWorkRequired(prevNBits, prevHeaderReceivedTime, | auto minWork = GetNextRTTWorkRequired(prevNBits, prevHeaderReceivedTime, | ||||
| now + t, consensusParams); | now + t, consensusParams); | ||||
| BOOST_CHECK(!minWork.has_value()); | BOOST_CHECK(!minWork.has_value()); | ||||
| } | } | ||||
| } | } | ||||
| BOOST_AUTO_TEST_CASE(rtt_policy) { | |||||
| const Consensus::Params &consensusParams = Params().GetConsensus(); | |||||
| auto checkRTTPolicy = [&](const CBlockIndex &blockIndex, bool expected) { | |||||
| BlockPolicyValidationState state; | |||||
| BOOST_CHECK_EQUAL(RTTPolicy(consensusParams, blockIndex)(state), | |||||
| expected); | |||||
| BOOST_CHECK_EQUAL(state.IsValid(), expected); | |||||
| if (!expected) { | |||||
| BOOST_CHECK_EQUAL(state.GetRejectReason(), "policy-bad-rtt"); | |||||
| } | |||||
| }; | |||||
| int64_t now = GetTime(); | |||||
| SetMockTime(now); | |||||
| CBlockIndex prev; | |||||
| arith_uint256 prevWork = UintToArith256(consensusParams.powLimit) >> 10; | |||||
| prev.nBits = prevWork.GetCompact(); | |||||
| prev.nTimeReceived = now; | |||||
| gArgs.ForceSetArg("-enablertt", "1"); | |||||
| CBlockIndex index; | |||||
| index.nTimeReceived = now + 100; | |||||
| index.pprev = &prev; | |||||
| BlockHash hash{ArithToUint256(prevWork)}; | |||||
| index.phashBlock = &hash; | |||||
| // Diff time is 0, same hash as before won't cut it | |||||
| checkRTTPolicy(index, false); | |||||
| // Policy is disabled | |||||
| gArgs.ForceSetArg("-enablertt", "0"); | |||||
| checkRTTPolicy(index, true); | |||||
| gArgs.ForceSetArg("-enablertt", "1"); | |||||
| checkRTTPolicy(index, false); | |||||
| // Prev block index is null, the policy doesn't apply | |||||
| index.pprev = nullptr; | |||||
| checkRTTPolicy(index, true); | |||||
| index.pprev = &prev; | |||||
| checkRTTPolicy(index, false); | |||||
| // Hash is low enough | |||||
| hash = BlockHash( | |||||
| ArithToUint256(UintToArith256(consensusParams.powLimit) >> 20)); | |||||
| checkRTTPolicy(index, true); | |||||
| hash = BlockHash{ArithToUint256(prevWork)}; | |||||
| checkRTTPolicy(index, false); | |||||
| // Difftime is large enough, same hash as previous block is acceptable | |||||
| for (int64_t t : {431, 600, 1000, 3600, 24 * 60 * 60, 365 * 24 * 60 * 60}) { | |||||
| index.nTimeReceived = now + t; | |||||
| checkRTTPolicy(index, true); | |||||
| } | |||||
| for (int64_t t : {-1, 0, 1, 10, 100, 430}) { | |||||
| index.nTimeReceived = now + t; | |||||
| checkRTTPolicy(index, false); | |||||
| } | |||||
| gArgs.ClearForcedArg("-enablertt"); | |||||
| } | |||||
| BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() | ||||