move-onlyish: break out CreateUTXOSnapshot from dumptxoutset

This move/refactor is needed to set up a decent unittest for UTXO snapshot activation.
This commit is contained in:
James O'Beirne
2020-08-25 13:46:58 -04:00
parent ad949ba449
commit 6606a4f8c6
2 changed files with 24 additions and 8 deletions

View File

@@ -2411,10 +2411,21 @@ static RPCHelpMan dumptxoutset()
FILE* file{fsbridge::fopen(temppath, "wb")}; FILE* file{fsbridge::fopen(temppath, "wb")};
CAutoFile afile{file, SER_DISK, CLIENT_VERSION}; CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
NodeContext& node = EnsureNodeContext(request.context);
UniValue result = CreateUTXOSnapshot(node, node.chainman->ActiveChainstate(), afile);
fs::rename(temppath, path);
result.pushKV("path", path.string());
return result;
},
};
}
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
{
std::unique_ptr<CCoinsViewCursor> pcursor; std::unique_ptr<CCoinsViewCursor> pcursor;
CCoinsStats stats; CCoinsStats stats;
CBlockIndex* tip; CBlockIndex* tip;
NodeContext& node = EnsureNodeContext(request.context);
{ {
// We need to lock cs_main to ensure that the coinsdb isn't written to // We need to lock cs_main to ensure that the coinsdb isn't written to
@@ -2431,13 +2442,13 @@ static RPCHelpMan dumptxoutset()
// //
LOCK(::cs_main); LOCK(::cs_main);
::ChainstateActive().ForceFlushStateToDisk(); chainstate.ForceFlushStateToDisk();
if (!GetUTXOStats(&::ChainstateActive().CoinsDB(), stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) { if (!GetUTXOStats(&chainstate.CoinsDB(), stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
} }
pcursor = std::unique_ptr<CCoinsViewCursor>(::ChainstateActive().CoinsDB().Cursor()); pcursor = std::unique_ptr<CCoinsViewCursor>(chainstate.CoinsDB().Cursor());
tip = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock); tip = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock);
CHECK_NONFATAL(tip); CHECK_NONFATAL(tip);
} }
@@ -2462,16 +2473,13 @@ static RPCHelpMan dumptxoutset()
} }
afile.fclose(); afile.fclose();
fs::rename(temppath, path);
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.pushKV("coins_written", stats.coins_count); result.pushKV("coins_written", stats.coins_count);
result.pushKV("base_hash", tip->GetBlockHash().ToString()); result.pushKV("base_hash", tip->GetBlockHash().ToString());
result.pushKV("base_height", tip->nHeight); result.pushKV("base_height", tip->nHeight);
result.pushKV("path", path.string());
return result; return result;
},
};
} }
void RegisterBlockchainRPCCommands(CRPCTable &t) void RegisterBlockchainRPCCommands(CRPCTable &t)

View File

@@ -6,6 +6,7 @@
#define BITCOIN_RPC_BLOCKCHAIN_H #define BITCOIN_RPC_BLOCKCHAIN_H
#include <amount.h> #include <amount.h>
#include <streams.h>
#include <sync.h> #include <sync.h>
#include <stdint.h> #include <stdint.h>
@@ -16,6 +17,7 @@ extern RecursiveMutex cs_main;
class CBlock; class CBlock;
class CBlockIndex; class CBlockIndex;
class CBlockPolicyEstimator; class CBlockPolicyEstimator;
class CChainState;
class CTxMemPool; class CTxMemPool;
class ChainstateManager; class ChainstateManager;
class UniValue; class UniValue;
@@ -57,4 +59,10 @@ CTxMemPool& EnsureMemPool(const util::Ref& context);
ChainstateManager& EnsureChainman(const util::Ref& context); ChainstateManager& EnsureChainman(const util::Ref& context);
CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context); CBlockPolicyEstimator& EnsureFeeEstimator(const util::Ref& context);
/**
* Helper to create UTXO snapshots given a chainstate and a file handle.
* @return a UniValue map containing metadata about the snapshot.
*/
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile);
#endif #endif