mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-12 15:03:18 +02:00
refactor: use _MiB consistently for Mebibyte conversions
Replace hard-coded MiB byte conversions (e.g. `1024*1024`, `1<<20`, `1048576`) with the existing `_MiB` literal to improve readability and avoid repeating constants.
In the few spots where arithmetic involves signed values, the result is identical to the previous code assuming those quantities never turn negative.
Also switch to brace init on every declaration assigned from `_MiB`/`_GiB` literals so a future oversized value (e.g. `unsigned int x{4096_MiB}`) becomes a compile error through the C++11 narrowing check instead of silently truncating.
Extend unit tests to cover the 32-bit `size_t` overflow boundary and to assert equivalence for integer and floating-point conversions.
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <node/blockstorage.h>
|
||||
#include <node/database_args.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/byte_units.h>
|
||||
#include <util/result.h>
|
||||
#include <util/translation.h>
|
||||
#include <validation.h>
|
||||
@@ -23,12 +24,12 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Op
|
||||
if (nPruneArg < 0) {
|
||||
return util::Error{_("Prune cannot be configured with a negative value.")};
|
||||
}
|
||||
uint64_t nPruneTarget{uint64_t(nPruneArg) * 1024 * 1024};
|
||||
uint64_t nPruneTarget{uint64_t(nPruneArg) * 1_MiB};
|
||||
if (nPruneArg == 1) { // manual pruning: -prune=1
|
||||
nPruneTarget = BlockManager::PRUNE_TARGET_MANUAL;
|
||||
} else if (nPruneTarget) {
|
||||
if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
|
||||
return util::Error{strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024)};
|
||||
return util::Error{strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1_MiB)};
|
||||
}
|
||||
}
|
||||
opts.prune_target = nPruneTarget;
|
||||
|
||||
@@ -394,8 +394,8 @@ void BlockManager::FindFilesToPrune(
|
||||
}
|
||||
|
||||
LogDebug(BCLog::PRUNE, "[%s] target=%dMiB actual=%dMiB diff=%dMiB min_height=%d max_prune_height=%d removed %d blk/rev pairs\n",
|
||||
chain.GetRole(), target / 1024 / 1024, nCurrentUsage / 1024 / 1024,
|
||||
(int64_t(target) - int64_t(nCurrentUsage)) / 1024 / 1024,
|
||||
chain.GetRole(), target / 1_MiB, nCurrentUsage / 1_MiB,
|
||||
(int64_t(target) - int64_t(nCurrentUsage)) / int64_t(1_MiB),
|
||||
min_block_to_prune, last_block_can_prune, count);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <streams.h>
|
||||
#include <sync.h>
|
||||
#include <uint256.h>
|
||||
#include <util/byte_units.h> // IWYU pragma: keep
|
||||
#include <util/expected.h>
|
||||
#include <util/fs.h>
|
||||
#include <util/hasher.h>
|
||||
@@ -116,11 +117,11 @@ using kernel::CBlockFileInfo;
|
||||
using kernel::BlockTreeDB;
|
||||
|
||||
/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
|
||||
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
|
||||
static const unsigned int BLOCKFILE_CHUNK_SIZE{16_MiB};
|
||||
/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
|
||||
static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
|
||||
static const unsigned int UNDOFILE_CHUNK_SIZE{1_MiB};
|
||||
/** The maximum size of a blk?????.dat file (since 0.8) */
|
||||
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
|
||||
static const unsigned int MAX_BLOCKFILE_SIZE{128_MiB};
|
||||
|
||||
/** Size of header written by WriteBlock before a serialized CBlock (8 bytes) */
|
||||
static constexpr uint32_t STORAGE_HEADER_BYTES{std::tuple_size_v<MessageStartChars> + sizeof(unsigned int)};
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <tinyformat.h>
|
||||
#include <txdb.h>
|
||||
#include <uint256.h>
|
||||
#include <util/byte_units.h>
|
||||
#include <util/fs.h>
|
||||
#include <util/log.h>
|
||||
#include <util/signalinterrupt.h>
|
||||
@@ -163,7 +164,7 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
|
||||
LogInfo("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.");
|
||||
} else if (chainman.m_blockman.GetPruneTarget()) {
|
||||
LogInfo("Prune configured to target %u MiB on disk for block and undo files.",
|
||||
chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
|
||||
chainman.m_blockman.GetPruneTarget() / 1_MiB);
|
||||
}
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <node/database_args.h>
|
||||
#include <tinyformat.h>
|
||||
#include <uint256.h>
|
||||
#include <util/byte_units.h>
|
||||
#include <util/result.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/translation.h>
|
||||
@@ -64,7 +65,7 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
|
||||
// script execution cache create the minimum possible cache (2
|
||||
// elements). Therefore, we can use 0 as a floor here.
|
||||
// 2. Multiply first, divide after to avoid integer truncation.
|
||||
size_t clamped_size_each = std::max<int64_t>(*max_size, 0) * (1 << 20) / 2;
|
||||
size_t clamped_size_each{size_t(std::max<int64_t>(*max_size, 0) * 1_MiB / 2)};
|
||||
opts.script_execution_cache_bytes = clamped_size_each;
|
||||
opts.signature_cache_bytes = clamped_size_each;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user