Skip to content

Commit 271c23e

Browse files
blockstorage: Adjust fastprune limit if block exceeds blockfile size
If the added block exceeds the blockfile size in test-only -fastprune mode, the node would get stuck in an infinite loop and run out of memory. Avoid this by raising the blockfile size to the size of the added block in this situation. Co-authored-by: TheCharlatan <[email protected]>
1 parent d908877 commit 271c23e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/node/blockstorage.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,18 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
619619

620620
bool finalize_undo = false;
621621
if (!fKnown) {
622-
while (m_blockfile_info[nFile].nSize + nAddSize >= (gArgs.GetBoolArg("-fastprune", false) ? 0x10000 /* 64kb */ : MAX_BLOCKFILE_SIZE)) {
622+
unsigned int max_blockfile_size{MAX_BLOCKFILE_SIZE};
623+
// Use smaller blockfiles in test-only -fastprune mode - but avoid
624+
// the possibility of having a block not fit into the block file.
625+
if (gArgs.GetBoolArg("-fastprune", false)) {
626+
max_blockfile_size = 0x10000; // 64kiB
627+
if (nAddSize >= max_blockfile_size) {
628+
// dynamically adjust the blockfile size to be larger than the added size
629+
max_blockfile_size = nAddSize + 1;
630+
}
631+
}
632+
assert(nAddSize < max_blockfile_size);
633+
while (m_blockfile_info[nFile].nSize + nAddSize >= max_blockfile_size) {
623634
// when the undo file is keeping up with the block file, we want to flush it explicitly
624635
// when it is lagging behind (more blocks arrive than are being connected), we let the
625636
// undo block write case handle it

0 commit comments

Comments
 (0)