5 #include <boost/test/unit_test.hpp>
7 #include <chainparams.h>
29 std::shared_ptr<CBlock>
FinalizeBlock(std::shared_ptr<CBlock> pblock);
30 void BuildChain(
const uint256& 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);
51 m_expected_tip = block->GetHash();
59 m_expected_tip = block->hashPrevBlock;
63 std::shared_ptr<CBlock> MinerTestingSetup::Block(
const uint256& prev_hash)
68 BlockAssembler::Options options;
70 auto pblock = std::make_shared<CBlock>(ptemplate->block);
71 pblock->hashPrevBlock = prev_hash;
72 pblock->nTime = ++time;
78 txCoinbase.
vout.resize(2);
80 txCoinbase.
vout[1].nValue = txCoinbase.
vout[0].nValue;
81 txCoinbase.
vout[0].nValue = 0;
82 txCoinbase.
vin[0].scriptWitness.SetNull();
90 std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
110 std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(
const uint256& prev_hash)
112 return FinalizeBlock(Block(prev_hash));
116 std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(
const uint256& prev_hash)
118 auto pblock = Block(prev_hash);
122 coinbase_spend.
vout.push_back(pblock->vtx[0]->vout[0]);
125 pblock->vtx.push_back(tx);
127 auto ret = FinalizeBlock(pblock);
132 void MinerTestingSetup::BuildChain(
const uint256& 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)
134 if (height <= 0 || blocks.size() >= max_size)
return;
139 const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
140 blocks.push_back(pblock);
142 BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
146 blocks.push_back(GoodBlock(root));
147 BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
154 std::vector<std::shared_ptr<const CBlock>> blocks;
155 while (blocks.size() < 50) {
157 BuildChain(
Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
171 auto sub = std::make_shared<TestSubscriber>(initial_tip->
GetBlockHash());
177 std::vector<std::thread> threads;
179 for (
int i = 0; i < 10; i++) {
180 threads.emplace_back([&]() {
183 for (
int i = 0; i < 1000; i++) {
184 auto block = blocks[insecure.
randrange(blocks.size() - 1)];
189 for (
const auto& block : blocks) {
190 if (block->vtx.size() == 1) {
191 bool processed = Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored);
198 for (
auto&
t : threads) {
229 auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) ->
bool {
234 BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(
Params().GenesisBlock())));
235 auto last_mined = GoodBlock(
Params().GenesisBlock().GetHash());
236 BOOST_REQUIRE(ProcessBlock(last_mined));
239 for (
int test_runs = 3; test_runs > 0; --test_runs) {
243 const uint256 split_hash{last_mined->hashPrevBlock};
247 std::vector<CTransactionRef> txs;
248 for (
int num_txs = 22; num_txs > 0; --num_txs) {
252 mtx.
vout.push_back(last_mined->vtx[0]->vout[1]);
253 mtx.
vout[0].nValue -= 1000;
256 last_mined = GoodBlock(last_mined->GetHash());
257 BOOST_REQUIRE(ProcessBlock(last_mined));
262 last_mined = GoodBlock(last_mined->GetHash());
263 BOOST_REQUIRE(ProcessBlock(last_mined));
267 const uint256 tip_init{last_mined->GetHash()};
269 std::vector<std::shared_ptr<const CBlock>> reorg;
270 last_mined = GoodBlock(split_hash);
271 reorg.push_back(last_mined);
273 last_mined = GoodBlock(last_mined->GetHash());
274 reorg.push_back(last_mined);
280 for (
const auto& tx : txs) {
293 std::thread rpc_thread{[&]() {
317 for (
const auto& b : reorg) {
333 BlockAssembler::Options options;
335 CBlock pblock = ptemplate->block;
347 CTxOut min_plus_one = witness;
354 txCoinbase.
vout.resize(4);
355 txCoinbase.
vout[0] = witness;
356 txCoinbase.
vout[1] = witness;
357 txCoinbase.
vout[2] = min_plus_one;
358 txCoinbase.
vout[3] = invalid;
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint256 GetBlockHash() const
const CBlock & GenesisBlock() const
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
An output of a transaction.
Implement this to subscribe to events generated in validation and mempool.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Generate a new block, without valid proof-of-work.
void resize(size_type new_size)
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
std::vector< CTxOut > vout
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
Identical to TestingSetup, but chain set to regtest.
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected Provides the block that was disconnected.
TestSubscriber(uint256 tip)
void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< ChainstateManager > chainman
void BuildChain(const uint256 &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)
std::shared_ptr< const CBlock > BadBlock(const uint256 &prev_hash)
std::shared_ptr< CBlock > Block(const uint256 &prev_hash)
std::shared_ptr< const CBlock > GoodBlock(const uint256 &prev_hash)
std::shared_ptr< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static uint64_t InsecureRandRange(uint64_t range)
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
static const CScript P2WSH_OP_TRUE
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)