Bitcoin Core  25.99.0
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 
5 #include <node/utxo_snapshot.h>
6 
7 #include <common/args.h>
8 #include <logging.h>
9 #include <streams.h>
10 #include <sync.h>
11 #include <tinyformat.h>
12 #include <txdb.h>
13 #include <uint256.h>
14 #include <util/fs.h>
15 #include <validation.h>
16 
17 #include <cassert>
18 #include <cstdio>
19 #include <optional>
20 #include <string>
21 
22 namespace node {
23 
24 bool WriteSnapshotBaseBlockhash(Chainstate& snapshot_chainstate)
25 {
27  assert(snapshot_chainstate.m_from_snapshot_blockhash);
28 
29  const std::optional<fs::path> chaindir = snapshot_chainstate.CoinsDB().StoragePath();
30  assert(chaindir); // Sanity check that chainstate isn't in-memory.
31  const fs::path write_to = *chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
32 
33  FILE* file{fsbridge::fopen(write_to, "wb")};
34  AutoFile afile{file};
35  if (afile.IsNull()) {
36  LogPrintf("[snapshot] failed to open base blockhash file for writing: %s\n",
37  fs::PathToString(write_to));
38  return false;
39  }
40  afile << *snapshot_chainstate.m_from_snapshot_blockhash;
41 
42  if (afile.fclose() != 0) {
43  LogPrintf("[snapshot] failed to close base blockhash file %s after writing\n",
44  fs::PathToString(write_to));
45  return false;
46  }
47  return true;
48 }
49 
50 std::optional<uint256> ReadSnapshotBaseBlockhash(fs::path chaindir)
51 {
52  if (!fs::exists(chaindir)) {
53  LogPrintf("[snapshot] cannot read base blockhash: no chainstate dir " /* Continued */
54  "exists at path %s\n", fs::PathToString(chaindir));
55  return std::nullopt;
56  }
57  const fs::path read_from = chaindir / node::SNAPSHOT_BLOCKHASH_FILENAME;
58  const std::string read_from_str = fs::PathToString(read_from);
59 
60  if (!fs::exists(read_from)) {
61  LogPrintf("[snapshot] snapshot chainstate dir is malformed! no base blockhash file " /* Continued */
62  "exists at path %s. Try deleting %s and calling loadtxoutset again?\n",
63  fs::PathToString(chaindir), read_from_str);
64  return std::nullopt;
65  }
66 
67  uint256 base_blockhash;
68  FILE* file{fsbridge::fopen(read_from, "rb")};
69  AutoFile afile{file};
70  if (afile.IsNull()) {
71  LogPrintf("[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", 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 
85 std::optional<fs::path> FindSnapshotChainstateDir()
86 {
87  fs::path possible_dir =
89 
90  if (fs::exists(possible_dir)) {
91  return possible_dir;
92  }
93  return std::nullopt;
94 }
95 
96 } // namespace node
ArgsManager gArgs
Definition: args.cpp:42
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:231
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:488
std::optional< fs::path > StoragePath()
Definition: txdb.h:86
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:459
CCoinsViewDB & CoinsDB() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:579
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Definition: validation.h:556
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
256-bit opaque blob.
Definition: uint256.h:105
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
#define LogPrintf(...)
Definition: logging.h:236
static path u8path(const std::string &utf8_str)
Definition: fs.h:70
static bool exists(const path &p)
Definition: fs.h:88
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
Definition: init.h:25
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
The file in the snapshot chainstate dir which stores the base blockhash.
Definition: utxo_snapshot.h:51
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
std::optional< fs::path > FindSnapshotChainstateDir()
Return a path to the snapshot-based chainstate dir, if one exists.
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > constexpr ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(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:66
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())