5#include <boost/test/unit_test.hpp>
27 std::shared_ptr<CBlock>
FinalizeBlock(std::shared_ptr<CBlock> pblock);
28 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);
49 m_expected_tip = block->GetHash();
57 m_expected_tip = block->hashPrevBlock;
61std::shared_ptr<CBlock> MinerTestingSetup::Block(
const uint256& prev_hash)
67 auto pblock = std::make_shared<CBlock>(ptemplate->block);
68 pblock->hashPrevBlock = prev_hash;
69 pblock->nTime = ++time;
75 txCoinbase.
vout.resize(2);
77 txCoinbase.
vout[1].nValue = txCoinbase.
vout[0].nValue;
78 txCoinbase.
vout[0].nValue = 0;
79 txCoinbase.
vin[0].scriptWitness.SetNull();
87std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
107std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(
const uint256& prev_hash)
109 return FinalizeBlock(Block(prev_hash));
113std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(
const uint256& prev_hash)
115 auto pblock = Block(prev_hash);
119 coinbase_spend.
vout.push_back(pblock->vtx[0]->vout[0]);
122 pblock->vtx.push_back(tx);
124 auto ret = FinalizeBlock(pblock);
128void 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)
130 if (height <= 0 || blocks.size() >= max_size)
return;
135 const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
136 blocks.push_back(pblock);
138 BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
142 blocks.push_back(GoodBlock(root));
143 BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
150 std::vector<std::shared_ptr<const CBlock>> blocks;
151 while (blocks.size() < 50) {
153 BuildChain(
Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
167 auto sub = std::make_shared<TestSubscriber>(initial_tip->
GetBlockHash());
173 std::vector<std::thread> threads;
174 for (
int i = 0; i < 10; i++) {
175 threads.emplace_back([&]() {
178 for (
int i = 0; i < 1000; i++) {
179 auto block = blocks[insecure.
randrange(blocks.size() - 1)];
184 for (
auto block : blocks) {
185 if (block->vtx.size() == 1) {
186 bool processed = Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, &ignored);
193 for (
auto& t : threads) {
224 auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) ->
bool {
229 BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(
Params().GenesisBlock())));
230 auto last_mined = GoodBlock(
Params().GenesisBlock().GetHash());
231 BOOST_REQUIRE(ProcessBlock(last_mined));
234 for (
int test_runs = 3; test_runs > 0; --test_runs) {
238 const uint256 split_hash{last_mined->hashPrevBlock};
242 std::vector<CTransactionRef> txs;
243 for (
int num_txs = 22; num_txs > 0; --num_txs) {
247 mtx.
vout.push_back(last_mined->vtx[0]->vout[1]);
248 mtx.
vout[0].nValue -= 1000;
251 last_mined = GoodBlock(last_mined->GetHash());
252 BOOST_REQUIRE(ProcessBlock(last_mined));
257 last_mined = GoodBlock(last_mined->GetHash());
258 BOOST_REQUIRE(ProcessBlock(last_mined));
262 const uint256 tip_init{last_mined->GetHash()};
264 std::vector<std::shared_ptr<const CBlock>> reorg;
265 last_mined = GoodBlock(split_hash);
266 reorg.push_back(last_mined);
268 last_mined = GoodBlock(last_mined->GetHash());
269 reorg.push_back(last_mined);
275 for (
const auto& tx : txs) {
289 std::thread rpc_thread{[&]() {
313 for (
const auto& b : reorg) {
329 CBlock pblock = ptemplate->block;
341 CTxOut min_plus_one = witness;
348 txCoinbase.
vout.resize(4);
349 txCoinbase.
vout[0] = witness;
350 txCoinbase.
vout[1] = witness;
351 txCoinbase.
vout[2] = min_plus_one;
352 txCoinbase.
vout[3] = invalid;
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Generate a new block, without valid proof-of-work.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
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 input of a transaction.
An output of a transaction.
Implement this to subscribe to events generated in validation.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
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)
BOOST_AUTO_TEST_SUITE_END()
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#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
static uint64_t InsecureRandRange(uint64_t range)
A mutable version of CTransaction.
std::vector< CTxOut > vout
Validation result for a single transaction mempool acceptance.
const ResultType m_result_type
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< CTxMemPool > mempool
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.
TestSubscriber(uint256 tip)
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
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)
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< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
static const CScript P2WSH_OP_TRUE
MempoolAcceptResult AcceptToMemoryPool(CChainState &active_chainstate, CTxMemPool &pool, const CTransactionRef &tx, bool bypass_limits, bool test_accept)
(Try to) add a transaction to the memory pool.
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.