5#include <boost/test/unit_test.hpp>
7#include <chainparams.h>
30 std::shared_ptr<CBlock>
FinalizeBlock(std::shared_ptr<CBlock> pblock);
31 void BuildChain(
const uint256& root,
int height,
unsigned int invalid_rate,
unsigned int branch_rate,
unsigned int max_size, std::vector<std::shared_ptr<const CBlock>>& blocks);
52 m_expected_tip = block->GetHash();
60 m_expected_tip = block->hashPrevBlock;
64std::shared_ptr<CBlock> MinerTestingSetup::Block(
const uint256& prev_hash)
69 BlockAssembler::Options options;
71 options.include_dummy_extranonce =
true;
73 auto pblock = std::make_shared<CBlock>(ptemplate->block);
74 pblock->hashPrevBlock = prev_hash;
75 pblock->nTime = ++time;
81 txCoinbase.
vout.resize(2);
83 txCoinbase.
vout[1].nValue = txCoinbase.
vout[0].nValue;
84 txCoinbase.
vout[0].nValue = 0;
85 txCoinbase.
vin[0].scriptWitness.SetNull();
88 txCoinbase.
vin[0].scriptSig =
CScript{} << prev_height + 1 <<
OP_0;
89 txCoinbase.
nLockTime =
static_cast<uint32_t
>(prev_height);
95std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
115std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(
const uint256& prev_hash)
117 return FinalizeBlock(Block(prev_hash));
121std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(
const uint256& prev_hash)
123 auto pblock = Block(prev_hash);
127 coinbase_spend.
vout.push_back(pblock->vtx[0]->vout[0]);
130 pblock->vtx.push_back(tx);
132 auto ret = FinalizeBlock(pblock);
137void 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)
139 if (height <= 0 || blocks.size() >= max_size)
return;
141 bool gen_invalid = m_rng.randrange(100U) < invalid_rate;
142 bool gen_fork = m_rng.randrange(100U) < branch_rate;
144 const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
145 blocks.push_back(pblock);
147 BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
151 blocks.push_back(GoodBlock(root));
152 BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
159 std::vector<std::shared_ptr<const CBlock>> blocks;
160 while (blocks.size() < 50) {
162 BuildChain(
Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
176 auto sub = std::make_shared<TestSubscriber>(initial_tip->
GetBlockHash());
182 std::vector<std::thread> threads;
184 for (
int i = 0; i < 10; i++) {
185 threads.emplace_back([&]() {
188 for (
int i = 0; i < 1000; i++) {
189 const auto& block = blocks[insecure.
randrange(blocks.size() - 1)];
194 for (
const auto& block : blocks) {
195 if (block->vtx.size() == 1) {
196 bool processed = Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored);
203 for (
auto&
t : threads) {
234 auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) ->
bool {
240 auto last_mined = GoodBlock(
Params().GenesisBlock().GetHash());
244 for (
int test_runs = 3; test_runs > 0; --test_runs) {
248 const uint256 split_hash{last_mined->hashPrevBlock};
252 std::vector<CTransactionRef> txs;
253 for (
int num_txs = 22; num_txs > 0; --num_txs) {
257 mtx.
vout.push_back(last_mined->vtx[0]->vout[1]);
258 mtx.
vout[0].nValue -= 1000;
261 last_mined = GoodBlock(last_mined->GetHash());
267 last_mined = GoodBlock(last_mined->GetHash());
272 const uint256 tip_init{last_mined->GetHash()};
274 std::vector<std::shared_ptr<const CBlock>> reorg;
275 last_mined = GoodBlock(split_hash);
276 reorg.push_back(last_mined);
278 last_mined = GoodBlock(last_mined->GetHash());
279 reorg.push_back(last_mined);
285 for (
const auto& tx : txs) {
298 std::thread rpc_thread{[&]() {
322 for (
const auto& b : reorg) {
338 BlockAssembler::Options options;
339 options.coinbase_output_script = pubKey;
340 options.include_dummy_extranonce =
true;
342 CBlock pblock = ptemplate->block;
354 CTxOut min_plus_one = witness;
361 txCoinbase.
vout.resize(4);
362 txCoinbase.
vout[0] = witness;
363 txCoinbase.
vout[1] = witness;
364 txCoinbase.
vout[2] = min_plus_one;
365 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)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
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_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#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(const 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.
Information about chainstate that notifications are sent from.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< ChainstateManager > chainman
std::shared_ptr< const CBlock > BadBlock(const uint256 &prev_hash)
std::shared_ptr< CBlock > Block(const uint256 &prev_hash)
void BuildChain(const uint256 &root, int height, unsigned int invalid_rate, unsigned int branch_rate, unsigned int max_size, std::vector< std::shared_ptr< const CBlock > > &blocks)
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.
COutPoint ProcessBlock(const NodeContext &node, const std::shared_ptr< CBlock > &block)
Returns the generated coin (or Null if the block was invalid).
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
static const CScript P2WSH_OP_TRUE
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)