diff --git a/src/kernel/coinstats.cpp b/src/kernel/coinstats.cpp index e165d3e212c..930023425c5 100644 --- a/src/kernel/coinstats.cpp +++ b/src/kernel/coinstats.cpp @@ -108,9 +108,17 @@ static void ApplyStats(CCoinsStats& stats, const std::map& outpu //! Calculate statistics about the unspent transaction output set template -static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function& interruption_point, std::unique_ptr pcursor) +static std::optional ComputeUTXOStats(T hash_obj, CCoinsView* view, node::BlockManager& blockman, const std::function& interruption_point) { + std::unique_ptr pcursor; + CBlockIndex* pindex; + { + LOCK(::cs_main); + pcursor = view->Cursor(); + pindex = blockman.LookupBlockIndex(pcursor->GetBestBlock()); + } assert(pcursor); + CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()}; Txid prevkey; std::map outputs; @@ -129,7 +137,7 @@ static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, c stats.coins_count++; } else { LogError("%s: unable to read value\n", __func__); - return false; + return std::nullopt; } pcursor->Next(); } @@ -141,42 +149,27 @@ static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, c FinalizeHash(hash_obj, stats); stats.nDiskSize = view->EstimateSize(); - - return true; + return stats; } std::optional ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function& interruption_point) { - std::unique_ptr pcursor; - CBlockIndex* pindex; - { - LOCK(::cs_main); - pcursor = view->Cursor(); - pindex = blockman.LookupBlockIndex(pcursor->GetBestBlock()); - } - CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()}; - - bool success = [&]() -> bool { + return [&]() -> std::optional { switch (hash_type) { case(CoinStatsHashType::HASH_SERIALIZED): { HashWriter ss{}; - return ComputeUTXOStats(view, stats, ss, interruption_point, std::move(pcursor)); + return ComputeUTXOStats(ss, view, blockman, interruption_point); } case(CoinStatsHashType::MUHASH): { MuHash3072 muhash; - return ComputeUTXOStats(view, stats, muhash, interruption_point, std::move(pcursor)); + return ComputeUTXOStats(muhash, view, blockman, interruption_point); } case(CoinStatsHashType::NONE): { - return ComputeUTXOStats(view, stats, nullptr, interruption_point, std::move(pcursor)); + return ComputeUTXOStats(nullptr, view, blockman, interruption_point); } } // no default case, so the compiler can warn about missing cases assert(false); }(); - - if (!success) { - return std::nullopt; - } - return stats; } static void FinalizeHash(HashWriter& ss, CCoinsStats& stats) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 0821ebba18a..4953107bbf3 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1078,7 +1078,6 @@ static RPCMethod gettxoutsetinfo() { UniValue ret(UniValue::VOBJ); - const CBlockIndex* pindex{nullptr}; const CoinStatsHashType hash_type{ParseHashType(self.Arg("hash_type"))}; bool index_requested = request.params[2].isNull() || request.params[2].get_bool(); @@ -1095,6 +1094,7 @@ static RPCMethod gettxoutsetinfo() blockman = &active_chainstate.m_blockman; } + const CBlockIndex* pindex{nullptr}; if (!request.params[1].isNull()) { if (!g_coin_stats_index) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Querying specific block heights requires coinstatsindex");