forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdynamic_activation_thresholds_tests.cpp
More file actions
161 lines (140 loc) · 6.69 KB
/
dynamic_activation_thresholds_tests.cpp
File metadata and controls
161 lines (140 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2021-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <chainparams.h>
#include <deploymentstatus.h>
#include <node/miner.h>
#include <validation.h>
#include <versionbits.h>
#include <boost/test/unit_test.hpp>
using node::BlockAssembler;
const auto deployment_id = Consensus::DEPLOYMENT_TESTDUMMY;
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
{
TestChainDATSetup() :
TestChainSetup(window - 2, CBaseChainParams::REGTEST, {"-vbparams=testdummy:0:999999999999:0:100:80:60:5:0"}) {}
void signal(int num_blocks, bool expected_lockin)
{
const auto& consensus_params = Params().GetConsensus();
CScript coinbasePubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
// Mine non-signalling blocks
gArgs.ForceSetArg("-blockversion", "536870912");
for (int i = 0; i < window - num_blocks; ++i) {
CreateAndProcessBlock({}, coinbasePubKey);
}
gArgs.ForceRemoveArg("blockversion");
if (num_blocks > 0) {
// Mine signalling blocks
for (int i = 0; i < num_blocks; ++i) {
CreateAndProcessBlock({}, coinbasePubKey);
}
}
LOCK(cs_main);
if (expected_lockin) {
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::LOCKED_IN);
} else {
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::STARTED);
}
}
void test(int activation_index, bool check_activation_at_min)
{
const auto& consensus_params = Params().GetConsensus();
CScript coinbasePubKey = GetScriptForRawPubKey(coinbaseKey.GetPubKey());
{
LOCK(cs_main);
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Height(), window - 2);
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::DEFINED);
}
CreateAndProcessBlock({}, coinbasePubKey);
{
LOCK(cs_main);
// Advance from DEFINED to STARTED at height = window - 1
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Height(), window - 1);
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::STARTED);
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
.threshold,
threshold(0));
// Next block should be signaling by default
const auto pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), m_node, m_node.mempool.get(), Params()).CreateNewBlock(coinbasePubKey);
const uint32_t bitmask = ((uint32_t)1) << consensus_params.vDeployments[deployment_id].bit;
BOOST_CHECK_EQUAL(m_node.chainman->ActiveChain().Tip()->nVersion & bitmask, 0);
BOOST_CHECK_EQUAL(pblocktemplate->block.nVersion & bitmask, bitmask);
}
// 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(m_node.chainman->ActiveChain().Height(), window * (i + 2) - 1);
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::STARTED);
const auto vbts = m_node.chainman->m_versionbitscache.Statistics(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id);
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) {
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
.threshold,
th_end);
} else {
BOOST_CHECK(m_node.chainman->m_versionbitscache
.Statistics(m_node.chainman->ActiveChain().Tip(), consensus_params, deployment_id)
.threshold > th_end);
}
// activate
signal(threshold(activation_index), true);
for (int i = 0; i < window; ++i) {
CreateAndProcessBlock({}, coinbasePubKey);
}
{
LOCK(cs_main);
BOOST_CHECK_EQUAL(m_node.chainman->m_versionbitscache.State(m_node.chainman->ActiveChain().Tip(),
consensus_params, deployment_id),
ThresholdState::ACTIVE);
}
}
};
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()