6#include <chainparams.h>
14#include <validation.h>
16#include <boost/test/unit_test.hpp>
34 const BlockManager::Options blockman_opts{
35 .chainparams = *params,
36 .blocks_dir = m_args.GetBlocksDirPath(),
37 .notifications = notifications,
39 .
path = m_args.GetDataDirNet() /
"blocks" /
"index",
51 blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
58 FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
66 auto& blockman{chainman.m_blockman};
74 LOCK(chainman.GetMutex());
75 file_number = old_tip->GetBlockPos().nFile;
76 blockman.PruneOneBlockFile(file_number);
83 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
84 BOOST_CHECK(!blockman.OpenBlockFile(pos,
true).IsNull());
88 blockman.m_have_pruned =
true;
89 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
90 BOOST_CHECK(blockman.OpenBlockFile(pos,
true).IsNull());
93 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
97 BOOST_CHECK_NE(old_tip, new_tip);
98 const int new_file_number{
WITH_LOCK(chainman.GetMutex(),
return new_tip->GetBlockPos().nFile)};
100 BOOST_CHECK(!blockman.OpenBlockFile(new_pos,
true).IsNull());
108 auto& blockman = chainman->m_blockman;
112 const auto& func_prune_blocks = [&](
CBlockIndex* last_pruned_block)
117 it->nStatus &= ~BLOCK_HAVE_DATA;
124 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
128 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
131 CBlockIndex* upper_block = chainman->ActiveChain()[2];
133 BOOST_CHECK(blockman.CheckBlockDataAvailability(*upper_block, *genesis,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
135 chainman->ActiveChain()[1]->nStatus &= ~BLOCK_HAVE_UNDO;
136 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *genesis,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
139 int height_to_prune = tip.
nHeight / 2;
140 CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
142 func_prune_blocks(last_pruned_block);
146 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
147 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
151 first_available_block->nStatus &= ~BLOCK_HAVE_UNDO;
152 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *first_available_block,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
153 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block,
BlockStatus{BLOCK_HAVE_DATA}));
160 auto& blockman{chainman->m_blockman};
162 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
164 auto block{blockman.ReadRawBlock(tip_block_pos)};
165 BOOST_REQUIRE(block);
166 BOOST_REQUIRE_GE(block->size(), 200);
168 const auto expect_part{[&](
size_t offset,
size_t size) {
169 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
171 const auto& part{res.value()};
172 BOOST_CHECK_EQUAL_COLLECTIONS(part.begin(), part.end(), block->begin() + offset, block->begin() + offset + size);
176 expect_part(0, block->size() - 1);
177 expect_part(0, block->size() - 10);
178 expect_part(0, block->size());
179 expect_part(1, block->size() - 1);
181 expect_part(block->size() - 1, 1);
188 auto& blockman{chainman->m_blockman};
190 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
192 auto block{blockman.ReadRawBlock(tip_block_pos)};
193 BOOST_REQUIRE(block);
194 BOOST_REQUIRE_GE(block->size(), 200);
196 const auto expect_part_error{[&](
size_t offset,
size_t size) {
197 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
202 expect_part_error(0, 0);
203 expect_part_error(0, block->size() + 1);
204 expect_part_error(0, std::numeric_limits<size_t>::max());
205 expect_part_error(1, block->size());
206 expect_part_error(2, block->size() - 1);
207 expect_part_error(block->size() - 1, 2);
208 expect_part_error(block->size() - 2, 3);
209 expect_part_error(block->size() + 1, 0);
210 expect_part_error(block->size() + 1, 1);
211 expect_part_error(block->size() + 2, 2);
212 expect_part_error(block->size(), 0);
213 expect_part_error(block->size(), 1);
214 expect_part_error(std::numeric_limits<size_t>::max(), 1);
215 expect_part_error(std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max());
224 index.nStatus = tip->nStatus;
225 index.nDataPos = tip->nDataPos;
239 .blocks_dir = m_args.GetBlocksDirPath(),
240 .notifications = notifications,
242 .
path = m_args.GetDataDirNet() /
"blocks" /
"index",
257 constexpr int TEST_BLOCK_SIZE{81};
277 BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));
282 BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
292 blockman.UpdateBlockInfo(block3, 3, pos2);
299 BOOST_CHECK(!blockman.ReadBlock(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
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
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
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
uint32_t nBlocks
number of blocks stored in file
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
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 constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes)
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)
static constexpr TransactionSerParams TX_WITH_WITNESS
uint64_t GetSerializeSize(const T &t)
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Testing setup that configures a complete environment.
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
const CChainParams & chainparams
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.
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.
#define ASSERT_DEBUG_LOG(message)