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",
52 blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
59 FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
66 auto& chainman{*
Assert(m_node.chainman)};
67 auto& blockman{chainman.m_blockman};
75 LOCK(chainman.GetMutex());
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());
101 BOOST_CHECK(!blockman.OpenBlockFile(new_pos,
true).IsNull());
108 auto& chainman = m_node.chainman;
109 auto& blockman = chainman->m_blockman;
113 const auto& func_prune_blocks = [&](
CBlockIndex* last_pruned_block)
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}));
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));
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}));
160 auto& chainman{m_node.chainman};
161 auto& blockman{chainman->m_blockman};
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()};
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);
188 auto& chainman{m_node.chainman};
189 auto& blockman{chainman->m_blockman};
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());
224 const auto tip{m_node.chainman->ActiveTip()};
225 index.nStatus = tip->nStatus;
226 index.nDataPos = tip->nDataPos;
232 BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(block, index));
237 KernelNotifications notifications{
Assert(m_node.shutdown_request), m_node.exit_status, *
Assert(m_node.warnings)};
240 .blocks_dir = m_args.GetBlocksDirPath(),
241 .notifications = notifications,
243 .path = m_args.GetDataDirNet() /
"blocks" /
"index",
258 constexpr int TEST_BLOCK_SIZE{81};
294 blockman.UpdateBlockInfo(block3, 3, pos2);
308 auto& chainman{*
Assert(m_node.chainman)};
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_FIXTURE_TEST_CASE(util_CheckValue, CheckValueTest)
constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes).
constexpr 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...
bool ReadBlock(CBlock &block, const FlatFilePos &pos, const std::optional< uint256 > &expected_hash) const
Functions for disk access for blocks.
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)
constexpr uint32_t STORAGE_HEADER_BYTES
Size of header written by WriteBlock before a serialized CBlock (8 bytes).
constexpr unsigned int MAX_BLOCKFILE_SIZE
The maximum size of a blk?
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
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())