Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
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.
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",
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",
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",
53 return std::nullopt;
54 }
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",
63 return std::nullopt;
64 }
65
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",
73 return std::nullopt;
74 }
76
77 if (std::fgetc(afile.Get()) != EOF) {
78 LogPrintf("[snapshot] warning: unexpected trailing data in %s\n",
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() {
90
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
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:699
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.
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.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
A BlockHash is a unqiue identifier for a block.
Definition blockhash.h:13
#define AssertLockHeld(cs)
Definition sync.h:146
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
assert(!tx.IsCoinBase())