Bitcoin Core  25.99.0
P2P Digital Currency
utxo_total_supply.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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/consensus.h>
7 #include <consensus/merkle.h>
8 #include <kernel/coinstats.h>
9 #include <node/miner.h>
10 #include <script/interpreter.h>
11 #include <streams.h>
13 #include <test/fuzz/fuzz.h>
14 #include <test/fuzz/util.h>
15 #include <test/util/mining.h>
16 #include <test/util/setup_common.h>
17 #include <util/chaintype.h>
18 #include <validation.h>
19 #include <version.h>
20 
21 FUZZ_TARGET(utxo_total_supply)
22 {
24  ChainTestingSetup test_setup{
26  {
27  "-testactivationheight=bip34@2",
28  },
29  };
30  // Create chainstate
31  test_setup.LoadVerifyActivateChainstate();
32  auto& node{test_setup.m_node};
33  auto& chainman{*Assert(test_setup.m_node.chainman)};
34  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
35 
36  const auto ActiveHeight = [&]() {
37  LOCK(chainman.GetMutex());
38  return chainman.ActiveHeight();
39  };
40  const auto PrepareNextBlock = [&]() {
41  // Use OP_FALSE to avoid BIP30 check from hitting early
42  auto block = PrepareBlock(node, CScript{} << OP_FALSE);
43  // Replace OP_FALSE with OP_TRUE
44  {
45  CMutableTransaction tx{*block->vtx.back()};
46  tx.vout.at(0).scriptPubKey = CScript{} << OP_TRUE;
47  block->vtx.back() = MakeTransactionRef(tx);
48  }
49  return block;
50  };
51 
53  auto current_block = PrepareNextBlock();
55  std::vector<std::pair<COutPoint, CTxOut>> txos;
57  kernel::CCoinsStats utxo_stats;
59  CAmount circulation{0};
60 
61 
62  // Store the tx out in the txo map
63  const auto StoreLastTxo = [&]() {
64  // get last tx
65  const CTransaction& tx = *current_block->vtx.back();
66  // get last out
67  const uint32_t i = tx.vout.size() - 1;
68  // store it
69  txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
70  if (current_block->vtx.size() == 1 && tx.vout.at(i).scriptPubKey[0] == OP_RETURN) {
71  // also store coinbase
72  const uint32_t i = tx.vout.size() - 2;
73  txos.emplace_back(COutPoint{tx.GetHash(), i}, tx.vout.at(i));
74  }
75  };
76  const auto AppendRandomTxo = [&](CMutableTransaction& tx) {
77  const auto& txo = txos.at(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, txos.size() - 1));
78  tx.vin.emplace_back(txo.first);
79  tx.vout.emplace_back(txo.second.nValue, txo.second.scriptPubKey); // "Forward" coin with no fee
80  };
81  const auto UpdateUtxoStats = [&]() {
82  LOCK(chainman.GetMutex());
83  chainman.ActiveChainstate().ForceFlushStateToDisk();
84  utxo_stats = std::move(
85  *Assert(kernel::ComputeUTXOStats(kernel::CoinStatsHashType::NONE, &chainman.ActiveChainstate().CoinsDB(), chainman.m_blockman, {})));
86  // Check that miner can't print more money than they are allowed to
87  assert(circulation == utxo_stats.total_amount);
88  };
89 
90 
91  // Update internal state to chain tip
92  StoreLastTxo();
93  UpdateUtxoStats();
94  assert(ActiveHeight() == 0);
95  // Get at which height we duplicate the coinbase
96  // Assuming that the fuzzer will mine relatively short chains (less than 200 blocks), we want the duplicate coinbase to be not too high.
97  // Up to 2000 seems reasonable.
98  int64_t duplicate_coinbase_height = fuzzed_data_provider.ConsumeIntegralInRange(0, 20 * COINBASE_MATURITY);
99  // Always pad with OP_0 at the end to avoid bad-cb-length error
100  const CScript duplicate_coinbase_script = CScript() << duplicate_coinbase_height << OP_0;
101  // Mine the first block with this duplicate
102  current_block = PrepareNextBlock();
103  StoreLastTxo();
104 
105  {
106  // Create duplicate (CScript should match exact format as in CreateNewBlock)
107  CMutableTransaction tx{*current_block->vtx.front()};
108  tx.vin.at(0).scriptSig = duplicate_coinbase_script;
109 
110  // Mine block and create next block template
111  current_block->vtx.front() = MakeTransactionRef(tx);
112  }
113  current_block->hashMerkleRoot = BlockMerkleRoot(*current_block);
114  assert(!MineBlock(node, current_block).IsNull());
115  circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
116 
117  assert(ActiveHeight() == 1);
118  UpdateUtxoStats();
119  current_block = PrepareNextBlock();
120  StoreLastTxo();
121 
122  // Limit to avoid timeout, but enough to cover duplicate_coinbase_height
123  // and CVE-2018-17144.
124  LIMITED_WHILE(fuzzed_data_provider.remaining_bytes(), 2'000)
125  {
126  CallOneOf(
127  fuzzed_data_provider,
128  [&] {
129  // Append an input-output pair to the last tx in the current block
130  CMutableTransaction tx{*current_block->vtx.back()};
131  AppendRandomTxo(tx);
132  current_block->vtx.back() = MakeTransactionRef(tx);
133  StoreLastTxo();
134  },
135  [&] {
136  // Append a tx to the list of txs in the current block
137  CMutableTransaction tx{};
138  AppendRandomTxo(tx);
139  current_block->vtx.push_back(MakeTransactionRef(tx));
140  StoreLastTxo();
141  },
142  [&] {
143  // Append the current block to the active chain
144  node::RegenerateCommitments(*current_block, chainman);
145  const bool was_valid = !MineBlock(node, current_block).IsNull();
146 
147  if (duplicate_coinbase_height == ActiveHeight()) {
148  // we mined the duplicate coinbase
149  assert(current_block->vtx.at(0)->vin.at(0).scriptSig == duplicate_coinbase_script);
150  }
151 
152  const auto prev_utxo_stats = utxo_stats;
153  if (was_valid) {
154  circulation += GetBlockSubsidy(ActiveHeight(), Params().GetConsensus());
155  }
156 
157  UpdateUtxoStats();
158 
159  if (!was_valid) {
160  // utxo stats must not change
161  assert(prev_utxo_stats.hashSerialized == utxo_stats.hashSerialized);
162  }
163 
164  current_block = PrepareNextBlock();
165  StoreLastTxo();
166  });
167  }
168 }
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:73
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
bool IsNull() const
Definition: transaction.h:49
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
const std::vector< CTxOut > vout
Definition: transaction.h:306
const uint256 & GetHash() const
Definition: transaction.h:337
T ConsumeIntegralInRange(T min, T max)
T & front()
Definition: prevector.h:439
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
static bool ComputeUTXOStats(CCoinsView *view, CCoinsStats &stats, T hash_obj, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
Definition: coinstats.cpp:116
Definition: init.h:25
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
Definition: miner.cpp:47
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:422
@ OP_FALSE
Definition: script.h:73
@ OP_TRUE
Definition: script.h:80
@ OP_0
Definition: script.h:72
@ OP_RETURN
Definition: script.h:107
A mutable version of CTransaction.
Definition: transaction.h:380
std::vector< CTxOut > vout
Definition: transaction.h:382
std::vector< CTxIn > vin
Definition: transaction.h:381
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:94
std::optional< CAmount > total_amount
The total amount, or nullopt if an overflow occurred calculating it.
Definition: coinstats.h:40
uint256 hashSerialized
Definition: coinstats.h:37
#define LOCK(cs)
Definition: sync.h:258
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
COutPoint MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:64
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey, const BlockAssembler::Options &assembler_options)
Definition: mining.cpp:112
FUZZ_TARGET(utxo_total_supply)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
assert(!tx.IsCoinBase())