Skip to content

Commit 6606a4f

Browse files
committed
move-onlyish: break out CreateUTXOSnapshot from dumptxoutset
This move/refactor is needed to set up a decent unittest for UTXO snapshot activation.
1 parent ad949ba commit 6606a4f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/rpc/blockchain.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,10 +2411,21 @@ static RPCHelpMan dumptxoutset()
24112411

24122412
FILE* file{fsbridge::fopen(temppath, "wb")};
24132413
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
2414+
NodeContext& node = EnsureNodeContext(request.context);
2415+
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
2416+
fs::rename(temppath, path);
2417+
2418+
result.pushKV("path", path.string());
2419+
return result;
2420+
},
2421+
};
2422+
}
2423+
2424+
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
2425+
{
24142426
std::unique_ptr<CCoinsViewCursor> pcursor;
24152427
CCoinsStats stats;
24162428
CBlockIndex* tip;
2417-
NodeContext& node = EnsureNodeContext(request.context);
24182429

24192430
{
24202431
// We need to lock cs_main to ensure that the coinsdb isn't written to
@@ -2431,13 +2442,13 @@ static RPCHelpMan dumptxoutset()
24312442
//
24322443
LOCK(::cs_main);
24332444

2434-
::ChainstateActive().ForceFlushStateToDisk();
2445+
chainstate.ForceFlushStateToDisk();
24352446

2436-
if (!GetUTXOStats(&::ChainstateActive().CoinsDB(), stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) {
2447+
if (!GetUTXOStats(&chainstate.CoinsDB(), stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) {
24372448
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
24382449
}
24392450

2440-
pcursor = std::unique_ptr<CCoinsViewCursor>(::ChainstateActive().CoinsDB().Cursor());
2451+
pcursor = std::unique_ptr<CCoinsViewCursor>(chainstate.CoinsDB().Cursor());
24412452
tip = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock);
24422453
CHECK_NONFATAL(tip);
24432454
}
@@ -2462,16 +2473,13 @@ static RPCHelpMan dumptxoutset()
24622473
}
24632474

24642475
afile.fclose();
2465-
fs::rename(temppath, path);
24662476

24672477
UniValue result(UniValue::VOBJ);
24682478
result.pushKV("coins_written", stats.coins_count);
24692479
result.pushKV("base_hash", tip->GetBlockHash().ToString());
24702480
result.pushKV("base_height", tip->nHeight);
2471-
result.pushKV("path", path.string());
2481+
24722482
return result;
2473-
},
2474-
};
24752483
}
24762484

24772485
void RegisterBlockchainRPCCommands(CRPCTable &t)

src/rpc/blockchain.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_RPC_BLOCKCHAIN_H
77

88
#include <amount.h>
9+
#include <streams.h>
910
#include <sync.h>
1011

1112
#include <stdint.h>
@@ -16,6 +17,7 @@ extern RecursiveMutex cs_main;
1617
class CBlock;
1718
class CBlockIndex;
1819
class CBlockPolicyEstimator;
20+
class CChainState;
1921
class CTxMemPool;
2022
class ChainstateManager;
2123
class UniValue;
@@ -57,4 +59,10 @@ CTxMemPool& EnsureMemPool(const util::Ref& context);
5759
ChainstateManager& EnsureChainman(const util::Ref& context);
5860
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context);
5961

62+
/**
63+
* Helper to create UTXO snapshots given a chainstate and a file handle.
64+
* @return a UniValue map containing metadata about the snapshot.
65+
*/
66+
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile);
67+
6068
#endif

0 commit comments

Comments
 (0)