Skip to content

Commit faa3d38

Browse files
author
MacroFake
committed
refactor: Pass reference to LookUpStats
1 parent 0897b18 commit faa3d38

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/index/coinstatsindex.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <coins.h>
77
#include <crypto/muhash.h>
88
#include <index/coinstatsindex.h>
9+
#include <kernel/coinstats.h>
910
#include <node/blockstorage.h>
1011
#include <serialize.h>
1112
#include <txdb.h>
@@ -322,13 +323,13 @@ static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, D
322323
return db.Read(DBHashKey(block.hash), result);
323324
}
324325

325-
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex* block_index) const
326+
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_index) const
326327
{
327-
CCoinsStats stats{Assert(block_index)->nHeight, block_index->GetBlockHash()};
328+
CCoinsStats stats{block_index.nHeight, block_index.GetBlockHash()};
328329
stats.index_used = true;
329330

330331
DBVal entry;
331-
if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
332+
if (!LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
332333
return std::nullopt;
333334
}
334335

src/index/coinstatsindex.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
#ifndef BITCOIN_INDEX_COINSTATSINDEX_H
66
#define BITCOIN_INDEX_COINSTATSINDEX_H
77

8-
#include <chain.h>
98
#include <crypto/muhash.h>
10-
#include <flatfile.h>
119
#include <index/base.h>
12-
#include <kernel/coinstats.h>
10+
11+
class CBlockIndex;
12+
class CDBBatch;
13+
namespace kernel {
14+
struct CCoinsStats;
15+
}
1316

1417
/**
1518
* CoinStatsIndex maintains statistics on the UTXO set.
@@ -56,7 +59,7 @@ class CoinStatsIndex final : public BaseIndex
5659
explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
5760

5861
// Look up stats for a specific block using CBlockIndex
59-
std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex* block_index) const;
62+
std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex& block_index) const;
6063
};
6164

6265
/// The global UTXO set hash object.

src/rpc/blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,9 @@ static std::optional<kernel::CCoinsStats> GetUTXOStats(CCoinsView* view, node::B
832832
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
833833
if ((hash_type == kernel::CoinStatsHashType::MUHASH || hash_type == kernel::CoinStatsHashType::NONE) && g_coin_stats_index && index_requested) {
834834
if (pindex) {
835-
return g_coin_stats_index->LookUpStats(pindex);
835+
return g_coin_stats_index->LookUpStats(*pindex);
836836
} else {
837-
CBlockIndex* block_index = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
837+
CBlockIndex& block_index = *CHECK_NONFATAL(WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock())));
838838
return g_coin_stats_index->LookUpStats(block_index);
839839
}
840840
}

src/test/coinstatsindex_tests.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chainparams.h>
66
#include <index/coinstatsindex.h>
77
#include <interfaces/chain.h>
8+
#include <kernel/coinstats.h>
89
#include <test/util/setup_common.h>
910
#include <test/util/validation.h>
1011
#include <util/time.h>
@@ -38,7 +39,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
3839
}
3940

4041
// CoinStatsIndex should not be found before it is started.
41-
BOOST_CHECK(!coin_stats_index.LookUpStats(block_index));
42+
BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
4243

4344
// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
4445
// is started.
@@ -54,10 +55,10 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
5455
LOCK(cs_main);
5556
genesis_block_index = m_node.chainman->ActiveChain().Genesis();
5657
}
57-
BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index));
58+
BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
5859

5960
// Check that CoinStatsIndex updates with new blocks.
60-
BOOST_CHECK(coin_stats_index.LookUpStats(block_index));
61+
BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
6162

6263
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
6364
std::vector<CMutableTransaction> noTxns;
@@ -71,7 +72,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
7172
LOCK(cs_main);
7273
new_block_index = m_node.chainman->ActiveChain().Tip();
7374
}
74-
BOOST_CHECK(coin_stats_index.LookUpStats(new_block_index));
75+
BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
7576

7677
BOOST_CHECK(block_index != new_block_index);
7778

0 commit comments

Comments
 (0)