Skip to content

Commit 9b3fc10

Browse files
committed
interfaces: Rename and enhance UTXO snapshot methods
- Renamed the `loadSnapshot` method to `utxoSnapshot` in the node interface for clarity. - Added a new `generate` method in the `Snapshot` interface to create a UTXO snapshot of the current chainstate and write it to disk. - Updated the `SnapshotImpl` class to implement the new `generate` method, including file handling and error management. These changes improve the clarity and functionality of UTXO snapshot handling in the node interface.
1 parent d4a12d0 commit 9b3fc10

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Node
208208
virtual std::vector<std::string> listRpcCommands() = 0;
209209

210210
//! Load UTXO Snapshot.
211-
virtual std::unique_ptr<Snapshot> loadSnapshot(const fs::path& path) = 0;
211+
virtual std::unique_ptr<Snapshot> utxoSnapshot(const fs::path& path) = 0;
212212

213213
//! Get unspent output associated with a transaction.
214214
virtual std::optional<Coin> getUnspentOutput(const COutPoint& output) = 0;

src/interfaces/snapshot.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <memory>
99
#include <uint256.h>
10+
#include <util/fs.h>
1011

1112
namespace node {
1213
class SnapshotMetadata;
@@ -22,6 +23,11 @@ class Snapshot
2223

2324
//! Activate the snapshot, making it the active chainstate.
2425
virtual bool activate() = 0;
26+
27+
//! Generate a UTXO snapshot of the current chainstate and write it to disk.
28+
//! Returns true on success, false on failure. The snapshot content matches
29+
//! the current tip (equivalent to the "latest" type in the dumptxoutset RPC).
30+
virtual bool generate(const fs::path& output_path) = 0;
2531
};
2632

2733
} // namespace interfaces

src/node/interfaces.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ namespace {
9494
class SnapshotImpl : public interfaces::Snapshot
9595
{
9696
public:
97-
SnapshotImpl(ChainstateManager& chainman, const fs::path& path, bool in_memory)
98-
: m_chainman(chainman), m_path(path), m_in_memory(in_memory) {}
97+
SnapshotImpl(NodeContext& node, ChainstateManager& chainman, const fs::path& path, bool in_memory)
98+
: m_node(node), m_chainman(chainman), m_path(path), m_in_memory(in_memory) {}
9999

100100
bool activate() override
101101
{
@@ -120,7 +120,37 @@ class SnapshotImpl : public interfaces::Snapshot
120120
return activation_result.has_value();
121121
}
122122

123+
bool generate(const fs::path& output_path) override
124+
{
125+
try {
126+
const fs::path path = fsbridge::AbsPathJoin(Assert(m_node.args)->GetDataDirNet(), output_path);
127+
const std::string out_str = fs::PathToString(output_path);
128+
const fs::path temppath = fsbridge::AbsPathJoin(Assert(m_node.args)->GetDataDirNet(), fs::u8path(out_str + ".incomplete"));
129+
130+
if (fs::exists(path)) {
131+
// Do not overwrite existing files
132+
return false;
133+
}
134+
135+
FILE* file{fsbridge::fopen(temppath, "wb")};
136+
AutoFile afile{file};
137+
if (afile.IsNull()) {
138+
return false;
139+
}
140+
141+
Chainstate& chainstate = m_chainman.ActiveChainstate();
142+
/* Create the snapshot at the current tip ("latest"). */
143+
(void)::CreateUTXOSnapshot(m_node, chainstate, std::move(afile), path, temppath);
144+
145+
fs::rename(temppath, path);
146+
return true;
147+
} catch (const std::exception&) {
148+
return false;
149+
}
150+
}
151+
123152
private:
153+
NodeContext& m_node;
124154
ChainstateManager& m_chainman;
125155
fs::path m_path;
126156
bool m_in_memory;
@@ -393,9 +423,9 @@ class NodeImpl : public Node
393423
return ::tableRPC.execute(req);
394424
}
395425
std::vector<std::string> listRpcCommands() override { return ::tableRPC.listCommands(); }
396-
std::unique_ptr<interfaces::Snapshot> loadSnapshot(const fs::path& path) override
426+
std::unique_ptr<interfaces::Snapshot> utxoSnapshot(const fs::path& path) override
397427
{
398-
return std::make_unique<SnapshotImpl>(chainman(), path, /*in_memory=*/ false);
428+
return std::make_unique<SnapshotImpl>(*Assert(m_context), chainman(), path, /*in_memory=*/ false);
399429
}
400430
std::optional<Coin> getUnspentOutput(const COutPoint& output) override
401431
{

0 commit comments

Comments
 (0)