LCOV - code coverage report
Current view: top level - src/test - blockmanager_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.0 % 99 98
Test Date: 2024-11-04 04:45:35 Functions: 100.0 % 16 16
Branches: 50.2 % 470 236

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

Generated by: LCOV version 2.0-1