Skip to content

Commit e5a3519

Browse files
committed
merge bitcoin#25222: Pass reference to LookUpStats
1 parent fec94a5 commit e5a3519

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>
@@ -320,13 +321,13 @@ static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, D
320321
return db.Read(DBHashKey(block.hash), result);
321322
}
322323

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

328329
DBVal entry;
329-
if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
330+
if (!LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
330331
return std::nullopt;
331332
}
332333

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
@@ -1112,9 +1112,9 @@ std::optional<kernel::CCoinsStats> GetUTXOStats(CCoinsView* view, node::BlockMan
11121112
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
11131113
if ((hash_type == kernel::CoinStatsHashType::MUHASH || hash_type == kernel::CoinStatsHashType::NONE) && g_coin_stats_index && index_requested) {
11141114
if (pindex) {
1115-
return g_coin_stats_index->LookUpStats(pindex);
1115+
return g_coin_stats_index->LookUpStats(*pindex);
11161116
} else {
1117-
CBlockIndex* block_index = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
1117+
CBlockIndex& block_index = *CHECK_NONFATAL(WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock())));
11181118
return g_coin_stats_index->LookUpStats(block_index);
11191119
}
11201120
}

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/index.h>
910
#include <test/util/setup_common.h>
1011
#include <test/util/validation.h>
@@ -29,7 +30,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
2930
}
3031

3132
// CoinStatsIndex should not be found before it is started.
32-
BOOST_CHECK(!coin_stats_index.LookUpStats(block_index));
33+
BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
3334

3435
// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
3536
// is started.
@@ -45,10 +46,10 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
4546
LOCK(cs_main);
4647
genesis_block_index = m_node.chainman->ActiveChain().Genesis();
4748
}
48-
BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index));
49+
BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
4950

5051
// Check that CoinStatsIndex updates with new blocks.
51-
BOOST_CHECK(coin_stats_index.LookUpStats(block_index));
52+
BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
5253

5354
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
5455
std::vector<CMutableTransaction> noTxns;
@@ -62,7 +63,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
6263
LOCK(cs_main);
6364
new_block_index = m_node.chainman->ActiveChain().Tip();
6465
}
65-
BOOST_CHECK(coin_stats_index.LookUpStats(new_block_index));
66+
BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
6667

6768
BOOST_CHECK(block_index != new_block_index);
6869

0 commit comments

Comments
 (0)