forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_block_tests.cpp
More file actions
241 lines (196 loc) · 8.34 KB
/
Copy pathvalidation_block_tests.cpp
File metadata and controls
241 lines (196 loc) · 8.34 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
// Copyright (c) 2018 The Bitcoin Core 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 <chainparams.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <injector.h>
#include <miner.h>
#include <random.h>
#include <test/test_unite.h>
#include <validation.h>
#include <validationinterface.h>
#include <snapshot/messages.h>
#include <esperanza/finalizationstate.h>
#include <wallet/test/wallet_test_fixture.h>
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, TestChain100Setup)
struct TestSubscriber : public CValidationInterface {
uint256 m_expected_tip;
TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload)
{
BOOST_CHECK_EQUAL(m_expected_tip, pindexNew->GetBlockHash());
}
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex, const std::vector<CTransactionRef>& txnConflicted)
{
BOOST_CHECK_EQUAL(m_expected_tip, block->hashPrevBlock);
BOOST_CHECK_EQUAL(m_expected_tip, pindex->pprev->GetBlockHash());
m_expected_tip = block->GetHash();
}
void BlockDisconnected(const std::shared_ptr<const CBlock>& block)
{
BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash());
m_expected_tip = block->hashPrevBlock;
}
};
struct BlockData {
std::shared_ptr<CBlock> block;
CBlockIndex block_index;
snapshot::SnapshotHash hash;
uint32_t height;
};
BlockData Block(const BlockData &prevData)
{
static int i = 0;
static uint64_t time = Params().GenesisBlock().nTime;
CScript pubKey;
pubKey << i++ << OP_TRUE;
auto ptemplate = BlockAssembler(Params()).CreateNewBlock(pubKey);
auto pblock = std::make_shared<CBlock>(ptemplate->block);
pblock->hashPrevBlock = prevData.block->GetHash();
pblock->nTime = ++time;
CMutableTransaction txCoinbase(*pblock->vtx[0]);
txCoinbase.vout.resize(1);
std::vector<uint8_t> snapshotHash = prevData.hash.GetHashVector(prevData.block_index);
txCoinbase.vin[0].scriptSig = CScript() << (prevData.height + 1) << snapshotHash << OP_0;
txCoinbase.vin[0].scriptWitness.SetNull();
txCoinbase.vin[1].scriptWitness.SetNull();
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
snapshot::SnapshotHash newHash(prevData.hash.GetData());
const COutPoint out(pblock->vtx[0]->GetHash(), 0);
const Coin coin(pblock->vtx[0]->vout[0], prevData.height + 1, TxType::COINBASE);
newHash.AddUTXO(snapshot::UTXO(out, coin));
CBlockIndex bi;
bi.stake_modifier = prevData.block_index.stake_modifier;
bi.nBits = pblock->nBits;
bi.nChainWork = prevData.block_index.nChainWork + GetBlockProof(bi);
return BlockData{pblock, bi, newHash, prevData.height + 1};
}
std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
{
pblock->ComputeMerkleTrees();
return pblock;
}
// construct a valid block
const BlockData GoodBlock(const BlockData& prevData)
{
BlockData data = Block(prevData);
FinalizeBlock(data.block);
return data;
}
// construct an invalid block (but with a valid header)
const BlockData BadBlock(const BlockData& prevData)
{
BlockData data = Block(prevData);
CMutableTransaction coinbase_spend;
coinbase_spend.vin.push_back(CTxIn(COutPoint(data.block->vtx[0]->GetHash(), 0), CScript(), 0));
coinbase_spend.vout.push_back(data.block->vtx[0]->vout[0]);
CTransactionRef tx = MakeTransactionRef(coinbase_spend);
data.block->vtx.push_back(tx);
FinalizeBlock(data.block);
return data;
}
void BuildChain(const BlockData &root, int height, const unsigned int invalid_rate, const unsigned int branch_rate, const unsigned int max_size, std::vector<std::shared_ptr<const CBlock>>& blocks)
{
if (height <= 0 || blocks.size() >= max_size) return;
bool gen_invalid = GetRand(100) < invalid_rate;
bool gen_fork = GetRand(100) < branch_rate;
const BlockData blockData = gen_invalid ? BadBlock(root) : GoodBlock(root);
blocks.push_back(blockData.block);
if (!gen_invalid) {
BuildChain(blockData, height - 1, invalid_rate, branch_rate, max_size, blocks);
}
if (gen_fork) {
const BlockData data = GoodBlock(root);
blocks.push_back(data.block);
BuildChain(data, height - 1, invalid_rate, branch_rate, max_size, blocks);
}
}
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
{
// build a large-ish chain that's likely to have some forks
std::vector<std::shared_ptr<const CBlock>> blocks;
CChainParams params = Params();
esperanza::FinalizationParams fin_params = params.GetFinalization();
fin_params.epoch_length = 999999;
GetComponent<finalization::StateRepository>()->Reset(fin_params, params.GetAdminParams());
GetComponent<finalization::StateRepository>()->ResetToTip(*chainActive.Tip());
BlockData genesisData;
{
genesisData.block = std::make_shared<CBlock>(Params().GenesisBlock());
genesisData.height = 0;
CBlockIndex bi;
bi.nBits = genesisData.block->nBits;
bi.nChainWork = GetBlockProof(bi);
genesisData.block_index = bi;
for (size_t txIdx = 0; txIdx < genesisData.block->vtx.size(); ++txIdx) {
auto &tx = genesisData.block->vtx[txIdx];
for (size_t i = 0; i < tx->vout.size(); ++i) {
auto &out = tx->vout[i];
if (out.scriptPubKey.IsUnspendable()) {
continue;
}
const COutPoint outPoint(tx->GetHash(), i);
const Coin coin(out, 0, txIdx == 0 ? TxType::COINBASE : TxType::REGULAR);
genesisData.hash.AddUTXO(snapshot::UTXO(outPoint, coin));
}
}
}
while (blocks.size() < 50) {
blocks.clear();
BuildChain(genesisData, 100, 15, 10, 500, blocks);
}
bool ignored;
CValidationState state;
std::vector<CBlockHeader> headers;
std::transform(blocks.begin(), blocks.end(), std::back_inserter(headers), [](std::shared_ptr<const CBlock> b) { return b->GetBlockHeader(); });
// Process all the headers so we understand the toplogy of the chain
BOOST_CHECK(ProcessNewBlockHeaders(headers, state, Params()));
// Connect the genesis block and drain any outstanding events
ProcessNewBlock(Params(), std::make_shared<CBlock>(Params().GenesisBlock()), true, &ignored);
SyncWithValidationInterfaceQueue();
// subscribe to events (this subscriber will validate event ordering)
const CBlockIndex* initial_tip = nullptr;
{
LOCK(cs_main);
initial_tip = chainActive.Tip();
}
TestSubscriber sub(initial_tip->GetBlockHash());
RegisterValidationInterface(&sub);
// create a bunch of threads that repeatedly process a block generated above at random
// this will create parallelism and randomness inside validation - the ValidationInterface
// will subscribe to events generated during block validation and assert on ordering invariance
boost::thread_group threads;
// boost unit test is not thread safe as the checks record the results in some shared memory
// which is not synchronized / does not happen under mutual exclusion.
std::mutex cs;
const auto check = [&cs](bool condition) {
std::lock_guard<decltype(cs)> lock(cs);
BOOST_CHECK(condition);
};
for (int i = 0; i < 10; i++) {
threads.create_thread([&]() {
bool ignored;
for (int i = 0; i < 1000; i++) {
auto block = blocks[GetRand(blocks.size() - 1)];
bool processed = ProcessNewBlock(Params(), block, true, &ignored);
check(processed);
}
// to make sure that eventually we process the full chain - do it here
for (auto block : blocks) {
if (block->vtx.size() == 1) {
bool processed = ProcessNewBlock(Params(), block, true, &ignored);
check(processed);
}
}
});
}
threads.join_all();
while (GetMainSignals().CallbacksPending() > 0) {
MilliSleep(100);
}
UnregisterValidationInterface(&sub);
BOOST_CHECK_EQUAL(sub.m_expected_tip, chainActive.Tip()->GetBlockHash());
}
BOOST_AUTO_TEST_SUITE_END()