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",
52 blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
59 FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
67 auto& blockman{chainman.m_blockman};
75 LOCK(chainman.GetMutex());
76 file_number = old_tip->GetBlockPos().nFile;
77 blockman.PruneOneBlockFile(file_number);
84 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
85 BOOST_CHECK(!blockman.OpenBlockFile(pos,
true).IsNull());
89 blockman.m_have_pruned =
true;
90 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
91 BOOST_CHECK(blockman.OpenBlockFile(pos,
true).IsNull());
94 WITH_LOCK(chainman.GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
98 BOOST_CHECK_NE(old_tip, new_tip);
99 const int new_file_number{
WITH_LOCK(chainman.GetMutex(),
return new_tip->GetBlockPos().nFile)};
101 BOOST_CHECK(!blockman.OpenBlockFile(new_pos,
true).IsNull());
109 auto& blockman = chainman->m_blockman;
113 const auto& func_prune_blocks = [&](
CBlockIndex* last_pruned_block)
118 it->nStatus &= ~BLOCK_HAVE_DATA;
125 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
129 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
132 CBlockIndex* upper_block = chainman->ActiveChain()[2];
134 BOOST_CHECK(blockman.CheckBlockDataAvailability(*upper_block, *genesis,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
136 chainman->ActiveChain()[1]->nStatus &= ~BLOCK_HAVE_UNDO;
137 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *genesis,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
140 int height_to_prune = tip.
nHeight / 2;
141 CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
143 func_prune_blocks(last_pruned_block);
147 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
148 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
152 first_available_block->nStatus &= ~BLOCK_HAVE_UNDO;
153 BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *first_available_block,
BlockStatus{BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO}));
154 BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block,
BlockStatus{BLOCK_HAVE_DATA}));
161 auto& blockman{chainman->m_blockman};
163 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
165 auto block{blockman.ReadRawBlock(tip_block_pos)};
166 BOOST_REQUIRE(block);
167 BOOST_REQUIRE_GE(block->size(), 200);
169 const auto expect_part{[&](
size_t offset,
size_t size) {
170 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
172 const auto& part{res.value()};
173 BOOST_CHECK_EQUAL_COLLECTIONS(part.begin(), part.end(), block->begin() + offset, block->begin() + offset + size);
177 expect_part(0, block->size() - 1);
178 expect_part(0, block->size() - 10);
179 expect_part(0, block->size());
180 expect_part(1, block->size() - 1);
182 expect_part(block->size() - 1, 1);
189 auto& blockman{chainman->m_blockman};
191 const FlatFilePos tip_block_pos{tip.GetBlockPos()};
193 auto block{blockman.ReadRawBlock(tip_block_pos)};
194 BOOST_REQUIRE(block);
195 BOOST_REQUIRE_GE(block->size(), 200);
197 const auto expect_part_error{[&](
size_t offset,
size_t size) {
198 auto res{blockman.ReadRawBlock(tip_block_pos, std::pair{offset, size})};
203 expect_part_error(0, 0);
204 expect_part_error(0, block->size() + 1);
205 expect_part_error(0, std::numeric_limits<size_t>::max());
206 expect_part_error(1, block->size());
207 expect_part_error(2, block->size() - 1);
208 expect_part_error(block->size() - 1, 2);
209 expect_part_error(block->size() - 2, 3);
210 expect_part_error(block->size() + 1, 0);
211 expect_part_error(block->size() + 1, 1);
212 expect_part_error(block->size() + 2, 2);
213 expect_part_error(block->size(), 0);
214 expect_part_error(block->size(), 1);
215 expect_part_error(std::numeric_limits<size_t>::max(), 1);
216 expect_part_error(std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max());
225 index.nStatus = tip->nStatus;
226 index.nDataPos = tip->nDataPos;
240 .blocks_dir = m_args.GetBlocksDirPath(),
241 .notifications = notifications,
243 .
path = m_args.GetDataDirNet() /
"blocks" /
"index",
258 constexpr int TEST_BLOCK_SIZE{81};
279 BOOST_CHECK(!blockman.ReadBlock(read_block, pos1, {}));
284 BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
294 blockman.UpdateBlockInfo(block3, 3, pos2);
301 BOOST_CHECK(!blockman.ReadBlock(read_block, pos2, {}));
309 auto& blockman{chainman.m_blockman};
318 BOOST_CHECK(blockman.DeletePruneLock(
"test_lock"));
321 BOOST_CHECK(!blockman.DeletePruneLock(
"test_lock"));
324 BOOST_CHECK(!blockman.DeletePruneLock(
"nonexistent"));
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()
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
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(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
int height_first
Height of earliest block that should be kept and not pruned.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
#define ASSERT_DEBUG_LOG(message)