mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-15 00:14:08 +02:00
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>
44 lines
1.6 KiB
C++
44 lines
1.6 KiB
C++
// Copyright (c) 2023-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <node/blockmanager_args.h>
|
|
|
|
#include <common/args.h>
|
|
#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>
|
|
|
|
#include <cstdint>
|
|
|
|
namespace node {
|
|
util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Options& opts)
|
|
{
|
|
if (auto value{args.GetBoolArg("-blocksxor")}) opts.use_xor = *value;
|
|
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
|
|
int64_t nPruneArg{args.GetIntArg("-prune", opts.prune_target)};
|
|
if (nPruneArg < 0) {
|
|
return util::Error{_("Prune cannot be configured with a negative value.")};
|
|
}
|
|
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 / 1_MiB)};
|
|
}
|
|
}
|
|
opts.prune_target = nPruneTarget;
|
|
|
|
if (auto value{args.GetBoolArg("-fastprune")}) opts.fast_prune = *value;
|
|
|
|
ReadDatabaseArgs(args, opts.block_tree_db_params.options);
|
|
|
|
return {};
|
|
}
|
|
} // namespace node
|