|
8 | 8 | #include <node/context.h>
|
9 | 9 | #include <node/kernel_notifications.h>
|
10 | 10 | #include <script/solver.h>
|
| 11 | +#include <primitives/block.h> |
11 | 12 | #include <util/chaintype.h>
|
12 | 13 | #include <validation.h>
|
13 | 14 |
|
14 | 15 | #include <boost/test/unit_test.hpp>
|
| 16 | +#include <test/util/logging.h> |
15 | 17 | #include <test/util/setup_common.h>
|
16 | 18 |
|
17 | 19 | using node::BLOCK_SERIALIZATION_HEADER_SIZE;
|
@@ -130,4 +132,73 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
|
130 | 132 | BOOST_CHECK(!blockman.CheckBlockDataAvailability(tip, *last_pruned_block));
|
131 | 133 | }
|
132 | 134 |
|
| 135 | +BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file) |
| 136 | +{ |
| 137 | + KernelNotifications notifications{m_node.exit_status}; |
| 138 | + node::BlockManager::Options blockman_opts{ |
| 139 | + .chainparams = Params(), |
| 140 | + .blocks_dir = m_args.GetBlocksDirPath(), |
| 141 | + .notifications = notifications, |
| 142 | + }; |
| 143 | + BlockManager blockman{m_node.kernel->interrupt, blockman_opts}; |
| 144 | + |
| 145 | + // Test blocks with no transactions, not even a coinbase |
| 146 | + CBlock block1; |
| 147 | + block1.nVersion = 1; |
| 148 | + CBlock block2; |
| 149 | + block2.nVersion = 2; |
| 150 | + CBlock block3; |
| 151 | + block3.nVersion = 3; |
| 152 | + |
| 153 | + // They are 80 bytes header + 1 byte 0x00 for vtx length |
| 154 | + constexpr int TEST_BLOCK_SIZE{81}; |
| 155 | + |
| 156 | + // Blockstore is empty |
| 157 | + BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), 0); |
| 158 | + |
| 159 | + // Write the first block; dbp=nullptr means this block doesn't already have a disk |
| 160 | + // location, so allocate a free location and write it there. |
| 161 | + FlatFilePos pos1{blockman.SaveBlockToDisk(block1, /*nHeight=*/1, /*dbp=*/nullptr)}; |
| 162 | + |
| 163 | + // Write second block |
| 164 | + FlatFilePos pos2{blockman.SaveBlockToDisk(block2, /*nHeight=*/2, /*dbp=*/nullptr)}; |
| 165 | + |
| 166 | + // Two blocks in the file |
| 167 | + 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 | + CBlock read_block; |
| 172 | + BOOST_CHECK_EQUAL(read_block.nVersion, 0); |
| 173 | + { |
| 174 | + ASSERT_DEBUG_LOG("ReadBlockFromDisk: Errors in block header"); |
| 175 | + BOOST_CHECK(!blockman.ReadBlockFromDisk(read_block, pos1)); |
| 176 | + BOOST_CHECK_EQUAL(read_block.nVersion, 1); |
| 177 | + } |
| 178 | + { |
| 179 | + ASSERT_DEBUG_LOG("ReadBlockFromDisk: Errors in block header"); |
| 180 | + BOOST_CHECK(!blockman.ReadBlockFromDisk(read_block, pos2)); |
| 181 | + BOOST_CHECK_EQUAL(read_block.nVersion, 2); |
| 182 | + } |
| 183 | + |
| 184 | + // When FlatFilePos* dbp is given, SaveBlockToDisk() will not write or |
| 185 | + // overwrite anything to the flat file block storage. It will, however, |
| 186 | + // update the blockfile metadata. This is to facilitate reindexing |
| 187 | + // when the user has the blocks on disk but the metadata is being rebuilt. |
| 188 | + // Verify this behavior by attempting (and failing) to write block 3 data |
| 189 | + // to block 2 location. |
| 190 | + CBlockFileInfo* block_data = blockman.GetBlockFileInfo(0); |
| 191 | + BOOST_CHECK_EQUAL(block_data->nBlocks, 2); |
| 192 | + BOOST_CHECK(blockman.SaveBlockToDisk(block3, /*nHeight=*/3, /*dbp=*/&pos2) == pos2); |
| 193 | + // Metadata is updated... |
| 194 | + BOOST_CHECK_EQUAL(block_data->nBlocks, 3); |
| 195 | + // ...but there are still only two blocks in the file |
| 196 | + BOOST_CHECK_EQUAL(blockman.CalculateCurrentUsage(), (TEST_BLOCK_SIZE + BLOCK_SERIALIZATION_HEADER_SIZE) * 2); |
| 197 | + |
| 198 | + // Block 2 was not overwritten: |
| 199 | + // SaveBlockToDisk() did not call WriteBlockToDisk() because `FlatFilePos* dbp` was non-null |
| 200 | + blockman.ReadBlockFromDisk(read_block, pos2); |
| 201 | + BOOST_CHECK_EQUAL(read_block.nVersion, 2); |
| 202 | +} |
| 203 | + |
133 | 204 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments