Skip to content

Commit 17556cb

Browse files
committed
flatfile: store position as a 64 bits integer
This is the last field that is only bounded by the usage callers make of it (unlike the file number, for instance). Promoting it to a 64 bits integer also does not change on-disk representation since we serialize positions as varints. Therefore, widen them to prevent any possible future misuse.
1 parent 71fa043 commit 17556cb

9 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎src/chain.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ class CBlockIndex
109109
int nFile GUARDED_BY(::cs_main){0};
110110

111111
//! Byte offset within blk?????.dat where this block's data is stored
112-
unsigned int nDataPos GUARDED_BY(::cs_main){0};
112+
uint64_t nDataPos GUARDED_BY(::cs_main){0};
113113

114114
//! Byte offset within rev?????.dat where this block's undo data is stored
115-
unsigned int nUndoPos GUARDED_BY(::cs_main){0};
115+
uint64_t nUndoPos GUARDED_BY(::cs_main){0};
116116

117117
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
118118
arith_uint256 nChainWork{};

‎src/flatfile.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_
6060
{
6161
out_of_space = false;
6262

63-
unsigned int n_old_chunks = CeilDiv(pos.nPos, m_chunk_size);
64-
unsigned int n_new_chunks = CeilDiv(pos.nPos + add_size, m_chunk_size);
63+
uint64_t n_old_chunks = CeilDiv(pos.nPos, uint64_t{m_chunk_size});
64+
uint64_t n_new_chunks = CeilDiv(pos.nPos + add_size, uint64_t{m_chunk_size});
6565
if (n_new_chunks > n_old_chunks) {
66-
size_t old_size = pos.nPos;
67-
size_t new_size = n_new_chunks * m_chunk_size;
66+
uint64_t old_size = pos.nPos;
67+
uint64_t new_size = n_new_chunks * m_chunk_size;
6868
size_t inc_size = new_size - old_size;
6969

7070
if (CheckDiskSpace(m_dir, inc_size)) {

‎src/flatfile.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
struct FlatFilePos
1515
{
1616
int32_t nFile{-1};
17-
uint32_t nPos{0};
17+
uint64_t nPos{0};
1818

1919
SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }
2020

2121
FlatFilePos() = default;
2222

23-
FlatFilePos(int32_t nFileIn, uint32_t nPosIn)
23+
FlatFilePos(int32_t nFileIn, uint64_t nPosIn)
2424
: nFile{nFileIn},
2525
nPos{nPosIn}
2626
{}

‎src/kernel/chain.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <attributes.h>
99

10+
#include <cstdint>
1011
#include <iostream>
1112

1213
class CBlock;
@@ -21,7 +22,7 @@ struct BlockInfo {
2122
const uint256* prev_hash = nullptr;
2223
int height = -1;
2324
int file_number = -1;
24-
unsigned data_pos = 0;
25+
uint64_t data_pos = 0;
2526
const CBlock* data = nullptr;
2627
const CBlockUndo* undo_data = nullptr;
2728
// The maximum time in the chain up to and including this block.

‎src/node/blockstorage.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class CBlockFileInfo
5858
{
5959
public:
6060
uint32_t nBlocks{}; //!< number of blocks stored in file
61-
uint32_t nSize{}; //!< number of used bytes of block file
62-
uint32_t nUndoSize{}; //!< number of used bytes in the undo file
61+
uint64_t nSize{}; //!< number of used bytes of block file
62+
uint64_t nUndoSize{}; //!< number of used bytes in the undo file
6363
uint32_t nHeightFirst{}; //!< lowest height of block in file
6464
uint32_t nHeightLast{}; //!< highest height of block in file
6565
uint64_t nTimeFirst{}; //!< earliest time of block in file

‎src/streams.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ bool AutoFile::Commit()
131131
return ::FileCommit(m_file);
132132
}
133133

134-
bool AutoFile::Truncate(unsigned size)
134+
bool AutoFile::Truncate(uint64_t size)
135135
{
136136
m_was_written = true;
137137
return ::TruncateFile(m_file, size);

‎src/streams.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ class AutoFile
466466
bool Commit();
467467

468468
/** Wrapper around TruncateFile(). */
469-
bool Truncate(unsigned size);
469+
bool Truncate(uint64_t size);
470470

471471
//! Write a mutable buffer more efficiently than write(), obfuscating the buffer in-place.
472472
void write_buffer(std::span<std::byte> src);

‎src/util/fs_helpers.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void DirectoryCommit(const fs::path& dirname)
147147
#endif
148148
}
149149

150-
bool TruncateFile(FILE* file, unsigned int length)
150+
bool TruncateFile(FILE* file, uint64_t length)
151151
{
152152
#if defined(WIN32)
153153
return _chsize(_fileno(file), length) == 0;
@@ -197,7 +197,7 @@ int RaiseFileDescriptorLimit(int min_fd)
197197
* this function tries to make a particular range of a file allocated (corresponding to disk space)
198198
* it is advisory, and the range specified in the arguments will never contain live data
199199
*/
200-
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
200+
void AllocateFileRange(FILE* file, uint64_t offset, unsigned int length)
201201
{
202202
#if defined(WIN32)
203203
// Windows-specific version

‎src/util/fs_helpers.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool FileCommit(FILE* file);
4444
*/
4545
void DirectoryCommit(const fs::path& dirname);
4646

47-
bool TruncateFile(FILE* file, unsigned int length);
47+
bool TruncateFile(FILE* file, uint64_t length);
4848

4949
/**
5050
* Try to raise the file descriptor limit to the requested number.
@@ -57,7 +57,7 @@ bool TruncateFile(FILE* file, unsigned int length);
5757
*/
5858
int RaiseFileDescriptorLimit(int min_fd);
5959

60-
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
60+
void AllocateFileRange(FILE* file, uint64_t offset, unsigned int length);
6161

6262
/**
6363
* Rename src to dest.

0 commit comments

Comments
 (0)