26#include <validation.h>
41static std::vector<std::shared_ptr<CBlock>> g_blocks;
43static std::vector<CTxIn> g_spend_candidate_txins;
50static std::vector<std::vector<uint8_t>> TAPROOT_OP_TRUE_WITNESS;
55static void InitTaprootScript()
58 uint256 internal_key{std::vector<uint8_t>(32, 1)};
59 auto res =
XOnlyPubKey(internal_key).CreateTapTweak(&merkle_tree_hash);
65 TAPROOT_OP_TRUE_WITNESS.clear();
67 TAPROOT_OP_TRUE_WITNESS.emplace_back(std::move(control));
98static void MaybeAddSpendCandidate(std::vector<CTxIn>& pool,
const CTransaction& tx, uint32_t vout_index)
101 if (tx.
vout[vout_index].scriptPubKey.IsUnspendable())
return;
102 pool.push_back(GetSpendingScript(tx, vout_index));
114 if (g_blocks.size() <= (
size_t)current_block->
nHeight) {
115 g_blocks.resize(current_block->
nHeight + 1);
118 g_blocks[current_block->
nHeight] = std::make_shared<CBlock>();
122 for (
const auto& tx : g_blocks[current_block->
nHeight]->vtx) {
123 for (uint32_t vout_index{0}; vout_index < tx->
vout.size(); ++vout_index) {
124 MaybeAddSpendCandidate(g_spend_candidate_txins, *tx, vout_index);
133static void LoadCurrentChain()
137 g_spend_candidate_txins.clear();
147 auto current_block = chainstate.
m_chain.
Tip();
149 while (current_block !=
nullptr) {
150 LoadCurrentBlock(chainstate, current_block);
152 current_block = current_block->
pprev;
158 std::reverse(g_spend_candidate_txins.begin(), g_spend_candidate_txins.end());
190 for (
size_t i = 1; i <= 10; i++) {
196 ctx.
vin[0] = g_spend_candidate_txins[i];
201 ctx.
vout[0].scriptPubKey = P2WSH_OP_TRUE;
204 ctx.
vout[1].scriptPubKey = P2SH_OP_TRUE;
207 ctx.
vout[2].scriptPubKey = TAPROOT_OP_TRUE;
224static void initialize_connect_block()
248 AddExtraTxsToMempool(*
g_setup);
258 auto current_block = chainstate.
m_chain.
Tip();
259 LoadCurrentBlock(chainstate, current_block);
269 std::vector<CTxIn>& additional_txins,
270 bool coinbase =
false,
271 int target_height = 0)
285 tx.
vin[0].prevout.SetNull();
288 tx.
vin[0].scriptSig =
CScript() << target_height;
292 tx.
vin[0].scriptSig.assign(script_sig.begin(), script_sig.end());
297 tx.
vin.resize(num_inputs);
298 for (
int i = 0; i < num_inputs; i++) {
301 uint32_t input_index = fuzzed_data_provider.
ConsumeIntegralInRange<uint32_t>(0, g_spend_candidate_txins.size() + additional_txins.size() - 1);
302 if (input_index < g_spend_candidate_txins.size()) {
304 tx.
vin[i] = g_spend_candidate_txins[input_index];
307 Assert((input_index - g_spend_candidate_txins.size()) < additional_txins.size());
308 tx.
vin[i] = additional_txins[input_index - g_spend_candidate_txins.size()];
326 tx.
vin[i].scriptWitness.stack.clear();
328 for (
int j = 0; j < num_wit; j++) {
337 tx.
vout.resize(num_outputs);
338 for (
int i = 0; i < num_outputs; i++) {
344 fuzzed_data_provider,
347 tx.
vout[i].scriptPubKey = P2WSH_OP_TRUE;
351 tx.
vout[i].scriptPubKey = P2SH_OP_TRUE;
355 tx.
vout[i].scriptPubKey = TAPROOT_OP_TRUE;
368 auto res = MakeTransactionRef(tx);
373 for (
int i = 0; i < num_outputs; i++) {
374 MaybeAddSpendCandidate(additional_txins, *res, i);
386 std::vector<CTxIn>& additional_txins)
391 block.
nVersion = g_blocks.back()->nVersion;
393 block.
nTime = g_blocks.back()->nTime + 2;
394 block.
nBits = g_blocks.back()->nBits;
412 block.
vtx.push_back(
ConsumeTransaction(fuzzed_data_provider, additional_txins,
true, target_height));
416 for (
int i = 0; i < num_tx; i++) {
453 std::vector<CTxIn> additional_txins;
454 CBlock block = ConsumeBlock(fuzzed_data_provider, *g_blocks.back(), active_tip->
nHeight + 1, additional_txins);
459 const auto& duplicates = g_blocks.back()->vtx;
466 new_index.pprev = active_tip;
468 new_index.phashBlock = ¤t_hash;
int64_t CAmount
Amount in satoshis (Can be negative).
constexpr CAmount COIN
The amount of satoshis in one BTC.
const TestingSetup * g_setup
#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
int nHeight
height of the entry in the chain. The genesis block has height 0
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
static constexpr uint32_t CURRENT_VERSION
const std::vector< CTxOut > vout
const Txid & GetHash() const LIFETIMEBOUND
An input of a transaction.
CScriptWitness scriptWitness
Only serialized through CTransaction.
An output of a transaction.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CTxMemPool * GetMempool()
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(bool ConnectBlock(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the best known block, and make it the tip of the block chain.
node::BlockManager & m_blockman
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
T ConsumeIntegralInRange(T min, T max)
XOnlyPubKey()=default
Construct an empty x-only pubkey.
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
static transaction_identifier FromUint256(const uint256 &id)
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
constexpr 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.
uint256 ComputeTapleafHash(uint8_t leaf_version, std::span< const unsigned char > script)
Compute the BIP341 tapleaf hash from leaf version & script.
constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
std::shared_ptr< const CTransaction > CTransactionRef
std::vector< unsigned char > ToByteVector(const T &in)
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< std::vector< unsigned char > > stack
std::function< void()> m_make_chainman
void LoadVerifyActivateChainstate()
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
Testing setup that configures a complete environment.
Block template creation options.
CScript coinbase_output_script
Script to put in the coinbase transaction.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< KernelNotifications > notifications
Issues blocking calls about sync status, errors and warnings.
SeedRandomStateForTest(SeedRand::ZEROS)
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
CMutableTransaction ConsumeTransaction(FuzzedDataProvider &fuzzed_data_provider, const std::optional< std::vector< Txid > > &prevout_txids, const int max_num_in, const int max_num_out) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(std::chrono::time_point< NodeClock, std::chrono::seconds > mock)