8#include <chainparams.h>
44#include <boost/multi_index/detail/hash_index_iterator.hpp>
45#include <boost/operators.hpp>
65std::vector<std::pair<COutPoint, CAmount>> g_mature_coinbase;
70 std::shared_ptr<CBlock> block;
82 prefilledtxn.push_back(std::move(prefilledtx));
85 void RemoveCoinbasePrefill()
87 prefilledtxn.erase(prefilledtxn.begin());
90 void InsertCoinbaseShortTxID(uint64_t shorttxid)
92 shorttxids.insert(shorttxids.begin(), shorttxid);
95 void EraseShortTxIDs(
size_t index)
97 shorttxids.erase(shorttxids.begin() + index);
100 size_t PrefilledTxCount() {
101 return prefilledtxn.size();
104 size_t ShortTxIDCount() {
105 return shorttxids.size();
114 setup.m_node.mempool.reset();
118 setup.m_node.chainman.reset();
119 setup.m_make_chainman();
120 setup.LoadVerifyActivateChainstate();
126 g_mature_coinbase.clear();
132 CAmount subsidy{
setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
133 g_mature_coinbase.emplace_back(prevout, subsidy);
144 static const auto testing_setup = MakeNoLogFileContext<TestingSetup>();
147 ResetChainmanAndMempool(*
g_setup);
158 auto& mempool = *
setup->m_node.mempool;
161 chainman.DisableNextWrite();
162 const size_t initial_index_size{
WITH_LOCK(chainman.GetMutex(),
return chainman.BlockIndex().size())};
168 mempool, *
setup->m_node.warnings,
170 .deterministic_rng = true,
172 connman.SetMsgProc(peerman.get());
174 setup->m_node.validation_signals->RegisterValidationInterface(peerman.get());
175 setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
179 std::vector<CNode*> peers;
180 for (
int i = 0; i < 4; ++i) {
182 CNode& p2p_node = *peers.back();
184 connman.AddTestNode(p2p_node);
188 std::vector<BlockInfo> info;
191 std::vector<std::pair<COutPoint, CAmount>> mature_coinbase = g_mature_coinbase;
193 const uint64_t initial_sequence{
WITH_LOCK(mempool.cs,
return mempool.GetSequence())};
203 unsigned long mempool_size = mempool.size();
208 amount_in = tx->vout[0].nValue;
211 auto info_it = info.begin();
213 auto tx_it = info_it->block->vtx.begin();
215 outpoint =
COutPoint(tx_it->get()->GetHash(), 0);
216 amount_in = tx_it->get()->vout[0].nValue;
218 auto coinbase_it = mature_coinbase.begin();
220 outpoint = coinbase_it->first;
221 amount_in = coinbase_it->second;
225 const auto script_sig =
CScript{};
233 tx_mut.
vin.push_back(in);
235 const CAmount amount_out = amount_in - AMOUNT_FEE;
242 auto create_block = [&]() {
248 prev = chainman.ActiveChain().Tip()->GetBlockHash();
249 height = chainman.ActiveChain().Height() + 1;
252 prev = info[index].hash;
253 height = info[index].height + 1;
256 const auto new_time =
WITH_LOCK(
::cs_main,
return chainman.ActiveChain().Tip()->GetMedianTimePast() + 1);
261 header.
nBits = g_nBits;
262 header.
nTime = new_time;
265 std::shared_ptr<CBlock> block = std::make_shared<CBlock>();
269 coinbase_tx.
vin.resize(1);
270 coinbase_tx.
vin[0].prevout.SetNull();
272 coinbase_tx.
vout.resize(1);
277 const auto mempool_size = mempool.size();
284 for (
size_t i = random_idx; i < random_idx + num_txns; ++i) {
285 CTransactionRef mempool_tx = mempool.txns_randomized[i % mempool_size].second->GetSharedTx();
286 block->vtx.push_back(mempool_tx);
293 for (
size_t i = 0; i < new_txns; ++i) {
295 block->vtx.push_back(non_mempool_tx);
300 chainman.GenerateCoinbaseCommitment(*block, pindexPrev);
306 BlockInfo block_info;
307 block_info.block = block;
308 block_info.hash = block->GetHash();
309 block_info.height = height;
317 bool sent_net_msg =
true;
318 bool requested_hb =
false;
319 bool sent_sendcmpct =
false;
320 bool valid_sendcmpct =
false;
326 std::shared_ptr<CBlock> cblock;
330 size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, info.size() - 1);
331 cblock = info[index].block;
333 BlockInfo block_info = create_block();
334 cblock = block_info.block;
335 info.push_back(block_info);
339 FuzzedCBlockHeaderAndShortTxIDs cmpctblock(*cblock,
nonce);
342 CBlockHeaderAndShortTxIDs base_cmpctblock = cmpctblock;
343 net_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, base_cmpctblock);
348 size_t num_erased = 1;
349 size_t num_txs = cblock->vtx.size();
351 for (
size_t i = 0; i < num_txs; ++i) {
358 uint64_t coinbase_shortid = cmpctblock.GetShortID(cblock->vtx[0]->GetWitnessHash());
359 cmpctblock.RemoveCoinbasePrefill();
360 cmpctblock.InsertCoinbaseShortTxID(coinbase_shortid);
366 uint16_t prefill_idx = num_erased == 0 ? i : i - prev_idx - 1;
370 cmpctblock.AddPrefilledTx(std::move(prefilledtx));
373 cmpctblock.EraseShortTxIDs(i - num_erased);
377 assert(cmpctblock.PrefilledTxCount() + cmpctblock.ShortTxIDCount() == num_txs);
384 size_t num_blocks = info.size();
385 if (num_blocks == 0) {
386 sent_net_msg =
false;
392 const BlockInfo& block_info = info[index];
395 std::shared_ptr<CBlock> cblock = block_info.block;
397 for (
size_t i = 0; i < cblock->vtx.size(); i++) {
400 block_txn.
txn.push_back(cblock->vtx[i]);
407 size_t num_blocks = info.size();
408 if (num_blocks == 0) {
409 sent_net_msg =
false;
415 CBlock block = *info[index].block;
417 std::vector<CBlock> headers;
418 headers.emplace_back(block);
428 sent_sendcmpct =
true;
433 BlockInfo block_info = create_block();
434 info.push_back(block_info);
435 sent_net_msg =
false;
448 clock_ctx.set(tip_time);
451 sent_net_msg =
false;
459 connman.FlushSendBuffer(random_node);
460 (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
462 bool more_work{
true};
466 more_work = connman.ProcessMessagesOnce(random_node);
467 peerman->SendMessages(random_node);
470 std::vector<CNodeStats> stats;
471 connman.GetNodeStats(stats);
476 if (stat.m_bip152_highbandwidth_to) {
478 CNode* hb_peer = peers[stat.nodeid];
492 setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
493 setup->m_node.validation_signals->UnregisterAllValidationInterfaces();
496 const size_t end_index_size{
WITH_LOCK(chainman.GetMutex(),
return chainman.BlockIndex().size())};
497 const uint64_t end_sequence{
WITH_LOCK(mempool.cs,
return mempool.GetSequence())};
499 if (initial_index_size != end_index_size || initial_sequence != end_sequence) {
501 ResetChainmanAndMempool(*
g_setup);
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
const TestingSetup * g_setup
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Stochastic address manager.
std::vector< CTransactionRef > txn
CBlockHeaderAndShortTxIDs()=default
Dummy for deserialization.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
const CBlock & GenesisBlock() const
Information about a peer.
bool IsInboundConn() const
bool IsOutboundOrBlockRelayConn() const
bool IsManualConn() const
bool IsAddrFetchConn() const
std::atomic_bool fPauseSend
std::atomic_bool fDisconnect
bool m_bip152_highbandwidth_from
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
static const uint32_t CURRENT_VERSION
An input of a transaction.
CScriptWitness scriptWitness
Only serialized through CTransaction.
T ConsumeIntegralInRange(T min, T max)
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
static const uint256 ZERO
void MakeRandDeterministicDANGEROUS(const uint256 &seed) noexcept
Internal function to set g_determinstic_rng.
FUZZ_TARGET(cmpctblock,.init=initialize_cmpctblock)
void initialize_cmpctblock()
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
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.
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
constexpr const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
constexpr const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
constexpr const char * BLOCKTXN
Contains a BlockTransactions.
constexpr const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
constexpr const char * TX
The tx message transmits a single transaction.
static constexpr uint64_t CMPCTBLOCKS_VERSION
The compactblocks version we support.
static constexpr TransactionSerParams TX_WITH_WITNESS
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
void ResetIbd()
Reset the ibd cache to its initial state.
Testing setup that configures a complete environment.
bool include_dummy_extranonce
Whether to include an OP_0 as a dummy extraNonce in the template's coinbase.
CScript coinbase_output_script
Script to put in the coinbase transaction.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
void FinalizeHeader(CBlockHeader &header, const ChainstateManager &chainman)
COutPoint MineBlock(const NodeContext &node, const node::BlockAssembler::Options &assembler_options)
Returns the generated coin.
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
static const CScript P2WSH_OP_TRUE
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
static constexpr decltype(CTransaction::version) TRUC_VERSION
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
for(const CTxIn &txin :tx.vin)
FuzzedDataProvider & fuzzed_data_provider