20 pk_type = fuzzed_data_provider.PickValueInArray({0x02, 0x03});
22 pk_type = fuzzed_data_provider.PickValueInArray({0x04, 0x06, 0x07});
31 return fuzzed_data_provider.ConsumeIntegralInRange<
CAmount>(0, max.value_or(
MAX_MONEY));
39 return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(min.value_or(time_min), max.value_or(time_max));
45 const auto p2wsh_op_true = fuzzed_data_provider.ConsumeBool();
46 tx_mut.
version = fuzzed_data_provider.ConsumeBool() ?
48 fuzzed_data_provider.ConsumeIntegral<uint32_t>();
49 tx_mut.
nLockTime = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
50 const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, max_num_in);
51 const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, max_num_out);
52 for (
int i = 0; i < num_in; ++i) {
53 const auto& txid_prev = prevout_txids ?
54 PickValue(fuzzed_data_provider, *prevout_txids) :
56 const auto index_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, max_num_out);
71 tx_mut.
vin.push_back(in);
73 for (
int i = 0; i < num_out; ++i) {
74 const auto amount = fuzzed_data_provider.ConsumeIntegralInRange<
CAmount>(-10, 50 *
COIN + 10);
75 const auto script_pk = p2wsh_op_true ?
78 tx_mut.
vout.emplace_back(amount, script_pk);
86 const auto n_elements = fuzzed_data_provider.ConsumeIntegralInRange<
size_t>(0, max_stack_elem_size);
87 for (
size_t i = 0; i < n_elements; ++i) {
99 static constexpr unsigned MAX_BUFFER_SZ{128};
100 std::vector<uint8_t> buffer(MAX_BUFFER_SZ, uint8_t{
'a'});
101 while (fuzzed_data_provider.ConsumeBool()) {
103 fuzzed_data_provider,
106 r_script.insert(r_script.end(), buffer.begin(), buffer.begin() + fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BUFFER_SZ));
110 r_script << std::vector<uint8_t>{buffer.begin(), buffer.begin() + fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BUFFER_SZ)};
116 r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
117 int num_data{fuzzed_data_provider.ConsumeIntegralInRange(1, 22)};
119 auto pubkey_bytes{
ConstructPubKeyBytes(fuzzed_data_provider, buffer, fuzzed_data_provider.ConsumeBool())};
120 if (fuzzed_data_provider.ConsumeBool()) {
121 pubkey_bytes.back() = num_data;
123 r_script << pubkey_bytes;
125 r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
130 std::copy(vec.begin(), vec.end(), buffer.begin());
134 r_script << fuzzed_data_provider.ConsumeIntegral<int64_t>();
146 if (maybe_p2wsh && fuzzed_data_provider.ConsumeBool()) {
157 return fuzzed_data_provider.ConsumeBool() ?
158 fuzzed_data_provider.PickValueInArray({
163 fuzzed_data_provider.ConsumeIntegral<uint32_t>();
168 std::map<COutPoint, Coin> coins;
170 const std::optional<COutPoint> outpoint{ConsumeDeserializable<COutPoint>(fuzzed_data_provider)};
174 const std::optional<Coin> coin{ConsumeDeserializable<Coin>(fuzzed_data_provider)};
178 coins[*outpoint] = *coin;
188 fuzzed_data_provider,
193 bool compressed = fuzzed_data_provider.ConsumeBool();
195 fuzzed_data_provider,
221 if (program.size() < 2) {
224 tx_destination =
WitnessUnknown{fuzzed_data_provider.ConsumeIntegralInRange<
unsigned int>(2, 16), program};
226 Assert(call_size == std::variant_size_v<CTxDestination>);
227 return tx_destination;
232 auto key_data = fuzzed_data_provider.ConsumeBytes<uint8_t>(32);
235 bool compressed_value = compressed ? *compressed : fuzzed_data_provider.ConsumeBool();
236 key.
Set(key_data.begin(), key_data.end(), compressed_value);
242 for (
const CTxIn& tx_in : tx.vin) {
243 const Coin& coin = inputs.AccessCoin(tx_in.
prevout);
278#if defined _GNU_SOURCE && (defined(__linux__) || defined(__FreeBSD__))
279 const cookie_io_functions_t io_hooks = {
285 return fopencookie(
this, mode.c_str(), io_hooks);
300 if (random_bytes.empty()) {
303 std::memcpy(buf, random_bytes.data(), random_bytes.size());
307 fuzzed_file->
m_offset += random_bytes.size();
308 return random_bytes.size();
325 assert(whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END);
328 int64_t new_offset = 0;
329 if (whence == SEEK_SET) {
330 new_offset = *offset;
331 }
else if (whence == SEEK_CUR) {
335 new_offset = fuzzed_file->
m_offset + *offset;
336 }
else if (whence == SEEK_END) {
341 new_offset = n + *offset;
343 if (new_offset < 0) {
347 *offset = new_offset;
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
An encapsulated private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
An outpoint - a combination of a transaction hash and an index n into its vout.
An encapsulated public key.
static constexpr unsigned int COMPRESSED_SIZE
static constexpr unsigned int SIZE
secp256k1:
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
static const uint32_t CURRENT_VERSION
An input of a transaction.
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
CScriptWitness scriptWitness
Only serialized through CTransaction.
bool IsSpent() const
Either this coin never existed (see e.g.
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
static ssize_t write(void *cookie, const char *buf, size_t size)
FuzzedDataProvider & m_fuzzed_data_provider
static int seek(void *cookie, int64_t *offset, int whence)
static int close(void *cookie)
static ssize_t read(void *cookie, char *buf, size_t size)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr unsigned char * begin()
static transaction_identifier FromUint256(const uint256 &id)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
bool AdditionOverflow(const T i, const T j) noexcept
std::vector< unsigned char > ToByteVector(const T &in)
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
CTxDestination subtype to encode any future Witness version.
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) 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
bool ContainsSpentInput(const CTransaction &tx, const CCoinsViewCache &inputs) noexcept
CScriptWitness ConsumeScriptWitness(FuzzedDataProvider &fuzzed_data_provider, const size_t max_stack_elem_size) noexcept
CKey ConsumePrivateKey(FuzzedDataProvider &fuzzed_data_provider, std::optional< bool > compressed) noexcept
std::vector< uint8_t > ConstructPubKeyBytes(FuzzedDataProvider &fuzzed_data_provider, Span< const uint8_t > byte_data, const bool compressed) noexcept
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
std::map< COutPoint, Coin > ConsumeCoins(FuzzedDataProvider &fuzzed_data_provider) noexcept
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
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
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
void SetFuzzedErrNo(FuzzedDataProvider &fuzzed_data_provider, const std::array< T, size > &errnos)
Sets errno to a value selected from the given std::array errnos.
uint160 ConsumeUInt160(FuzzedDataProvider &fuzzed_data_provider) noexcept
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
static const CScript P2WSH_OP_TRUE
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
static constexpr uint32_t MAX_BIP125_RBF_SEQUENCE