Skip to content

Commit a789f3f

Browse files
committed
coinstats: Extract hash_type in-member to in-param
Currently, CCoinsStats is a struct with both in-params and out-params where the hash_type and index_requested members are the only in-params. This change removes CCoinsStats' hash_type in-param member and adds it to the relevant functions instead. [META] In subsequent commits, all of CCoinsStats' members which serve as in-params will be moved out so as to make CCoinsStats a pure out-param struct.
1 parent 1022948 commit a789f3f

File tree

5 files changed

+19
-24
lines changed

5 files changed

+19
-24
lines changed

src/node/coinstats.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<u
9393

9494
//! Calculate statistics about the unspent transaction output set
9595
template <typename T>
96-
static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point, const CBlockIndex* pindex)
96+
static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point, const CBlockIndex* pindex, CoinStatsHashType& hash_type)
9797
{
9898
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
9999
assert(pcursor);
@@ -106,7 +106,7 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
106106
stats.hashBlock = pindex->GetBlockHash();
107107

108108
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
109-
if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && stats.index_requested) {
109+
if ((hash_type == CoinStatsHashType::MUHASH || hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && stats.index_requested) {
110110
stats.index_used = true;
111111
return g_coin_stats_index->LookUpStats(pindex, stats);
112112
}
@@ -144,19 +144,19 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
144144
return true;
145145
}
146146

147-
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, const std::function<void()>& interruption_point, const CBlockIndex* pindex)
147+
bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point, const CBlockIndex* pindex)
148148
{
149-
switch (stats.m_hash_type) {
149+
switch (hash_type) {
150150
case(CoinStatsHashType::HASH_SERIALIZED): {
151151
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
152-
return GetUTXOStats(view, blockman, stats, ss, interruption_point, pindex);
152+
return GetUTXOStats(view, blockman, stats, ss, interruption_point, pindex, hash_type);
153153
}
154154
case(CoinStatsHashType::MUHASH): {
155155
MuHash3072 muhash;
156-
return GetUTXOStats(view, blockman, stats, muhash, interruption_point, pindex);
156+
return GetUTXOStats(view, blockman, stats, muhash, interruption_point, pindex, hash_type);
157157
}
158158
case(CoinStatsHashType::NONE): {
159-
return GetUTXOStats(view, blockman, stats, nullptr, interruption_point, pindex);
159+
return GetUTXOStats(view, blockman, stats, nullptr, interruption_point, pindex, hash_type);
160160
}
161161
} // no default case, so the compiler can warn about missing cases
162162
assert(false);

src/node/coinstats.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ enum class CoinStatsHashType {
2828
};
2929

3030
struct CCoinsStats {
31-
//! Which hash type to use
32-
const CoinStatsHashType m_hash_type;
33-
3431
int nHeight{0};
3532
uint256 hashBlock{};
3633
uint64_t nTransactions{0};
@@ -69,12 +66,10 @@ struct CCoinsStats {
6966
CAmount total_unspendables_scripts{0};
7067
//! Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block
7168
CAmount total_unspendables_unclaimed_rewards{0};
72-
73-
CCoinsStats(CoinStatsHashType hash_type) : m_hash_type(hash_type) {}
7469
};
7570

7671
//! Calculate statistics about the unspent transaction output set
77-
bool GetUTXOStats(CCoinsView* view, node::BlockManager& blockman, CCoinsStats& stats, const std::function<void()>& interruption_point = {}, const CBlockIndex* pindex = nullptr);
72+
bool GetUTXOStats(CCoinsView* view, node::BlockManager& blockman, CCoinsStats& stats, CoinStatsHashType hash_type, const std::function<void()>& interruption_point = {}, const CBlockIndex* pindex = nullptr);
7873

7974
uint64_t GetBogoSize(const CScript& script_pub_key);
8075

src/rpc/blockchain.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ static RPCHelpMan gettxoutsetinfo()
862862

863863
const CBlockIndex* pindex{nullptr};
864864
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
865-
CCoinsStats stats{hash_type};
865+
CCoinsStats stats{};
866866
stats.index_requested = request.params[2].isNull() || request.params[2].get_bool();
867867

868868
NodeContext& node = EnsureAnyNodeContext(request.context);
@@ -884,7 +884,7 @@ static RPCHelpMan gettxoutsetinfo()
884884
throw JSONRPCError(RPC_INVALID_PARAMETER, "Querying specific block heights requires coinstatsindex");
885885
}
886886

887-
if (stats.m_hash_type == CoinStatsHashType::HASH_SERIALIZED) {
887+
if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
888888
throw JSONRPCError(RPC_INVALID_PARAMETER, "hash_serialized_2 hash type cannot be queried for a specific block");
889889
}
890890

@@ -903,7 +903,7 @@ static RPCHelpMan gettxoutsetinfo()
903903
}
904904
}
905905

