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)};
65 TAPROOT_OP_TRUE_WITNESS.clear();
67 TAPROOT_OP_TRUE_WITNESS.emplace_back(std::move(control));
84 res.scriptSig = P2SH_OP_TRUE_UNLOCK;
89 res.scriptWitness.stack = TAPROOT_OP_TRUE_WITNESS;
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());
170 setup.m_node.chainman.reset();
171 setup.m_node.notifications->m_shutdown_on_fatal_error =
false;
172 setup.m_make_chainman();
173 setup.LoadVerifyActivateChainstate();
180 setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
189 Assert(
setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == 0);
190 for (
size_t i = 1; i <= 10; i++) {
196 ctx.
vin[0] = g_spend_candidate_txins[i];
204 ctx.
vout[1].scriptPubKey = P2SH_OP_TRUE;
207 ctx.
vout[2].scriptPubKey = TAPROOT_OP_TRUE;
217 Assert(
setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == i);
219 setup.m_node.chainman->ActiveChainstate().GetMempool()->PrioritiseTransaction(ctx.
GetHash(),
COIN);
224static void initialize_connect_block()
227 static auto testing_setup = MakeNoLogFileContext<TestingSetup>(
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++) {
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++) {
329 tx.
vin[i].scriptWitness.stack.push_back(ConsumeRandomLengthByteVector<unsigned char>(
fuzzed_data_provider, 100));
337 tx.
vout.resize(num_outputs);
338 for (
int i = 0; i < num_outputs; i++) {
351 tx.
vout[i].scriptPubKey = P2SH_OP_TRUE;
355 tx.
vout[i].scriptPubKey = TAPROOT_OP_TRUE;
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;
416 for (
int i = 0; i < num_tx; i++) {
453 std::vector<CTxIn> 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.
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)
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.
const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
const CScript P2WSH_OP_TRUE
void SetMockTime(std::chrono::time_point< NodeClock, std::chrono::seconds > mock)
FuzzedDataProvider & fuzzed_data_provider