Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
node
utxo_snapshot.cpp
Go to the documentation of this file.
1
// Copyright (c) 2022-present 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 <
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>
14
15
#include <cassert>
16
#include <cstdio>
17
#include <optional>
18
#include <span>
19
#include <string>
20
21
namespace
node
{
22
23
bool
WriteSnapshotBaseBlockhash
(
Chainstate
& snapshot_chainstate)
24
{
25
AssertLockHeld
(
::cs_main
);
26
assert
(snapshot_chainstate.
m_from_snapshot_blockhash
);
27
28
const
std::optional<fs::path> chaindir = snapshot_chainstate.
StoragePath
();
29
assert
(chaindir);
// Sanity check that chainstate isn't in-memory.
30
const
fs::path write_to = *chaindir /
node::SNAPSHOT_BLOCKHASH_FILENAME
;
31
32
FILE* file{
fsbridge::fopen
(write_to,
"wb"
)};
33
AutoFile
afile{file};
34
if
(afile.
IsNull
()) {
35
LogError
(
"[snapshot] failed to open base blockhash file for writing: %s"
,
36
fs::PathToString(write_to));
37
return
false
;
38
}
39
afile << *snapshot_chainstate.
m_from_snapshot_blockhash
;
40
41
if
(afile.
fclose
() != 0) {
42
LogError
(
"[snapshot] failed to close base blockhash file %s after writing"
,
43
fs::PathToString(write_to));
44
return
false
;
45
}
46
return
true
;
47
}
48
49
std::optional<uint256>
ReadSnapshotBaseBlockhash
(fs::path chaindir)
50
{
51
if
(!fs::exists(chaindir)) {
52
LogWarning
(
"[snapshot] cannot read base blockhash: no chainstate dir "
53
"exists at path %s"
, fs::PathToString(chaindir));
54
return
std::nullopt;
55
}
56
const
fs::path read_from = chaindir /
node::SNAPSHOT_BLOCKHASH_FILENAME
;
57
const
std::string read_from_str = fs::PathToString(read_from);
58
59
if
(!fs::exists(read_from)) {
60
LogWarning
(
"[snapshot] snapshot chainstate dir is malformed! no base blockhash file "
61
"exists at path %s. Try deleting %s and calling loadtxoutset again?"
,
62
fs::PathToString(chaindir), read_from_str);
63
return
std::nullopt;
64
}
65
66
uint256
base_blockhash;
67
FILE* file{
fsbridge::fopen
(read_from,
"rb"
)};
68
AutoFile
afile{file};
69
if
(afile.
IsNull
()) {
70
LogWarning
(
"[snapshot] failed to open base blockhash file for reading: %s"
,
71
read_from_str);
72
return
std::nullopt;
73
}
74
afile >> base_blockhash;
75
76
int64_t position = afile.
tell
();
77
afile.
seek
(0, SEEK_END);
78
if
(position != afile.
tell
()) {
79
LogWarning
(
"[snapshot] unexpected trailing data in %s"
, read_from_str);
80
}
81
return
base_blockhash;
82
}
83
84
std::optional<fs::path>
FindAssumeutxoChainstateDir
(
const
fs::path& data_dir)
85
{
86
fs::path possible_dir =
87
data_dir / fs::u8path(
strprintf
(
"chainstate%s"
,
SNAPSHOT_CHAINSTATE_SUFFIX
));
88
89
if
(fs::exists(possible_dir)) {
90
return
possible_dir;
91
}
92
return
std::nullopt;
93
}
94
95
}
// namespace node
AutoFile
Non-refcounted RAII wrapper for FILE*.
Definition
streams.h:373
AutoFile::seek
void seek(int64_t offset, int origin)
Wrapper around fseek().
Definition
streams.cpp:33
AutoFile::IsNull
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition
streams.h:426
AutoFile::tell
int64_t tell()
Find position within the file.
Definition
streams.cpp:54
AutoFile::fclose
int fclose()
Definition
streams.h:407
Chainstate
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition
validation.h:551
Chainstate::m_from_snapshot_blockhash
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:637
Chainstate::StoragePath
fs::path StoragePath() const
Return path to chainstate leveldb directory.
Definition
validation.cpp:1878
uint256
256-bit opaque blob.
Definition
uint256.h:195
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition
cs_main.cpp:8
fs.h
log.h
LogWarning
#define LogWarning(...)
Definition
log.h:96
LogError
#define LogError(...)
Definition
log.h:97
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition
fs.cpp:25
node
Definition
messages.h:21
node::SNAPSHOT_BLOCKHASH_FILENAME
const fs::path SNAPSHOT_BLOCKHASH_FILENAME
Definition
utxo_snapshot.h:113
node::WriteSnapshotBaseBlockhash
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate)
Definition
utxo_snapshot.cpp:23
node::FindAssumeutxoChainstateDir
std::optional< fs::path > FindAssumeutxoChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
Definition
utxo_snapshot.cpp:84
node::SNAPSHOT_CHAINSTATE_SUFFIX
bool WriteSnapshotBaseBlockhash(Chainstate &snapshot_chainstate) EXCLUSIVE_LOCKS_REQUIRED(std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir) EXCLUSIVE_LOCKS_REQUIRED(constexpr std::string_vie SNAPSHOT_CHAINSTATE_SUFFIX)
Definition
utxo_snapshot.h:128
node::ReadSnapshotBaseBlockhash
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
Definition
utxo_snapshot.cpp:49
streams.h
sync.h
AssertLockHeld
#define AssertLockHeld(cs)
Definition
sync.h:136
tinyformat.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
uint256.h
utxo_snapshot.h
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.17.0