Skip to content

Commit 81e8e73

Browse files
mzumsandejanus
authored andcommitted
validation, blockstorage: Separate code paths for reindex and saving new blocks
By calling SaveBlockToDisk only when we actually want to save a new block to disk. In the reindex case, we now call UpdateBlockInfo directly from validation. This commit doesn't change behavior.
1 parent e819dda commit 81e8e73

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

src/bench/readblock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static FlatFilePos WriteBlockToDisk(ChainstateManager& chainman)
1818
CBlock block;
1919
stream >> TX_WITH_WITNESS(block);
2020

21-
return chainman.m_blockman.SaveBlockToDisk(block, 0, nullptr);
21+
return chainman.m_blockman.SaveBlockToDisk(block, 0);
2222
}
2323

2424
static void ReadBlockFromDiskTest(benchmark::Bench& bench)

src/node/blockstorage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,15 +1149,15 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
11491149
return true;
11501150
}
11511151

1152-
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
1152+
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
11531153
{
11541154
unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
1155+
FlatFilePos blockPos;
11551156
// Account for the 4 magic message start bytes + the 4 length bytes (8 bytes total,
11561157
// defined as BLOCK_SERIALIZATION_HEADER_SIZE)
11571158
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
1158-
FlatFilePos blockPos{FindNextBlockPos(nBlockSize, nHeight, block.GetBlockTime())};
1159-
if (blockPos.IsNull()) {
1160-
LogError("%s: FindNextBlockPos failed\n", __func__);
1159+
if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime())) {
1160+
LogError("%s: FindBlockPos failed\n", __func__);
11611161
return FlatFilePos();
11621162
}
11631163
if (!WriteBlockToDisk(block, blockPos)) {

src/node/blockstorage.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,14 @@ class BlockManager
328328
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
329329

330330
/** Store block on disk and update block file statistics.
331-
* If dbp is non-null, it means the block data is already stored, and dbp contains the file position.
332-
* In this case, the block data will not be written, only the block file statistics will be updated. This case should only happen during reindexing.
333331
*
334332
* @param[in] block the block to be stored
335333
* @param[in] nHeight the height of the block
336334
*
337335
* @returns in case of success, the position to which the block was written to
338336
* in case of an error, an empty FlatFilePos
339337
*/
340-
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp);
338+
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight);
341339

342340
/** Update blockfile info while processing a block during reindex. The block must be available on disk.
343341
*

src/test/blockmanager_tests.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/validation.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,10 +4353,16 @@ bool ChainstateManager::AcceptBlock(const std::shared_ptr<const CBlock>& pblock,
43534353
// Write block to history file
43544354
if (fNewBlock) *fNewBlock = true;
43554355
try {
4356-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, dbp)};
4357-
if (blockPos.IsNull()) {
4358-
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
4359-
return false;
4356+
FlatFilePos blockPos{};
4357+
if (dbp) {
4358+
blockPos = *dbp;
4359+
m_blockman.UpdateBlockInfo(block, pindex->nHeight, blockPos);
4360+
} else {
4361+
blockPos = m_blockman.SaveBlockToDisk(block, pindex->nHeight);
4362+
if (blockPos.IsNull()) {
4363+
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
4364+
return false;
4365+
}
43604366
}
43614367
ReceivedBlockTransactions(block, pindex, blockPos);
43624368
} catch (const std::runtime_error& e) {
@@ -4856,7 +4862,7 @@ bool Chainstate::LoadGenesisBlock()
48564862

48574863
try {
48584864
const CBlock& block = params.GenesisBlock();
4859-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, nullptr)};
4865+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0)};
48604866
if (blockPos.IsNull()) {
48614867
LogError("%s: writing genesis block to disk failed\n", __func__);
48624868
return false;

0 commit comments

Comments
 (0)