/ src / test / blockmanager_tests.cpp
blockmanager_tests.cpp
  1  // Copyright (c) 2022 The Bitcoin Core developers
  2  // Distributed under the MIT software license, see the accompanying
  3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4  
  5  #include <chain.h>
  6  #include <chainparams.h>
  7  #include <clientversion.h>
  8  #include <node/blockstorage.h>
  9  #include <node/context.h>
 10  #include <node/kernel_notifications.h>
 11  #include <script/solver.h>
 12  #include <primitives/block.h>
 13  #include <util/chaintype.h>
 14  #include <validation.h>
 15  
 16  #include <boost/test/unit_test.hpp>
 17  #include <test/util/logging.h>
 18  #include <test/util/setup_common.h>
 19  
 20  using node::STORAGE_HEADER_BYTES;
 21  using node::BlockManager;
 22  using node::KernelNotifications;
 23  using node::MAX_BLOCKFILE_SIZE;
 24  
 25  // use BasicTestingSetup here for the data directory configuration, setup, and cleanup
 26  BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
 27  
 28  BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
 29  {
 30      const auto params {CreateChainParams(ArgsManager{}, ChainType::MAIN)};
 31      KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
 32      const BlockManager::Options blockman_opts{
 33          .chainparams = *params,
 34          .blocks_dir = m_args.GetBlocksDirPath(),
 35          .notifications = notifications,
 36          .block_tree_db_params = DBParams{
 37              .path = m_args.GetDataDirNet() / "blocks" / "index",
 38              .cache_bytes = 0,
 39          },
 40      };
 41      BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
 42      // simulate adding a genesis block normally
 43      BOOST_CHECK_EQUAL(blockman.WriteBlock(params->GenesisBlock(), 0).nPos, STORAGE_HEADER_BYTES);
 44      // simulate what happens during reindex
 45      // simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
 46      // the block is found at offset 8 because there is an 8 byte serialization header
 47      // consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
 48      const FlatFilePos pos{0, STORAGE_HEADER_BYTES};
 49      blockman.UpdateBlockInfo(params->GenesisBlock(), 0, pos);
 50      // now simulate what happens after reindex for the first new block processed
 51      // the actual block contents don't matter, just that it's a block.
 52      // verify that the write position is at offset 0x12d.
 53      // this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
 54      // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
 55      // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
 56      FlatFilePos actual{blockman.WriteBlock(params->GenesisBlock(), 1)};
 57      BOOST_CHECK_EQUAL(actual.nPos, STORAGE_HEADER_BYTES + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + STORAGE_HEADER_BYTES);
 58  }
 59  
 60  BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)
 61  {
 62      // Cap last block file size, and mine new block in a new block file.
 63      const auto& chainman = Assert(m_node.chainman);
 64      auto& blockman = chainman->m_blockman;
 65      const CBlockIndex* old_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
 66      WITH_LOCK(chainman->GetMutex(), blockman.GetBlockFileInfo(old_tip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE);
 67      CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
 68  
 69      // Prune the older block file, but don't unlink it
 70      int file_number;
 71      {
 72          LOCK(chainman->GetMutex());
 73          file_number = old_tip->GetBlockPos().nFile;
 74          blockman.PruneOneBlockFile(file_number);
 75      }
 76  
 77      const FlatFilePos pos(file_number, 0);
 78  
 79      // Check that the file is not unlinked after ScanAndUnlinkAlreadyPrunedFiles
 80      // if m_have_pruned is not yet set
 81      WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
 82      BOOST_CHECK(!blockman.OpenBlockFile(pos, true).IsNull());
 83  
 84      // Check that the file is unlinked after ScanAndUnlinkAlreadyPrunedFiles
 85      // once m_have_pruned is set
 86      blockman.m_have_pruned = true;
 87      WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
 88      BOOST_CHECK(blockman.OpenBlockFile(pos, true).IsNull());
 89  
 90      // Check that calling with already pruned files doesn't cause an error
 91      WITH_LOCK(chainman->GetMutex(), blockman.ScanAndUnlinkAlreadyPrunedFiles());
 92  
 93      // Check that the new tip file has not been removed
 94      const CBlockIndex* new_tip{WITH_LOCK(chainman->GetMutex(), return chainman->ActiveChain().Tip())};
 95      BOOST_CHECK_NE(old_tip, new_tip);
 96      const int new_file_number{WITH_LOCK(chainman->GetMutex(), return new_tip->GetBlockPos().nFile)};
 97      const FlatFilePos new_pos(new_file_number, 0);
 98      BOOST_CHECK(!blockman.OpenBlockFile(new_pos, true).IsNull());
 99  }
100  
101  BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
102  {
103      // The goal of the function is to return the first not pruned block in the range [upper_block, lower_block].
104      LOCK(::cs_main);
105      auto& chainman = m_node.chainman;
106      auto& blockman = chainman->m_blockman;
107      const CBlockIndex& tip = *chainman->ActiveTip();
108  
109      // Function to prune all blocks from 'last_pruned_block' down to the genesis block
110      const auto& func_prune_blocks = [&](CBlockIndex* last_pruned_block)
111      {
112          LOCK(::cs_main);
113          CBlockIndex* it = last_pruned_block;
114          while (it != nullptr && it->nStatus & BLOCK_HAVE_DATA) {
115              it->nStatus &= ~BLOCK_HAVE_DATA;
116              it = it->pprev;
117          }
118      };
119  
120      // 1) Return genesis block when all blocks are available
121      BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), chainman->ActiveChain()[0]);
122      BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *chainman->ActiveChain()[0]));
123  
124      // 2) Check lower_block when all blocks are available
125      CBlockIndex* lower_block = chainman->ActiveChain()[tip.nHeight / 2];
126      BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *lower_block));
127  
128      // Prune half of the blocks
129      int height_to_prune = tip.nHeight / 2;
130      CBlockIndex* first_available_block = chainman->ActiveChain()[height_to_prune + 1];
131      CBlockIndex* last_pruned_block = first_available_block->pprev;
132      func_prune_blocks(last_pruned_block);
133  
134      // 3) The last block not pruned is in-between upper-block and the genesis block
135      BOOST_CHECK_EQUAL(blockman.GetFirstBlock(tip, BLOCK_HAVE_DATA), first_available_block);
136      BOOST_CHECK(blockman.CheckBlockDataAvailability(tip, *first_available_block));
137      BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
138  }
139  
140  BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
141  {
142      KernelNotifications notifications{Assert(m_node.shutdown_request), m_node.exit_status, *Assert(m_node.warnings)};
143      node::BlockManager::Options blockman_opts{
144          .chainparams = Params(),
145          .blocks_dir = m_args.GetBlocksDirPath(),
146          .notifications = notifications,
147          .block_tree_db_params = DBParams{
148              .path = m_args.GetDataDirNet() / "blocks" / "index",
149              .cache_bytes = 0,
150          },
151      };
152      BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
153  
154      // Test blocks with no transactions, not even a coinbase
155      CBlock block1;
156      block1.nVersion = 1;
157      CBlock block2;
158      block2.nVersion = 2;
159      CBlock block3;
160      block3.nVersion = 3;
161  
162      // They are 80 bytes header + 1 byte 0x00 for vtx length
163      constexpr int TEST_BLOCK_SIZE{81};
164  
165      // Blockstore is empty
166      BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0);
167  
168      // Write the first block to a new location.
169      FlatFilePos pos1{blockman.WriteBlock(block1, /*nHeight=*/1)};
170  
171      // Write second block
172      FlatFilePos pos2{blockman.WriteBlock(block2, /*nHeight=*/2)};
173  
174      // Two blocks in the file
175      BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
176  
177      // First two blocks are written as expected
178      // Errors are expected because block data is junk, thrown AFTER successful read
179      CBlock read_block;
180      BOOST_CHECK_EQUAL(read_block.nVersion, 0);
181      {
182          ASSERT_DEBUG_LOG("Errors in block header");
183          BOOST_CHECK(!blockman.ReadBlock(read_block, pos1));
184          BOOST_CHECK_EQUAL(read_block.nVersion, 1);
185      }
186      {
187          ASSERT_DEBUG_LOG("Errors in block header");
188          BOOST_CHECK(!blockman.ReadBlock(read_block, pos2));
189          BOOST_CHECK_EQUAL(read_block.nVersion, 2);
190      }
191  
192      // During reindex, the flat file block storage will not be written to.
193      // UpdateBlockInfo will, however, update the blockfile metadata.
194      // Verify this behavior by attempting (and failing) to write block 3 data
195      // to block 2 location.
196      CBlockFileInfo* block_data = blockman.GetBlockFileInfo(0);
197      BOOST_CHECK_EQUAL(block_data->nBlocks, 2);
198      blockman.UpdateBlockInfo(block3, /*nHeight=*/3, /*pos=*/pos2);
199      // Metadata is updated...
200      BOOST_CHECK_EQUAL(block_data->nBlocks, 3);
201      // ...but there are still only two blocks in the file
202      BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + STORAGE_HEADER_BYTES) * 2);
203  
204      // Block 2 was not overwritten:
205      blockman.ReadBlock(read_block, pos2);
206      BOOST_CHECK_EQUAL(read_block.nVersion, 2);
207  }
208  
209  BOOST_AUTO_TEST_SUITE_END()