LCOV - code coverage report
Current view: top level - src/index - coinstatsindex.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 89.8 % 266 239
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 15 15
Branches: 49.6 % 250 124

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <chainparams.h>
       6                 :             : #include <coins.h>
       7                 :             : #include <common/args.h>
       8                 :             : #include <crypto/muhash.h>
       9                 :             : #include <index/coinstatsindex.h>
      10                 :             : #include <kernel/coinstats.h>
      11                 :             : #include <logging.h>
      12                 :             : #include <node/blockstorage.h>
      13                 :             : #include <serialize.h>
      14                 :             : #include <txdb.h>
      15                 :             : #include <undo.h>
      16                 :             : #include <validation.h>
      17                 :             : 
      18                 :             : using kernel::ApplyCoinHash;
      19                 :             : using kernel::CCoinsStats;
      20                 :             : using kernel::GetBogoSize;
      21                 :             : using kernel::RemoveCoinHash;
      22                 :             : 
      23                 :             : static constexpr uint8_t DB_BLOCK_HASH{'s'};
      24                 :             : static constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
      25                 :             : static constexpr uint8_t DB_MUHASH{'M'};
      26                 :             : 
      27                 :             : namespace {
      28                 :             : 
      29                 :       21374 : struct DBVal {
      30                 :             :     uint256 muhash;
      31                 :             :     uint64_t transaction_output_count;
      32                 :             :     uint64_t bogo_size;
      33                 :             :     CAmount total_amount;
      34                 :             :     CAmount total_subsidy;
      35                 :             :     CAmount total_unspendable_amount;
      36                 :             :     CAmount total_prevout_spent_amount;
      37                 :             :     CAmount total_new_outputs_ex_coinbase_amount;
      38                 :             :     CAmount total_coinbase_amount;
      39                 :             :     CAmount total_unspendables_genesis_block;
      40                 :             :     CAmount total_unspendables_bip30;
      41                 :             :     CAmount total_unspendables_scripts;
      42                 :             :     CAmount total_unspendables_unclaimed_rewards;
      43                 :             : 
      44                 :       42700 :     SERIALIZE_METHODS(DBVal, obj)
      45                 :             :     {
      46                 :       21350 :         READWRITE(obj.muhash);
      47                 :       21350 :         READWRITE(obj.transaction_output_count);
      48                 :       21350 :         READWRITE(obj.bogo_size);
      49                 :       21350 :         READWRITE(obj.total_amount);
      50                 :       21350 :         READWRITE(obj.total_subsidy);
      51                 :       21350 :         READWRITE(obj.total_unspendable_amount);
      52                 :       21350 :         READWRITE(obj.total_prevout_spent_amount);
      53                 :       21350 :         READWRITE(obj.total_new_outputs_ex_coinbase_amount);
      54                 :       21350 :         READWRITE(obj.total_coinbase_amount);
      55                 :       21350 :         READWRITE(obj.total_unspendables_genesis_block);
      56                 :       21350 :         READWRITE(obj.total_unspendables_bip30);
      57                 :       21350 :         READWRITE(obj.total_unspendables_scripts);
      58                 :       21350 :         READWRITE(obj.total_unspendables_unclaimed_rewards);
      59                 :       21350 :     }
      60                 :             : };
      61                 :             : 
      62                 :             : struct DBHeightKey {
      63                 :             :     int height;
      64                 :             : 
      65   [ +  -  +  -  :       21123 :     explicit DBHeightKey(int height_in) : height(height_in) {}
                   +  - ]
      66                 :             : 
      67                 :             :     template <typename Stream>
      68                 :       21220 :     void Serialize(Stream& s) const
      69                 :             :     {
      70                 :       21220 :         ser_writedata8(s, DB_BLOCK_HEIGHT);
      71                 :       21220 :         ser_writedata32be(s, height);
      72                 :       21220 :     }
      73                 :             : 
      74                 :             :     template <typename Stream>
      75                 :          67 :     void Unserialize(Stream& s)
      76                 :             :     {
      77                 :          67 :         const uint8_t prefix{ser_readdata8(s)};
      78         [ -  + ]:          67 :         if (prefix != DB_BLOCK_HEIGHT) {
      79         [ #  # ]:           0 :             throw std::ios_base::failure("Invalid format for coinstatsindex DB height key");
      80                 :             :         }
      81                 :          67 :         height = ser_readdata32be(s);
      82                 :          67 :     }
      83                 :             : };
      84                 :             : 
      85                 :             : struct DBHashKey {
      86                 :             :     uint256 block_hash;
      87                 :             : 
      88   [ #  #  #  # ]:           0 :     explicit DBHashKey(const uint256& hash_in) : block_hash(hash_in) {}
      89                 :             : 
      90                 :          69 :     SERIALIZE_METHODS(DBHashKey, obj)
      91                 :             :     {
      92                 :          69 :         uint8_t prefix{DB_BLOCK_HASH};
      93                 :          69 :         READWRITE(prefix);
      94         [ -  + ]:          69 :         if (prefix != DB_BLOCK_HASH) {
      95         [ #  # ]:           0 :             throw std::ios_base::failure("Invalid format for coinstatsindex DB hash key");
      96                 :             :         }
      97                 :             : 
      98                 :          69 :         READWRITE(obj.block_hash);
      99                 :          69 :     }
     100                 :             : };
     101                 :             : 
     102                 :             : }; // namespace
     103                 :             : 
     104                 :             : std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
     105                 :             : 
     106                 :          40 : CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
     107         [ +  - ]:          40 :     : BaseIndex(std::move(chain), "coinstatsindex")
     108                 :             : {
     109   [ +  -  +  - ]:          80 :     fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"};
     110         [ +  - ]:          40 :     fs::create_directories(path);
     111                 :             : 
     112   [ +  -  +  -  :         160 :     m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
                   +  - ]
     113                 :          40 : }
     114                 :             : 
     115                 :       10538 : bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
     116                 :             : {
     117                 :       10538 :     CBlockUndo block_undo;
     118   [ +  -  +  - ]:       10538 :     const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
     119                 :       10538 :     m_total_subsidy += block_subsidy;
     120                 :             : 
     121                 :             :     // Ignore genesis block
     122         [ +  + ]:       10538 :     if (block.height > 0) {
     123                 :             :         // pindex variable gives indexing code access to node internals. It
     124                 :             :         // will be removed in upcoming commit
     125   [ +  -  +  -  :       31569 :         const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
                   +  - ]
     126   [ +  -  +  - ]:       10523 :         if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
     127                 :             :             return false;
     128                 :             :         }
     129                 :             : 
     130                 :       10523 :         std::pair<uint256, DBVal> read_out;
     131   [ +  -  +  - ]:       10523 :         if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
     132                 :             :             return false;
     133                 :             :         }
     134                 :             : 
     135         [ +  - ]:       10523 :         uint256 expected_block_hash{*Assert(block.prev_hash)};
     136         [ -  + ]:       10523 :         if (read_out.first != expected_block_hash) {
     137   [ #  #  #  #  :           0 :             LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
                   #  # ]
     138                 :             :                       read_out.first.ToString(), expected_block_hash.ToString());
     139                 :             : 
     140   [ #  #  #  # ]:           0 :             if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
     141   [ #  #  #  # ]:           0 :                 LogError("%s: previous block header not found; expected %s\n",
     142                 :             :                              __func__, expected_block_hash.ToString());
     143                 :           0 :                 return false;
     144                 :             :             }
     145                 :             :         }
     146                 :             : 
     147                 :             :         // Add the new utxos created from the block
     148         [ -  + ]:       10523 :         assert(block.data);
     149         [ +  + ]:       21225 :         for (size_t i = 0; i < block.data->vtx.size(); ++i) {
     150         [ +  - ]:       10702 :             const auto& tx{block.data->vtx.at(i)};
     151                 :             : 
     152                 :             :             // Skip duplicate txid coinbase transactions (BIP30).
     153   [ +  -  +  -  :       10702 :             if (IsBIP30Unspendable(*pindex) && tx->IsCoinBase()) {
                   -  - ]
     154                 :           0 :                 m_total_unspendable_amount += block_subsidy;
     155                 :           0 :                 m_total_unspendables_bip30 += block_subsidy;
     156                 :           0 :                 continue;
     157                 :             :             }
     158                 :             : 
     159         [ +  + ]:       31930 :             for (uint32_t j = 0; j < tx->vout.size(); ++j) {
     160                 :       21228 :                 const CTxOut& out{tx->vout[j]};
     161                 :       21228 :                 Coin coin{out, block.height, tx->IsCoinBase()};
     162         [ +  + ]:       21228 :                 COutPoint outpoint{tx->GetHash(), j};
     163                 :             : 
     164                 :             :                 // Skip unspendable coins
     165         [ +  + ]:       21228 :                 if (coin.out.scriptPubKey.IsUnspendable()) {
     166                 :       10523 :                     m_total_unspendable_amount += coin.out.nValue;
     167                 :       10523 :                     m_total_unspendables_scripts += coin.out.nValue;
     168                 :       10523 :                     continue;
     169                 :             :                 }
     170                 :             : 
     171         [ +  - ]:       10705 :                 ApplyCoinHash(m_muhash, outpoint, coin);
     172                 :             : 
     173         [ +  + ]:       10705 :                 if (tx->IsCoinBase()) {
     174                 :       10526 :                     m_total_coinbase_amount += coin.out.nValue;
     175                 :             :                 } else {
     176                 :         179 :                     m_total_new_outputs_ex_coinbase_amount += coin.out.nValue;
     177                 :             :                 }
     178                 :             : 
     179                 :       10705 :                 ++m_transaction_output_count;
     180                 :       10705 :                 m_total_amount += coin.out.nValue;
     181         [ +  - ]:       10705 :                 m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
     182                 :       21228 :             }
     183                 :             : 
     184                 :             :             // The coinbase tx has no undo data since no former output is spent
     185         [ +  + ]:       10702 :             if (!tx->IsCoinBase()) {
     186         [ +  - ]:         179 :                 const auto& tx_undo{block_undo.vtxundo.at(i - 1)};
     187                 :             : 
     188         [ +  + ]:         358 :                 for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
     189                 :         179 :                     Coin coin{tx_undo.vprevout[j]};
     190         [ +  - ]:         179 :                     COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
     191                 :             : 
     192         [ +  - ]:         179 :                     RemoveCoinHash(m_muhash, outpoint, coin);
     193                 :             : 
     194                 :         179 :                     m_total_prevout_spent_amount += coin.out.nValue;
     195                 :             : 
     196                 :         179 :                     --m_transaction_output_count;
     197                 :         179 :                     m_total_amount -= coin.out.nValue;
     198         [ +  - ]:         179 :                     m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
     199                 :         179 :                 }
     200                 :             :             }
     201                 :             :         }
     202                 :             :     } else {
     203                 :             :         // genesis block
     204                 :          15 :         m_total_unspendable_amount += block_subsidy;
     205                 :          15 :         m_total_unspendables_genesis_block += block_subsidy;
     206                 :             :     }
     207                 :             : 
     208                 :             :     // If spent prevouts + block subsidy are still a higher amount than
     209                 :             :     // new outputs + coinbase + current unspendable amount this means
     210                 :             :     // the miner did not claim the full block reward. Unclaimed block
     211                 :             :     // rewards are also unspendable.
     212                 :       10538 :     const CAmount unclaimed_rewards{(m_total_prevout_spent_amount + m_total_subsidy) - (m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + m_total_unspendable_amount)};
     213                 :       10538 :     m_total_unspendable_amount += unclaimed_rewards;
     214                 :       10538 :     m_total_unspendables_unclaimed_rewards += unclaimed_rewards;
     215                 :             : 
     216                 :       10538 :     std::pair<uint256, DBVal> value;
     217                 :       10538 :     value.first = block.hash;
     218                 :       10538 :     value.second.transaction_output_count = m_transaction_output_count;
     219                 :       10538 :     value.second.bogo_size = m_bogo_size;
     220                 :       10538 :     value.second.total_amount = m_total_amount;
     221                 :       10538 :     value.second.total_subsidy = m_total_subsidy;
     222                 :       10538 :     value.second.total_unspendable_amount = m_total_unspendable_amount;
     223                 :       10538 :     value.second.total_prevout_spent_amount = m_total_prevout_spent_amount;
     224                 :       10538 :     value.second.total_new_outputs_ex_coinbase_amount = m_total_new_outputs_ex_coinbase_amount;
     225                 :       10538 :     value.second.total_coinbase_amount = m_total_coinbase_amount;
     226                 :       10538 :     value.second.total_unspendables_genesis_block = m_total_unspendables_genesis_block;
     227                 :       10538 :     value.second.total_unspendables_bip30 = m_total_unspendables_bip30;
     228                 :       10538 :     value.second.total_unspendables_scripts = m_total_unspendables_scripts;
     229                 :       10538 :     value.second.total_unspendables_unclaimed_rewards = m_total_unspendables_unclaimed_rewards;
     230                 :             : 
     231                 :       10538 :     uint256 out;
     232                 :       10538 :     m_muhash.Finalize(out);
     233                 :       10538 :     value.second.muhash = out;
     234                 :             : 
     235                 :             :     // Intentionally do not update DB_MUHASH here so it stays in sync with
     236                 :             :     // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
     237         [ +  - ]:       10538 :     return m_db->Write(DBHeightKey(block.height), value);
     238                 :       10538 : }
     239                 :             : 
     240                 :           5 : [[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
     241                 :             :                                        const std::string& index_name,
     242                 :             :                                        int start_height, int stop_height)
     243                 :             : {
     244                 :           5 :     DBHeightKey key{start_height};
     245                 :           5 :     db_it.Seek(key);
     246                 :             : 
     247         [ +  + ]:          72 :     for (int height = start_height; height <= stop_height; ++height) {
     248   [ +  -  +  - ]:          67 :         if (!db_it.GetKey(key) || key.height != height) {
     249                 :           0 :             LogError("%s: unexpected key in %s: expected (%c, %d)\n",
     250                 :             :                          __func__, index_name, DB_BLOCK_HEIGHT, height);
     251                 :           0 :             return false;
     252                 :             :         }
     253                 :             : 
     254                 :          67 :         std::pair<uint256, DBVal> value;
     255         [ -  + ]:          67 :         if (!db_it.GetValue(value)) {
     256                 :           0 :             LogError("%s: unable to read value in %s at key (%c, %d)\n",
     257                 :             :                          __func__, index_name, DB_BLOCK_HEIGHT, height);
     258                 :           0 :             return false;
     259                 :             :         }
     260                 :             : 
     261                 :          67 :         batch.Write(DBHashKey(value.first), std::move(value.second));
     262                 :             : 
     263                 :          67 :         db_it.Next();
     264                 :             :     }
     265                 :             :     return true;
     266                 :             : }
     267                 :             : 
     268                 :           5 : bool CoinStatsIndex::CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip)
     269                 :             : {
     270                 :           5 :     CDBBatch batch(*m_db);
     271   [ +  -  +  - ]:           5 :     std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
     272                 :             : 
     273                 :             :     // During a reorg, we need to copy all hash digests for blocks that are
     274                 :             :     // getting disconnected from the height index to the hash index so we can
     275                 :             :     // still find them when the height index entries are overwritten.
     276   [ +  -  +  - ]:           5 :     if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip.height, current_tip.height)) {
     277                 :             :         return false;
     278                 :             :     }
     279                 :             : 
     280   [ +  -  +  - ]:           5 :     if (!m_db->WriteBatch(batch)) return false;
     281                 :             : 
     282                 :           5 :     {
     283         [ +  - ]:           5 :         LOCK(cs_main);
     284         [ +  - ]:           5 :         const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip.hash)};
     285         [ +  - ]:           5 :         const CBlockIndex* new_tip_index{m_chainstate->m_blockman.LookupBlockIndex(new_tip.hash)};
     286                 :             : 
     287                 :          62 :         do {
     288                 :          62 :             CBlock block;
     289                 :             : 
     290   [ +  -  -  + ]:          62 :             if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *iter_tip)) {
     291   [ #  #  #  # ]:           0 :                 LogError("%s: Failed to read block %s from disk\n",
     292                 :             :                              __func__, iter_tip->GetBlockHash().ToString());
     293                 :           0 :                 return false;
     294                 :             :             }
     295                 :             : 
     296   [ +  -  +  - ]:          62 :             if (!ReverseBlock(block, iter_tip)) {
     297                 :             :                 return false; // failure cause logged internally
     298                 :             :             }
     299                 :             : 
     300         [ +  - ]:          62 :             iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
     301   [ +  +  -  - ]:          62 :         } while (new_tip_index != iter_tip);
     302                 :           0 :     }
     303                 :             : 
     304                 :           5 :     return true;
     305                 :           5 : }
     306                 :             : 
     307                 :          92 : static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockRef& block, DBVal& result)
     308                 :             : {
     309                 :             :     // First check if the result is stored under the height index and the value
     310                 :             :     // there matches the block hash. This should be the case if the block is on
     311                 :             :     // the active chain.
     312                 :          92 :     std::pair<uint256, DBVal> read_out;
     313         [ +  + ]:          92 :     if (!db.Read(DBHeightKey(block.height), read_out)) {
     314                 :             :         return false;
     315                 :             :     }
     316         [ +  + ]:          91 :     if (read_out.first == block.hash) {
     317                 :          89 :         result = std::move(read_out.second);
     318                 :          89 :         return true;
     319                 :             :     }
     320                 :             : 
     321                 :             :     // If value at the height index corresponds to an different block, the
     322                 :             :     // result will be stored in the hash index.
     323                 :           2 :     return db.Read(DBHashKey(block.hash), result);
     324                 :             : }
     325                 :             : 
     326                 :          64 : std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_index) const
     327                 :             : {
     328                 :          64 :     CCoinsStats stats{block_index.nHeight, block_index.GetBlockHash()};
     329                 :          64 :     stats.index_used = true;
     330                 :             : 
     331                 :          64 :     DBVal entry;
     332         [ +  + ]:          64 :     if (!LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
     333                 :           1 :         return std::nullopt;
     334                 :             :     }
     335                 :             : 
     336                 :          63 :     stats.hashSerialized = entry.muhash;
     337                 :          63 :     stats.nTransactionOutputs = entry.transaction_output_count;
     338                 :          63 :     stats.nBogoSize = entry.bogo_size;
     339                 :          63 :     stats.total_amount = entry.total_amount;
     340                 :          63 :     stats.total_subsidy = entry.total_subsidy;
     341                 :          63 :     stats.total_unspendable_amount = entry.total_unspendable_amount;
     342                 :          63 :     stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
     343                 :          63 :     stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
     344                 :          63 :     stats.total_coinbase_amount = entry.total_coinbase_amount;
     345                 :          63 :     stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
     346                 :          63 :     stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
     347                 :          63 :     stats.total_unspendables_scripts = entry.total_unspendables_scripts;
     348                 :          63 :     stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
     349                 :             : 
     350                 :          63 :     return stats;
     351                 :             : }
     352                 :             : 
     353                 :          43 : bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
     354                 :             : {
     355         [ +  + ]:          43 :     if (!m_db->Read(DB_MUHASH, m_muhash)) {
     356                 :             :         // Check that the cause of the read failure is that the key does not
     357                 :             :         // exist. Any other errors indicate database corruption or a disk
     358                 :             :         // failure, and starting the index would cause further corruption.
     359         [ -  + ]:          15 :         if (m_db->Exists(DB_MUHASH)) {
     360                 :           0 :             LogError("%s: Cannot read current %s state; index may be corrupted\n",
     361                 :             :                          __func__, GetName());
     362                 :           0 :             return false;
     363                 :             :         }
     364                 :             :     }
     365                 :             : 
     366         [ +  + ]:          43 :     if (block) {
     367                 :          28 :         DBVal entry;
     368         [ -  + ]:          28 :         if (!LookUpOne(*m_db, *block, entry)) {
     369                 :           0 :             LogError("%s: Cannot read current %s state; index may be corrupted\n",
     370                 :             :                          __func__, GetName());
     371                 :           0 :             return false;
     372                 :             :         }
     373                 :             : 
     374                 :          28 :         uint256 out;
     375                 :          28 :         m_muhash.Finalize(out);
     376         [ -  + ]:          28 :         if (entry.muhash != out) {
     377                 :           0 :             LogError("%s: Cannot read current %s state; index may be corrupted\n",
     378                 :             :                          __func__, GetName());
     379                 :           0 :             return false;
     380                 :             :         }
     381                 :             : 
     382                 :          28 :         m_transaction_output_count = entry.transaction_output_count;
     383                 :          28 :         m_bogo_size = entry.bogo_size;
     384                 :          28 :         m_total_amount = entry.total_amount;
     385                 :          28 :         m_total_subsidy = entry.total_subsidy;
     386                 :          28 :         m_total_unspendable_amount = entry.total_unspendable_amount;
     387                 :          28 :         m_total_prevout_spent_amount = entry.total_prevout_spent_amount;
     388                 :          28 :         m_total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
     389                 :          28 :         m_total_coinbase_amount = entry.total_coinbase_amount;
     390                 :          28 :         m_total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
     391                 :          28 :         m_total_unspendables_bip30 = entry.total_unspendables_bip30;
     392                 :          28 :         m_total_unspendables_scripts = entry.total_unspendables_scripts;
     393                 :          28 :         m_total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
     394                 :             :     }
     395                 :             : 
     396                 :             :     return true;
     397                 :             : }
     398                 :             : 
     399                 :         135 : bool CoinStatsIndex::CustomCommit(CDBBatch& batch)
     400                 :             : {
     401                 :             :     // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
     402                 :             :     // to prevent an inconsistent state of the DB.
     403                 :         135 :     batch.Write(DB_MUHASH, m_muhash);
     404                 :         135 :     return true;
     405                 :             : }
     406                 :             : 
     407                 :             : // Reverse a single block as part of a reorg
     408                 :          62 : bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex)
     409                 :             : {
     410                 :          62 :     CBlockUndo block_undo;
     411                 :          62 :     std::pair<uint256, DBVal> read_out;
     412                 :             : 
     413   [ +  -  +  - ]:          62 :     const CAmount block_subsidy{GetBlockSubsidy(pindex->nHeight, Params().GetConsensus())};
     414                 :          62 :     m_total_subsidy -= block_subsidy;
     415                 :             : 
     416                 :             :     // Ignore genesis block
     417         [ +  - ]:          62 :     if (pindex->nHeight > 0) {
     418   [ +  -  +  - ]:          62 :         if (!m_chainstate->m_blockman.UndoReadFromDisk(block_undo, *pindex)) {
     419                 :             :             return false;
     420                 :             :         }
     421                 :             : 
     422   [ +  -  +  - ]:          62 :         if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
     423                 :             :             return false;
     424                 :             :         }
     425                 :             : 
     426                 :          62 :         uint256 expected_block_hash{pindex->pprev->GetBlockHash()};
     427         [ -  + ]:          62 :         if (read_out.first != expected_block_hash) {
     428   [ #  #  #  #  :           0 :             LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
                   #  # ]
     429                 :             :                       read_out.first.ToString(), expected_block_hash.ToString());
     430                 :             : 
     431   [ #  #  #  # ]:           0 :             if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
     432   [ #  #  #  # ]:           0 :                 LogError("%s: previous block header not found; expected %s\n",
     433                 :             :                              __func__, expected_block_hash.ToString());
     434                 :           0 :                 return false;
     435                 :             :             }
     436                 :             :         }
     437                 :             :     }
     438                 :             : 
     439                 :             :     // Remove the new UTXOs that were created from the block
     440         [ +  + ]:         127 :     for (size_t i = 0; i < block.vtx.size(); ++i) {
     441         [ +  - ]:          65 :         const auto& tx{block.vtx.at(i)};
     442                 :             : 
     443         [ +  + ]:         193 :         for (uint32_t j = 0; j < tx->vout.size(); ++j) {
     444                 :         128 :             const CTxOut& out{tx->vout[j]};
     445                 :         128 :             COutPoint outpoint{tx->GetHash(), j};
     446                 :         128 :             Coin coin{out, pindex->nHeight, tx->IsCoinBase()};
     447                 :             : 
     448                 :             :             // Skip unspendable coins
     449         [ +  + ]:         128 :             if (coin.out.scriptPubKey.IsUnspendable()) {
     450                 :          62 :                 m_total_unspendable_amount -= coin.out.nValue;
     451                 :          62 :                 m_total_unspendables_scripts -= coin.out.nValue;
     452                 :          62 :                 continue;
     453                 :             :             }
     454                 :             : 
     455         [ +  - ]:          66 :             RemoveCoinHash(m_muhash, outpoint, coin);
     456                 :             : 
     457         [ +  + ]:          66 :             if (tx->IsCoinBase()) {
     458                 :          63 :                 m_total_coinbase_amount -= coin.out.nValue;
     459                 :             :             } else {
     460                 :           3 :                 m_total_new_outputs_ex_coinbase_amount -= coin.out.nValue;
     461                 :             :             }
     462                 :             : 
     463                 :          66 :             --m_transaction_output_count;
     464                 :          66 :             m_total_amount -= coin.out.nValue;
     465         [ +  - ]:          66 :             m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
     466                 :         128 :         }
     467                 :             : 
     468                 :             :         // The coinbase tx has no undo data since no former output is spent
     469         [ +  + ]:          65 :         if (!tx->IsCoinBase()) {
     470         [ +  - ]:           3 :             const auto& tx_undo{block_undo.vtxundo.at(i - 1)};
     471                 :             : 
     472         [ +  + ]:           6 :             for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
     473                 :           3 :                 Coin coin{tx_undo.vprevout[j]};
     474         [ +  - ]:           3 :                 COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
     475                 :             : 
     476         [ +  - ]:           3 :                 ApplyCoinHash(m_muhash, outpoint, coin);
     477                 :             : 
     478                 :           3 :                 m_total_prevout_spent_amount -= coin.out.nValue;
     479                 :             : 
     480                 :           3 :                 m_transaction_output_count++;
     481                 :           3 :                 m_total_amount += coin.out.nValue;
     482         [ +  - ]:           3 :                 m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
     483                 :           3 :             }
     484                 :             :         }
     485                 :             :     }
     486                 :             : 
     487                 :          62 :     const CAmount unclaimed_rewards{(m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + m_total_unspendable_amount) - (m_total_prevout_spent_amount + m_total_subsidy)};
     488                 :          62 :     m_total_unspendable_amount -= unclaimed_rewards;
     489                 :          62 :     m_total_unspendables_unclaimed_rewards -= unclaimed_rewards;
     490                 :             : 
     491                 :             :     // Check that the rolled back internal values are consistent with the DB read out
     492                 :          62 :     uint256 out;
     493                 :          62 :     m_muhash.Finalize(out);
     494         [ +  - ]:          62 :     Assert(read_out.second.muhash == out);
     495                 :             : 
     496         [ +  - ]:          62 :     Assert(m_transaction_output_count == read_out.second.transaction_output_count);
     497         [ +  - ]:          62 :     Assert(m_total_amount == read_out.second.total_amount);
     498         [ +  - ]:          62 :     Assert(m_bogo_size == read_out.second.bogo_size);
     499         [ +  - ]:          62 :     Assert(m_total_subsidy == read_out.second.total_subsidy);
     500         [ +  - ]:          62 :     Assert(m_total_unspendable_amount == read_out.second.total_unspendable_amount);
     501         [ +  - ]:          62 :     Assert(m_total_prevout_spent_amount == read_out.second.total_prevout_spent_amount);
     502         [ +  - ]:          62 :     Assert(m_total_new_outputs_ex_coinbase_amount == read_out.second.total_new_outputs_ex_coinbase_amount);
     503         [ +  - ]:          62 :     Assert(m_total_coinbase_amount == read_out.second.total_coinbase_amount);
     504         [ +  - ]:          62 :     Assert(m_total_unspendables_genesis_block == read_out.second.total_unspendables_genesis_block);
     505         [ +  - ]:          62 :     Assert(m_total_unspendables_bip30 == read_out.second.total_unspendables_bip30);
     506         [ +  - ]:          62 :     Assert(m_total_unspendables_scripts == read_out.second.total_unspendables_scripts);
     507         [ +  - ]:          62 :     Assert(m_total_unspendables_unclaimed_rewards == read_out.second.total_unspendables_unclaimed_rewards);
     508                 :             : 
     509                 :             :     return true;
     510                 :          62 : }
        

Generated by: LCOV version 2.0-1