Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_NODE_UTXO_SNAPSHOT_H7#define BITCOIN_NODE_UTXO_SNAPSHOT_H9#include <kernel/chainparams.h>10#include <kernel/cs_main.h>11#include <kernel/messagestartchars.h>12#include <sync.h>13#include <tinyformat.h>14#include <uint256.h>15#include <util/chaintype.h>16#include <util/fs.h>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>27// UTXO set snapshot magic bytes28inline constexpr std::array<uint8_t, 5> SNAPSHOT_MAGIC_BYTES = {'u', 't', 'x', 'o', 0xff};30class Chainstate;32namespace node {33//! Metadata describing a serialized version of a UTXO set from which an34//! assumeutxo Chainstate can be constructed.35//! All metadata fields come from an untrusted file, so must be validated36//! before being used. Thus, new fields should be added only if needed.37class SnapshotMetadata38{39static constexpr uint16_t VERSION{2};40const std::set<uint16_t> m_supported_versions{VERSION};41const MessageStartChars m_network_magic;42public:43//! The hash of the block that reflects the tip of the chain for the44//! UTXO set contained in this snapshot.45uint256 m_base_blockhash;48//! The number of coins in the UTXO set contained in this snapshot. Used49//! during snapshot load to estimate progress of UTXO set reconstruction.50uint64_t m_coins_count = 0;52SnapshotMetadata(53const MessageStartChars network_magic) :54m_network_magic(network_magic) { }55SnapshotMetadata(56const MessageStartChars network_magic,57const uint256& base_blockhash,58uint64_t coins_count) :59m_network_magic(network_magic),60m_base_blockhash(base_blockhash),61m_coins_count(coins_count) { }63template <typename Stream>64inline void Serialize(Stream& s) const {65s << SNAPSHOT_MAGIC_BYTES;66s << VERSION;67s << m_network_magic;68s << m_base_blockhash;69s << m_coins_count;70}72template <typename Stream>73inline void Unserialize(Stream& s) {74// Read the snapshot magic bytes75std::array<uint8_t, SNAPSHOT_MAGIC_BYTES.size()> snapshot_magic;76s >> snapshot_magic;77if (snapshot_magic != SNAPSHOT_MAGIC_BYTES) {78throw 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}81// Read the version82uint16_t version;83s >> version;84if (!m_supported_versions.contains(version)) {85throw std::ios_base::failure(strprintf("Version of snapshot %s does not match any of the supported versions.", version));86}88// Read the network magic (pchMessageStart)89MessageStartChars message;90s >> message;91if (!std::equal(message.begin(), message.end(), m_network_magic.data())) {92auto metadata_network{GetNetworkForMagic(message)};93if (metadata_network) {94std::string network_string{ChainTypeToString(metadata_network.value())};95auto node_network{GetNetworkForMagic(m_network_magic)};96std::string node_network_string{ChainTypeToString(node_network.value())};97throw 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 {99throw 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}103s >> m_base_blockhash;104s >> m_coins_count;105}106};108//! The file in the snapshot chainstate dir which stores the base blockhash. This is109//! needed to reconstruct snapshot chainstates on init.110//!111//! Because we only allow loading a single snapshot at a time, there will only be one112//! chainstate directory with this filename present within it.113inline const fs::path SNAPSHOT_BLOCKHASH_FILENAME{"base_blockhash"};115//! Write out the blockhash of the snapshot base block that was used to construct116//! this chainstate. This value is read in during subsequent initializations and117//! used to reconstruct snapshot-based chainstates.118bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)119EXCLUSIVE_LOCKS_REQUIRED(::cs_main);121//! Read the blockhash of the snapshot base block that was used to construct the122//! chainstate.123std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)124EXCLUSIVE_LOCKS_REQUIRED(::cs_main);126//! Suffix appended to the chainstate (leveldb) dir when created based upon127//! a snapshot.128inline constexpr std::string_view SNAPSHOT_CHAINSTATE_SUFFIX = "_snapshot";131//! Return a path to the snapshot-based chainstate dir, if one exists.132std::optional<fs::path> FindAssumeutxoChainstateDir(const fs::path& data_dir);134} // namespace node136#endif // BITCOIN_NODE_UTXO_SNAPSHOT_H