Bitcoin ABC 0.30.5
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 <common/args.h>
8#include <logging.h>
10#include <streams.h>
11#include <util/fs.h>
12#include <validation.h>
13
14#include <cstdio>
15#include <optional>
16
17namespace node {
18
19bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) {
21 assert(snapshot_chainstate.m_from_snapshot_blockhash);
22
23 const std::optional<fs::path> chaindir =
24 snapshot_chainstate.CoinsDB().StoragePath();
25 // Sanity check that chainstate isn't in-memory.
26 assert(chaindir);
27 const fs::path write_to = *chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
28
29 FILE *file{fsbridge::fopen(write_to, "wb")};
30 AutoFile afile{file};
31 if (afile.IsNull()) {
33 "[snapshot] failed to open base blockhash file for writing: %s\n",
34 fs::PathToString(write_to));
35 return false;
36 }
37 afile << *snapshot_chainstate.m_from_snapshot_blockhash;
38
39 if (afile.fclose() != 0) {
41 "[snapshot] failed to close base blockhash file %s after writing\n",
42 fs::PathToString(write_to));
43 return false;
44 }
45 return true;
46}
47
48std::optional<BlockHash> ReadSnapshotBaseBlockhash(const fs::path &chaindir) {
49 if (!fs::exists(chaindir)) {
50 LogPrintf("[snapshot] cannot read base blockhash: no chainstate dir "
51 "exists at path %s\n",
52 fs::PathToString(chaindir));
53 return std::nullopt;
54 }
55 const fs::path read_from = chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
56 const std::string read_from_str = fs::PathToString(read_from);
57
58 if (!fs::exists(read_from)) {
59 LogPrintf("[snapshot] snapshot chainstate dir is malformed! no base "
60 "blockhash file exists at path %s. Try deleting %s and "
61 "calling loadtxoutset again?\n",
62 fs::PathToString(chaindir), read_from_str);
63 return std::nullopt;
64 }
65
66 BlockHash base_blockhash;
67 FILE *file{fsbridge::fopen(read_from, "rb")};
68 AutoFile afile{file};
69 if (afile.IsNull()) {
71 "[snapshot] failed to open base blockhash file for reading: %s\n",
72 read_from_str);
73 return std::nullopt;
74 }
75 afile >> base_blockhash;
76
77 if (std::fgetc(afile.Get()) != EOF) {
78 LogPrintf("[snapshot] warning: unexpected trailing data in %s\n",
79 read_from_str);
80 } else if (std::ferror(afile.Get())) {
81 LogPrintf("[snapshot] warning: i/o error reading %s\n", read_from_str);
82 }
83 return base_blockhash;
84}
85
86std::optional<fs::path> FindSnapshotChainstateDir() {
87 fs::path possible_dir =
90
91 if (fs::exists(possible_dir)) {
92 return possible_dir;
93 }
94 return std::nullopt;
95}
96
97} // namespace node
ArgsManager gArgs
Definition: args.cpp:38
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:215
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:528
std::optional< fs::path > StoragePath()
Definition: txdb.h:92
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:699
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:816
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:841
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:207
static path u8path(const std::string &utf8_str)
Definition: fs.h:90
static bool exists(const path &p)
Definition: fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
Definition: init.h:28
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
Definition: utxo_snapshot.h:46
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.
Definition: utxo_snapshot.h:61
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())