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) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <node/utxo_snapshot.h>7#include <streams.h>8#include <sync.h>9#include <tinyformat.h>10#include <uint256.h>11#include <util/fs.h>12#include <util/log.h>13#include <validation.h>15#include <cassert>16#include <cstdio>17#include <optional>18#include <string>20namespace node {22bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)23{24AssertLockHeld(::cs_main);25assert(snapshot_chainstate.m_from_snapshot_blockhash);27const std::optional<fs::path> chaindir = snapshot_chainstate.StoragePath();28assert(chaindir); // Sanity check that chainstate isn't in-memory.29const fs::path write_to = *chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;31FILE* file{fsbridge::fopen(write_to, "wb")};32AutoFile afile{file};33if (afile.IsNull()) {34LogError("[snapshot] failed to open base blockhash file for writing: %s",35fs::PathToString(write_to));36return false;37}38afile << *snapshot_chainstate.m_from_snapshot_blockhash;40if (afile.fclose() != 0) {41LogError("[snapshot] failed to close base blockhash file %s after writing",42fs::PathToString(write_to));43return false;44}45return true;46}48std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)49{50if (!fs::exists(chaindir)) {51LogWarning("[snapshot] cannot read base blockhash: no chainstate dir "52"exists at path %s", fs::PathToString(chaindir));53return std::nullopt;54}55const fs::path read_from = chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;56const std::string read_from_str = fs::PathToString(read_from);58if (!fs::exists(read_from)) {59LogWarning("[snapshot] snapshot chainstate dir is malformed! no base blockhash file "60"exists at path %s. Try deleting %s and calling loadtxoutset again?",61fs::PathToString(chaindir), read_from_str);62return std::nullopt;63}65uint256 base_blockhash;66FILE* file{fsbridge::fopen(read_from, "rb")};67AutoFile afile{file};68if (afile.IsNull()) {69LogWarning("[snapshot] failed to open base blockhash file for reading: %s",70read_from_str);71return std::nullopt;72}73afile >> base_blockhash;75int64_t position = afile.tell();76afile.seek(0, SEEK_END);77if (position != afile.tell()) {78LogWarning("[snapshot] unexpected trailing data in %s", read_from_str);79}80return base_blockhash;81}83std::optional<fs::path> FindAssumeutxoChainstateDir(const fs::path& data_dir)84{85fs::path possible_dir =86data_dir / fs::u8path(strprintf("chainstate%s", SNAPSHOT_CHAINSTATE_SUFFIX));88if (fs::exists(possible_dir)) {89return possible_dir;90}91return std::nullopt;92}94} // namespace node