LCOV - code coverage report
Current view: top level - src/kernel - coinstats.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 107 1.9 %
Date: 2023-11-10 23:46:46 Functions: 0 23 0.0 %
Branches: 2 196 1.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2022 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 <kernel/coinstats.h>
       6                 :            : 
       7                 :            : #include <chain.h>
       8                 :            : #include <coins.h>
       9                 :            : #include <crypto/muhash.h>
      10                 :            : #include <hash.h>
      11                 :            : #include <logging.h>
      12                 :            : #include <node/blockstorage.h>
      13                 :            : #include <primitives/transaction.h>
      14                 :            : #include <script/script.h>
      15                 :            : #include <serialize.h>
      16                 :            : #include <span.h>
      17         [ +  - ]:          2 : #include <streams.h>
      18         [ +  - ]:          2 : #include <sync.h>
      19                 :            : #include <tinyformat.h>
      20                 :            : #include <uint256.h>
      21                 :            : #include <util/check.h>
      22                 :            : #include <util/overflow.h>
      23                 :            : #include <validation.h>
      24                 :            : #include <version.h>
      25                 :            : 
      26                 :            : #include <cassert>
      27                 :            : #include <iosfwd>
      28                 :            : #include <iterator>
      29                 :            : #include <map>
      30                 :            : #include <memory>
      31                 :            : #include <string>
      32                 :            : #include <utility>
      33                 :            : 
      34                 :            : namespace kernel {
      35                 :          0 : 
      36                 :          0 : CCoinsStats::CCoinsStats(int block_height, const uint256& block_hash)
      37                 :          0 :     : nHeight(block_height),
      38                 :          0 :       hashBlock(block_hash) {}
      39                 :          0 : 
      40                 :            : // Database-independent metric indicating the UTXO set size
      41                 :          0 : uint64_t GetBogoSize(const CScript& script_pub_key)
      42                 :            : {
      43                 :          0 :     return 32 /* txid */ +
      44                 :          0 :            4 /* vout index */ +
      45                 :            :            4 /* height + coinbase */ +
      46                 :            :            8 /* amount */ +
      47                 :          0 :            2 /* scriptPubKey len */ +
      48                 :          0 :            script_pub_key.size() /* scriptPubKey */;
      49                 :            : }
      50                 :            : 
      51                 :            : template <typename T>
      52                 :          0 : static void TxOutSer(T& ss, const COutPoint& outpoint, const Coin& coin)
      53                 :            : {
      54                 :          0 :     ss << outpoint;
      55                 :          0 :     ss << static_cast<uint32_t>((coin.nHeight << 1) + coin.fCoinBase);
      56                 :          0 :     ss << coin.out;
      57                 :          0 : }
      58                 :          0 : 
      59                 :          0 : static void ApplyCoinHash(HashWriter& ss, const COutPoint& outpoint, const Coin& coin)
      60                 :          0 : {
      61                 :          0 :     TxOutSer(ss, outpoint, coin);
      62                 :          0 : }
      63                 :            : 
      64                 :          0 : void ApplyCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin)
      65                 :            : {
      66                 :          0 :     DataStream ss{};
      67         [ #  # ]:          0 :     TxOutSer(ss, outpoint, coin);
      68         [ #  # ]:          0 :     muhash.Insert(MakeUCharSpan(ss));
      69                 :          0 : }
      70                 :            : 
      71                 :          0 : void RemoveCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin)
      72                 :            : {
      73                 :          0 :     DataStream ss{};
      74         [ #  # ]:          0 :     TxOutSer(ss, outpoint, coin);
      75         [ #  # ]:          0 :     muhash.Remove(MakeUCharSpan(ss));
      76                 :          0 : }
      77                 :            : 
      78                 :          0 : static void ApplyCoinHash(std::nullptr_t, const COutPoint& outpoint, const Coin& coin) {}
      79                 :            : 
      80                 :            : //! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
      81                 :            : //! validation commitments are reliant on the hash constructed by this
      82                 :            : //! function.
      83                 :            : //!
      84                 :            : //! If the construction of this hash is changed, it will invalidate
      85                 :            : //! existing UTXO snapshots. This will not result in any kind of consensus
      86                 :            : //! failure, but it will force clients that were expecting to make use of
      87                 :            : //! assumeutxo to do traditional IBD instead.
      88                 :            : //!
      89                 :            : //! It is also possible, though very unlikely, that a change in this
      90                 :            : //! construction could cause a previously invalid (and potentially malicious)
      91                 :            : //! UTXO snapshot to be considered valid.
      92                 :            : template <typename T>
      93                 :          0 : static void ApplyHash(T& hash_obj, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
      94                 :            : {
      95 [ #  # ][ #  # ]:          0 :     for (auto it = outputs.begin(); it != outputs.end(); ++it) {
                 [ #  # ]
      96                 :          0 :         COutPoint outpoint = COutPoint(hash, it->first);
      97                 :          0 :         Coin coin = it->second;
      98 [ #  # ][ #  # ]:          0 :         ApplyCoinHash(hash_obj, outpoint, coin);
                 [ #  # ]
      99                 :          0 :     }
     100                 :          0 : }
     101                 :            : 
     102                 :          0 : static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
     103                 :            : {
     104         [ #  # ]:          0 :     assert(!outputs.empty());
     105                 :          0 :     stats.nTransactions++;
     106         [ #  # ]:          0 :     for (auto it = outputs.begin(); it != outputs.end(); ++it) {
     107                 :          0 :         stats.nTransactionOutputs++;
     108         [ #  # ]:          0 :         if (stats.total_amount.has_value()) {
     109                 :          0 :             stats.total_amount = CheckedAdd(*stats.total_amount, it->second.out.nValue);
     110                 :          0 :         }
     111                 :          0 :         stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
     112                 :          0 :     }
     113                 :          0 : }
     114                 :            : 
     115                 :            : //! Calculate statistics about the unspent transaction output set
     116                 :            : template <typename T>
     117                 :          0 : static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
     118                 :            : {
     119                 :          0 :     std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
     120 [ #  # ][ #  # ]:          0 :     assert(pcursor);
                 [ #  # ]
     121                 :            : 
     122 [ #  # ][ #  # ]:          0 :     uint256 prevkey;
                 [ #  # ]
     123                 :          0 :     std::map<uint32_t, Coin> outputs;
     124 [ #  # ][ #  # ]:          0 :     while (pcursor->Valid()) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     125 [ #  # ][ #  # ]:          0 :         if (interruption_point) interruption_point();
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     126 [ #  # ][ #  # ]:          0 :         COutPoint key;
                 [ #  # ]
     127 [ #  # ][ #  # ]:          0 :         Coin coin;
                 [ #  # ]
     128 [ #  # ][ #  # ]:          0 :         if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     129 [ #  # ][ #  # ]:          0 :             if (!outputs.empty() && key.hash != prevkey) {
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     130 [ #  # ][ #  # ]:          0 :                 ApplyStats(stats, prevkey, outputs);
                 [ #  # ]
     131 [ #  # ][ #  # ]:          0 :                 ApplyHash(hash_obj, prevkey, outputs);
                 [ #  # ]
     132                 :          0 :                 outputs.clear();
     133                 :          0 :             }
     134                 :          0 :             prevkey = key.hash;
     135 [ #  # ][ #  # ]:          0 :             outputs[key.n] = std::move(coin);
                 [ #  # ]
     136                 :          0 :             stats.coins_count++;
     137                 :          0 :         } else {
     138 [ #  # ][ #  # ]:          0 :             return error("%s: unable to read value", __func__);
                 [ #  # ]
     139                 :            :         }
     140 [ #  # ][ #  # ]:          0 :         pcursor->Next();
                 [ #  # ]
     141 [ #  # ][ #  # ]:          0 :     }
                 [ #  # ]
     142 [ #  # ][ #  # ]:          0 :     if (!outputs.empty()) {
                 [ #  # ]
     143 [ #  # ][ #  # ]:          0 :         ApplyStats(stats, prevkey, outputs);
                 [ #  # ]
     144 [ #  # ][ #  # ]:          0 :         ApplyHash(hash_obj, prevkey, outputs);
                 [ #  # ]
     145                 :          0 :     }
     146                 :            : 
     147 [ #  # ][ #  # ]:          0 :     FinalizeHash(hash_obj, stats);
                 [ #  # ]
     148                 :            : 
     149 [ #  # ][ #  # ]:          0 :     stats.nDiskSize = view->EstimateSize();
                 [ #  # ]
     150                 :            : 
     151                 :          0 :     return true;
     152                 :          0 : }
     153                 :            : 
     154                 :          0 : std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point)
     155                 :            : {
     156 [ #  # ][ #  # ]:          0 :     CBlockIndex* pindex = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
     157                 :          0 :     CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()};
     158                 :            : 
     159                 :          0 :     bool success = [&]() -> bool {
     160   [ #  #  #  # ]:          0 :         switch (hash_type) {
     161                 :            :         case(CoinStatsHashType::HASH_SERIALIZED): {
     162                 :          0 :             HashWriter ss{};
     163                 :          0 :             return ComputeUTXOStats(view, stats, ss, interruption_point);
     164                 :            :         }
     165                 :            :         case(CoinStatsHashType::MUHASH): {
     166                 :          0 :             MuHash3072 muhash;
     167                 :          0 :             return ComputeUTXOStats(view, stats, muhash, interruption_point);
     168                 :            :         }
     169                 :            :         case(CoinStatsHashType::NONE): {
     170                 :          0 :             return ComputeUTXOStats(view, stats, nullptr, interruption_point);
     171                 :            :         }
     172                 :            :         } // no default case, so the compiler can warn about missing cases
     173                 :          0 :         assert(false);
     174                 :          0 :     }();
     175                 :            : 
     176         [ #  # ]:          0 :     if (!success) {
     177                 :          0 :         return std::nullopt;
     178                 :            :     }
     179                 :          0 :     return stats;
     180                 :          0 : }
     181                 :            : 
     182                 :          0 : static void FinalizeHash(HashWriter& ss, CCoinsStats& stats)
     183                 :            : {
     184                 :          0 :     stats.hashSerialized = ss.GetHash();
     185                 :          0 : }
     186                 :          0 : static void FinalizeHash(MuHash3072& muhash, CCoinsStats& stats)
     187                 :            : {
     188                 :          0 :     uint256 out;
     189                 :          0 :     muhash.Finalize(out);
     190                 :          0 :     stats.hashSerialized = out;
     191                 :          0 : }
     192                 :          0 : static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}
     193                 :            : 
     194                 :            : } // namespace kernel

Generated by: LCOV version 1.14