906-
if (GetUTXOStats(coins_view, *blockman, stats, node.rpc_interruption_point, pindex)) {
906+
if (GetUTXOStats(coins_view, *blockman, stats, hash_type, node.rpc_interruption_point, pindex)) {
907907
ret.pushKV("height", (int64_t)stats.nHeight);
908908
ret.pushKV("bestblock", stats.hashBlock.GetHex());
909909
ret.pushKV("txouts", (int64_t)stats.nTransactionOutputs);
@@ -922,10 +922,10 @@ static RPCHelpMan gettxoutsetinfo()
922922
} else {
923923
ret.pushKV("total_unspendable_amount", ValueFromAmount(stats.total_unspendable_amount));
924924

925-
CCoinsStats prev_stats{hash_type};
925+
CCoinsStats prev_stats{};
926926

927927
if (pindex->nHeight > 0) {
928-
GetUTXOStats(coins_view, *blockman, prev_stats, node.rpc_interruption_point, pindex->pprev);
928+
GetUTXOStats(coins_view, *blockman, prev_stats, hash_type, node.rpc_interruption_point, pindex->pprev);
929929
}
930930

931931
UniValue block_info(UniValue::VOBJ);
@@ -2285,7 +2285,7 @@ UniValue CreateUTXOSnapshot(
22852285
const fs::path& temppath)
22862286
{
22872287
std::unique_ptr<CCoinsViewCursor> pcursor;
2288-
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
2288+
CCoinsStats stats{};
22892289
const CBlockIndex* tip;
22902290

22912291
{
@@ -2305,7 +2305,7 @@ UniValue CreateUTXOSnapshot(
23052305

23062306
chainstate.ForceFlushStateToDisk();
23072307

2308-
if (!GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman, stats, node.rpc_interruption_point)) {
2308+
if (!GetUTXOStats(&chainstate.CoinsDB(), chainstate.m_blockman, stats, CoinStatsHashType::HASH_SERIALIZED, node.rpc_interruption_point)) {
23092309
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to read UTXO set");
23102310
}
23112311

src/test/coinstatsindex_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
3333
{
3434
CoinStatsIndex coin_stats_index{1 << 20, true};
3535

36-
CCoinsStats coin_stats{CoinStatsHashType::MUHASH};
36+
CCoinsStats coin_stats{};
3737
const CBlockIndex* block_index;
3838
{
3939
LOCK(cs_main);
@@ -69,7 +69,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
6969
// Let the CoinStatsIndex to catch up again.
7070
BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
7171

72-
CCoinsStats new_coin_stats{CoinStatsHashType::MUHASH};
72+
CCoinsStats new_coin_stats{};
7373
const CBlockIndex* new_block_index;
7474
{
7575
LOCK(cs_main);

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5095,14 +5095,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
50955095

50965096
assert(coins_cache.GetBestBlock() == base_blockhash);
50975097

5098-
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
5098+
CCoinsStats stats{};
50995099
auto breakpoint_fnc = [] { /* TODO insert breakpoint here? */ };
51005100

51015101
// As above, okay to immediately release cs_main here since no other context knows
51025102
// about the snapshot_chainstate.
51035103
CCoinsViewDB* snapshot_coinsdb = WITH_LOCK(::cs_main, return &snapshot_chainstate.CoinsDB());
51045104

5105-
if (!GetUTXOStats(snapshot_coinsdb, m_blockman, stats, breakpoint_fnc)) {
5105+
if (!GetUTXOStats(snapshot_coinsdb, m_blockman, stats, CoinStatsHashType::HASH_SERIALIZED, breakpoint_fnc)) {
51065106
LogPrintf("[snapshot] failed to generate coins stats\n");
51075107
return false;
51085108
}

0 commit comments

Comments
 (0)