Bitcoin ABC 0.33.12
P2P Digital Currency
utxo_snapshot.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 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>
12#include <serialize.h>
13#include <util/chaintype.h>
14#include <util/check.h>
15#include <util/fs.h>
16#include <validation.h>
17
18#include <optional>
19
20// UTXO set snapshot magic bytes
21static constexpr std::array<uint8_t, 5> SNAPSHOT_MAGIC_BYTES = {
22 {'u', 't', 'x', 'o', 0xff}};
23
24struct BlockHash;
25
26namespace node {
32 inline static const uint16_t VERSION{2};
33 const std::set<uint16_t> m_supported_versions{VERSION};
35
36public:
40
43 uint64_t m_coins_count = 0;
44
46 : m_network_magic(network_magic) {}
48 const BlockHash &base_blockhash, uint64_t coins_count)
49 : m_network_magic(network_magic), m_base_blockhash(base_blockhash),
50 m_coins_count(coins_count) {}
51
52 template <typename Stream> inline void Serialize(Stream &s) const {
54 s << VERSION;
55 s << m_network_magic;
57 s << m_coins_count;
58 }
59
60 template <typename Stream> inline void Unserialize(Stream &s) {
61 // Read the snapshot magic bytes
62 std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
63 s >> snapshot_magic;
64 if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
65 throw std::ios_base::failure(
66 "Invalid UTXO set snapshot magic bytes. Please check if this "
67 "is indeed a snapshot file or if you are using an outdated "
68 "snapshot format.");
69 }
70
71 // Read the version
72 uint16_t version;
73 s >> version;
74 if (m_supported_versions.find(version) == m_supported_versions.end()) {
75 throw std::ios_base::failure(
76 strprintf("Version of snapshot %s does not match any of the "
77 "supported versions.",
78 version));
79 }
80
81 // Read the network magic (pchMessageStart)
83 s >> message;
84 if (!std::equal(message.begin(), message.end(),
85 m_network_magic.data())) {
86 auto metadata_network{GetNetworkForMagic(message)};
87 if (metadata_network) {
88 std::string network_string{
89 ChainTypeToString(metadata_network.value())};
90 auto node_network{GetNetworkForMagic(m_network_magic)};
91 std::string node_network_string{
92 ChainTypeToString(node_network.value())};
93 throw std::ios_base::failure(
94 strprintf("The network of the snapshot (%s) does not match "
95 "the network of this node (%s).",
96 network_string, node_network_string));
97 } else {
98 throw std::ios_base::failure(
99 "This snapshot has been created for an unrecognized "
100 "network. This could be a new testnet or possibly caused "
101 "by data corruption.");
102 }
103 }
104
105 s >> m_base_blockhash;
106 s >> m_coins_count;
107 }
108};
109
115const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
116
120bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
122
125std::optional<BlockHash> ReadSnapshotBaseBlockhash(const fs::path &chaindir)
127
130constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
131
133std::optional<fs::path> FindSnapshotChainstateDir(const fs::path &data_dir);
134
135} // namespace node
136
137#endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
std::array< uint8_t, MESSAGE_START_SIZE > MessageMagic
Definition: protocol.h:47
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:725
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
Metadata describing a serialized version of a UTXO set from which an assumeutxo Chainstate can be con...
Definition: utxo_snapshot.h:31
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:43
const std::set< uint16_t > m_supported_versions
Definition: utxo_snapshot.h:33
SnapshotMetadata(const CMessageHeader::MessageMagic network_magic, const BlockHash &base_blockhash, uint64_t coins_count)
Definition: utxo_snapshot.h:47
BlockHash 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
void Unserialize(Stream &s)
Definition: utxo_snapshot.h:60
static const uint16_t VERSION
Definition: utxo_snapshot.h:32
void Serialize(Stream &s) const
Definition: utxo_snapshot.h:52
const CMessageHeader::MessageMagic m_network_magic
Definition: utxo_snapshot.h:34
SnapshotMetadata(const CMessageHeader::MessageMagic network_magic)
Definition: utxo_snapshot.h:45
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
std::optional< ChainType > GetNetworkForMagic(const CMessageHeader::MessageMagic &message)
Definition: messages.h:12
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< BlockHash > ReadSnapshotBaseBlockhash(const fs::path &chaindir)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< BlockHash > ReadSnapshotBaseBlockhash(const 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::optional< fs::path > FindSnapshotChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1203
static constexpr std::array< uint8_t, 5 > SNAPSHOT_MAGIC_BYTES
Definition: utxo_snapshot.h:21