-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinalization_header_tests.cpp
More file actions
281 lines (250 loc) · 11.7 KB
/
Copy pathfinalization_header_tests.cpp
File metadata and controls
281 lines (250 loc) · 11.7 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2020 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <boost/test/unit_test.hpp>
#include <chain.h>
#include <chainparams.h>
#include <config.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <pow.h>
#include <test/setup_common.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/time.h>
#include <validation.h>
BOOST_FIXTURE_TEST_SUITE(finalization_header_tests, TestChain100Setup)
static COutPoint InsecureRandOutPoint() {
return COutPoint(TxId(InsecureRand256()), 0);
}
// Construct a valid block
static CBlock GetBlock(const Config &config, CBlockIndex *pindexPrev) {
CBlock block;
CMutableTransaction tx;
const Consensus::Params ¶ms = config.GetChainParams().GetConsensus();
const uint32_t powLimit = UintToArith256(params.powLimit).GetCompact();
block.nVersion = 4;
block.hashPrevBlock = pindexPrev->GetBlockHash();
block.nBits = powLimit;
block.nTime = pindexPrev->nTime + 1;
tx.vin.resize(1);
tx.vin[0].scriptSig.resize(10);
tx.vout.resize(1);
tx.vout[0].nValue = 50 * COIN;
block.vtx.resize(2);
block.vtx[0] = MakeTransactionRef(tx);
tx.vin[0].prevout = InsecureRandOutPoint();
block.vtx[1] = MakeTransactionRef(tx);
block.hashMerkleRoot = BlockMerkleRoot(block);
while (!CheckProofOfWork(block.GetHash(), block.nBits, params)) {
++block.nNonce;
}
return block;
}
// Checks that a new header is accepted (processed and stored)
static void CreateNewHeaderAndCheckIsAccepted(const Config &config, CBlockIndex *hashPrev) {
const CBlockHeader &header = GetBlock(config, hashPrev).GetBlockHeader();
CValidationState state;
CBlockHeader invalid;
const CBlockIndex *pindex = nullptr;
BOOST_CHECK(ProcessNewBlockHeaders(config, {header}, state, &pindex, &invalid));
BOOST_CHECK(state.IsValid());
BOOST_CHECK(pindex != nullptr);
BOOST_CHECK(invalid.GetHash() != header.GetHash());
// Sanity check: accepted header should be stored in memory
{
LOCK(cs_main);
BOOST_CHECK(LookupBlockIndex(header.GetHash()) != nullptr);
}
}
// Checks that a new header is rejected (not processed and not stored)
static void CreateNewHeaderAndCheckIsRejected(const Config &config, CBlockIndex *hashPrev) {
const CBlockHeader &header = GetBlock(config, hashPrev).GetBlockHeader();
CValidationState state;
CBlockHeader invalid;
const CBlockIndex *pindex = nullptr;
BOOST_CHECK(!ProcessNewBlockHeaders(config, {header}, state, &pindex, &invalid));
BOOST_CHECK(state.IsInvalid());
BOOST_CHECK(state.GetRejectCode() == REJECT_AGAINST_FINALIZED);
BOOST_CHECK(state.GetRejectReason() == "bad-header-finalization");
BOOST_CHECK(pindex == nullptr);
BOOST_CHECK(invalid.GetHash() == header.GetHash());
// Sanity check: rejected header should not be stored in memory
{
LOCK(cs_main);
BOOST_CHECK(LookupBlockIndex(header.GetHash()) == nullptr);
}
}
BOOST_AUTO_TEST_CASE(headerFinalizationEnabledDefault) {
const Config &config = GetConfig();
CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CBlock block;
{
LOCK(cs_main);
// We should have no finalized block because the 100 blocks
// generated by the test setup are too close to "now"
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == nullptr,
"No block finalized (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
const int64_t finalizationStartTime = GetTime() + DEFAULT_MIN_FINALIZATION_DELAY;
for (uint32_t i = 0; i < DEFAULT_MAX_REORG_DEPTH; i++) {
CBlockIndex *blockToFinalize = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - DEFAULT_MAX_REORG_DEPTH + 1);
SetMockTime(finalizationStartTime + 1 + i);
block = CreateAndProcessBlock({}, p2pk_scriptPubKey);
LOCK(cs_main);
// Should get blocks finalizing
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == blockToFinalize,
"Block finalized at height "
<< blockToFinalize->nHeight
<< " (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
// Block at height=100 is now finalized.
// Working back from chain tip depth, we create headers for fork blocks.
// Headers above finalized block depth should be accepted regardless if
// `-finalizeheaders` is enabled or disabled, as long as the other header
// checks pass.
CBlockIndex *blockAtHeightOfNewHeader;
for (uint32_t i = 0; i < DEFAULT_MAX_REORG_DEPTH; i++) {
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i);
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader->pprev);
}
// A header deeper than DEFAULT_MAX_REORG_DEPTH should be rejected
{
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - DEFAULT_MAX_REORG_DEPTH);
CreateNewHeaderAndCheckIsRejected(config, blockAtHeightOfNewHeader->pprev);
}
} // headerFinalizationEnabledDefault
BOOST_AUTO_TEST_CASE(headerFinalizationEnabledNonDefault) {
gArgs.ForceSetArg("-maxreorgdepth", std::string("1"));
gArgs.ForceSetArg("-finalizeheaders", std::string("1"));
const Config &config = GetConfig();
CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CBlock block;
{
LOCK(cs_main);
// We should have no finalized block because the 100 blocks
// generated by the test setup are too close to "now"
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == nullptr,
"No block finalized (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
// Create a finalized tip by moving up sufficiently in time
const int64_t finalizationStartTime = GetTime() + DEFAULT_MIN_FINALIZATION_DELAY;
SetMockTime(finalizationStartTime);
block = CreateAndProcessBlock({}, p2pk_scriptPubKey);
{
LOCK(cs_main);
CBlockIndex *blockToFinalize = ::ChainActive().Tip()->pprev;
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == blockToFinalize,
"Block finalized at height "
<< blockToFinalize->nHeight
<< " (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
CBlockIndex *blockAtHeightOfNewHeader;
// Block at height=100 is now finalized.
// A header at same height as tip should be rejected
{
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - 1);
CreateNewHeaderAndCheckIsRejected(config, blockAtHeightOfNewHeader->pprev);
}
// A header one below tip should also be rejected
{
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - 2);
CreateNewHeaderAndCheckIsRejected(config, blockAtHeightOfNewHeader->pprev);
}
// A header building on tip should be accepted
{
blockAtHeightOfNewHeader = ::ChainActive().Tip();
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader);
}
gArgs.ClearArg("-finalizeheaders");
gArgs.ClearArg("-maxreorgdepth");
} // headerFinalizationEnabledNonDefault
BOOST_AUTO_TEST_CASE(headerFinalizationDisabledOne) {
// Set to finalize block below tip
gArgs.ForceSetArg("-maxreorgdepth", std::string("1"));
// Disable the finalization of headers
gArgs.ForceSetArg("-finalizeheaders", std::string("0"));
// Turn off finalization delay so we can omit the time advances.
// We are only interested in the finalized block depth and whether
// we can fork headers anywhere
gArgs.ForceSetArg("-finalizationdelay", std::string("0"));
const Config &config = GetConfig();
CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CBlock block;
{
LOCK(cs_main);
// We should have no finalized block because the 100 blocks
// generated by the test setup are too close to "now";
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == nullptr,
"No block finalized (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
CBlockIndex *blockToFinalize = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - gArgs.GetArg("-maxreorgdepth", DEFAULT_MAX_REORG_DEPTH));
for (uint32_t i = 0; i < DEFAULT_MAX_REORG_DEPTH; i++) {
blockToFinalize = ::ChainActive().Next(blockToFinalize);
block = CreateAndProcessBlock({}, p2pk_scriptPubKey);
LOCK(cs_main);
// Should get blocks finalizing
BOOST_CHECK_MESSAGE(GetFinalizedBlock() == blockToFinalize,
"Block not finalized at height "
<< blockToFinalize->nHeight
<< " (tip at height "
<< ::ChainActive().Tip()->nHeight << ")");
}
// Ancestor of tip is finalized.
// Working back from chain tip depth, we create headers for fork blocks.
// As long as we are not deeper than finalization depth, they must be
// accepted if finalizeheaders is disabled.
CBlockIndex *blockAtHeightOfNewHeader;
for (uint32_t i = 0; i < DEFAULT_MAX_REORG_DEPTH; i++) {
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i);
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader->pprev);
}
// With header finalization disabled, a header deeper than
// DEFAULT_MAX_REORG_DEPTH should be accepted
{
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - DEFAULT_MAX_REORG_DEPTH);
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader->pprev);
}
// And another deeper one is accepted too.
{
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(
::ChainActive().Tip()->nHeight - DEFAULT_MAX_REORG_DEPTH - 1);
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader->pprev);
}
gArgs.ClearArg("-finalizationdelay");
gArgs.ClearArg("-finalizeheaders");
gArgs.ClearArg("-maxreorgdepth");
} // headerFinalizationDisabledOne
BOOST_AUTO_TEST_CASE(headerFinalizationDisabledZero) {
// Disable the finalization of headers
gArgs.ForceSetArg("-finalizeheaders", std::string("0"));
// 0 also finalizes block below tip
gArgs.ForceSetArg("-maxreorgdepth", std::string("0"));
const Config &config = GetConfig();
CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CBlock block;
// Ancestor of tip is finalized.
// Working back from chain tip depth to genesis, we create headers for fork
// blocks. They must all be accepted if finalizeheaders is disabled. (no
// checkpoints on this regtest chain)
CBlockIndex *blockAtHeightOfNewHeader;
auto tipHeight = ::ChainActive().Tip()->nHeight;
for (auto i = 0; i < tipHeight; i++) {
blockAtHeightOfNewHeader = ::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i);
CreateNewHeaderAndCheckIsAccepted(config, blockAtHeightOfNewHeader->pprev);
}
gArgs.ClearArg("-maxreorgdepth");
gArgs.ClearArg("-finalizeheaders");
} // headerFinalizationDisabledZero
BOOST_AUTO_TEST_SUITE_END()