Bitcoin ABC 0.31.6
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/fs.h>
15#include <validation.h>
16
17#include <optional>
18
19// UTXO set snapshot magic bytes
20static constexpr std::array<uint8_t, 5> SNAPSHOT_MAGIC_BYTES = {
21 {'u', 't', 'x', 'o', 0xff}};
22
23struct BlockHash;
24
25namespace node {
31 inline static const uint16_t VERSION{2};
32 const std::set<uint16_t> m_supported_versions{VERSION};
33
34public:
38
41 uint64_t m_coins_count = 0;
42
44 SnapshotMetadata(const BlockHash &base_blockhash, uint64_t coins_count)
45 : m_base_blockhash(base_blockhash), m_coins_count(coins_count) {}
46
47 template <typename Stream> inline void Serialize(Stream &s) const {
49 s << VERSION;
50 s << Params().DiskMagic();
52 s << m_coins_count;
53 }
54
55 template <typename Stream> inline void Unserialize(Stream &s) {
56 // Read the snapshot magic bytes
57 std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;
58 s >> snapshot_magic;
59 if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {
60 throw std::ios_base::failure(
61 "Invalid UTXO set snapshot magic bytes. Please check if this "
62 "is indeed a snapshot file or if you are using an outdated "
63 "snapshot format.");
64 }
65
66 // Read the version
67 uint16_t version;
68 s >> version;
69 if (m_supported_versions.find(version) == m_supported_versions.end()) {
70 throw std::ios_base::failure(
71 strprintf("Version of snapshot %s does not match any of the "
72 "supported versions.",
73 version));
74 }
75
76 // Read the network magic (pchMessageStart)
78 s >> message;
79 if (!std::equal(message.begin(), message.end(),
80 Params().DiskMagic().data())) {
81 auto metadata_network = GetNetworkForMagic(message);
82 if (metadata_network) {
83 std::string network_string{
84 ChainTypeToString(metadata_network.value())};
85 throw std::ios_base::failure(
86 strprintf("The network of the snapshot (%s) does not match "
87 "the network of this node (%s).",
88 network_string, Params().GetChainTypeString()));
89 } else {
90 throw std::ios_base::failure(
91 "This snapshot has been created for an unrecognized "
92 "network. This could be a new testnet or possibly caused "
93 "by data corruption.");
94 }
95 }
96
98 s >> m_coins_count;
99 }
100};
101
107const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};
108
112bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
114
117std::optional<BlockHash> ReadSnapshotBaseBlockhash(const fs::path &chaindir)
119
122constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";
123
125std::optional<fs::path> FindSnapshotChainstateDir();
126
127} // namespace node
128
129#endif // BITCOIN_NODE_UTXO_SNAPSHOT_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
std::string ChainTypeToString(ChainType chain)
Definition: chaintype.cpp:11
const CMessageHeader::MessageMagic & DiskMagic() const
Definition: chainparams.h:99
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:705
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:30
uint64_t m_coins_count
The number of coins in the UTXO set contained in this snapshot.
Definition: utxo_snapshot.h:41
const std::set< uint16_t > m_supported_versions
Definition: utxo_snapshot.h:32
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:37
void Unserialize(Stream &s)
Definition: utxo_snapshot.h:55
SnapshotMetadata(const BlockHash &base_blockhash, uint64_t coins_count)
Definition: utxo_snapshot.h:44
static const uint16_t VERSION
Definition: utxo_snapshot.h:31
void Serialize(Stream &s) const
Definition: utxo_snapshot.h:47
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(CMessageHeader::MessageMagic &message)
Definition: init.h:31
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< fs::path > FindSnapshotChainstateDir()
Return a path to the snapshot-based chainstate dir, if one exists.
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.
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:1202
static constexpr std::array< uint8_t, 5 > SNAPSHOT_MAGIC_BYTES
Definition: utxo_snapshot.h:20