5 #include <boost/test/unit_test.hpp>
19 #define ASSERT_WITH_MSG(cond, msg) if (!cond) { BOOST_ERROR(msg); }
39 m_expected_tip = block->GetHash();
42 void BlockDisconnected(
const std::shared_ptr<const CBlock> &block,
const uint256& blockHash,
int nBlockHeight, int64_t blockTime)
46 m_expected_tip = block->hashPrevBlock;
59 auto pblock = std::make_shared<CBlock>(ptemplate->block);
60 pblock->hashPrevBlock = prev_hash;
61 pblock->nTime = ++time;
64 txCoinbase.
vout.resize(1);
65 pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
79 auto pblock =
Block(prev_hash);
83 coinbase_spend.
vout.emplace_back(pblock->vtx[0]->vout[0]);
86 pblock->vtx.emplace_back(tx);
92 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)
94 if (height <= 0 || blocks.size() >= max_size)
return;
96 bool gen_invalid =
GetRand(100) < invalid_rate;
97 bool gen_fork =
GetRand(100) < branch_rate;
99 const std::shared_ptr<const CBlock> pblock = gen_invalid ?
BadBlock(root) :
GoodBlock(root);
100 blocks.emplace_back(pblock);
102 BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
107 BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
114 std::vector<std::shared_ptr<const CBlock>> blocks;
115 while (blocks.size() < 50) {
121 BOOST_CHECK_MESSAGE(
ProcessNewBlock(std::make_shared<CBlock>(
Params().GenesisBlock()),
nullptr),
"Error: genesis not connected");
132 boost::thread_group threads;
133 for (
int i = 0; i < 10; i++) {
134 threads.create_thread([&blocks]() {
135 for (
int i = 0; i < 1000; i++) {
136 auto block = blocks[
GetRand(blocks.size() - 1)];
143 for (
const auto& block : blocks) {
144 if (block->vtx.size() == 1) {
149 if (sc.
get().
found && (stateReason ==
"duplicate" || stateReason ==
"prevblk-not-found" ||
150 stateReason ==
"bad-prevblk" || stateReason ==
"blk-out-of-order"))
continue;
const CChainParams & Params()
Return the currently selected parameters.
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn, CWallet *pwallet=nullptr, bool fProofOfStake=false, std::vector< CStakeableOutput > *availableCoins=nullptr, bool fNoMempoolTx=false, bool fTestValidity=true, CBlockIndex *prevBlock=nullptr, bool stopPoSOnNewBlock=true, bool fIncludeQfc=true)
Construct a new block template with coinbase to scriptPubKeyIn.
void setBlockHash(const uint256 &_hash)
BlockStateCatcher & get() const
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
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or nullptr if none.
const CBlock & GenesisBlock() const
size_t CallbacksPending()
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.
Implement this to subscribe to events generated in validation.
std::string GetRejectReason() const
BOOST_AUTO_TEST_SUITE_END()
RecursiveMutex cs_main
Global state.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
uint64_t GetRand(uint64_t nMax) noexcept
A mutable version of CTransaction.
std::vector< CTxOut > vout
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
TestSubscriber(uint256 tip)
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const uint256 &blockHash, int nBlockHeight, int64_t blockTime)
Notifies listeners of a block being disconnected.
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::shared_ptr< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
std::shared_ptr< const CTransaction > CTransactionRef
const uint256 UINT256_ZERO
constant uint256 instances
void MilliSleep(int64_t n)
bool ProcessNewBlock(const std::shared_ptr< const CBlock > &pblock, const FlatFilePos *dbp)
Process an incoming block.
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
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)
#define ASSERT_WITH_MSG(cond, msg)
const std::shared_ptr< const CBlock > GoodBlock(const uint256 &prev_hash)
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
std::shared_ptr< CBlock > Block(const uint256 &prev_hash)
const std::shared_ptr< const CBlock > BadBlock(const uint256 &prev_hash)
CMainSignals & GetMainSignals()
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
void RegisterValidationInterface(CValidationInterface *pwalletIn)
Register a wallet to receive updates from core.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...