@@ -35,20 +35,20 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
3535 };
3636 BlockManager blockman{*Assert (m_node.shutdown ), blockman_opts};
3737 // simulate adding a genesis block normally
38- BOOST_CHECK_EQUAL (blockman.SaveBlockToDisk (params->GenesisBlock (), 0 , nullptr ).nPos , BLOCK_SERIALIZATION_HEADER_SIZE);
38+ BOOST_CHECK_EQUAL (blockman.SaveBlockToDisk (params->GenesisBlock (), 0 ).nPos , BLOCK_SERIALIZATION_HEADER_SIZE);
3939 // simulate what happens during reindex
4040 // simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
4141 // the block is found at offset 8 because there is an 8 byte serialization header
4242 // consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
43- FlatFilePos pos{0 , BLOCK_SERIALIZATION_HEADER_SIZE};
44- BOOST_CHECK_EQUAL ( blockman.SaveBlockToDisk (params->GenesisBlock (), 0 , & pos). nPos , BLOCK_SERIALIZATION_HEADER_SIZE );
43+ const FlatFilePos pos{0 , BLOCK_SERIALIZATION_HEADER_SIZE};
44+ blockman.UpdateBlockInfo (params->GenesisBlock (), 0 , pos);
4545 // now simulate what happens after reindex for the first new block processed
4646 // the actual block contents don't matter, just that it's a block.
4747 // verify that the write position is at offset 0x12d.
4848 // this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
4949 // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
5050 // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
51- FlatFilePos actual{blockman.SaveBlockToDisk (params->GenesisBlock (), 1 , nullptr )};
51+ FlatFilePos actual{blockman.SaveBlockToDisk (params->GenesisBlock (), 1 )};
5252 BOOST_CHECK_EQUAL (actual.nPos , BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize (TX_WITH_WITNESS (params->GenesisBlock ())) + BLOCK_SERIALIZATION_HEADER_SIZE);
5353}
5454
@@ -156,12 +156,11 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
156156 // Blockstore is empty
157157 BOOST_CHECK_EQUAL (blockman.CalculateCurrentUsage (), 0 );
158158
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 )};
159+ // Write the first block to a new location.
160+ FlatFilePos pos1{blockman.SaveBlockToDisk (block1, /* nHeight=*/ 1 )};
162161
163162 // Write second block
164- FlatFilePos pos2{blockman.SaveBlockToDisk (block2, /* nHeight=*/ 2 , /* dbp= */ nullptr )};
163+ FlatFilePos pos2{blockman.SaveBlockToDisk (block2, /* nHeight=*/ 2 )};
165164
166165 // Two blocks in the file
167166 BOOST_CHECK_EQUAL (blockman.CalculateCurrentUsage (), (TEST_BLOCK_SIZE + BLOCK_SERIALIZATION_HEADER_SIZE) * 2 );
@@ -181,22 +180,19 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
181180 BOOST_CHECK_EQUAL (read_block.nVersion , 2 );
182181 }
183182
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.
183+ // During reindex, the flat file block storage will not be written to.
184+ // UpdateBlockInfo will, however, update the blockfile metadata.
188185 // Verify this behavior by attempting (and failing) to write block 3 data
189186 // to block 2 location.
190187 CBlockFileInfo* block_data = blockman.GetBlockFileInfo (0 );
191188 BOOST_CHECK_EQUAL (block_data->nBlocks , 2 );
192- BOOST_CHECK ( blockman.SaveBlockToDisk (block3, /* nHeight=*/ 3 , /* dbp =*/ &pos2) == pos2);
189+ blockman.UpdateBlockInfo (block3, /* nHeight=*/ 3 , /* pos =*/ pos2);
193190 // Metadata is updated...
194191 BOOST_CHECK_EQUAL (block_data->nBlocks , 3 );
195192 // ...but there are still only two blocks in the file
196193 BOOST_CHECK_EQUAL (blockman.CalculateCurrentUsage (), (TEST_BLOCK_SIZE + BLOCK_SERIALIZATION_HEADER_SIZE) * 2 );
197194
198195 // Block 2 was not overwritten:
199- // SaveBlockToDisk() did not call WriteBlockToDisk() because `FlatFilePos* dbp` was non-null
200196 blockman.ReadBlockFromDisk (read_block, pos2);
201197 BOOST_CHECK_EQUAL (read_block.nVersion , 2 );
202198}
0 commit comments