Skip to content

Commit f6e2da5

Browse files
committed
simplify ChainstateManager::SnapshotBlockhash() return semantics
Don't return null snapshotblockhash values to avoid caller complexity/confusion.
1 parent 7a6c46b commit f6e2da5

File tree

7 files changed

+348
-3
lines changed

7 files changed

+348
-3
lines changed

src/chain.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,27 @@ class CBlockIndex
163163

164164
//! Number of transactions in this block.
165165
//! Note: in a potential headers-first mode, this number cannot be relied upon
166+
//! Note: this value is faked during UTXO snapshot load to ensure that
167+
//! LoadBlockIndex() will load index entries for blocks that we lack data for.
168+
//! @sa ActivateSnapshot
166169
unsigned int nTx{0};
167170

168171
//! (memory only) Number of transactions in the chain up to and including this block.
169172
//! This value will be non-zero only if and only if transactions for this block and all its parents are available.
170173
//! Change to 64-bit type when necessary; won't happen before 2030
174+
//!
175+
//! Note: this value is faked during use of a UTXO snapshot because we don't
176+
//! have the underlying block data available during snapshot load.
177+
//! @sa AssumeutxoData
178+
//! @sa ActivateSnapshot
171179
unsigned int nChainTx{0};
172180

173181
//! Verification status of this block. See enum BlockStatus
182+
//!
183+
//! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot
184+
//! load to avoid the block index being spuriously rewound.
185+
//! @sa RewindBlockIndex
186+
//! @sa ActivateSnapshot
174187
uint32_t nStatus{0};
175188

176189
//! block header

src/coins.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
9797
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
9898
}
9999

100+
void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
101+
cachedCoinsUsage += coin.DynamicMemoryUsage();
102+
cacheCoins.emplace(
103+
std::piecewise_construct,
104+
std::forward_as_tuple(std::move(outpoint)),
105+
std::forward_as_tuple(std::move(coin), CCoinsCacheEntry::DIRTY));
106+
}
107+
100108
void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
101109
bool fCoinbase = tx.IsCoinBase();
102110
const uint256& txid = tx.GetHash();

src/coins.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <functional>
2121
#include <unordered_map>
2222

23+
class ChainstateManager;
24+
2325
/**
2426
* A UTXO entry.
2527
*
@@ -125,6 +127,7 @@ struct CCoinsCacheEntry
125127

126128
CCoinsCacheEntry() : flags(0) {}
127129
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
130+
CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::move(coin_)), flags(flag) {}
128131
};
129132

130133
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
@@ -262,6 +265,15 @@ class CCoinsViewCache : public CCoinsViewBacked
262265
*/
263266
void AddCoin(const COutPoint& outpoint, Coin&& coin, bool possible_overwrite);
264267

268+
/**
269+
* Emplace a coin into cacheCoins without performing any checks, marking
270+
* the emplaced coin as dirty.
271+
*
272+
* NOT FOR GENERAL USE. Used only when loading coins from a UTXO snapshot.
273+
* @sa ChainstateManager::PopulateAndValidateSnapshot()
274+
*/
275+
void EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin);
276+
265277
/**
266278
* Spend a coin. Pass moveto in order to get the deleted data.
267279
* If no unspent output exists for the passed outpoint, this call

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
2828
std::vector<CChainState*> chainstates;
2929
const CChainParams& chainparams = Params();
3030

31+
BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
32+
3133
// Create a legacy (IBD) chainstate.
3234
//
3335
CChainState& c1 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(mempool));
@@ -54,10 +56,17 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
5456
auto& validated_cs = manager.ValidatedChainstate();
5557
BOOST_CHECK_EQUAL(&validated_cs, &c1);
5658

59+
BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
60+
5761
// Create a snapshot-based chainstate.
5862
//
59-
CChainState& c2 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(mempool, GetRandHash()));
63+
const uint256 snapshot_blockhash = GetRandHash();
64+
CChainState& c2 = WITH_LOCK(::cs_main, return manager.InitializeChainstate(
65+
mempool, snapshot_blockhash));
6066
chainstates.push_back(&c2);
67+
68+
BOOST_CHECK_EQUAL(manager.SnapshotBlockhash().value(), snapshot_blockhash);
69+
6170
c2.InitCoinsDB(
6271
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
6372
WITH_LOCK(::cs_main, c2.InitCoinsCache(1 << 23));

0 commit comments

Comments
 (0)