Bitcoin ABC 0.32.4
P2P Digital Currency
utxo_snapshot.cpp
Go to the documentation of this file.
1// Copyright (c) 2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
7#include <logging.h>
9#include <streams.h>
10#include <util/fs.h>
11#include <validation.h>
12
13#include <cstdio>
14#include <optional>
15
16namespace node {
17
18bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) {
20 assert(snapshot_chainstate.m_from_snapshot_blockhash);
21
22 const std::optional<fs::path> chaindir =
23 snapshot_chainstate.CoinsDB().StoragePath();
24 // Sanity check that chainstate isn't in-memory.
25 assert(chaindir);
26 const fs::path write_to = *chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
27
28 FILE *file{fsbridge::fopen(write_to, "wb")};
29 AutoFile afile{file};
30 if (afile.IsNull()) {
32 "[snapshot] failed to open base blockhash file for writing: %s\n",
33 fs::PathToString(write_to));
34 return false;
35 }
36 afile << *snapshot_chainstate.m_from_snapshot_blockhash;
37
38 if (afile.fclose() != 0) {
40 "[snapshot] failed to close base blockhash file %s after writing\n",
41 fs::PathToString(write_to));
42 return false;
43 }
44 return true;
45}
46
47std::optional<BlockHash> ReadSnapshotBaseBlockhash(const fs::path &chaindir) {
48 if (!fs::exists(chaindir)) {
49 LogPrintf("[snapshot] cannot read base blockhash: no chainstate dir "
50 "exists at path %s\n",
51 fs::PathToString(chaindir));
52 return std::nullopt;
53 }
54 const fs::path read_from = chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
55 const std::string read_from_str = fs::PathToString(read_from);
56
57 if (!fs::exists(read_from)) {
58 LogPrintf("[snapshot] snapshot chainstate dir is malformed! no base "
59 "blockhash file exists at path %s. Try deleting %s and "
60 "calling loadtxoutset again?\n",
61 fs::PathToString(chaindir), read_from_str);
62 return std::nullopt;
63 }
64
65 BlockHash base_blockhash;
66 FILE *file{fsbridge::fopen(read_from, "rb")};
67 AutoFile afile{file};
68 if (afile.IsNull()) {
70 "[snapshot] failed to open base blockhash file for reading: %s\n",
71 read_from_str);
72 return std::nullopt;
73 }
74 afile >> base_blockhash;
75
76 if (std::fgetc(afile.Get()) != EOF) {
77 LogPrintf("[snapshot] warning: unexpected trailing data in %s\n",
78 read_from_str);
79 } else if (std::ferror(afile.Get())) {
80 LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str);
81 }
82 return base_blockhash;
83}
84
85std::optional<fs::path> FindSnapshotChainstateDir(const fs::path &data_dir) {
86 fs::path possible_dir =
87 data_dir /
89
90 if (fs::exists(possible_dir)) {
91 return possible_dir;
92 }
93 return std::nullopt;
94}
95
96} // namespace node
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:526
std::optional< fs::path > StoragePath()
Definition: txdb.h:75
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:734
const std::optional< BlockHash > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:841
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:867
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
#define LogPrintf(...)
Definition: logging.h:424
static path u8path(const std::string &utf8_str)
Definition: fs.h:90
static bool exists(const path &p)
Definition: fs.h:107
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:147
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
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< 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 strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())