2022-06-08 01:36:46 +02:00
|
|
|
// Copyright (c) 2021-2022 The Dash Core developers
|
2021-12-11 21:01:20 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2022-02-25 18:19:25 +01:00
|
|
|
#include <test/util/setup_common.h>
|
2021-12-11 21:01:20 +01:00
|
|
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <consensus/validation.h>
|
2022-08-26 23:52:53 +02:00
|
|
|
#include <governance/governance.h>
|
2022-09-22 13:14:48 +02:00
|
|
|
#include <llmq/blockprocessor.h>
|
|
|
|
#include <llmq/chainlocks.h>
|
2022-11-07 19:09:44 +01:00
|
|
|
#include <llmq/context.h>
|
2022-09-22 13:14:48 +02:00
|
|
|
#include <llmq/instantsend.h>
|
refactor: remove the g_evoDb global; use NodeContext and locals (#5058)
<!--
*** Please remove the following help text before submitting: ***
Provide a general summary of your changes in the Title above
Pull requests without a rationale and clear improvement may be closed
immediately.
Please provide clear motivation for your patch and explain how it
improves
Dash Core user experience or Dash Core developer experience
significantly:
* Any test improvements or new tests that improve coverage are always
welcome.
* All other changes should have accompanying unit tests (see
`src/test/`) or
functional tests (see `test/`). Contributors should note which tests
cover
modified code. If no tests exist for a region of modified code, new
tests
should accompany the change.
* Bug fixes are most welcome when they come with steps to reproduce or
an
explanation of the potential issue as well as reasoning for the way the
bug
was fixed.
* Features are welcome, but might be rejected due to design or scope
issues.
If a feature is based on a lot of dependencies, contributors should
first
consider building the system outside of Dash Core, if possible.
-->
## Issue being fixed or feature implemented
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->
globals should be avoided to avoid annoying lifetime / nullptr /
initialization issues
## What was done?
<!--- Describe your changes in detail -->
removed a global, g_evoDB
## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
make check
## Breaking Changes
<!--- Please describe any breaking changes your code introduces -->
none
## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
**For repository code-owners and collaborators only**
- [ ] I have assigned this pull request to a milestone
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com>
2022-12-10 18:58:17 +01:00
|
|
|
#include <evo/evodb.h>
|
2021-12-11 21:01:20 +01:00
|
|
|
#include <miner.h>
|
|
|
|
#include <script/interpreter.h>
|
2022-08-26 23:52:53 +02:00
|
|
|
#include <spork.h>
|
2021-12-11 21:01:20 +01:00
|
|
|
#include <validation.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2023-05-17 13:11:33 +02:00
|
|
|
const auto deployment_id = Consensus::DEPLOYMENT_TESTDUMMY;
|
2021-12-11 21:01:20 +01:00
|
|
|
constexpr int window{100}, th_start{80}, th_end{60};
|
|
|
|
|
|
|
|
static constexpr int threshold(int attempt)
|
|
|
|
{
|
|
|
|
// An implementation of VersionBitsConditionChecker::Threshold()
|
|
|
|
int threshold_calc = th_start - attempt * attempt * window / 100 / 5;
|
|
|
|
if (threshold_calc < th_end) {
|
|
|
|
return th_end;
|
|
|
|
}
|
|
|
|
return threshold_calc;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestChainDATSetup : public TestChainSetup
|
|
|
|
{
|
2023-05-17 00:44:44 +02:00
|
|
|
TestChainDATSetup() : TestChainSetup(window - 2, {"-vbparams=testdummy:0:999999999999:100:80:60:5"}) {}
|
2021-12-11 21:01:20 +01:00
|
|
|
|
|
|
|
void signal(int num_blocks, bool expected_lockin)
|
|
|
|
{
|
|
|
|
const auto& consensus_params = Params().GetConsensus();
|
|
|
|
// Mine non-signalling blocks
|
|
|
|
gArgs.ForceSetArg("-blockversion", "536870912");
|
|
|
|
for (int i = 0; i < window - num_blocks; ++i) {
|
|
|
|
CreateAndProcessBlock({}, coinbaseKey);
|
|
|
|
}
|
2022-06-02 22:25:16 +02:00
|
|
|
gArgs.ForceRemoveArg("blockversion");
|
2021-12-11 21:01:20 +01:00
|
|
|
if (num_blocks > 0) {
|
|
|
|
// Mine signalling blocks
|
|
|
|
for (int i = 0; i < num_blocks; ++i) {
|
|
|
|
CreateAndProcessBlock({}, coinbaseKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOCK(cs_main);
|
|
|
|
if (expected_lockin) {
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::LOCKED_IN);
|
2021-12-11 21:01:20 +01:00
|
|
|
} else {
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::STARTED);
|
2021-12-11 21:01:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test(int activation_index, bool check_activation_at_min)
|
|
|
|
{
|
|
|
|
const auto& consensus_params = Params().GetConsensus();
|
|
|
|
CScript coinbasePubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
BOOST_CHECK_EQUAL(::ChainActive().Height(), window - 2);
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::DEFINED);
|
2021-12-11 21:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CreateAndProcessBlock({}, coinbaseKey);
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
// Advance from DEFINED to STARTED at height = window - 1
|
|
|
|
BOOST_CHECK_EQUAL(::ChainActive().Height(), window - 1);
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::STARTED);
|
|
|
|
BOOST_CHECK_EQUAL(VersionBitsStatistics(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache).threshold, threshold(0));
|
2021-12-11 21:01:20 +01:00
|
|
|
// Next block should be signaling by default
|
2021-03-08 21:48:42 +01:00
|
|
|
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx->quorum_block_processor, *m_node.llmq_ctx->clhandler, *m_node.llmq_ctx->isman, *m_node.evodb, *m_node.mempool, Params()).CreateNewBlock(::ChainstateActive(), coinbasePubKey);
|
2022-04-16 16:46:04 +02:00
|
|
|
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
|
|
|
|
BOOST_CHECK_EQUAL(::ChainActive().Tip()->nVersion & bitmask, 0);
|
|
|
|
BOOST_CHECK_EQUAL(pblocktemplate->block.nVersion & bitmask, bitmask);
|
2021-12-11 21:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reach activation_index level
|
|
|
|
for (int i = 0; i < activation_index; ++i) {
|
|
|
|
signal(threshold(i) - 1, false); // 1 block short
|
|
|
|
|
|
|
|
{
|
|
|
|
// Still STARTED but with a (potentially) new threshold
|
|
|
|
LOCK(cs_main);
|
|
|
|
BOOST_CHECK_EQUAL(::ChainActive().Height(), window * (i + 2) - 1);
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::STARTED);
|
|
|
|
const auto vbts = VersionBitsStatistics(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache);
|
2021-12-11 21:01:20 +01:00
|
|
|
BOOST_CHECK_EQUAL(vbts.threshold, threshold(i + 1));
|
|
|
|
BOOST_CHECK(vbts.threshold <= th_start);
|
|
|
|
BOOST_CHECK(vbts.threshold >= th_end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (LOCK(cs_main); check_activation_at_min) {
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsStatistics(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache).threshold, th_end);
|
2021-12-11 21:01:20 +01:00
|
|
|
} else {
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK(VersionBitsStatistics(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache).threshold > th_end);
|
2021-12-11 21:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// activate
|
|
|
|
signal(threshold(activation_index), true);
|
|
|
|
for (int i = 0; i < window; ++i) {
|
|
|
|
CreateAndProcessBlock({}, coinbaseKey);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2023-03-13 16:37:00 +01:00
|
|
|
BOOST_CHECK_EQUAL(VersionBitsState(::ChainActive().Tip(), consensus_params, deployment_id, versionbitscache), ThresholdState::ACTIVE);
|
2021-12-11 21:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(dynamic_activation_thresholds_tests)
|
|
|
|
|
|
|
|
#define TEST(INDEX, activate_at_min_level) BOOST_FIXTURE_TEST_CASE(activate_at_##INDEX##_level, TestChainDATSetup) \
|
|
|
|
{ \
|
|
|
|
test(INDEX, activate_at_min_level); \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(1, false)
|
|
|
|
TEST(2, false)
|
|
|
|
TEST(3, false)
|
|
|
|
TEST(4, false)
|
|
|
|
TEST(5, false)
|
|
|
|
TEST(6, false)
|
|
|
|
TEST(7, false)
|
|
|
|
TEST(8, false)
|
|
|
|
TEST(9, false)
|
|
|
|
TEST(10, true)
|
|
|
|
TEST(11, true)
|
|
|
|
TEST(12, true)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|