Bitcoin Core  28.1.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-2022 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 <chainparams.h>
10 #include <kernel/chainparams.h>
11 #include <kernel/cs_main.h>
12 #include <serialize.h>
13 #include <sync.h>
14 #include <uint256.h>
15 #include <util/chaintype.h>
16 #include <util/check.h>
17 #include <util/fs.h>
18 
19 #include <cstdint>
20 #include <optional>
21 #include <string_view>
22 
23 // UTXO set snapshot magic bytes
24 static constexpr std::array<uint8_t, 5> SNAPSHOT_MAGIC_BYTES = {'u', 't', 'x', 'o', 0xff};
25 
26 class Chainstate;
27 
28 namespace node {
34 {
35  inline static const uint16_t VERSION{2};
36  const std::set<uint16_t> m_supported_versions{VERSION};
38 public:
42 
43 
46  uint64_t m_coins_count = 0;
47 
49  const MessageStartChars network_magic) :
50  m_network_magic(network_magic) { }
52  const MessageStartChars network_magic,
53  const uint256& base_blockhash,
54  uint64_t coins_count) :
55  m_network_magic(network_magic),
56  m_base_blockhash(base_blockhash),
57  m_coins_count(coins_count) { }
58 
59  template <typename Stream>
60  inline void Serialize(Stream& s) const {
62  s << VERSION;
63  s << m_network_magic;
64  s << m_base_blockhash;
65  s << m_coins_count;
66  }
67 
68  template <typename Stream>
69  inline void Unserialize(Stream& s) {
70  // Read the snapshot magic bytes
71  std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
72  s >> snapshot_magic;
73  if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
74  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.");
75  }
76 
77  // Read the version
78  uint16_t version;
79  s >> version;
80  if (m_supported_versions.find(version) == m_supported_versions.end()) {
81  throw std::ios_base::failure(strprintf("Version of snapshot %s does not match any of the supported versions.", version));
82  }
83 
84  // Read the network magic (pchMessageStart)
85  MessageStartChars message;
86  s >> message;
87  if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
88  auto metadata_network{GetNetworkForMagic(message)};
89  if (metadata_network) {
90  std::string network_string{ChainTypeToString(metadata_network.value())};
91  auto node_network{GetNetworkForMagic(m_network_magic)};
92  std::string node_network_string{ChainTypeToString(node_network.value())};
93  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));
94  } else {
95  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.");
96  }
97  }
98 
99  s >> m_base_blockhash;
100  s >> m_coins_count;
101  }
102 };
103 
109 const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
110 
114 bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)
116 
119 std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
121 
124 constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
125 
126 
128 std::optional<fs::path> FindSnapshotChainstateDir(const fs::path& data_dir);
129 
130 } // namespace node
131 
132 #endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
const MessageStartChars m_network_magic
Definition: utxo_snapshot.h:37
SnapshotMetadata(const MessageStartChars network_magic)
Definition: utxo_snapshot.h:48
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1161
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:46
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:512
void Serialize(Stream &s) const
Definition: utxo_snapshot.h:60
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
Definition: messages.h:20
SnapshotMetadata(const MessageStartChars network_magic, const uint256 &base_blockhash, uint64_t coins_count)
Definition: utxo_snapshot.h:51
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:41
256-bit opaque blob.
Definition: uint256.h:178
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::optional< ChainType > GetNetworkForMagic(const MessageStartChars &message)
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
static constexpr std::array< uint8_t, 5 > SNAPSHOT_MAGIC_BYTES
Definition: utxo_snapshot.h:24
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
const std::set< uint16_t > m_supported_versions
Definition: utxo_snapshot.h:36
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:35
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:33
void Unserialize(Stream &s)
Definition: utxo_snapshot.h:69