5#include <chainparams.h>
30#include <boost/test/unit_test.hpp>
75 1 << 23,
true,
false);
78 c2.InitCoinsCache(1 << 23);
80 for (
const auto&
cs :
manager.m_chainstates) {
81 cs->ClearBlockIndexCandidates();
84 for (
const auto&
cs :
manager.m_chainstates) {
85 cs->PopulateBlockIndexCandidates();
136 c1.InitCoinsCache(1 << 23);
137 manager.MaybeRebalanceCaches();
149 1 << 23,
true,
false);
162 c2.InitCoinsCache(1 << 23);
163 manager.MaybeRebalanceCaches();
178 chainman.ResetChainstates();
183 chainman.m_cached_is_ibd.store(
cached_is_ibd, std::memory_order_relaxed);
186 tip.nChainWork = chainman.MinimumChainWork() - (
enough_work ? 0 : 1);
188 chainman.ActiveChain().SetTip(
tip);
190 assert(!chainman.ActiveChain().Tip());
192 chainman.UpdateIBDStatus();
220 .coins_db_in_memory =
false,
221 .block_tree_db_in_memory =
false,
343 for (
const auto&
chainstate : chainman.m_chainstates) {
376 for (
const auto&
chainstate : chainman.m_chainstates) {
417 for (
const auto&
cs : chainman.m_chainstates) {
418 if (
cs->CanFlushToDisk())
cs->ForceFlushStateToDisk();
425 chainman.ResetChainstates();
436 .blocks_dir = m_args.GetBlocksDirPath(),
440 .cache_bytes = m_kernel_cache_sizes.block_tree_db,
441 .memory_only = m_block_tree_db_in_memory,
456 this->SetupSnapshot();
496 for (
const auto&
cs : chainman.m_chainstates) {
497 cs->ClearBlockIndexCandidates();
501 for (
const auto&
cs : chainman.m_chainstates) {
502 cs->PopulateBlockIndexCandidates();
512 for (
int i = 0; i <=
cs1.m_chain.Height(); ++i) {
514 auto index =
cs1.m_chain[i];
520 index->m_chain_tx_count = 0;
613 auto* parent{
child->pprev};
635 this->SetupSnapshot();
668 this->LoadVerifyActivateChainstate();
689 "Ensure we can mine blocks on top of the initialized snapshot chainstate");
706 this->SetupSnapshot();
756 this->LoadVerifyActivateChainstate();
776 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
800 badcoin.out.scriptPubKey.assign(m_rng.randbits(6), 0);
817 BOOST_CHECK(!chainman.m_chainstates[0]->SnapshotBase());
819 BOOST_CHECK(chainman.m_chainstates[1]->SnapshotBase());
834 this->LoadVerifyActivateChainstate();
847 "Ensure we can mine blocks on top of the \"new\" IBD chainstate");
856template <
typename Options>
858 const std::vector<const char*>&
args)
873 auto get_opts = [&](
const std::vector<const char*>&
args) {
878 .notifications = notifications};
895 std::string
assume_valid{
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
899 BOOST_CHECK(!
get_opts({
"-assumevalid=01234567890123456789012345678901234567890123456789012345678901234"}));
907 std::string
minimum_chainwork{
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"};
911 BOOST_CHECK(!
get_opts({
"-minimumchainwork=01234567890123456789012345678901234567890123456789012345678901234"}));
arith_uint256 UintToArith256(const uint256 &a)
@ BLOCK_VALID_TREE
All parent headers found, difficulty matches, timestamp >= median previous.
@ BLOCK_FAILED_CHILD
Unused flag that was previously set when descending from failed block.
@ BLOCK_FAILED_VALID
stage after last reached validness failed
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Non-refcounted RAII wrapper for FILE*.
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
void SetTip(CBlockIndex &block)
Set/initialize a chain with a given tip.
std::optional< AssumeutxoData > AssumeutxoForHeight(int height) const
CCoinsView that adds a memory cache for transactions to another CCoinsView.
unsigned int GetCacheSize() const
Size of the cache (in number of transaction outputs)
An outpoint - a combination of a transaction hash and an index n into its vout.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
size_t m_coinstip_cache_size_bytes
The cache size of the in-memory coins view.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
kernel::ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe)
Return the current role of the chainstate.
const std::optional< uint256 > m_from_snapshot_blockhash
The blockhash which is the base of the snapshot this chainstate was created from.
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Chainstate * HistoricalChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Return historical chainstate targeting a specific block, if any.
SnapshotCompletionResult MaybeValidateSnapshot(Chainstate &validated_cs, Chainstate &unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(Chainstate CurrentChainstate)() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Try to validate an assumeutxo snapshot by using a validated historical chainstate targeted at the sna...
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
CTxOut out
unspent transaction output
DisconnectedBlockTransactions.
256-bit unsigned big integer.
A base class defining functions for notifying about certain kernel events.
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
kernel::BlockManagerOpts Options
static transaction_identifier FromUint256(const uint256 &id)
static const uint256 ZERO
static std::optional< uint256 > FromHex(std::string_view str)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES
Maximum bytes for transactions to store for processing during reorg.
static bool exists(const path &p)
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
std::optional< fs::path > FindAssumeutxoChainstateDir(const fs::path &data_dir)
Return a path to the snapshot-based chainstate dir, if one exists.
std::optional< uint256 > ReadSnapshotBaseBlockhash(fs::path chaindir)
bilingual_str ErrorString(const Result< T > &result)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::shared_ptr< const CTransaction > CTransactionRef
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
std::tuple< Chainstate *, Chainstate * > SetupSnapshot()
ChainstateManager & SimulateNodeRestart()
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Testing setup that configures a complete environment.
const CChainParams & chainparams
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
const CChainParams & chainparams
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
std::unique_ptr< CTxMemPool > mempool
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< node::Warnings > warnings
Manages all the node warnings.
std::function< bool()> shutdown_request
Function to request a shutdown.
std::unique_ptr< KernelNotifications > notifications
Issues blocking calls about sync status, errors and warnings.
util::SignalInterrupt * shutdown_signal
Interrupt object used to track whether node shutdown was requested.
std::atomic< int > exit_status
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
static bool CreateAndActivateUTXOSnapshot(TestingSetup *fixture, F malleation=NoMalleation, bool reset_chainstate=false, bool in_memory_chainstate=false)
Create and activate a UTXO snapshot, optionally providing a function to malleate the snapshot.
#define ASSERT_DEBUG_LOG(message)
consteval auto _(util::TranslatedLiteral str)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
@ VALIDATED
Every block in the chain has been validated.
@ UNVALIDATED
Blocks after an assumeutxo snapshot have been validated but the snapshot itself has not been validate...
@ INVALID
The assumeutxo snapshot failed validation.
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
Basic tests for ChainstateManager.
util::Result< Options > SetOptsFromArgs(ArgsManager &args_man, Options opts, const std::vector< const char * > &args)
Helper function to parse args into args_man and return the result of applying them to opts.
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.