From 63879f0a4760c0c0f784029849cb5d21ee088abb Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 27 Mar 2021 20:17:56 +1000 Subject: tests: pull ComputeBlockVersion test into its own function The intent here is to allow checking ComputeBlockVersion behaviour with each deployment, rather than only testdummy on mainnet. This commit does the trivial refactoring component of that change. --- src/test/versionbits_tests.cpp | 74 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'src/test') diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 8841a540f2..b09827ffe5 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -242,17 +242,15 @@ BOOST_AUTO_TEST_CASE(versionbits_test) } } -BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) +/** Check that ComputeBlockVersion will set the appropriate bit correctly */ +static void check_computeblockversion(const Consensus::Params& params, Consensus::DeploymentPos dep) { - // Check that ComputeBlockVersion will set the appropriate bit correctly - // on mainnet. - const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); - const Consensus::Params &mainnetParams = chainParams->GetConsensus(); + // This implicitly uses versionbitscache, so clear it every time + versionbitscache.Clear(); - // Use the TESTDUMMY deployment for testing purposes. - int64_t bit = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit; - int64_t nStartTime = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime; - int64_t nTimeout = mainnetParams.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout; + int64_t bit = params.vDeployments[dep].bit; + int64_t nStartTime = params.vDeployments[dep].nStartTime; + int64_t nTimeout = params.vDeployments[dep].nTimeout; assert(nStartTime < nTimeout); @@ -267,40 +265,40 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) // Before MedianTimePast of the chain has crossed nStartTime, the bit // should not be set. CBlockIndex *lastBlock = nullptr; - lastBlock = firstChain.Mine(mainnetParams.nMinerConfirmationWindow, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); - BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1< 0) { lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); - BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY); +} BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3 From 593274445004506c921d5d851361aefb3434d744 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sun, 28 Mar 2021 03:10:48 +1000 Subject: tests: test ComputeBlockVersion for all deployments This generalises the ComputeBlockVersion test so that it can apply to any activation parameters we might set, and checks all the parameters set for each deployment on each chain, to simultaneously ensure that the deployments we have configured work sensibly, and that the test code does not suffer bitrot in the event that all interesting deployments are buried. --- src/test/versionbits_tests.cpp | 106 +++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 35 deletions(-) (limited to 'src/test') diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index b09827ffe5..7e96ef923e 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -252,38 +252,61 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus int64_t nStartTime = params.vDeployments[dep].nStartTime; int64_t nTimeout = params.vDeployments[dep].nTimeout; - assert(nStartTime < nTimeout); + // should not be any signalling for first block + BOOST_CHECK_EQUAL(ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS); + + // always active deployments shouldn't need to be tested further + if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE) return; + + BOOST_REQUIRE(nStartTime < nTimeout); + BOOST_REQUIRE(nStartTime >= 0); + BOOST_REQUIRE(nTimeout <= std::numeric_limits::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT); + BOOST_REQUIRE(0 <= bit && bit < 32); + BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0); // In the first chain, test that the bit is set by CBV until it has failed. // In the second chain, test the bit is set by CBV while STARTED and // LOCKED-IN, and then no longer set while ACTIVE. VersionBitsTester firstChain, secondChain; - // Start generating blocks before nStartTime - int64_t nTime = nStartTime - 1; + int64_t nTime = nStartTime; - // Before MedianTimePast of the chain has crossed nStartTime, the bit - // should not be set. CBlockIndex *lastBlock = nullptr; - lastBlock = firstChain.Mine(params.nMinerConfirmationWindow, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); - BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, params) & (1<GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY); + // check that any deployment on any chain can conceivably reach both + // ACTIVE and FAILED states in roughly the way we expect + for (const auto& chain_name : {CBaseChainParams::MAIN, CBaseChainParams::TESTNET, CBaseChainParams::SIGNET, CBaseChainParams::REGTEST}) { + const auto chainParams = CreateChainParams(*m_node.args, chain_name); + for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++i) { + check_computeblockversion(chainParams->GetConsensus(), static_cast(i)); + } + } } BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3 From 9e6b65f6fa205eee5c3b99343988adcb8d320460 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sun, 28 Mar 2021 00:07:49 +1000 Subject: tests: clean up versionbits test Simplify the versionbits unit test slightly to make the next set of changes a little easier to follow. --- src/test/versionbits_tests.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src/test') diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 7e96ef923e..b42aaff9ad 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -100,7 +100,7 @@ public: while (vpblock.size() < height) { CBlockIndex* pindex = new CBlockIndex(); pindex->nHeight = vpblock.size(); - pindex->pprev = vpblock.size() > 0 ? vpblock.back() : nullptr; + pindex->pprev = Tip(); pindex->nTime = nTime; pindex->nVersion = nVersion; pindex->BuildSkip(); @@ -110,13 +110,14 @@ public: } VersionBitsTester& TestStateSinceHeight(int height) { + const CBlockIndex* tip = Tip(); for (int i = 0; i < CHECKERS; i++) { if (InsecureRandBits(i) == 0) { - BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num)); - BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == 0, strprintf("Test %i for StateSinceHeight (always active)", num)); + BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(tip) == height, strprintf("Test %i for StateSinceHeight", num)); + BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (always active)", num)); // never active may go from DEFINED -> FAILED at the first period - const auto never_height = checker_never[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()); + const auto never_height = checker_never[i].GetStateSinceHeightFor(tip); BOOST_CHECK_MESSAGE(never_height == 0 || never_height == checker_never[i].Period(paramsDummy), strprintf("Test %i for StateSinceHeight (never active)", num)); } } @@ -125,9 +126,9 @@ public: } VersionBitsTester& TestState(ThresholdState exp) { + const CBlockIndex* pindex = Tip(); for (int i = 0; i < CHECKERS; i++) { if (InsecureRandBits(i) == 0) { - const CBlockIndex* pindex = vpblock.empty() ? nullptr : vpblock.back(); ThresholdState got = checker[i].GetStateFor(pindex); ThresholdState got_always = checker_always[i].GetStateFor(pindex); ThresholdState got_never = checker_never[i].GetStateFor(pindex); @@ -149,7 +150,7 @@ public: VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); } VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); } - CBlockIndex * Tip() { return vpblock.size() ? vpblock.back() : nullptr; } + CBlockIndex* Tip() { return vpblock.empty() ? nullptr : vpblock.back(); } }; BOOST_FIXTURE_TEST_SUITE(versionbits_tests, TestingSetup) @@ -217,7 +218,10 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000) .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000); } +} +BOOST_AUTO_TEST_CASE(versionbits_sanity) +{ // Sanity checks of version bit deployments const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN); const Consensus::Params &mainnetParams = chainParams->GetConsensus(); @@ -271,7 +275,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus int64_t nTime = nStartTime; - CBlockIndex *lastBlock = nullptr; + const CBlockIndex *lastBlock = nullptr; // Before MedianTimePast of the chain has crossed nStartTime, the bit // should not be set. -- cgit v1.2.3 From 73d4a706393e6dbd6b6d6b6428f8d3233ac0a2d8 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 6 Mar 2021 18:18:49 +1000 Subject: versionbits: Add support for delayed activation --- src/test/versionbits_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/test') diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index b42aaff9ad..ae0d47b2b4 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -255,6 +255,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus int64_t bit = params.vDeployments[dep].bit; int64_t nStartTime = params.vDeployments[dep].nStartTime; int64_t nTimeout = params.vDeployments[dep].nTimeout; + int min_activation_height = params.vDeployments[dep].min_activation_height; // should not be any signalling for first block BOOST_CHECK_EQUAL(ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS); @@ -267,6 +268,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus BOOST_REQUIRE(nTimeout <= std::numeric_limits::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT); BOOST_REQUIRE(0 <= bit && bit < 32); BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0); + BOOST_REQUIRE(min_activation_height == 0); // In the first chain, test that the bit is set by CBV until it has failed. // In the second chain, test the bit is set by CBV while STARTED and -- cgit v1.2.3 From dd85d5411c1702c8ae259610fe55050ba212e21e Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 7 Apr 2021 11:37:47 +1000 Subject: tests: test versionbits delayed activation --- src/test/versionbits_tests.cpp | 83 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 9 deletions(-) (limited to 'src/test') diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index ae0d47b2b4..1ce31c9af9 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -44,6 +44,12 @@ public: int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, paramsDummy, cache); } }; +class TestDelayedActivationConditionChecker : public TestConditionChecker +{ +public: + int MinActivationHeight(const Consensus::Params& params) const override { return 15000; } +}; + class TestAlwaysActiveConditionChecker : public TestConditionChecker { public: @@ -68,6 +74,8 @@ class VersionBitsTester // The first one performs all checks, the second only 50%, the third only 25%, etc... // This is to test whether lack of cached information leads to the same results. TestConditionChecker checker[CHECKERS]; + // Another 6 that assume delayed activation + TestDelayedActivationConditionChecker checker_delayed[CHECKERS]; // Another 6 that assume always active activation TestAlwaysActiveConditionChecker checker_always[CHECKERS]; // Another 6 that assume never active activation @@ -77,14 +85,18 @@ class VersionBitsTester int num; public: - VersionBitsTester() : num(0) {} + VersionBitsTester() : num(1000) {} VersionBitsTester& Reset() { + // Have each group of tests be counted by the 1000s part, starting at 1000 + num = num - (num % 1000) + 1000; + for (unsigned int i = 0; i < vpblock.size(); i++) { delete vpblock[i]; } for (unsigned int i = 0; i < CHECKERS; i++) { checker[i] = TestConditionChecker(); + checker_delayed[i] = TestDelayedActivationConditionChecker(); checker_always[i] = TestAlwaysActiveConditionChecker(); checker_never[i] = TestNeverActiveConditionChecker(); } @@ -109,11 +121,18 @@ public: return *this; } - VersionBitsTester& TestStateSinceHeight(int height) { + VersionBitsTester& TestStateSinceHeight(int height) + { + return TestStateSinceHeight(height, height); + } + + VersionBitsTester& TestStateSinceHeight(int height, int height_delayed) + { const CBlockIndex* tip = Tip(); for (int i = 0; i < CHECKERS; i++) { if (InsecureRandBits(i) == 0) { BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(tip) == height, strprintf("Test %i for StateSinceHeight", num)); + BOOST_CHECK_MESSAGE(checker_delayed[i].GetStateSinceHeightFor(tip) == height_delayed, strprintf("Test %i for StateSinceHeight (delayed)", num)); BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (always active)", num)); // never active may go from DEFINED -> FAILED at the first period @@ -125,17 +144,31 @@ public: return *this; } - VersionBitsTester& TestState(ThresholdState exp) { + VersionBitsTester& TestState(ThresholdState exp) + { + return TestState(exp, exp); + } + + VersionBitsTester& TestState(ThresholdState exp, ThresholdState exp_delayed) + { + if (exp != exp_delayed) { + // only expected differences are that delayed stays in locked_in longer + BOOST_CHECK_EQUAL(exp, ThresholdState::ACTIVE); + BOOST_CHECK_EQUAL(exp_delayed, ThresholdState::LOCKED_IN); + } + const CBlockIndex* pindex = Tip(); for (int i = 0; i < CHECKERS; i++) { if (InsecureRandBits(i) == 0) { ThresholdState got = checker[i].GetStateFor(pindex); + ThresholdState got_delayed = checker_delayed[i].GetStateFor(pindex); ThresholdState got_always = checker_always[i].GetStateFor(pindex); ThresholdState got_never = checker_never[i].GetStateFor(pindex); // nHeight of the next block. If vpblock is empty, the next (ie first) // block should be the genesis block with nHeight == 0. int height = pindex == nullptr ? 0 : pindex->nHeight + 1; BOOST_CHECK_MESSAGE(got == exp, strprintf("Test %i for %s height %d (got %s)", num, StateName(exp), height, StateName(got))); + BOOST_CHECK_MESSAGE(got_delayed == exp_delayed, strprintf("Test %i for %s height %d (got %s; delayed case)", num, StateName(exp_delayed), height, StateName(got_delayed))); BOOST_CHECK_MESSAGE(got_always == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE height %d (got %s; always active case)", num, height, StateName(got_always))); BOOST_CHECK_MESSAGE(got_never == ThresholdState::DEFINED|| got_never == ThresholdState::FAILED, strprintf("Test %i for DEFINED/FAILED height %d (got %s; never active case)", num, height, StateName(got_never))); } @@ -150,6 +183,9 @@ public: VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); } VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); } + // non-delayed should be active; delayed should still be locked in + VersionBitsTester& TestActiveDelayed() { return TestState(ThresholdState::ACTIVE, ThresholdState::LOCKED_IN); } + CBlockIndex* Tip() { return vpblock.empty() ? nullptr : vpblock.back(); } }; @@ -170,7 +206,6 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(1000) .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(1000) .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(1000) - // DEFINED -> STARTED -> FAILED .Reset().TestDefined().TestStateSinceHeight(0) .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) @@ -203,9 +238,10 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(2999, TestTime(19999), 0x200).TestStarted().TestStateSinceHeight(2000) // 49 old blocks .Mine(3000, TestTime(29999), 0x200).TestLockedIn().TestStateSinceHeight(3000) // 1 old block (so 900 out of the past 1000) .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000) - .Mine(4000, TestTime(30002), 0).TestActive().TestStateSinceHeight(4000) - .Mine(14333, TestTime(30003), 0).TestActive().TestStateSinceHeight(4000) - .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000) + .Mine(4000, TestTime(30002), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000) // delayed will not become active until height=15000 + .Mine(14333, TestTime(30003), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000) + .Mine(15000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000) + .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000) // DEFINED multiple periods -> STARTED multiple periods -> FAILED .Reset().TestDefined().TestStateSinceHeight(0) @@ -216,7 +252,8 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(4000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) .Mine(5000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000) - .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000); + .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000) + ; } } @@ -230,6 +267,13 @@ BOOST_AUTO_TEST_CASE(versionbits_sanity) // Make sure that no deployment tries to set an invalid bit. BOOST_CHECK_EQUAL(bitmask & ~(uint32_t)VERSIONBITS_TOP_MASK, bitmask); + // Check min_activation_height is on a retarget boundary + BOOST_CHECK_EQUAL(mainnetParams.vDeployments[i].min_activation_height % mainnetParams.nMinerConfirmationWindow, 0); + // Check min_activation_height is 0 for ALWAYS_ACTIVE and never active deployments + if (mainnetParams.vDeployments[i].nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE || mainnetParams.vDeployments[i].nTimeout <= 1230768000) { + BOOST_CHECK_EQUAL(mainnetParams.vDeployments[i].min_activation_height, 0); + } + // Verify that the deployment windows of different deployment using the // same bit are disjoint. // This test may need modification at such time as a new deployment @@ -268,7 +312,8 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus BOOST_REQUIRE(nTimeout <= std::numeric_limits::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT); BOOST_REQUIRE(0 <= bit && bit < 32); BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0); - BOOST_REQUIRE(min_activation_height == 0); + BOOST_REQUIRE(min_activation_height >= 0); + BOOST_REQUIRE_EQUAL(min_activation_height % params.nMinerConfirmationWindow, 0); // In the first chain, test that the bit is set by CBV until it has failed. // In the second chain, test the bit is set by CBV while STARTED and @@ -378,6 +423,16 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus lastBlock = secondChain.Mine((params.nMinerConfirmationWindow * 3) - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); BOOST_CHECK((ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0); lastBlock = secondChain.Mine(params.nMinerConfirmationWindow * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); + + if (lastBlock->nHeight + 1 < min_activation_height) { + // check signalling continues while min_activation_height is not reached + lastBlock = secondChain.Mine(min_activation_height - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); + BOOST_CHECK((ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0); + // then reach min_activation_height, which was already REQUIRE'd to start a new period + lastBlock = secondChain.Mine(min_activation_height, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip(); + } + + // Check that we don't signal after activation BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, params) & (1<GetConsensus(), static_cast(i)); } } + + { + // Use regtest/testdummy to ensure we always exercise the + // min_activation_height test, even if we're not using that in a + // live deployment + ArgsManager args; + args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999:403200"); // January 1, 2008 - December 31, 2008, min act height 403200 + const auto chainParams = CreateChainParams(args, CBaseChainParams::REGTEST); + check_computeblockversion(chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY); + } } BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3 From dd07e6da48040dc7eae46bc7941db48d98a669fd Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 16 Mar 2021 22:12:19 +1000 Subject: fuzz: test versionbits delayed activation --- src/test/fuzz/versionbits.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/test') diff --git a/src/test/fuzz/versionbits.cpp b/src/test/fuzz/versionbits.cpp index 88c1a1a9cb..11640d1ee9 100644 --- a/src/test/fuzz/versionbits.cpp +++ b/src/test/fuzz/versionbits.cpp @@ -29,14 +29,16 @@ public: const int64_t m_end; const int m_period; const int m_threshold; + const int m_min_activation_height; const int m_bit; - TestConditionChecker(int64_t begin, int64_t end, int period, int threshold, int bit) - : m_begin{begin}, m_end{end}, m_period{period}, m_threshold{threshold}, m_bit{bit} + TestConditionChecker(int64_t begin, int64_t end, int period, int threshold, int min_activation_height, int bit) + : m_begin{begin}, m_end{end}, m_period{period}, m_threshold{threshold}, m_min_activation_height{min_activation_height}, m_bit{bit} { assert(m_period > 0); assert(0 <= m_threshold && m_threshold <= m_period); assert(0 <= m_bit && m_bit < 32 && m_bit < VERSIONBITS_NUM_BITS); + assert(0 <= m_min_activation_height); } bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override { return Condition(pindex->nVersion); } @@ -44,6 +46,7 @@ public: int64_t EndTime(const Consensus::Params& params) const override { return m_end; } int Period(const Consensus::Params& params) const override { return m_period; } int Threshold(const Consensus::Params& params) const override { return m_threshold; } + int MinActivationHeight(const Consensus::Params& params) const override { return m_min_activation_height; } ThresholdState GetStateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, dummy_params, m_cache); } int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, dummy_params, m_cache); } @@ -168,8 +171,9 @@ FUZZ_TARGET_INIT(versionbits, initialize) never_active_test = true; } } + int min_activation = fuzzed_data_provider.ConsumeIntegralInRange(0, period * max_periods); - TestConditionChecker checker(start_time, timeout, period, threshold, bit); + TestConditionChecker checker(start_time, timeout, period, threshold, min_activation, bit); // Early exit if the versions don't signal sensibly for the deployment if (!checker.Condition(ver_signal)) return; @@ -306,11 +310,16 @@ FUZZ_TARGET_INIT(versionbits, initialize) } break; case ThresholdState::LOCKED_IN: - assert(exp_state == ThresholdState::STARTED); - assert(current_block->GetMedianTimePast() < checker.m_end); - assert(blocks_sig >= threshold); + if (exp_state == ThresholdState::LOCKED_IN) { + assert(current_block->nHeight + 1 < min_activation); + } else { + assert(exp_state == ThresholdState::STARTED); + assert(current_block->GetMedianTimePast() < checker.m_end); + assert(blocks_sig >= threshold); + } break; case ThresholdState::ACTIVE: + assert(always_active_test || min_activation <= current_block->nHeight + 1); assert(exp_state == ThresholdState::ACTIVE || exp_state == ThresholdState::LOCKED_IN); break; case ThresholdState::FAILED: -- cgit v1.2.3 From 55ac5f568a3b73d6f1ef4654617fb76e8bcbccdf Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sat, 27 Mar 2021 23:00:14 +1000 Subject: versionbits: Add explicit NEVER_ACTIVE deployments Previously we used deployments that would timeout prior to Bitcoin's invention, which allowed the deployment to still be activated in unit tests. This switches those deployments to be truly never active. --- src/test/fuzz/versionbits.cpp | 27 ++++++++++----------------- src/test/versionbits_tests.cpp | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 26 deletions(-) (limited to 'src/test') diff --git a/src/test/fuzz/versionbits.cpp b/src/test/fuzz/versionbits.cpp index 11640d1ee9..4177323233 100644 --- a/src/test/fuzz/versionbits.cpp +++ b/src/test/fuzz/versionbits.cpp @@ -163,13 +163,12 @@ FUZZ_TARGET_INIT(versionbits, initialize) } else { if (fuzzed_data_provider.ConsumeBool()) { start_time = Consensus::BIP9Deployment::ALWAYS_ACTIVE; - timeout = Consensus::BIP9Deployment::NO_TIMEOUT; always_active_test = true; } else { - start_time = 1199145601; // January 1, 2008 - timeout = 1230767999; // December 31, 2008 + start_time = Consensus::BIP9Deployment::NEVER_ACTIVE; never_active_test = true; } + timeout = fuzzed_data_provider.ConsumeBool() ? Consensus::BIP9Deployment::NO_TIMEOUT : fuzzed_data_provider.ConsumeIntegral(); } int min_activation = fuzzed_data_provider.ConsumeIntegralInRange(0, period * max_periods); @@ -323,7 +322,7 @@ FUZZ_TARGET_INIT(versionbits, initialize) assert(exp_state == ThresholdState::ACTIVE || exp_state == ThresholdState::LOCKED_IN); break; case ThresholdState::FAILED: - assert(current_block->GetMedianTimePast() >= checker.m_end); + assert(never_active_test || current_block->GetMedianTimePast() >= checker.m_end); assert(exp_state != ThresholdState::LOCKED_IN && exp_state != ThresholdState::ACTIVE); break; default: @@ -335,26 +334,20 @@ FUZZ_TARGET_INIT(versionbits, initialize) assert(state == ThresholdState::ACTIVE || state == ThresholdState::FAILED); } - // "always active" has additional restrictions if (always_active_test) { + // "always active" has additional restrictions assert(state == ThresholdState::ACTIVE); assert(exp_state == ThresholdState::ACTIVE); assert(since == 0); + } else if (never_active_test) { + // "never active" does too + assert(state == ThresholdState::FAILED); + assert(exp_state == ThresholdState::FAILED); + assert(since == 0); } else { - // except for always active, the initial state is always DEFINED + // for signalled deployments, the initial state is always DEFINED assert(since > 0 || state == ThresholdState::DEFINED); assert(exp_since > 0 || exp_state == ThresholdState::DEFINED); } - - // "never active" does too - if (never_active_test) { - assert(state == ThresholdState::FAILED); - assert(since == period); - if (exp_since == 0) { - assert(exp_state == ThresholdState::DEFINED); - } else { - assert(exp_state == ThresholdState::FAILED); - } - } } } // namespace diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 1ce31c9af9..80072eff9c 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -59,8 +59,7 @@ public: class TestNeverActiveConditionChecker : public TestConditionChecker { public: - int64_t BeginTime(const Consensus::Params& params) const override { return 0; } - int64_t EndTime(const Consensus::Params& params) const override { return 1230768000; } + int64_t BeginTime(const Consensus::Params& params) const override { return Consensus::BIP9Deployment::NEVER_ACTIVE; } }; #define CHECKERS 6 @@ -134,10 +133,7 @@ public: BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(tip) == height, strprintf("Test %i for StateSinceHeight", num)); BOOST_CHECK_MESSAGE(checker_delayed[i].GetStateSinceHeightFor(tip) == height_delayed, strprintf("Test %i for StateSinceHeight (delayed)", num)); BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (always active)", num)); - - // never active may go from DEFINED -> FAILED at the first period - const auto never_height = checker_never[i].GetStateSinceHeightFor(tip); - BOOST_CHECK_MESSAGE(never_height == 0 || never_height == checker_never[i].Period(paramsDummy), strprintf("Test %i for StateSinceHeight (never active)", num)); + BOOST_CHECK_MESSAGE(checker_never[i].GetStateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (never active)", num)); } } num++; @@ -170,7 +166,7 @@ public: BOOST_CHECK_MESSAGE(got == exp, strprintf("Test %i for %s height %d (got %s)", num, StateName(exp), height, StateName(got))); BOOST_CHECK_MESSAGE(got_delayed == exp_delayed, strprintf("Test %i for %s height %d (got %s; delayed case)", num, StateName(exp_delayed), height, StateName(got_delayed))); BOOST_CHECK_MESSAGE(got_always == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE height %d (got %s; always active case)", num, height, StateName(got_always))); - BOOST_CHECK_MESSAGE(got_never == ThresholdState::DEFINED|| got_never == ThresholdState::FAILED, strprintf("Test %i for DEFINED/FAILED height %d (got %s; never active case)", num, height, StateName(got_never))); + BOOST_CHECK_MESSAGE(got_never == ThresholdState::FAILED, strprintf("Test %i for FAILED height %d (got %s; never active case)", num, height, StateName(got_never))); } } num++; @@ -270,7 +266,7 @@ BOOST_AUTO_TEST_CASE(versionbits_sanity) // Check min_activation_height is on a retarget boundary BOOST_CHECK_EQUAL(mainnetParams.vDeployments[i].min_activation_height % mainnetParams.nMinerConfirmationWindow, 0); // Check min_activation_height is 0 for ALWAYS_ACTIVE and never active deployments - if (mainnetParams.vDeployments[i].nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE || mainnetParams.vDeployments[i].nTimeout <= 1230768000) { + if (mainnetParams.vDeployments[i].nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE || mainnetParams.vDeployments[i].nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE) { BOOST_CHECK_EQUAL(mainnetParams.vDeployments[i].min_activation_height, 0); } @@ -304,8 +300,9 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus // should not be any signalling for first block BOOST_CHECK_EQUAL(ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS); - // always active deployments shouldn't need to be tested further + // always/never active deployments shouldn't need to be tested further if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE) return; + if (nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE) return; BOOST_REQUIRE(nStartTime < nTimeout); BOOST_REQUIRE(nStartTime >= 0); @@ -447,6 +444,15 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion) } } + { + // Use regtest/testdummy to ensure we always exercise some + // deployment that's not always/never active + ArgsManager args; + args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999"); // January 1, 2008 - December 31, 2008 + const auto chainParams = CreateChainParams(args, CBaseChainParams::REGTEST); + check_computeblockversion(chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY); + } + { // Use regtest/testdummy to ensure we always exercise the // min_activation_height test, even if we're not using that in a -- cgit v1.2.3 From f054f6bcd2c2ce5fea84cf8681013f85a444e7ea Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Wed, 7 Apr 2021 10:20:46 +1000 Subject: versionbits: simplify state transitions This removes the DEFINED->FAILED transition and changes the STARTED->FAILED transition to only occur if signalling didn't pass the threshold. This ensures that it is always possible for activation to occur, no matter what settings are chosen, or the speed at which blocks are found. --- src/test/fuzz/versionbits.cpp | 17 +++++++---------- src/test/versionbits_tests.cpp | 34 +++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 25 deletions(-) (limited to 'src/test') diff --git a/src/test/fuzz/versionbits.cpp b/src/test/fuzz/versionbits.cpp index 4177323233..9186821836 100644 --- a/src/test/fuzz/versionbits.cpp +++ b/src/test/fuzz/versionbits.cpp @@ -147,19 +147,14 @@ FUZZ_TARGET_INIT(versionbits, initialize) // pick the timestamp to switch based on a block // note states will change *after* these blocks because mediantime lags int start_block = fuzzed_data_provider.ConsumeIntegralInRange(0, period * (max_periods - 3)); - int end_block = fuzzed_data_provider.ConsumeIntegralInRange(start_block, period * (max_periods - 3)); + int end_block = fuzzed_data_provider.ConsumeIntegralInRange(0, period * (max_periods - 3)); start_time = block_start_time + start_block * interval; timeout = block_start_time + end_block * interval; - assert(start_time <= timeout); - // allow for times to not exactly match a block if (fuzzed_data_provider.ConsumeBool()) start_time += interval / 2; if (fuzzed_data_provider.ConsumeBool()) timeout += interval / 2; - - // this may make timeout too early; if so, don't run the test - if (start_time > timeout) return; } else { if (fuzzed_data_provider.ConsumeBool()) { start_time = Consensus::BIP9Deployment::ALWAYS_ACTIVE; @@ -297,13 +292,12 @@ FUZZ_TARGET_INIT(versionbits, initialize) assert(since == 0); assert(exp_state == ThresholdState::DEFINED); assert(current_block->GetMedianTimePast() < checker.m_begin); - assert(current_block->GetMedianTimePast() < checker.m_end); break; case ThresholdState::STARTED: assert(current_block->GetMedianTimePast() >= checker.m_begin); - assert(current_block->GetMedianTimePast() < checker.m_end); if (exp_state == ThresholdState::STARTED) { assert(blocks_sig < threshold); + assert(current_block->GetMedianTimePast() < checker.m_end); } else { assert(exp_state == ThresholdState::DEFINED); } @@ -313,7 +307,6 @@ FUZZ_TARGET_INIT(versionbits, initialize) assert(current_block->nHeight + 1 < min_activation); } else { assert(exp_state == ThresholdState::STARTED); - assert(current_block->GetMedianTimePast() < checker.m_end); assert(blocks_sig >= threshold); } break; @@ -323,7 +316,11 @@ FUZZ_TARGET_INIT(versionbits, initialize) break; case ThresholdState::FAILED: assert(never_active_test || current_block->GetMedianTimePast() >= checker.m_end); - assert(exp_state != ThresholdState::LOCKED_IN && exp_state != ThresholdState::ACTIVE); + if (exp_state == ThresholdState::STARTED) { + assert(blocks_sig < threshold); + } else { + assert(exp_state == ThresholdState::FAILED); + } break; default: assert(false); diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 80072eff9c..05838ec19a 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -190,18 +190,20 @@ BOOST_FIXTURE_TEST_SUITE(versionbits_tests, TestingSetup) BOOST_AUTO_TEST_CASE(versionbits_test) { for (int i = 0; i < 64; i++) { - // DEFINED -> FAILED + // DEFINED -> STARTED after timeout reached -> FAILED VersionBitsTester().TestDefined().TestStateSinceHeight(0) .Mine(1, TestTime(1), 0x100).TestDefined().TestStateSinceHeight(0) .Mine(11, TestTime(11), 0x100).TestDefined().TestStateSinceHeight(0) .Mine(989, TestTime(989), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(999, TestTime(20000), 0x100).TestDefined().TestStateSinceHeight(0) - .Mine(1000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(1999, TestTime(30001), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2000, TestTime(30002), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(1000) - .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(1000) + .Mine(999, TestTime(20000), 0x100).TestDefined().TestStateSinceHeight(0) // Timeout and start time reached simultaneously + .Mine(1000, TestTime(20000), 0).TestStarted().TestStateSinceHeight(1000) // Hit started, stop signalling + .Mine(1999, TestTime(30001), 0).TestStarted().TestStateSinceHeight(1000) + .Mine(2000, TestTime(30002), 0x100).TestFailed().TestStateSinceHeight(2000) // Hit failed, start signalling again + .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(2000) + .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(2000) + .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(2000) + .Mine(4000, TestTime(30006), 0x100).TestFailed().TestStateSinceHeight(2000) + // DEFINED -> STARTED -> FAILED .Reset().TestDefined().TestStateSinceHeight(0) .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) @@ -212,19 +214,19 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(3000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(3000) // 50 old blocks (so 899 out of the past 1000) .Mine(4000, TestTime(20010), 0x100).TestFailed().TestStateSinceHeight(3000) - // DEFINED -> STARTED -> FAILED while threshold reached + // DEFINED -> STARTED -> LOCKEDIN after timeout reached -> ACTIVE .Reset().TestDefined().TestStateSinceHeight(0) .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period .Mine(2999, TestTime(30000), 0x100).TestStarted().TestStateSinceHeight(2000) // 999 new blocks - .Mine(3000, TestTime(30000), 0x100).TestFailed().TestStateSinceHeight(3000) // 1 new block (so 1000 out of the past 1000 are new) - .Mine(3999, TestTime(30001), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(4000, TestTime(30002), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(14333, TestTime(30003), 0).TestFailed().TestStateSinceHeight(3000) - .Mine(24000, TestTime(40000), 0).TestFailed().TestStateSinceHeight(3000) + .Mine(3000, TestTime(30000), 0x100).TestLockedIn().TestStateSinceHeight(3000) // 1 new block (so 1000 out of the past 1000 are new) + .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000) + .Mine(4000, TestTime(30002), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000) + .Mine(14333, TestTime(30003), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000) + .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000) - // DEFINED -> STARTED -> LOCKEDIN at the last minute -> ACTIVE + // DEFINED -> STARTED -> LOCKEDIN before timeout -> ACTIVE .Reset().TestDefined() .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0) .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined @@ -247,8 +249,10 @@ BOOST_AUTO_TEST_CASE(versionbits_test) .Mine(3000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) .Mine(4000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) .Mine(5000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000) + .Mine(5999, TestTime(20000), 0).TestStarted().TestStateSinceHeight(3000) .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000) .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000) + .Mine(24000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000) // stay in FAILED no matter how much we signal ; } } -- cgit v1.2.3