LCOV - code coverage report
Current view: top level - src/node - utxo_snapshot.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 36 36
Test Date: 2026-01-01 04:18:06 Functions: 100.0 % 6 6
Branches: 71.4 % 28 20

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #ifndef BITCOIN_NODE_UTXO_SNAPSHOT_H
       7                 :             : #define BITCOIN_NODE_UTXO_SNAPSHOT_H
       8                 :             : 
       9                 :             : #include <kernel/chainparams.h>
      10                 :             : #include <kernel/cs_main.h>
      11                 :             : #include <kernel/messagestartchars.h>
      12                 :             : #include <sync.h>
      13                 :             : #include <tinyformat.h>
      14                 :             : #include <uint256.h>
      15                 :             : #include <util/chaintype.h>
      16                 :             : #include <util/fs.h>
      17                 :             : 
      18                 :             : #include <algorithm>
      19                 :             : #include <array>
      20                 :             : #include <cstdint>
      21                 :             : #include <ios>
      22                 :             : #include <optional>
      23                 :             : #include <set>
      24                 :             : #include <string>
      25                 :             : #include <string_view>
      26                 :             : 
      27                 :             : // UTXO set snapshot magic bytes
      28                 :             : static constexpr std::array<uint8_t, 5> SNAPSHOT_MAGIC_BYTES = {'u', 't', 'x', 'o', 0xff};
      29                 :             : 
      30                 :             : class Chainstate;
      31                 :             : 
      32                 :             : namespace node {
      33                 :             : //! Metadata describing a serialized version of a UTXO set from which an
      34                 :             : //! assumeutxo Chainstate can be constructed.
      35                 :             : //! All metadata fields come from an untrusted file, so must be validated
      36                 :             : //! before being used. Thus, new fields should be added only if needed.
      37                 :        4035 : class SnapshotMetadata
      38                 :             : {
      39                 :             :     inline static const uint16_t VERSION{2};
      40                 :             :     const std::set<uint16_t> m_supported_versions{VERSION};
      41                 :             :     const MessageStartChars m_network_magic;
      42                 :             : public:
      43                 :             :     //! The hash of the block that reflects the tip of the chain for the
      44                 :             :     //! UTXO set contained in this snapshot.
      45                 :             :     uint256 m_base_blockhash;
      46                 :             : 
      47                 :             : 
      48                 :             :     //! The number of coins in the UTXO set contained in this snapshot. Used
      49                 :             :     //! during snapshot load to estimate progress of UTXO set reconstruction.
      50                 :             :     uint64_t m_coins_count = 0;
      51                 :             : 
      52                 :        2884 :     SnapshotMetadata(
      53                 :        2884 :         const MessageStartChars network_magic) :
      54                 :        2884 :             m_network_magic(network_magic) { }
      55                 :        1151 :     SnapshotMetadata(
      56                 :             :         const MessageStartChars network_magic,
      57                 :             :         const uint256& base_blockhash,
      58                 :        1151 :         uint64_t coins_count) :
      59                 :        1151 :             m_network_magic(network_magic),
      60                 :        1151 :             m_base_blockhash(base_blockhash),
      61                 :        1151 :             m_coins_count(coins_count) { }
      62                 :             : 
      63                 :             :     template <typename Stream>
      64                 :        1154 :     inline void Serialize(Stream& s) const {
      65                 :        1154 :         s << SNAPSHOT_MAGIC_BYTES;
      66                 :        1154 :         s << VERSION;
      67                 :        1154 :         s << m_network_magic;
      68                 :        1154 :         s << m_base_blockhash;
      69                 :        1154 :         s << m_coins_count;
      70                 :        1154 :     }
      71                 :             : 
      72                 :             :     template <typename Stream>
      73                 :        2884 :     inline void Unserialize(Stream& s) {
      74                 :             :         // Read the snapshot magic bytes
      75                 :             :         std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
      76         [ +  + ]:        2849 :         s >> snapshot_magic;
      77         [ +  + ]:        2849 :         if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
      78         [ +  - ]:         384 :             throw std::ios_base::failure("Invalid UTXO set snapshot magic bytes. Please check if this is indeed a snapshot file or if you are using an outdated snapshot format.");
      79                 :             :         }
      80                 :             : 
      81                 :             :         // Read the version
      82                 :             :         uint16_t version;
      83                 :        2657 :         s >> version;
      84         [ +  + ]:        2657 :         if (!m_supported_versions.contains(version)) {
      85   [ +  -  +  - ]:          58 :             throw std::ios_base::failure(strprintf("Version of snapshot %s does not match any of the supported versions.", version));
      86                 :             :         }
      87                 :             : 
      88                 :             :         // Read the network magic (pchMessageStart)
      89                 :             :         MessageStartChars message;
      90                 :        2628 :         s >> message;
      91         [ +  + ]:        2628 :         if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
      92         [ +  + ]:          97 :             auto metadata_network{GetNetworkForMagic(message)};
      93         [ +  + ]:          97 :             if (metadata_network) {
      94                 :          66 :                 std::string network_string{ChainTypeToString(metadata_network.value())};
      95         [ +  - ]:          66 :                 auto node_network{GetNetworkForMagic(m_network_magic)};
      96         [ +  - ]:          66 :                 std::string node_network_string{ChainTypeToString(node_network.value())};
      97   [ +  -  +  - ]:         132 :                 throw std::ios_base::failure(strprintf("The network of the snapshot (%s) does not match the network of this node (%s).", network_string, node_network_string));
      98                 :         132 :             } else {
      99         [ +  - ]:          62 :                 throw std::ios_base::failure("This snapshot has been created for an unrecognized network. This could be a custom signet, a new testnet or possibly caused by data corruption.");
     100                 :             :             }
     101                 :             :         }
     102                 :             : 
     103                 :        2531 :         s >> m_base_blockhash;
     104                 :        2531 :         s >> m_coins_count;
     105                 :        2531 :     }
     106                 :             : };
     107                 :             : 
     108                 :             : //! The file in the snapshot chainstate dir which stores the base blockhash. This is
     109                 :             : //! needed to reconstruct snapshot chainstates on init.
     110                 :             : //!
     111                 :             : //! Because we only allow loading a single snapshot at a time, there will only be one
     112                 :             : //! chainstate directory with this filename present within it.
     113                 :             : const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
     114                 :             : 
     115                 :             : //! Write out the blockhash of the snapshot base block that was used to construct
     116                 :             : //! this chainstate. This value is read in during subsequent initializations and
     117                 :             : //! used to reconstruct snapshot-based chainstates.
     118                 :             : bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)
     119                 :             :     EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     120                 :             : 
     121                 :             : //! Read the blockhash of the snapshot base block that was used to construct the
     122                 :             : //! chainstate.
     123                 :             : std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
     124                 :             :     EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
     125                 :             : 
     126                 :             : //! Suffix appended to the chainstate (leveldb) dir when created based upon
     127                 :             : //! a snapshot.
     128                 :             : constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
     129                 :             : 
     130                 :             : 
     131                 :             : //! Return a path to the snapshot-based chainstate dir, if one exists.
     132                 :             : std::optional<fs::path> FindAssumeutxoChainstateDir(const fs::path& data_dir);
     133                 :             : 
     134                 :             : } // namespace node
     135                 :             : 
     136                 :             : #endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
        

Generated by: LCOV version 2.0-1