Skip to content

Commit b7634fe

Browse files
committed
Move logic from LookupUTXOStatsWithIndex to CoinStatsIndex::LookUpStats
The indexing codepath logic in node/coinstats.cpp is simple enough to be moved into CoinStatsIndex::LookUpStats, avoiding an additional layer of function calls. Callers are modified accordingly. Also, add 2 missed BOOST_CHECKs to the coinstatsindex_initial_sync unit test.
1 parent 1352e41 commit b7634fe

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

src/index/coinstatsindex.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,28 +316,31 @@ static bool LookUpOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVa
316316
return db.Read(DBHashKey(block_index->GetBlockHash()), result);
317317
}
318318

319-
bool CoinStatsIndex::LookUpStats(const CBlockIndex* block_index, CCoinsStats& coins_stats) const
319+
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex* block_index) const
320320
{
321+
CCoinsStats stats{Assert(block_index)->nHeight, block_index->GetBlockHash()};
322+
stats.index_used = true;
323+
321324
DBVal entry;
322325
if (!LookUpOne(*m_db, block_index, entry)) {
323-
return false;
326+
return std::nullopt;
324327
}
325328

326-
coins_stats.hashSerialized = entry.muhash;
327-
coins_stats.nTransactionOutputs = entry.transaction_output_count;
328-
coins_stats.nBogoSize = entry.bogo_size;
329-
coins_stats.total_amount = entry.total_amount;
330-
coins_stats.total_subsidy = entry.total_subsidy;
331-
coins_stats.total_unspendable_amount = entry.total_unspendable_amount;
332-
coins_stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
333-
coins_stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
334-
coins_stats.total_coinbase_amount = entry.total_coinbase_amount;
335-
coins_stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
336-
coins_stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
337-
coins_stats.total_unspendables_scripts = entry.total_unspendables_scripts;
338-
coins_stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
339-
340-
return true;
329+
stats.hashSerialized = entry.muhash;
330+
stats.nTransactionOutputs = entry.transaction_output_count;
331+
stats.nBogoSize = entry.bogo_size;
332+
stats.total_amount = entry.total_amount;
333+
stats.total_subsidy = entry.total_subsidy;
334+
stats.total_unspendable_amount = entry.total_unspendable_amount;
335+
stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
336+
stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
337+
stats.total_coinbase_amount = entry.total_coinbase_amount;
338+
stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
339+
stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
340+
stats.total_unspendables_scripts = entry.total_unspendables_scripts;
341+
stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
342+
343+
return stats;
341344
}
342345

343346
bool CoinStatsIndex::Init()

src/index/coinstatsindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CoinStatsIndex final : public BaseIndex
5656
explicit CoinStatsIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
5757

5858
// Look up stats for a specific block using CBlockIndex
59-
bool LookUpStats(const CBlockIndex* block_index, node::CCoinsStats& coins_stats) const;
59+
std::optional<node::CCoinsStats> LookUpStats(const CBlockIndex* block_index) const;
6060
};
6161

6262
/// The global UTXO set hash object.

src/node/coinstats.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -187,33 +187,15 @@ static void FinalizeHash(MuHash3072& muhash, CCoinsStats& stats)
187187
}
188188
static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}
189189

190-
std::optional<CCoinsStats> LookupUTXOStatsWithIndex(CoinStatsIndex& coin_stats_index, const CBlockIndex* pindex)
191-
{
192-
CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()};
193-
194-
stats.index_used = true;
195-
if (!coin_stats_index.LookUpStats(pindex, stats)) {
196-
return std::nullopt;
197-
}
198-
199-
return stats;
200-
}
201-
202-
std::optional<CCoinsStats> LookupUTXOStatsWithIndex(CoinStatsIndex& coin_stats_index, CCoinsView* view, BlockManager& blockman)
203-
{
204-
CBlockIndex* pindex = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
205-
206-
return LookupUTXOStatsWithIndex(coin_stats_index, pindex);
207-
}
208-
209190
std::optional<CCoinsStats> GetUTXOStats(CCoinsView* view, BlockManager& blockman, CoinStatsHashType hash_type, const std::function<void()>& interruption_point, const CBlockIndex* pindex, bool index_requested)
210191
{
211192
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
212193
if ((hash_type == CoinStatsHashType::MUHASH || hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && index_requested) {
213194
if (pindex) {
214-
return LookupUTXOStatsWithIndex(*g_coin_stats_index, pindex);
195+
return g_coin_stats_index->LookUpStats(pindex);
215196
} else {
216-
return LookupUTXOStatsWithIndex(*g_coin_stats_index, view, blockman);
197+
CBlockIndex* block_index = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
198+
return g_coin_stats_index->LookUpStats(block_index);
217199
}
218200
}
219201

src/test/coinstatsindex_tests.cpp

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

36-
CCoinsStats coin_stats{};
3736
const CBlockIndex* block_index;
3837
{
3938
LOCK(cs_main);
4039
block_index = m_node.chainman->ActiveChain().Tip();
4140
}
4241

4342
// CoinStatsIndex should not be found before it is started.
44-
BOOST_CHECK(!coin_stats_index.LookUpStats(block_index, coin_stats));
43+
BOOST_CHECK(!coin_stats_index.LookUpStats(block_index));
4544

4645
// BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
4746
// is started.
@@ -57,10 +56,10 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
5756
LOCK(cs_main);
5857
genesis_block_index = m_node.chainman->ActiveChain().Genesis();
5958
}
60-
BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index, coin_stats));
59+
BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index));
6160

6261
// Check that CoinStatsIndex updates with new blocks.
63-
coin_stats_index.LookUpStats(block_index, coin_stats);
62+
BOOST_CHECK(coin_stats_index.LookUpStats(block_index));
6463

6564
const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
6665
std::vector<CMutableTransaction> noTxns;
@@ -69,13 +68,12 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
6968
// Let the CoinStatsIndex to catch up again.
7069
BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
7170

72-
CCoinsStats new_coin_stats{};
7371
const CBlockIndex* new_block_index;
7472
{
7573
LOCK(cs_main);
7674
new_block_index = m_node.chainman->ActiveChain().Tip();
7775
}
78-
coin_stats_index.LookUpStats(new_block_index, new_coin_stats);
76+
BOOST_CHECK(coin_stats_index.LookUpStats(new_block_index));
7977

8078
BOOST_CHECK(block_index != new_block_index);
8179

0 commit comments

Comments
 (0)