5 #include <chainparams.h>
13 #include <validation.h>
15 #include <boost/test/unit_test.hpp>
31 const BlockManager::Options blockman_opts{
32 .chainparams = *params,
33 .blocks_dir = m_args.GetBlocksDirPath(),
34 .notifications = notifications,
51 FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1,
nullptr)};
59 auto& blockman = chainman->m_blockman;
60 const CBlockIndex* old_tip{
WITH_LOCK(chainman->GetMutex(),
return chainman->ActiveChain().Tip())};
67 LOCK(chainman->GetMutex());
68 file_number = old_tip->GetBlockPos().nFile;
69 blockman.PruneOneBlockFile(file_number);
76 WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
81 blockman.m_have_pruned =
true;
82 WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
86 WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
89 const CBlockIndex* new_tip{
WITH_LOCK(chainman->GetMutex(),
return chainman->ActiveChain().Tip())};
90 BOOST_CHECK_NE(old_tip, new_tip);
91 const int new_file_number{
WITH_LOCK(chainman->GetMutex(),
return new_tip->GetBlockPos().nFile)};
101 auto& blockman = chainman->m_blockman;
105 const auto& func_prune_blocks = [&](
CBlockIndex* last_pruned_block)
116 BOOST_CHECK_EQUAL(blockman.GetFirstStoredBlock(tip), chainman->ActiveChain()[0]);
117 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
121 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
124 int height_to_prune = tip.
nHeight / 2;
125 CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
127 func_prune_blocks(last_pruned_block);
131 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
132 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
140 .blocks_dir = m_args.GetBlocksDirPath(),
141 .notifications = notifications,
154 constexpr
int TEST_BLOCK_SIZE{81};
161 FlatFilePos pos1{blockman.SaveBlockToDisk(block1, 1,
nullptr)};
164 FlatFilePos pos2{blockman.SaveBlockToDisk(block2, 2,
nullptr)};
175 BOOST_CHECK(!blockman.ReadBlockFromDisk(read_block, pos1));
180 BOOST_CHECK(!blockman.ReadBlockFromDisk(read_block, pos2));
192 BOOST_CHECK(blockman.SaveBlockToDisk(block3, 3, &pos2) == pos2);
200 blockman.ReadBlockFromDisk(read_block, pos2);
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
@ BLOCK_HAVE_DATA
full block available in blk*.dat
const CChainParams & Params()
Return the currently selected parameters.
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
#define Assert(val)
Identity function.
Non-refcounted RAII wrapper for FILE*.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
unsigned int nBlocks
number of blocks stored in file
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int nHeight
height of the entry in the chain. The genesis block has height 0
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE
Size of header written by WriteBlockToDisk before a serialized CBlock.
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?????.dat file (since 0.8)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
size_t GetSerializeSize(const T &t, int nVersion=0)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const CChainParams & chainparams
std::unique_ptr< kernel::Context > kernel
libbitcoin_kernel context
std::unique_ptr< ChainstateManager > chainman
std::atomic< int > exit_status
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define ASSERT_DEBUG_LOG(message)