Bitcoin Core 31.99.0
P2P Digital Currency
utxo_total_supply.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <chainparams.h>
6#include <consensus/amount.h>
7#include <consensus/merkle.h>
8#include <kernel/coinstats.h>
9#include <node/miner.h>
10#include <primitives/block.h>
12#include <script/script.h>
13#include <sync.h>
15#include <test/fuzz/fuzz.h>
16#include <test/fuzz/util.h>
17#include <test/util/mining.h>
18#include <test/util/random.h>
20#include <test/util/time.h>
21#include <txdb.h>
22#include <uint256.h>
23#include <util/check.h>
24#include <validation.h>
25
26#include <cstddef>
27#include <cstdint>
28#include <functional>
29#include <memory>
30#include <optional>
31#include <utility>
32#include <vector>
33
34FUZZ_TARGET(utxo_total_supply)
35{
37 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
38 NodeClockContext clock_ctx{ConsumeTime(fuzzed_data_provider, /*min=*/1296688602)}; // regtest genesis block timestamp
40 ChainTestingSetup test_setup{
42 {
43 .extra_args = {
44 "-testactivationheight=bip34@2",
45 },
46 },
47 };
48 // Create chainstate
49 test_setup.LoadVerifyActivateChainstate();
50 auto& node{test_setup.m_node};
51 auto& chainman{*Assert(test_setup.m_node.chainman)};
52
53 const auto ActiveHeight = [&]() {
54 LOCK(chainman.GetMutex());
55 return chainman.ActiveHeight();
56 };
57 const auto PrepareNextBlock = [&]() {
58 // Use OP_FALSE to avoid BIP30 check from hitting early
59 auto block = PrepareBlock(node, {
60 .coinbase_output_script = CScript() << OP_FALSE,
61 });
62 // Replace OP_FALSE with OP_TRUE
63 {
64 CMutableTransaction tx{*block->vtx.back()};
65 tx.nLockTime = 0; // Use the same nLockTime for all as we want to duplicate one of them.
66 tx.vout.at(0).scriptPubKey = CScript{} << OP_TRUE;
67 block->vtx.back() = MakeTransactionRef(tx);
68 }
69 return block;
70 };
71
73 auto current_block = PrepareNextBlock();
75 std::vector<std::pair<COutPoint, CTxOut>> txos;
77 kernel::CCoinsStats utxo_stats;
79 CAmount circulation{0};
80
81
82 // Store the tx out in the txo map
83 const auto StoreLastTxo = [&]() {
84 // get last tx
85 const CTransaction& tx = *current_block->vtx.back();
86 // get last out
87 const uint32_t i = tx.vout.size() - 1;
88 // store it
89 txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
90 if (current_block->vtx.size() == 1 && tx.vout.at(i).scriptPubKey[0] == OP_RETURN) {
91 // also store coinbase
92 const uint32_t i = tx.vout.size() - 2;
93 txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
94 }
95 };
96 const auto AppendRandomTxo = [&](CMutableTransaction& tx) {
97 const auto& txo = txos.at(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, txos.size() - 1));
98 tx.vin.emplace_back(txo.first);
99 tx.vout.emplace_back(txo.second.nValue, txo.second.scriptPubKey); // "Forward" coin with no fee
100 };
101 const auto UpdateUtxoStats = [&](bool wipe_cache) {
102 LOCK(chainman.GetMutex());
103 chainman.ActiveChainstate().ForceFlushStateToDisk(wipe_cache);
104 utxo_stats = std::move(
105 *Assert(kernel::ComputeUTXOStats(kernel::CoinStatsHashType::NONE, &chainman.ActiveChainstate().CoinsDB(), chainman.m_blockman, {})));
106 // Check that miner can't print more money than they are allowed to
107 assert(circulation == utxo_stats.total_amount);
108 };
109
110
111 // Update internal state to chain tip
112 StoreLastTxo();
113 UpdateUtxoStats(/*wipe_cache=*/fuzzed_data_provider.ConsumeBool());
114 assert(ActiveHeight() == 0);
115 // Get at which height we duplicate the coinbase
116 // Assuming that the fuzzer will mine relatively short chains (less than 200 blocks), we want the duplicate coinbase to be not too high.
117 // Up to 300 seems reasonable.
118 int64_t duplicate_coinbase_height = fuzzed_data_provider.ConsumeIntegralInRange(0, 300);
119 // Avoid bad-cb-length error at heights <= 16. Pad the BIP34-encoded height
120 // with OP_0 to satisfy the minimum 2-byte coinbase scriptSig length.
121 CScript duplicate_coinbase_script = CScript() << duplicate_coinbase_height;
122 if (duplicate_coinbase_height <= 16) {
123 duplicate_coinbase_script << OP_0;
124 }
125 // Mine the first block with this duplicate
126 current_block = PrepareNextBlock();
127 StoreLastTxo();
128
129 {
130 // Create duplicate (CScript should match exact format as in CreateNewBlock)
131 CMutableTransaction tx{*current_block->vtx.front()};
132 tx.vin.at(0).scriptSig = duplicate_coinbase_script;
133
134 // Mine block and create next block template
135 current_block->vtx.front() = MakeTransactionRef(tx);
136 }
137 current_block->hashMerkleRoot = BlockMerkleRoot(*current_block);
138 assert(!MineBlock(node, current_block).IsNull());
139 circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
140
141 assert(ActiveHeight() == 1);
142 UpdateUtxoStats(/*wipe_cache=*/fuzzed_data_provider.ConsumeBool());
143 current_block = PrepareNextBlock();
144 StoreLastTxo();
145
146 // Limit to avoid timeout, but enough to cover duplicate_coinbase_height
147 // and CVE-2018-17144.
149 {
150 CallOneOf(
152 [&] {
153 // Append an input-output pair to the last tx in the current block
154 CMutableTransaction tx{*current_block->vtx.back()};
155 AppendRandomTxo(tx);
156 current_block->vtx.back() = MakeTransactionRef(tx);
157 StoreLastTxo();
158 },
159 [&] {
160 // Append a tx to the list of txs in the current block
162 AppendRandomTxo(tx);
163 current_block->vtx.push_back(MakeTransactionRef(tx));
164 StoreLastTxo();
165 },
166 [&] {
167 // Append the current block to the active chain
168 node::RegenerateCommitments(*current_block, chainman);
169 const bool was_valid = !MineBlock(node, current_block).IsNull();
170
171 const uint256 prev_hash_serialized{utxo_stats.hashSerialized};
172 if (was_valid) {
173 if (duplicate_coinbase_height == ActiveHeight()) {
174 // we mined the duplicate coinbase
175 assert(current_block->vtx.at(0)->vin.at(0).scriptSig == duplicate_coinbase_script);
176 }
177
178 circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
179 }
180
181 UpdateUtxoStats(/*wipe_cache=*/fuzzed_data_provider.ConsumeBool());
182
183 if (!was_valid) {
184 // utxo stats must not change
185 assert(prev_hash_serialized == utxo_stats.hashSerialized);
186 }
187
188 current_block = PrepareNextBlock();
189 StoreLastTxo();
190 });
191 }
192}
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:116
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
bool IsNull() const
Definition: transaction.h:42
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
const std::vector< CTxOut > vout
Definition: transaction.h:292
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:328
T ConsumeIntegralInRange(T min, T max)
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
T & front()
Definition: prevector.h:400
256-bit opaque blob.
Definition: uint256.h:196
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
static std::optional< CCoinsStats > ComputeUTXOStats(T hash_obj, CCoinsView *view, node::BlockManager &blockman, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
Definition: coinstats.cpp:111
Definition: messages.h:21
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
Definition: miner.cpp:88
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
@ OP_FALSE
Definition: script.h:78
@ OP_TRUE
Definition: script.h:85
@ OP_0
Definition: script.h:77
@ OP_RETURN
Definition: script.h:112
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxIn > vin
Definition: transaction.h:359
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:98
std::optional< CAmount > total_amount
The total amount, or nullopt if an overflow occurred calculating it.
Definition: coinstats.h:41
uint256 hashSerialized
Definition: coinstats.h:38
#define LOCK(cs)
Definition: sync.h:268
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:37
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Prepare a block to be mined.
Definition: mining.cpp:131
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
Definition: mining.cpp:78
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
FUZZ_TARGET(utxo_total_supply)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39