Skip to content

Commit bf09076

Browse files
jimpofurszy
authored andcommitted
validation: Refactor block file pre-allocation into FlatFileSeq.
1 parent c813080 commit bf09076

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

src/flatfile.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,29 @@ FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly)
4646
}
4747
return file;
4848
}
49+
50+
size_t FlatFileSeq::Allocate(const CDiskBlockPos& pos, size_t add_size, bool& out_of_space)
51+
{
52+
out_of_space = false;
53+
54+
unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
55+
unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
56+
if (n_new_chunks > n_old_chunks) {
57+
size_t old_size = pos.nPos;
58+
size_t new_size = n_new_chunks * m_chunk_size;
59+
size_t inc_size = new_size - old_size;
60+
61+
if (CheckDiskSpace(m_dir, inc_size)) {
62+
FILE *file = Open(pos);
63+
if (file) {
64+
LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
65+
AllocateFileRange(file, pos.nPos, inc_size);
66+
fclose(file);
67+
return inc_size;
68+
}
69+
} else {
70+
out_of_space = true;
71+
}
72+
}
73+
return 0;
74+
}

src/flatfile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ class FlatFileSeq
3434

3535
/** Open a handle to the file at the given position. */
3636
FILE* Open(const CDiskBlockPos& pos, bool fReadOnly = false);
37+
38+
/**
39+
* Allocate additional space in a file after the given starting position. The amount allocated
40+
* will be the minimum multiple of the sequence chunk size greater than add_size.
41+
*
42+
* @param[in] pos The starting position that bytes will be allocated after.
43+
* @param[in] add_size The minimum number of bytes to be allocated.
44+
* @param[out] out_of_space Whether the allocation failed due to insufficient disk space.
45+
* @return The number of bytes successfully allocated.
46+
*/
47+
size_t Allocate(const CDiskBlockPos& pos, size_t add_size, bool& out_of_space);
3748
};
3849

3950
#endif // BITCOIN_FLATFILE_H

src/validation.cpp

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,20 +2657,12 @@ bool FindBlockPos(CValidationState& state, CDiskBlockPos& pos, unsigned int nAdd
26572657
vinfoBlockFile[nFile].nSize += nAddSize;
26582658

26592659
if (!fKnown) {
2660-
unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2661-
unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2662-
if (nNewChunks > nOldChunks) {
2663-
if (CheckDiskSpace(GetBlocksDir(), nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
2664-
FILE* file = OpenBlockFile(pos);
2665-
if (file) {
2666-
LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2667-
AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
2668-
fclose(file);
2669-
}
2670-
} else {
2671-
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
2672-
}
2660+
bool out_of_space;
2661+
BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
2662+
if (out_of_space) {
2663+
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
26732664
}
2665+
// future: add prunning flag check
26742666
}
26752667

26762668
setDirtyFileInfo.insert(nFile);
@@ -2683,25 +2675,16 @@ bool FindUndoPos(CValidationState& state, int nFile, CDiskBlockPos& pos, unsigne
26832675

26842676
LOCK(cs_LastBlockFile);
26852677

2686-
unsigned int nNewSize;
26872678
pos.nPos = vinfoBlockFile[nFile].nUndoSize;
2688-
nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
2679+
vinfoBlockFile[nFile].nUndoSize += nAddSize;
26892680
setDirtyFileInfo.insert(nFile);
26902681

2691-
unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2692-
unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2693-
if (nNewChunks > nOldChunks) {
2694-
if (CheckDiskSpace(GetBlocksDir(), nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
2695-
FILE* file = OpenUndoFile(pos);
2696-
if (file) {
2697-
LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2698-
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
2699-
fclose(file);
2700-
}
2701-
} else {
2702-
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
2703-
}
2682+
bool out_of_space;
2683+
UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
2684+
if (out_of_space) {
2685+
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
27042686
}
2687+
// future: add prunning flag check
27052688

27062689
return true;
27072690
}

0 commit comments

Comments
 (0)