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)
69 auto pblock = std::make_shared<CBlock>(ptemplate->block);
70 pblock->hashPrevBlock = prev_hash;
71 pblock->nTime = ++time;
77 txCoinbase.
vout.resize(2);
79 txCoinbase.
vout[1].nValue = txCoinbase.
vout[0].nValue;
80 txCoinbase.
vout[0].nValue = 0;
81 txCoinbase.
vin[0].scriptWitness.SetNull();
89 std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
109 std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(
const uint256& prev_hash)
111 return FinalizeBlock(Block(prev_hash));
115 std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(
const uint256& prev_hash)
117 auto pblock = Block(prev_hash);
121 coinbase_spend.
vout.push_back(pblock->vtx[0]->vout[0]);
124 pblock->vtx.push_back(tx);
126 auto ret = FinalizeBlock(pblock);
130 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)
132 if (height <= 0 || blocks.size() >= max_size)
return;
137 const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
138 blocks.push_back(pblock);
140 BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
144 blocks.push_back(GoodBlock(root));
145 BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
152 std::vector<std::shared_ptr<const CBlock>> blocks;
153 while (blocks.size() < 50) {
155 BuildChain(
Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
169 auto sub = std::make_shared<TestSubscriber>(initial_tip->
GetBlockHash());
175 std::vector<std::thread> threads;
177 for (
int i = 0; i < 10; i++) {
178 threads.emplace_back([&]() {
181 for (
int i = 0; i < 1000; i++) {
182 auto block = blocks[insecure.
randrange(blocks.size() - 1)];
187 for (
const auto& block : blocks) {
188 if (block->vtx.size() == 1) {
196 for (
auto&
t : threads) {
227 auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) ->
bool {
232 BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(
Params().GenesisBlock())));
233 auto last_mined = GoodBlock(
Params().GenesisBlock().GetHash());
234 BOOST_REQUIRE(ProcessBlock(last_mined));
237 for (
int test_runs = 3; test_runs > 0; --test_runs) {
241 const uint256 split_hash{last_mined->hashPrevBlock};
245 std::vector<CTransactionRef> txs;
246 for (
int num_txs = 22; num_txs > 0; --num_txs) {
250 mtx.
vout.push_back(last_mined->vtx[0]->vout[1]);
251 mtx.
vout[0].nValue -= 1000;
254 last_mined = GoodBlock(last_mined->GetHash());
255 BOOST_REQUIRE(ProcessBlock(last_mined));
260 last_mined = GoodBlock(last_mined->GetHash());
261 BOOST_REQUIRE(ProcessBlock(last_mined));
265 const uint256 tip_init{last_mined->GetHash()};
267 std::vector<std::shared_ptr<const CBlock>> reorg;
268 last_mined = GoodBlock(split_hash);
269 reorg.push_back(last_mined);
271 last_mined = GoodBlock(last_mined->GetHash());
272 reorg.push_back(last_mined);
278 for (
const auto& tx : txs) {
292 std::thread rpc_thread{[&]() {
316 for (
const auto& b : reorg) {
333 CBlock pblock = ptemplate->block;
345 CTxOut min_plus_one = witness;
352 txCoinbase.
vout.resize(4);
353 txCoinbase.
vout[0] = witness;
354 txCoinbase.
vout[1] = witness;
355 txCoinbase.
vout[2] = min_plus_one;
356 txCoinbase.
vout[3] = invalid;
std::shared_ptr< const CTransaction > CTransactionRef
void resize(size_type new_size)
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Generate a new block, without valid proof-of-work.
CBlockIndex * pprev
pointer to the index of the predecessor of this block
static const CScript P2WSH_OP_TRUE
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule) ...
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)
const CBlock & GenesisBlock() const
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
const ResultType m_result_type
Result type.
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Implement this to subscribe to events generated in validation.
std::unique_ptr< CTxMemPool > mempool
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
uint256 GetBlockHash() const
std::shared_ptr< const CBlock > BadBlock(const uint256 &prev_hash)
static uint64_t InsecureRandRange(uint64_t range)
std::shared_ptr< const CBlock > GoodBlock(const uint256 &prev_hash)
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
BOOST_AUTO_TEST_SUITE_END()
std::shared_ptr< CBlock > Block(const uint256 &prev_hash)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
An output of a transaction.
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
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.
An outpoint - a combination of a transaction hash and an index n into its vout.
std::vector< CTxOut > vout
Validation result for a single transaction mempool acceptance.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::vector< CTransactionRef > vtx
#define BOOST_CHECK_EQUAL(v1, v2)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
Serialized script, used inside transaction inputs and outputs.
TestSubscriber(uint256 tip)
std::shared_ptr< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
A mutable version of CTransaction.
const Consensus::Params & GetConsensus() const
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found.
Identical to TestingSetup, but chain set to regtest.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
std::unique_ptr< ChainstateManager > chainman
#define Assert(val)
Identity function.
#define BOOST_CHECK(expr)