rpc: take ownership of the file by WriteUTXOSnapshot()

Have `WriteUTXOSnapshot()` take rvalue reference to make it obvious that
it takes ownership of the file.
This commit is contained in:
Hodlinator
2025-05-22 11:59:50 +02:00
committed by Vasil Dimov
parent 52e6e93c3f
commit a69c4098b2
3 changed files with 25 additions and 8 deletions

View File

@@ -82,7 +82,7 @@ UniValue WriteUTXOSnapshot(
CCoinsViewCursor* pcursor,
CCoinsStats* maybe_stats,
const CBlockIndex* tip,
AutoFile& afile,
AutoFile&& afile,
const fs::path& path,
const fs::path& temppath,
const std::function<void()>& interruption_point = {});
@@ -3114,7 +3114,14 @@ static RPCHelpMan dumptxoutset()
}
}
UniValue result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
UniValue result = WriteUTXOSnapshot(*chainstate,
cursor.get(),
&stats,
tip,
std::move(afile),
path,
temppath,
node.rpc_interruption_point);
fs::rename(temppath, path);
result.pushKV("path", path.utf8string());
@@ -3166,7 +3173,7 @@ UniValue WriteUTXOSnapshot(
CCoinsViewCursor* pcursor,
CCoinsStats* maybe_stats,
const CBlockIndex* tip,
AutoFile& afile,
AutoFile&& afile,
const fs::path& path,
const fs::path& temppath,
const std::function<void()>& interruption_point)
@@ -3240,12 +3247,19 @@ UniValue WriteUTXOSnapshot(
UniValue CreateUTXOSnapshot(
node::NodeContext& node,
Chainstate& chainstate,
AutoFile& afile,
AutoFile&& afile,
const fs::path& path,
const fs::path& tmppath)
{
auto [cursor, stats, tip]{WITH_LOCK(::cs_main, return PrepareUTXOSnapshot(chainstate, node.rpc_interruption_point))};
return WriteUTXOSnapshot(chainstate, cursor.get(), &stats, tip, afile, path, tmppath, node.rpc_interruption_point);
return WriteUTXOSnapshot(chainstate,
cursor.get(),
&stats,
tip,
std::move(afile),
path,
tmppath,
node.rpc_interruption_point);
}
static RPCHelpMan loadtxoutset()

View File

@@ -51,7 +51,7 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
UniValue CreateUTXOSnapshot(
node::NodeContext& node,
Chainstate& chainstate,
AutoFile& afile,
AutoFile&& afile,
const fs::path& path,
const fs::path& tmppath);

View File

@@ -47,8 +47,11 @@ CreateAndActivateUTXOSnapshot(
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
AutoFile auto_outfile{outfile};
UniValue result = CreateUTXOSnapshot(
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
UniValue result = CreateUTXOSnapshot(node,
node.chainman->ActiveChainstate(),
std::move(auto_outfile), // Will close auto_outfile.
snapshot_path,
snapshot_path);
LogPrintf(
"Wrote UTXO snapshot to %s: %s\n", fs::PathToString(snapshot_path.make_preferred()), result.write());