Skip to content

Commit fa340b8

Browse files
author
MarcoFalke
committed
refactor: Avoid magic value of all-zeros in assumeutxo base_blockhash
Just use std::optional
1 parent fae33f9 commit fa340b8

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,8 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
226226

227227
// Snapshot should refuse to load at this height.
228228
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(m_node, m_path_root));
229-
BOOST_CHECK(chainman.ActiveChainstate().m_from_snapshot_blockhash.IsNull());
230-
BOOST_CHECK_EQUAL(
231-
chainman.ActiveChainstate().m_from_snapshot_blockhash,
232-
chainman.SnapshotBlockhash().value_or(uint256()));
229+
BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash);
230+
BOOST_CHECK(!chainman.SnapshotBlockhash());
233231

234232
// Mine 10 more blocks, putting at us height 110 where a valid assumeutxo value can
235233
// be found.
@@ -274,9 +272,9 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
274272
BOOST_REQUIRE(CreateAndActivateUTXOSnapshot(m_node, m_path_root));
275273

276274
// Ensure our active chain is the snapshot chainstate.
277-
BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash.IsNull());
275+
BOOST_CHECK(!chainman.ActiveChainstate().m_from_snapshot_blockhash->IsNull());
278276
BOOST_CHECK_EQUAL(
279-
chainman.ActiveChainstate().m_from_snapshot_blockhash,
277+
*chainman.ActiveChainstate().m_from_snapshot_blockhash,
280278
*chainman.SnapshotBlockhash());
281279

282280
const AssumeutxoData& au_data = *ExpectedAssumeutxo(snapshot_height, ::Params());
@@ -352,7 +350,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
352350

353351
// Snapshot blockhash should be unchanged.
354352
BOOST_CHECK_EQUAL(
355-
chainman.ActiveChainstate().m_from_snapshot_blockhash,
353+
*chainman.ActiveChainstate().m_from_snapshot_blockhash,
356354
loaded_snapshot_blockhash);
357355
}
358356

src/validation.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ void CoinsViews::InitCache()
11581158
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
11591159
}
11601160

1161-
CChainState::CChainState(CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash)
1161+
CChainState::CChainState(CTxMemPool& mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash)
11621162
: m_mempool(mempool),
11631163
m_blockman(blockman),
11641164
m_from_snapshot_blockhash(from_snapshot_blockhash) {}
@@ -1169,8 +1169,8 @@ void CChainState::InitCoinsDB(
11691169
bool should_wipe,
11701170
std::string leveldb_name)
11711171
{
1172-
if (!m_from_snapshot_blockhash.IsNull()) {
1173-
leveldb_name += "_" + m_from_snapshot_blockhash.ToString();
1172+
if (m_from_snapshot_blockhash) {
1173+
leveldb_name += "_" + m_from_snapshot_blockhash->ToString();
11741174
}
11751175

11761176
m_coins_views = std::make_unique<CoinsViews>(
@@ -3877,7 +3877,7 @@ bool CVerifyDB::VerifyDB(
38773877
int reportDone = 0;
38783878
LogPrintf("[0%%]..."); /* Continued */
38793879

3880-
bool is_snapshot_cs = !chainstate.m_from_snapshot_blockhash.IsNull();
3880+
const bool is_snapshot_cs{!chainstate.m_from_snapshot_blockhash};
38813881

38823882
for (pindex = chainstate.m_chain.Tip(); pindex && pindex->pprev; pindex = pindex->pprev) {
38833883
const int percentageDone = std::max(1, std::min(99, (int)(((double)(chainstate.m_chain.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
@@ -4458,8 +4458,8 @@ std::string CChainState::ToString()
44584458
{
44594459
CBlockIndex* tip = m_chain.Tip();
44604460
return strprintf("Chainstate [%s] @ height %d (%s)",
4461-
m_from_snapshot_blockhash.IsNull() ? "ibd" : "snapshot",
4462-
tip ? tip->nHeight : -1, tip ? tip->GetBlockHash().ToString() : "null");
4461+
m_from_snapshot_blockhash ? "snapshot" : "ibd",
4462+
tip ? tip->nHeight : -1, tip ? tip->GetBlockHash().ToString() : "null");
44634463
}
44644464

44654465
bool CChainState::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
@@ -4662,10 +4662,10 @@ double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pin
46624662
return std::min<double>(pindex->nChainTx / fTxTotal, 1.0);
46634663
}
46644664

4665-
std::optional<uint256> ChainstateManager::SnapshotBlockhash() const {
4665+
std::optional<uint256> ChainstateManager::SnapshotBlockhash() const
4666+
{
46664667
LOCK(::cs_main);
4667-
if (m_active_chainstate != nullptr &&
4668-
!m_active_chainstate->m_from_snapshot_blockhash.IsNull()) {
4668+
if (m_active_chainstate && m_active_chainstate->m_from_snapshot_blockhash) {
46694669
// If a snapshot chainstate exists, it will always be our active.
46704670
return m_active_chainstate->m_from_snapshot_blockhash;
46714671
}
@@ -4688,9 +4688,9 @@ std::vector<CChainState*> ChainstateManager::GetAll()
46884688
return out;
46894689
}
46904690

4691-
CChainState& ChainstateManager::InitializeChainstate(CTxMemPool& mempool, const uint256& snapshot_blockhash)
4691+
CChainState& ChainstateManager::InitializeChainstate(CTxMemPool& mempool, const std::optional<uint256>& snapshot_blockhash)
46924692
{
4693-
bool is_snapshot = !snapshot_blockhash.IsNull();
4693+
bool is_snapshot = snapshot_blockhash.has_value();
46944694
std::unique_ptr<CChainState>& to_modify =
46954695
is_snapshot ? m_snapshot_chainstate : m_ibd_chainstate;
46964696

src/validation.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ class CChainState
553553
//! CChainState instances.
554554
BlockManager& m_blockman;
555555

556-
explicit CChainState(CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash = uint256());
556+
explicit CChainState(CTxMemPool& mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash = std::nullopt);
557557

558558
/**
559559
* Initialize the CoinsViews UTXO set database management data structures. The in-memory
@@ -584,9 +584,9 @@ class CChainState
584584
/**
585585
* The blockhash which is the base of the snapshot this chainstate was created from.
586586
*
587-
* IsNull() if this chainstate was not created from a snapshot.
587+
* std::nullopt if this chainstate was not created from a snapshot.
588588
*/
589-
const uint256 m_from_snapshot_blockhash{};
589+
const std::optional<uint256> m_from_snapshot_blockhash;
590590

591591
/**
592592
* The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
@@ -866,7 +866,7 @@ class ChainstateManager
866866
// constructor
867867
//! @param[in] snapshot_blockhash If given, signify that this chainstate
868868
//! is based on a snapshot.
869-
CChainState& InitializeChainstate(CTxMemPool& mempool, const uint256& snapshot_blockhash = uint256())
869+
CChainState& InitializeChainstate(CTxMemPool& mempool, const std::optional<uint256>& snapshot_blockhash = std::nullopt)
870870
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
871871

872872
//! Get all chainstates currently being used.

0 commit comments

Comments
 (0)