Bitcoin Core  27.99.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 {
32 {
33  const uint16_t m_version{1};
34  const std::set<uint16_t> m_supported_versions{1};
36 public:
41 
42 
45  uint64_t m_coins_count = 0;
46 
48  const MessageStartChars network_magic) :
49  m_network_magic(network_magic) { }
51  const MessageStartChars network_magic,
52  const uint256& base_blockhash,
53  const int base_blockheight,
54  uint64_t coins_count) :
55  m_network_magic(network_magic),
56  m_base_blockhash(base_blockhash),
57  m_base_blockheight(base_blockheight),
58  m_coins_count(coins_count) { }
59 
60  template <typename Stream>
61  inline void Serialize(Stream& s) const {
63  s << m_version;
64  s << m_network_magic;
65  s << m_base_blockheight;
66  s << m_base_blockhash;
67  s << m_coins_count;
68  }
69 
70  template <typename Stream>
71  inline void Unserialize(Stream& s) {
72  // Read the snapshot magic bytes
73  std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
74  s >> snapshot_magic;
75  if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
76  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.");
77  }
78 
79  // Read the version
80  uint16_t version;
81  s >> version;
82  if (m_supported_versions.find(version) == m_supported_versions.end()) {
83  throw std::ios_base::failure(strprintf("Version of snapshot %s does not match any of the supported versions.", version));
84  }
85 
86  // Read the network magic (pchMessageStart)
87  MessageStartChars message;
88  s >> message;
89  if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {
90  auto metadata_network{GetNetworkForMagic(message)};
91  if (metadata_network) {
92  std::string network_string{ChainTypeToString(metadata_network.value())};
93  auto node_network{GetNetworkForMagic(m_network_magic)};
94  std::string node_network_string{ChainTypeToString(node_network.value())};
95  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));
96  } else {
97  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.");
98  }
99  }
100 
101  s >> m_base_blockheight;
102  s >> m_base_blockhash;
103  s >> m_coins_count;
104  }
105 };
106 
112 const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
113 
117 bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)
119 
122 std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
124 
127 constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
128 
129 
131 std::optional<fs::path> FindSnapshotChainstateDir(const fs::path& data_dir);
132 
133 } // namespace node
134 
135 #endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:513
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:32
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:39
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:45
const std::set< uint16_t > m_supported_versions
Definition: utxo_snapshot.h:34
const MessageStartChars m_network_magic
Definition: utxo_snapshot.h:35
void Unserialize(Stream &s)
Definition: utxo_snapshot.h:71
void Serialize(Stream &s) const
Definition: utxo_snapshot.h:61
SnapshotMetadata(const MessageStartChars network_magic)
Definition: utxo_snapshot.h:47
SnapshotMetadata(const MessageStartChars network_magic, const uint256 &base_blockhash, const int base_blockheight, uint64_t coins_count)
Definition: utxo_snapshot.h:50
const uint16_t m_version
Definition: utxo_snapshot.h:33
256-bit opaque blob.
Definition: uint256.h:127
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
std::optional< ChainType > GetNetworkForMagic(const MessageStartChars &message)
std::array< uint8_t, 4 > MessageStartChars
Definition: messages.h:20
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > constexpr ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(std::string_view SNAPSHOT_CHAINSTATE_SUFFIX
Write out the blockhash of the snapshot base block that was used to construct this chainstate.
std::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1161
static constexpr std::array< uint8_t, 5 > SNAPSHOT_MAGIC_BYTES
Definition: utxo_snapshot.h:24