21 #include <boost/test/unit_test.hpp> 35 std::vector<Chainstate*> chainstates;
42 chainstates.push_back(&c1);
44 1 << 23,
true,
false);
49 auto all = manager.
GetAll();
50 BOOST_CHECK_EQUAL_COLLECTIONS(all.begin(), all.end(), chainstates.begin(), chainstates.end());
58 auto exp_tip = c1.m_chain.Tip();
67 &mempool, snapshot_blockhash));
68 chainstates.push_back(&c2);
73 1 << 23,
true,
false);
84 auto all2 = manager.
GetAll();
85 BOOST_CHECK_EQUAL_COLLECTIONS(all2.begin(), all2.end(), chainstates.begin(), chainstates.end());
110 size_t max_cache = 10000;
114 std::vector<Chainstate*> chainstates;
119 chainstates.push_back(&c1);
121 1 << 23,
true,
false);
125 c1.InitCoinsCache(1 << 23);
128 manager.MaybeRebalanceCaches();
137 chainstates.push_back(&c2);
139 1 << 23,
true,
false);
143 c2.InitCoinsCache(1 << 23);
146 manager.MaybeRebalanceCaches();
163 size_t initial_total_coins{100};
170 size_t total_coins{0};
189 constexpr
int snapshot_height = 110;
192 initial_total_coins += 10;
203 auto_infile >> outpoint;
251 int chains_tested{0};
254 BOOST_TEST_MESSAGE(
"Checking coins in " << chainstate->ToString());
260 size_t total_coins{0};
277 constexpr
size_t new_coins{100};
278 mineBlocks(new_coins);
282 size_t coins_in_active{0};
283 size_t coins_in_background{0};
284 size_t coins_missing_from_background{0};
287 BOOST_TEST_MESSAGE(
"Checking coins in " << chainstate->ToString());
293 if (coinscache.HaveCoin(op)) {
294 (is_background ? coins_in_background : coins_in_active)++;
295 }
else if (is_background) {
296 coins_missing_from_background++;
312 loaded_snapshot_blockhash);
332 int num_assumed_valid{0};
333 const int expected_assumed_valid{20};
334 const int last_assumed_valid_idx{40};
335 const int assumed_valid_start_idx = last_assumed_valid_idx - expected_assumed_valid;
340 auto reload_all_block_indexes = [&]() {
343 cs->UnloadBlockIndex();
352 reload_all_block_indexes();
360 if (i < last_assumed_valid_idx && i >= assumed_valid_start_idx) {
365 if (index->IsAssumedValid()) ++num_assumed_valid;
368 if (i == (assumed_valid_start_idx - 1)) {
369 validated_tip = index;
377 return chainman.InitializeChainstate(&mempool,
GetRandHash()));
379 reload_all_block_indexes();
std::shared_ptr< const CTransaction > CTransactionRef
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from. ...
Testing setup that performs all steps up until right before ChainstateManager gets initialized...
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
const unsigned int nChainTx
Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
std::set< CBlockIndex *, node::CBlockIndexWorkComparator > setBlockIndexCandidates
The set of all CBlockIndex entries with either BLOCK_VALID_TRANSACTIONS (for itself and all ancestors...
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
uint256 GetRandHash() noexcept
size_t m_coinsdb_cache_size_bytes
The cache size of the on-disk coins view.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
int Height() const
Return the maximal height in the chain.
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, fs::path leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
CChain m_chain
The current chain of blockheaders we consult and build on.
Non-refcounted RAII wrapper for FILE*.
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
std::unique_ptr< CTxMemPool > mempool
BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
Test basic snapshot activation.
void SetBestBlock(const uint256 &hashBlock)
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
bool IsSnapshotActive() const
Chainstate stores and provides an API to update our local knowledge of the current best chain...
bilingual_str _(const char *psz)
Translation function.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex...
BOOST_AUTO_TEST_SUITE_END()
static const uint256 ZERO
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
An outpoint - a combination of a transaction hash and an index n into its vout.
Chainstate &InitializeChainstate(CTxMemPool *mempool, const std::optional< uint256 > &snapshot_blockhash=std::nullopt) LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate and assign it based upon whether it is from a snapshot.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
#define BOOST_CHECK_EQUAL(v1, v2)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
const AssumeutxoData * ExpectedAssumeutxo(const int height, const CChainParams &chainparams)
Return the expected assumeutxo value for a given height, if one exists.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
bool IsAssumedValid() const EXCLUSIVE_LOCKS_REQUIRED(
Holds configuration for use during UTXO snapshot load and validation.
BOOST_AUTO_TEST_CASE(chainstatemanager)
Basic tests for ChainstateManager.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
std::optional< uint256 > SnapshotBlockhash() const
Chainstate & ActiveChainstate() const
The most-work chain.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
static uint256 InsecureRand256()
If set, this indicates that the block index entry is assumed-valid.
std::unique_ptr< ChainstateManager > chainman
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
#define Assert(val)
Identity function.
static bool CreateAndActivateUTXOSnapshot(node::NodeContext &node, const fs::path root, F malleation=NoMalleation)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot...
#define BOOST_CHECK(expr)
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.