Skip to content

Commit 30d9bdb

Browse files
committed
rpc: Refactor loadtxoutset to utilize new node interface for UTXO snapshot loading
- Updated the loadtxoutset function to use the new node interface for loading UTXO snapshots. - Replaced direct file handling with snapshot activation through the node interface. - Enhanced the result object to correctly reference metadata from the snapshot. This change streamlines the UTXO snapshot loading process and leverages the new interface capabilities.
1 parent 411e6bc commit 30d9bdb

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/rpc/blockchain.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <index/blockfilterindex.h>
2424
#include <index/coinstatsindex.h>
2525
#include <interfaces/mining.h>
26+
#include <interfaces/node.h>
27+
#include <interfaces/snapshot.h>
2628
#include <kernel/coinstats.h>
2729
#include <logging/timer.h>
2830
#include <net.h>
@@ -67,6 +69,8 @@ using kernel::CoinStatsHashType;
6769

6870
using interfaces::BlockRef;
6971
using interfaces::Mining;
72+
using interfaces::Node;
73+
using interfaces::Snapshot;
7074
using node::BlockManager;
7175
using node::NodeContext;
7276
using node::SnapshotMetadata;
@@ -3345,27 +3349,23 @@ static RPCHelpMan loadtxoutset()
33453349
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
33463350
{
33473351
NodeContext& node = EnsureAnyNodeContext(request.context);
3348-
ChainstateManager& chainman = EnsureChainman(node);
33493352
const fs::path path{AbsPathForConfigVal(EnsureArgsman(node), fs::u8path(self.Arg<std::string>("path")))};
3350-
3351-
FILE* file{fsbridge::fopen(path, "rb")};
3352-
AutoFile afile{file};
3353-
if (afile.IsNull()) {
3354-
throw JSONRPCError(
3355-
RPC_INVALID_PARAMETER,
3356-
"Couldn't open file " + path.utf8string() + " for reading.");
3353+
auto node_interface = interfaces::MakeNode(node);
3354+
auto snapshot = node_interface->snapshot(path);
3355+
if (!snapshot || !snapshot->activate()) {
3356+
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot: %s", path.utf8string()));
33573357
}
33583358

3359-
SnapshotMetadata metadata{chainman.GetParams().MessageStart()};
3359+
const node::SnapshotMetadata* metadata;
33603360
try {
3361-
afile >> metadata;
3362-
} catch (const std::ios_base::failure& e) {
3361+
metadata = &snapshot->getMetadata();
3362+
} catch (const std::exception& e) {
33633363
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("Unable to parse metadata: %s", e.what()));
33643364
}
33653365

3366-
auto activation_result{chainman.ActivateSnapshot(afile, metadata, false)};
3366+
auto activation_result = snapshot->getActivationResult();
33673367
if (!activation_result) {
3368-
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot: %s. (%s)", util::ErrorString(activation_result).original, path.utf8string()));
3368+
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot: %s", path.utf8string()));
33693369
}
33703370

33713371
// Because we can't provide historical blocks during tip or background sync.
@@ -3375,10 +3375,10 @@ static RPCHelpMan loadtxoutset()
33753375
// provide the last 288 blocks, but it doesn't hurt to set it.
33763376
node.connman->AddLocalServices(NODE_NETWORK_LIMITED);
33773377

3378-
CBlockIndex& snapshot_index{*CHECK_NONFATAL(*activation_result)};
3378+
const CBlockIndex& snapshot_index{*CHECK_NONFATAL(*activation_result)};
33793379

33803380
UniValue result(UniValue::VOBJ);
3381-
result.pushKV("coins_loaded", metadata.m_coins_count);
3381+
result.pushKV("coins_loaded", metadata->m_coins_count);
33823382
result.pushKV("tip_hash", snapshot_index.GetBlockHash().ToString());
33833383
result.pushKV("base_height", snapshot_index.nHeight);
33843384
result.pushKV("path", fs::PathToString(path));

0 commit comments

Comments
 (0)