Skip to content

Commit e252909

Browse files
committed
test: add unit test for ScanAndUnlinkAlreadyPrunedFiles
1 parent 77557dd commit e252909

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/test/blockmanager_tests.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
using node::BlockManager;
1414
using node::BLOCK_SERIALIZATION_HEADER_SIZE;
15+
using node::MAX_BLOCKFILE_SIZE;
16+
using node::OpenBlockFile;
1517

1618
// use BasicTestingSetup here for the data directory configuration, setup, and cleanup
1719
BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
@@ -39,4 +41,45 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
3941
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE);
4042
}
4143

44+
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
45+
{
46+
// Cap last block file size, and mine new block in a new block file.
47+
const auto& chainman = Assert(m_node.chainman);
48+
auto& blockman = chainman->m_blockman;
49+
const CBlockIndex* old_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
50+
WITH_LOCK(chainman->GetMutex(), blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
51+
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
52+
53+
// Prune the older block file, but don't unlink it
54+
int file_number;
55+
{
56+
LOCK(chainman->GetMutex());
57+
file_number = old_tip->GetBlockPos().nFile;
58+
blockman.PruneOneBlockFile(file_number);
59+
}
60+
61+
const FlatFilePos pos(file_number, 0);
62+
63+
// Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
64+
// if m_have_pruned is not yet set
65+
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
66+
BOOST_CHECK(!AutoFile(OpenBlockFile(pos, true)).IsNull());
67+
68+
// Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
69+
// once m_have_pruned is set
70+
blockman.m_have_pruned = true;
71+
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
72+
BOOST_CHECK(AutoFile(OpenBlockFile(pos, true)).IsNull());
73+
74+
// Check that calling with already pruned files doesn't cause an error
75+
WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
76+
77+
// Check that the new tip file has not been removed
78+
const CBlockIndex* new_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
79+
BOOST_CHECK_NE(old_tip, new_tip);
80+
const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)};
81+
const FlatFilePos new_pos(new_file_number, 0);
82+
BOOST_CHECK(!AutoFile(OpenBlockFile(new_pos, true)).IsNull());
83+
}
84+
4285
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)