6#include <chainparams.h>
14#include <validation.h>
16#include <boost/test/unit_test.hpp>
33 KernelNotifications notifications{
Assert(m_node.shutdown_request), m_node.exit_status, *
Assert(m_node.warnings)};
35 .chainparams = *params,
36 .blocks_dir = m_args.GetBlocksDirPath(),
37 .notifications = notifications,
39 .path = m_args.GetDataDirNet() /
"blocks" /
"index",
65 auto& chainman{*
Assert(m_node.chainman)};
66 auto& blockman{chainman.m_blockman};
74 LOCK(chainman.GetMutex());
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());
100 BOOST_CHECK(!blockman.OpenBlockFile(new_pos,
true).IsNull());
107 auto& chainman = m_node.chainman;
108 auto& blockman = chainman->m_blockman;
112 const auto& func_prune_blocks = [&](
CBlockIndex* last_pruned_block)
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}));
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));
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}));
159 auto& chainman{m_node.chainman};
160 auto& blockman{chainman->m_blockman};
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()};
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);
187 auto& chainman{m_node.chainman};
188 auto& blockman{chainman->m_blockman};
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());
223 const auto tip{m_node.chainman->ActiveTip()};
224 index.nStatus = tip->nStatus;
225 index.nDataPos = tip->nDataPos;
231 BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(block, index));
236 KernelNotifications notifications{
Assert(m_node.shutdown_request), m_node.exit_status, *
Assert(m_node.warnings)};
239 .blocks_dir = m_args.GetBlocksDirPath(),
240 .notifications = notifications,
242 .path = m_args.GetDataDirNet() /
"blocks" /
"index",
257 constexpr int TEST_BLOCK_SIZE{81};
static constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes).
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
static const unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?
@ BLOCK_HAVE_UNDO
undo data available in rev*.dat
@ 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.
kernel::BlockManagerOpts Options
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
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(
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...
void UpdateBlockInfo(const CBlock &block, unsigned int nHeight, const FlatFilePos &pos)
Update blockfile info while processing a block during reindex.
uint64_t CalculateCurrentUsage()
Calculate the amount of disk space the block & undo files currently use.
bool WriteBlockUndo(const CBlockUndo &blockundo, BlockValidationState &state, CBlockIndex &block) EXCLUSIVE_LOCKS_REQUIRED(FlatFilePo WriteBlock)(const CBlock &block, int nHeight)
Store block on disk and update block file statistics.
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
kernel::BlockManagerOpts Options
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_AUTO_TEST_CASE(ipc_tests)
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?
#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.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Testing setup that configures a complete environment.
#define WITH_LOCK(cs, code)
#define ASSERT_DEBUG_LOG(message)
BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32))
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array+uint256::size())