27#include <validation.h>
42static std::vector<std::shared_ptr<CBlock>> g_blocks;
44static std::vector<CTxIn> g_spend_candidate_txins;
51static std::vector<std::vector<uint8_t>> TAPROOT_OP_TRUE_WITNESS;
56static void InitTaprootScript()
59 uint256 internal_key{std::vector<uint8_t>(32, 1)};
66 TAPROOT_OP_TRUE_WITNESS.clear();
68 TAPROOT_OP_TRUE_WITNESS.emplace_back(std::move(control));
85 res.scriptSig = P2SH_OP_TRUE_UNLOCK;
90 res.scriptWitness.stack = TAPROOT_OP_TRUE_WITNESS;
99static void MaybeAddSpendCandidate(std::vector<CTxIn>& pool,
const CTransaction& tx, uint32_t vout_index)
102 if (tx.
vout[vout_index].scriptPubKey.IsUnspendable())
return;
103 pool.push_back(GetSpendingScript(tx, vout_index));
115 if (g_blocks.size() <= (
size_t)current_block->
nHeight) {
116 g_blocks.resize(current_block->
nHeight + 1);
119 g_blocks[current_block->
nHeight] = std::make_shared<CBlock>();
123 for (
const auto& tx : g_blocks[current_block->
nHeight]->vtx) {
124 for (uint32_t vout_index{0}; vout_index < tx->
vout.size(); ++vout_index) {
125 MaybeAddSpendCandidate(g_spend_candidate_txins, *tx, vout_index);
134static void LoadCurrentChain()
138 g_spend_candidate_txins.clear();
148 auto current_block = chainstate.
m_chain.
Tip();
150 while (current_block !=
nullptr) {
151 LoadCurrentBlock(chainstate, current_block);
153 current_block = current_block->
pprev;
159 std::reverse(g_spend_candidate_txins.begin(), g_spend_candidate_txins.end());
171 setup.m_node.block_template_manager.reset();
172 setup.m_node.chainman.reset();
173 setup.m_node.notifications->m_shutdown_on_fatal_error =
false;
174 setup.m_make_chainman();
175 setup.CreateBlockTemplateManager();
176 setup.LoadVerifyActivateChainstate();
183 setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
192 Assert(
setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == 0);
193 for (
size_t i = 1; i <= 10; i++) {
199 ctx.
vin[0] = g_spend_candidate_txins[i];
207 ctx.
vout[1].scriptPubKey = P2SH_OP_TRUE;
210 ctx.
vout[2].scriptPubKey = TAPROOT_OP_TRUE;
220 Assert(
setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == i);
222 setup.m_node.chainman->ActiveChainstate().GetMempool()->PrioritiseTransaction(ctx.
GetHash(),
COIN);
227static void initialize_connect_block()
230 static auto testing_setup = MakeNoLogFileContext<TestingSetup>(
251 AddExtraTxsToMempool(*
g_setup);
261 auto current_block = chainstate.
m_chain.
Tip();
262 LoadCurrentBlock(chainstate, current_block);
272 std::vector<CTxIn>& additional_txins,
273 bool coinbase =
false,
274 int target_height = 0)
288 tx.
vin[0].prevout.SetNull();
291 tx.
vin[0].scriptSig =
CScript() << target_height;
295 tx.
vin[0].scriptSig.assign(script_sig.begin(), script_sig.end());
300 auto& txin = tx.
vin.emplace_back();
304 if (input_index < g_spend_candidate_txins.size()) {
306 txin = g_spend_candidate_txins[input_index];
309 Assert((input_index - g_spend_candidate_txins.size()) < additional_txins.size());
310 txin = additional_txins[input_index - g_spend_candidate_txins.size()];
328 txin.scriptWitness.stack.clear();
330 txin.scriptWitness.stack.push_back(ConsumeRandomLengthByteVector<unsigned char>(
fuzzed_data_provider, 100));
338 auto& txout = tx.
vout.emplace_back();
351 txout.scriptPubKey = P2SH_OP_TRUE;
355 txout.scriptPubKey = TAPROOT_OP_TRUE;
359 txout.scriptPubKey =
CScript();
373 for (uint32_t i{0}; i < res->vout.size(); ++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;
452 std::vector<CTxIn> additional_txins;
458 const auto& duplicates = g_blocks.back()->vtx;
465 new_index.pprev = active_tip;
467 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.
An outpoint - a combination of a transaction hash and an index n into its vout.
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.
An output of a transaction.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
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(boo 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
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
T ConsumeIntegralInRange(T min, T max)
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
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)
LIMITED_WHILE(provider.remaining_bytes(), 10000)
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
is used externally by mining IPC clients, so it should only declare simple data definitions.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
std::vector< unsigned char > ToByteVector(const T &in)
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.
Validation result for a transaction evaluated by MemPoolAccept (single or package).
const ResultType m_result_type
Result type.
std::vector< const char * > extra_args
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< ChainstateManager > chainman
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)
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
@ 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
void SetMockTime(std::chrono::time_point< NodeClock, std::chrono::seconds > mock)
FuzzedDataProvider & fuzzed_data_provider