Bitcoin Core  31.0.0
P2P Digital Currency
utxo_snapshot.h
Go to the documentation of this file.
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>
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 {
38 {
39  inline static const uint16_t VERSION{2};
40  const std::set<uint16_t> m_supported_versions{VERSION};
42 public:
46 
47 
50  uint64_t m_coins_count = 0;
51 
53  const MessageStartChars network_magic) :
54  m_network_magic(network_magic) { }
56  const MessageStartChars network_magic,
57  const uint256& base_blockhash,
58  uint64_t coins_count) :
59  m_network_magic(network_magic),
60  m_base_blockhash(base_blockhash),
61  m_coins_count(coins_count) { }
62 
63  template <typename Stream>
64  inline void Serialize(Stream& s) const {
66  s << VERSION;
67  s << m_network_magic;
69  s << m_coins_count;
70  }
71 
72  template <typename Stream>
73  inline void Unserialize(Stream& s) {
74  // Read the snapshot magic bytes
75  std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
76  s >> snapshot_magic;
77  if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
78  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  s >> version;
84  if (!m_supported_versions.contains(version)) {
85  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  s >> message;
91  if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
92  auto metadata_network{GetNetworkForMagic(message)};
93  if (metadata_network) {
94  std::string network_string{ChainTypeToString(metadata_network.value())};
95  auto node_network{GetNetworkForMagic(m_network_magic)};
96  std::string node_network_string{ChainTypeToString(node_network.value())};
97  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  } else {
99  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  s >> m_base_blockhash;
104  s >> m_coins_count;
105  }
106 };
107 
113 const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
114 
118 bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)
120 
123 std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
125 
128 constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
129 
130 
132 std::optional<fs::path> FindAssumeutxoChainstateDir(const fs::path& data_dir);
133 
134 } // namespace node
135 
136 #endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
const MessageStartChars m_network_magic
Definition: utxo_snapshot.h:41
SnapshotMetadata(const MessageStartChars network_magic)
Definition: utxo_snapshot.h:52
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:50
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate...
std::array< uint8_t, 4 > MessageStartChars
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:550
void Serialize(Stream &s) const
Definition: utxo_snapshot.h:64
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
Definition: messages.h:21
SnapshotMetadata(const MessageStartChars network_magic, const uint256 &base_blockhash, uint64_t coins_count)
Definition: utxo_snapshot.h:55
uint256 m_base_blockhash
The hash of the block that reflects the tip of the chain for the UTXO set contained in this snapshot...
Definition: utxo_snapshot.h:45
256-bit opaque blob.
Definition: uint256.h:195
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::optional< ChainType > GetNetworkForMagic(const MessageStartChars &message)
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
std::optional< fs::path > FindAssumeutxoChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
static constexpr std::array< uint8_t, 5 > SNAPSHOT_MAGIC_BYTES
Definition: utxo_snapshot.h:28
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
const std::set< uint16_t > m_supported_versions
Definition: utxo_snapshot.h:40
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
static const uint16_t VERSION
Definition: utxo_snapshot.h:39
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:37
void Unserialize(Stream &s)
Definition: utxo_snapshot.h:73