Skip to content

Commit a69c409

Browse files
hodlinatorvasild
authored andcommitted
rpc: take ownership of the file by WriteUTXOSnapshot()
Have `WriteUTXOSnapshot()` take rvalue reference to make it obvious that it takes ownership of the file.
1 parent 52e6e93 commit a69c409

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/rpc/blockchain.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ UniValue WriteUTXOSnapshot(
8282
CCoinsViewCursor* pcursor,
8383
CCoinsStats* maybe_stats,
8484
const CBlockIndex* tip,
85-
AutoFile& afile,
85+
AutoFile&& afile,
8686
const fs::path& path,
8787
const fs::path& temppath,
8888
const std::function<void()>& interruption_point = {});
@@ -3114,7 +3114,14 @@ static RPCHelpMan dumptxoutset()
31143114
}
31153115
}
31163116

3117-
UniValue result = WriteUTXOSnapshot(*chainstate, cursor.get(), &stats, tip, afile, path, temppath, node.rpc_interruption_point);
3117+
UniValue result = WriteUTXOSnapshot(*chainstate,
3118+
cursor.get(),
3119+
&stats,
3120+
tip,
3121+
std::move(afile),
3122+
path,
3123+
temppath,
3124+
node.rpc_interruption_point);
31183125
fs::rename(temppath, path);
31193126

31203127
result.pushKV("path", path.utf8string());
@@ -3166,7 +3173,7 @@ UniValue WriteUTXOSnapshot(
31663173
CCoinsViewCursor* pcursor,
31673174
CCoinsStats* maybe_stats,
31683175
const CBlockIndex* tip,
3169-
AutoFile& afile,
3176+
AutoFile&& afile,
31703177
const fs::path& path,
31713178
const fs::path& temppath,
31723179
const std::function<void()>& interruption_point)
@@ -3240,12 +3247,19 @@ UniValue WriteUTXOSnapshot(
32403247
UniValue CreateUTXOSnapshot(
32413248
node::NodeContext& node,
32423249
Chainstate& chainstate,
3243-
AutoFile& afile,
3250+
AutoFile&& afile,
32443251
const fs::path& path,
32453252
const fs::path& tmppath)
32463253
{
32473254
auto [cursor, stats, tip]{WITH_LOCK(::cs_main, return PrepareUTXOSnapshot(chainstate, node.rpc_interruption_point))};
3248-
return WriteUTXOSnapshot(chainstate, cursor.get(), &stats, tip, afile, path, tmppath, node.rpc_interruption_point);
3255+
return WriteUTXOSnapshot(chainstate,
3256+
cursor.get(),
3257+
&stats,
3258+
tip,
3259+
std::move(afile),
3260+
path,
3261+
tmppath,
3262+
node.rpc_interruption_point);
32493263
}
32503264

32513265
static RPCHelpMan loadtxoutset()

src/rpc/blockchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void CalculatePercentilesByWeight(CAmount result[NUM_GETBLOCKSTATS_PERCENTILES],
5151
UniValue CreateUTXOSnapshot(
5252
node::NodeContext& node,
5353
Chainstate& chainstate,
54-
AutoFile& afile,
54+
AutoFile&& afile,
5555
const fs::path& path,
5656
const fs::path& tmppath);
5757

src/test/util/chainstate.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ CreateAndActivateUTXOSnapshot(
4747
FILE* outfile{fsbridge::fopen(snapshot_path, "wb")};
4848
AutoFile auto_outfile{outfile};
4949

50-
UniValue result = CreateUTXOSnapshot(
51-
node, node.chainman->ActiveChainstate(), auto_outfile, snapshot_path, snapshot_path);
50+
UniValue result = CreateUTXOSnapshot(node,
51+
node.chainman->ActiveChainstate(),
52+
std::move(auto_outfile), // Will close auto_outfile.
53+
snapshot_path,
54+
snapshot_path);
5255
LogPrintf(
5356
"Wrote UTXO snapshot to %s: %s\n", fs::PathToString(snapshot_path.make_preferred()), result.write());
5457

0 commit comments

Comments
 (0)