0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-13 11:25:02 -05:00

refactor: Pass hash_type to CoinsStats in stats object

This commit is contained in:
Fabian Jahr 2021-02-21 23:24:28 +01:00
parent 2e2648a902
commit 9c8a265fd2
No known key found for this signature in database
GPG key ID: F13D1E9D890798CD
5 changed files with 17 additions and 14 deletions

View file

@ -86,7 +86,6 @@ static void ApplyStats(CCoinsStats& stats, T& hash_obj, const uint256& hash, con
template <typename T> template <typename T>
static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point) static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
{ {
stats = CCoinsStats();
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor()); std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
assert(pcursor); assert(pcursor);
@ -129,9 +128,9 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
return true; return true;
} }
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point) bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, const std::function<void()>& interruption_point)
{ {
switch (hash_type) { switch (stats.m_hash_type) {
case(CoinStatsHashType::HASH_SERIALIZED): { case(CoinStatsHashType::HASH_SERIALIZED): {
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
return GetUTXOStats(view, blockman, stats, ss, interruption_point); return GetUTXOStats(view, blockman, stats, ss, interruption_point);

View file

@ -23,6 +23,7 @@ enum class CoinStatsHashType {
struct CCoinsStats struct CCoinsStats
{ {
CoinStatsHashType m_hash_type;
int nHeight{0}; int nHeight{0};
uint256 hashBlock{}; uint256 hashBlock{};
uint64_t nTransactions{0}; uint64_t nTransactions{0};
@ -34,9 +35,11 @@ struct CCoinsStats
//! The number of coins contained. //! The number of coins contained.
uint64_t coins_count{0}; uint64_t coins_count{0};
CCoinsStats(CoinStatsHashType hash_type) : m_hash_type(hash_type) {}
}; };
//! Calculate statistics about the unspent transaction output set //! Calculate statistics about the unspent transaction output set
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, const CoinStatsHashType hash_type, const std::function<void()>& interruption_point = {}); bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, const std::function<void()>& interruption_point = {});
#endif // BITCOIN_NODE_COINSTATS_H #endif // BITCOIN_NODE_COINSTATS_H

View file

@ -1093,14 +1093,14 @@ static RPCHelpMan gettxoutsetinfo()
{ {
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
CCoinsStats stats; const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
CCoinsStats stats{hash_type};
NodeContext& node = EnsureAnyNodeContext(request.context); NodeContext& node = EnsureAnyNodeContext(request.context);
ChainstateManager& chainman = EnsureChainman(node); ChainstateManager& chainman = EnsureChainman(node);
CChainState& active_chainstate = chainman.ActiveChainstate(); CChainState& active_chainstate = chainman.ActiveChainstate();
active_chainstate.ForceFlushStateToDisk(); active_chainstate.ForceFlushStateToDisk();
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
CCoinsView* coins_view; CCoinsView* coins_view;
BlockManager* blockman; BlockManager* blockman;
{ {
@ -1108,7 +1108,8 @@ static RPCHelpMan gettxoutsetinfo()
coins_view = &active_chainstate.CoinsDB(); coins_view = &active_chainstate.CoinsDB();
blockman = &active_chainstate.m_blockman; blockman = &active_chainstate.m_blockman;
} }
if (GetUTXOStats(coins_view, *blockman, stats, hash_type, node.rpc_interruption_point)) {
if (GetUTXOStats(coins_view, *blockman, stats, node.rpc_interruption_point)) {
ret.pushKV("height", (int64_t)stats.nHeight); ret.pushKV("height", (int64_t)stats.nHeight);
ret.pushKV("bestblock", stats.hashBlock.GetHex()); ret.pushKV("bestblock", stats.hashBlock.GetHex());
ret.pushKV("transactions", (int64_t)stats.nTransactions); ret.pushKV("transactions", (int64_t)stats.nTransactions);
@ -2491,7 +2492,7 @@ static RPCHelpMan dumptxoutset()
UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile) UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFile& afile)
{ {
std::unique_ptr<CCoinsViewCursor> pcursor; std::unique_ptr<CCoinsViewCursor> pcursor;
CCoinsStats stats; CCoinsStats stats{CoinStatsHashType::NONE};
CBlockIndex* tip; CBlockIndex* tip;
{ {
@ -2511,7 +2512,7 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
chainstate.ForceFlushStateToDisk(); chainstate.ForceFlushStateToDisk();
if (!GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman, stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) { if (!GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman, stats, node.rpc_interruption_point)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set"); throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
} }

View file

@ -258,10 +258,10 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
(void)GetTransactionSigOpCost(transaction, coins_view_cache, flags); (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
}, },
[&] { [&] {
CCoinsStats stats; CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
bool expected_code_path = false; bool expected_code_path = false;
try { try {
(void)GetUTXOStats(&coins_view_cache, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)), stats, CoinStatsHashType::HASH_SERIALIZED); (void)GetUTXOStats(&coins_view_cache, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)), stats);
} catch (const std::logic_error&) { } catch (const std::logic_error&) {
expected_code_path = true; expected_code_path = true;
} }

View file

@ -5285,14 +5285,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
return false; return false;
} }
CCoinsStats stats; CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
auto breakpoint_fnc = [] { /* TODO insert breakpoint here? */ }; auto breakpoint_fnc = [] { /* TODO insert breakpoint here? */ };
// As above, okay to immediately release cs_main here since no other context knows // As above, okay to immediately release cs_main here since no other context knows
// about the snapshot_chainstate. // about the snapshot_chainstate.
CCoinsViewDB* snapshot_coinsdb = WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB()); CCoinsViewDB* snapshot_coinsdb = WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
if (!GetUTXOStats(snapshot_coinsdb, WITH_LOCK(::cs_main, return std::ref(m_blockman)), stats, CoinStatsHashType::HASH_SERIALIZED, breakpoint_fnc)) { if (!GetUTXOStats(snapshot_coinsdb, WITH_LOCK(::cs_main, return std::ref(m_blockman)), stats, breakpoint_fnc)) {
LogPrintf("[snapshot] failed to generate coins stats\n"); LogPrintf("[snapshot] failed to generate coins stats\n");
return false; return false;
} }