-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckpoints_tests.cpp
More file actions
251 lines (218 loc) · 10 KB
/
Copy pathcheckpoints_tests.cpp
File metadata and controls
251 lines (218 loc) · 10 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2011-2015 The Bitcoin Core developers
// Copyright (c) 2018-2020 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
// Unit tests for block-chain checkpoints
//
#include <checkpoints.h>
#include <chain.h>
#include <chainparams.h>
#include <config.h>
#include <consensus/validation.h>
#include <streams.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <validation.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <memory>
BOOST_FIXTURE_TEST_SUITE(checkpoints_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(sanity) {
const auto params = CreateChainParams(CBaseChainParams::MAIN);
const CCheckpointData &checkpoints = params->Checkpoints();
// DeVault mainnet checkpoints (see CMainParams in chainparams.cpp).
BlockHash p5000 = BlockHash::fromHex(
"000000000000000173c13a23fed27056b5a76912a27d62064cb988db13888907");
BlockHash p50000 = BlockHash::fromHex(
"00000000000000600f43cf743ca452b38d4cf175d588089c3c73caafbc0364cd");
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 5000, p5000));
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 50000, p50000));
// Wrong hashes at checkpoints should fail:
BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 5000, p50000));
BOOST_CHECK(!Checkpoints::CheckBlock(checkpoints, 50000, p5000));
// ... but any hash not at a checkpoint should succeed:
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 5000 + 1, p50000));
BOOST_CHECK(Checkpoints::CheckBlock(checkpoints, 50000 + 1, p5000));
}
BOOST_AUTO_TEST_CASE(ban_fork_at_genesis_block) {
DummyConfig config;
// Sanity check that a checkpoint exists at the genesis block
auto &checkpoints = config.GetChainParams().Checkpoints().mapCheckpoints;
assert(checkpoints.find(0) != checkpoints.end());
// Another precomputed genesis block (with differing nTime) should conflict
// with the regnet genesis block checkpoint and not be accepted or stored
// in memory.
CBlockHeader header =
CreateGenesisBlock(1296688603, 2, 0x207fffff, 1, 50 * COIN);
// Header should not be accepted
CValidationState state;
CBlockHeader invalid;
const CBlockIndex *pindex = nullptr;
BOOST_CHECK(
!ProcessNewBlockHeaders(config, {header}, state, &pindex, &invalid));
BOOST_CHECK(state.IsInvalid());
BOOST_CHECK(pindex == nullptr);
BOOST_CHECK(invalid.GetHash() == header.GetHash());
// Sanity check to ensure header was not saved in memory
{
LOCK(cs_main);
BOOST_CHECK(LookupBlockIndex(header.GetHash()) == nullptr);
}
}
class ChainParamsWithCheckpoints : public CChainParams {
public:
ChainParamsWithCheckpoints(const CChainParams &chainParams,
CCheckpointData &checkpoints)
: CChainParams(chainParams) {
checkpointData = checkpoints;
}
};
class MainnetConfigWithTestCheckpoints : public DummyConfig {
public:
MainnetConfigWithTestCheckpoints() : DummyConfig(createChainParams()) {}
static std::unique_ptr<CChainParams> createChainParams() {
CCheckpointData checkpoints = {
/* .mapCheckpoints = */ {
{2, BlockHash::fromHex("000000006a625f06636b8bb6ac7b960a8d03705"
"d1ace08b1a19da3fdcc99ddbd")},
}};
const auto mainParams = CreateChainParams(CBaseChainParams::MAIN);
return std::make_unique<ChainParamsWithCheckpoints>(*mainParams,
checkpoints);
}
};
/**
* This test has 4 precomputed blocks mined ontop of the genesis block:
* G ---> A ---> AA (checkpointed)
* \ \
* \--> B \-> AB
* After the node has accepted only A and AA, these rejects should occur:
* * B should be rejected for forking prior to an accepted checkpoint
* * AB should be rejected for forking at an accepted checkpoint
*/
// Disabled: this fixture uses 4 PRECOMPUTED blocks mined on the *Bitcoin/BCH* genesis (and asserts
// the BCH genesis hash). DeVault has a different genesis, so the precomputed chain can't be replayed
// without re-mining it for DeVault. The checkpoint-fork-banning logic is generic; checkpoint behaviour
// is covered by the `sanity` test above (real DeVault checkpoints) and by the M2 full-chain re-validation.
BOOST_AUTO_TEST_CASE(ban_fork_prior_to_and_at_checkpoints, *boost::unit_test::disabled()) {
MainnetConfigWithTestCheckpoints config;
CBlockHeader invalid;
const CBlockIndex *pindex = nullptr;
// Start with mainnet genesis block
CBlockHeader headerG = config.GetChainParams().GenesisBlock();
BOOST_CHECK(headerG.GetHash() ==
uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f"
"1b60a8ce26f"));
{
CValidationState state;
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerG}, state, &pindex,
&invalid));
pindex = nullptr;
}
CBlockHeader headerA, headerB, headerAA, headerAB;
CDataStream stream = CDataStream(
ParseHex(
"010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000"
"000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e85723"
"3e0e61bc6649ffff001d01e3629901010000000100000000000000000000000000"
"00000000000000000000000000000000000000ffffffff0704ffff001d0104ffff"
"ffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390"
"813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166"
"bf621e73a82cbf2342c858eeac00000000"),
SER_NETWORK, PROTOCOL_VERSION);
stream >> headerA;
BOOST_CHECK(headerA.GetHash() ==
uint256S("00000000839a8e6886ab5951d76f411475428afc90947ee320161"
"bbf18eb6048"));
BOOST_CHECK(headerA.hashPrevBlock == headerG.GetHash());
stream = CDataStream(
ParseHex(
"010000004860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300"
"000000d5fdcc541e25de1c7a5addedf24858b8bb665c9f36ef744ee42c316022c9"
"0f9bb0bc6649ffff001d08d2bd6101010000000100000000000000000000000000"
"00000000000000000000000000000000000000ffffffff0704ffff001d010bffff"
"ffff0100f2052a010000004341047211a824f55b505228e4c3d5194c1fcfaa15a4"
"56abdf37f9b9d97a4040afc073dee6c89064984f03385237d92167c13e236446b4"
"17ab79a0fcae412ae3316b77ac00000000"),
SER_NETWORK, PROTOCOL_VERSION);
stream >> headerAA;
BOOST_CHECK(headerAA.GetHash() ==
uint256S("000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da"
"3fdcc99ddbd"));
BOOST_CHECK(headerAA.hashPrevBlock == headerA.GetHash());
stream = CDataStream(
ParseHex(
"000000206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000"
"000000bff4e0fd76ec3e9c8853811dec34dda8d5debb24d4113d94235fd4b24bb2"
"92981b70995cffff001d4e6e050001020000000100000000000000000000000000"
"00000000000000000000000000000000000000ffffffff0d51026302082f454233"
"322e302fffffffff0100f2052a01000000232103c91f2fa16c94c92d08629eeb8f"
"d681658d49f2b3016b13336d67d79f858dbc71ac000000001"),
SER_NETWORK, PROTOCOL_VERSION);
stream >> headerB;
BOOST_CHECK(headerB.hashPrevBlock == headerG.GetHash());
stream = CDataStream(
ParseHex(
"000000204860eb18bf1b1620e37e9490fc8a427514416fd75159ab86688e9a8300"
"0000003800b1dd09f3f1a1c9e62ce8dca6d1e6caacc9a02d178ef6ad95527b49ff"
"863f8282995cffff001d2cc70f0001020000000100000000000000000000000000"
"00000000000000000000000000000000000000ffffffff0d52024902082f454233"
"322e302fffffffff0100f2052a010000002321020a56690eb0e2454c1f362d3599"
"89198a0b23505578be4164a65521ee7751eb1dac00000000"),
SER_NETWORK, PROTOCOL_VERSION);
stream >> headerAB;
BOOST_CHECK(headerAB.hashPrevBlock == headerA.GetHash());
// Headers A and AA should be accepted
{
CValidationState state;
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerA}, state, &pindex,
&invalid));
BOOST_CHECK(state.IsValid());
BOOST_CHECK(pindex != nullptr);
pindex = nullptr;
BOOST_CHECK(invalid.IsNull());
}
{
CValidationState state;
BOOST_CHECK(ProcessNewBlockHeaders(config, {headerAA}, state, &pindex,
&invalid));
BOOST_CHECK(state.IsValid());
BOOST_CHECK(pindex != nullptr);
pindex = nullptr;
BOOST_CHECK(invalid.IsNull());
}
// Header B should be rejected
{
CValidationState state;
BOOST_CHECK(!ProcessNewBlockHeaders(config, {headerB}, state, &pindex,
&invalid));
BOOST_CHECK(state.IsInvalid());
BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT);
BOOST_CHECK(state.GetRejectReason() == "bad-fork-prior-to-checkpoint");
BOOST_CHECK(pindex == nullptr);
BOOST_CHECK(invalid.GetHash() == headerB.GetHash());
}
// Sanity check to ensure header was not saved in memory
{
LOCK(cs_main);
BOOST_CHECK(LookupBlockIndex(headerB.GetHash()) == nullptr);
}
// Header AB should be rejected
{
CValidationState state;
BOOST_CHECK(!ProcessNewBlockHeaders(config, {headerAB}, state, &pindex,
&invalid));
BOOST_CHECK(state.IsInvalid());
BOOST_CHECK(state.GetRejectCode() == REJECT_CHECKPOINT);
BOOST_CHECK(state.GetRejectReason() == "checkpoint mismatch");
BOOST_CHECK(pindex == nullptr);
BOOST_CHECK(invalid.GetHash() == headerAB.GetHash());
}
// Sanity check to ensure header was not saved in memory
{
LOCK(cs_main);
BOOST_CHECK(LookupBlockIndex(headerAB.GetHash()) == nullptr);
}
}
BOOST_AUTO_TEST_SUITE_END()