Skip to content

Commit 106bcd4

Browse files
committed
node/coinstats: Pass in BlockManager to GetUTXOStats
1 parent 2c3ba00 commit 106bcd4

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

src/node/coinstats.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void ApplyStats(CCoinsStats& stats, T& hash_obj, const uint256& hash, con
8383

8484
//! Calculate statistics about the unspent transaction output set
8585
template <typename T>
86-
static bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
86+
static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
8787
{
8888
stats = CCoinsStats();
8989
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
@@ -92,7 +92,8 @@ static bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const
9292
stats.hashBlock = pcursor->GetBestBlock();
9393
{
9494
LOCK(cs_main);
95-
stats.nHeight = g_chainman.m_blockman.LookupBlockIndex(stats.hashBlock)->nHeight;
95+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(blockman));
96+
stats.nHeight = blockman.LookupBlockIndex(stats.hashBlock)->nHeight;
9697
}
9798

9899
PrepareHash(hash_obj, stats);
@@ -126,19 +127,19 @@ static bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const
126127
return true;
127128
}
128129

129-
bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point)
130+
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point)
130131
{
131132
switch (hash_type) {
132133
case(CoinStatsHashType::HASH_SERIALIZED): {
133134
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
134-
return GetUTXOStats(view, stats, ss, interruption_point);
135+
return GetUTXOStats(view, blockman, stats, ss, interruption_point);
135136
}
136137
case(CoinStatsHashType::MUHASH): {
137138
MuHash3072 muhash;
138-
return GetUTXOStats(view, stats, muhash, interruption_point);
139+
return GetUTXOStats(view, blockman, stats, muhash, interruption_point);
139140
}
140141
case(CoinStatsHashType::NONE): {
141-
return GetUTXOStats(view, stats, nullptr, interruption_point);
142+
return GetUTXOStats(view, blockman, stats, nullptr, interruption_point);
142143
}
143144
} // no default case, so the compiler can warn about missing cases
144145
assert(false);

src/node/coinstats.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <amount.h>
1010
#include <uint256.h>
11+
#include <validation.h>
1112

1213
#include <cstdint>
1314
#include <functional>
@@ -36,6 +37,6 @@ struct CCoinsStats
3637
};
3738

3839
//! Calculate statistics about the unspent transaction output set
39-
bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, const CoinStatsHashType hash_type, const std::function<void()>& interruption_point = {});
40+
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, const CoinStatsHashType hash_type, const std::function<void()>& interruption_point = {});
4041

4142
#endif // BITCOIN_NODE_COINSTATS_H

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,9 @@ static RPCHelpMan gettxoutsetinfo()
10731073

10741074
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
10751075

1076-
CCoinsView* coins_view = WITH_LOCK(cs_main, return &ChainstateActive().CoinsDB());
1076+
CCoinsView* coins_view = WITH_LOCK(::cs_main, return &::ChainstateActive().CoinsDB());
10771077
NodeContext& node = EnsureNodeContext(request.context);
1078-
if (GetUTXOStats(coins_view, stats, hash_type, node.rpc_interruption_point)) {
1078+
if (GetUTXOStats(coins_view, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)), stats, hash_type, node.rpc_interruption_point)) {
10791079
ret.pushKV("height", (int64_t)stats.nHeight);
10801080
ret.pushKV("bestblock", stats.hashBlock.GetHex());
10811081
ret.pushKV("transactions", (int64_t)stats.nTransactions);
@@ -2444,7 +2444,7 @@ UniValue CreateUTXOSnapshot(NodeContext& node, CChainState& chainstate, CAutoFil
24442444

24452445
chainstate.ForceFlushStateToDisk();
24462446

2447-
if (!GetUTXOStats(&chainstate.CoinsDB(), stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) {
2447+
if (!GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman, stats, CoinStatsHashType::NONE, node.rpc_interruption_point)) {
24482448
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
24492449
}
24502450

src/test/fuzz/coins_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
264264
CCoinsStats stats;
265265
bool expected_code_path = false;
266266
try {
267-
(void)GetUTXOStats(&coins_view_cache, stats, CoinStatsHashType::HASH_SERIALIZED);
267+
(void)GetUTXOStats(&coins_view_cache, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)), stats, CoinStatsHashType::HASH_SERIALIZED);
268268
} catch (const std::logic_error&) {
269269
expected_code_path = true;
270270
}

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5425,7 +5425,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
54255425
// about the snapshot_chainstate.
54265426
CCoinsViewDB* snapshot_coinsdb = WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
54275427

5428-
if (!GetUTXOStats(snapshot_coinsdb, stats, CoinStatsHashType::HASH_SERIALIZED, breakpoint_fnc)) {
5428+
if (!GetUTXOStats(snapshot_coinsdb, WITH_LOCK(::cs_main, return std::ref(m_blockman)), stats, CoinStatsHashType::HASH_SERIALIZED, breakpoint_fnc)) {
54295429
LogPrintf("[snapshot] failed to generate coins stats\n");
54305430
return false;
54315431
}

0 commit comments

Comments
 (0)