mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
Merge bitcoin/bitcoin#23249: util: ParseByteUnits - Parse a string with suffix unit
21b58f430f
util: ParseByteUnits - Parse a string with suffix unit [k|K|m|M|g|G|t|T] (Douglas Chimento) Pull request description: A convenience utility for parsing human readable strings sizes e.g. `500G` is `500 * 1 << 30` The argument/setting `maxuploadtarget` now accept human readable byte units `[k|K|m|M|g|G||t|T]` This change backward compatible, defaults to `M` if no unit specified. ACKs for top commit: vasild: ACK21b58f430f
ryanofsky: Code review ACK21b58f430f
. Only changes since last review are dropping optional has_value call, fixing comment punctuation, squashing commits. Tree-SHA512: c9b85acc0f77c847a0290b27ac5dc586ecc078110cf133063140576a04c11aa9c553159b9b4993488edcf6e60db6837de7c83b2964639bc21e8ffa4d455a5eb7
This commit is contained in:
commit
73ac195e29
6 changed files with 138 additions and 4 deletions
|
@ -127,6 +127,10 @@ Updated settings
|
|||
mean `-persistmempool=1`. Passing `-persistmempool=0`, `-persistmempool=1`
|
||||
and `-nopersistmempool` is unaffected. (#23061)
|
||||
|
||||
- `-maxuploadtarget` now allows human readable byte units [k|K|m|M|g|G|t|T].
|
||||
E.g. `-maxuploadtarget=500g`. No whitespace, +- or fractions allowed.
|
||||
Default is `M` if no suffix provided. (#23249)
|
||||
|
||||
Tools and Utilities
|
||||
-------------------
|
||||
|
||||
|
|
12
src/init.cpp
12
src/init.cpp
|
@ -59,6 +59,7 @@
|
|||
#include <util/asmap.h>
|
||||
#include <util/check.h>
|
||||
#include <util/moneystr.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/syscall_sandbox.h>
|
||||
#include <util/system.h>
|
||||
|
@ -436,7 +437,7 @@ void SetupServerArgs(ArgsManager& argsman)
|
|||
argsman.AddArg("-maxreceivebuffer=<n>", strprintf("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXRECEIVEBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-maxsendbuffer=<n>", strprintf("Maximum per-connection send buffer, <n>*1000 bytes (default: %u)", DEFAULT_MAXSENDBUFFER), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-maxtimeadjustment", strprintf("Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by peers forward or backward by this amount. (default: %u seconds)", DEFAULT_MAX_TIME_ADJUSTMENT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-maxuploadtarget=<n>", strprintf("Tries to keep outbound traffic under the given target (in MiB per 24h). Limit does not apply to peers with 'download' permission. 0 = no limit (default: %d)", DEFAULT_MAX_UPLOAD_TARGET), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-maxuploadtarget=<n>", strprintf("Tries to keep outbound traffic under the given target per 24h. Limit does not apply to peers with 'download' permission or blocks created within past week. 0 = no limit (default: %s). Optional suffix units [k|K|m|M|g|G|t|T] (default: M). Lowercase is 1000 base while uppercase is 1024 base", DEFAULT_MAX_UPLOAD_TARGET), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-onion=<ip:port>", "Use separate SOCKS5 proxy to reach peers via Tor onion services, set -noonion to disable (default: -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-i2psam=<ip:port>", "I2P SAM proxy to reach I2P peers and accept I2P connections (default: none)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
argsman.AddArg("-i2pacceptincoming", "If set and -i2psam is also set then incoming I2P connections are accepted via the SAM proxy. If this is not set but -i2psam is set then only outgoing connections will be made to the I2P network. Ignored if -i2psam is not set. Listening for incoming I2P connections is done through the SAM proxy, not by binding to a local address and port (default: 1)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
|
||||
|
@ -1109,6 +1110,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
{
|
||||
const ArgsManager& args = *Assert(node.args);
|
||||
const CChainParams& chainparams = Params();
|
||||
|
||||
auto opt_max_upload = ParseByteUnits(args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET), ByteUnit::M);
|
||||
if (!opt_max_upload) {
|
||||
return InitError(strprintf(_("Unable to parse -maxuploadtarget: '%s' (possible integer overflow?)"), args.GetArg("-maxuploadtarget", "")));
|
||||
}
|
||||
|
||||
// ********************************************************* Step 4a: application initialization
|
||||
if (!CreatePidFile(args)) {
|
||||
// Detailed error printed inside CreatePidFile().
|
||||
|
@ -1760,8 +1767,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
connOptions.nSendBufferMaxSize = 1000 * args.GetIntArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
|
||||
connOptions.nReceiveFloodSize = 1000 * args.GetIntArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
|
||||
connOptions.m_added_nodes = args.GetArgs("-addnode");
|
||||
|
||||
connOptions.nMaxOutboundLimit = 1024 * 1024 * args.GetIntArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET);
|
||||
connOptions.nMaxOutboundLimit = *opt_max_upload;
|
||||
connOptions.m_peer_connect_timeout = peer_connect_timeout;
|
||||
|
||||
for (const std::string& bind_arg : args.GetArgs("-bind")) {
|
||||
|
|
|
@ -70,7 +70,7 @@ static const bool DEFAULT_LISTEN = true;
|
|||
/** The maximum number of peer connections to maintain. */
|
||||
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
|
||||
/** The default for -maxuploadtarget. 0 = Unlimited */
|
||||
static constexpr uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
|
||||
static const std::string DEFAULT_MAX_UPLOAD_TARGET{"0M"};
|
||||
/** Default for blocks only*/
|
||||
static const bool DEFAULT_BLOCKSONLY = false;
|
||||
/** -peertimeout default */
|
||||
|
|
|
@ -2456,4 +2456,52 @@ BOOST_AUTO_TEST_CASE(remove_prefix)
|
|||
BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_ParseByteUnits)
|
||||
{
|
||||
auto noop = ByteUnit::NOOP;
|
||||
|
||||
// no multiplier
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("1", noop).value(), 1);
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("0", noop).value(), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("1k", noop).value(), 1000ULL);
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("1K", noop).value(), 1ULL << 10);
|
||||
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("2m", noop).value(), 2'000'000ULL);
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("2M", noop).value(), 2ULL << 20);
|
||||
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("3g", noop).value(), 3'000'000'000ULL);
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("3G", noop).value(), 3ULL << 30);
|
||||
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("4t", noop).value(), 4'000'000'000'000ULL);
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("4T", noop).value(), 4ULL << 40);
|
||||
|
||||
// check default multiplier
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("5", ByteUnit::K).value(), 5ULL << 10);
|
||||
|
||||
// NaN
|
||||
BOOST_CHECK(!ParseByteUnits("", noop));
|
||||
BOOST_CHECK(!ParseByteUnits("foo", noop));
|
||||
|
||||
// whitespace
|
||||
BOOST_CHECK(!ParseByteUnits("123m ", noop));
|
||||
BOOST_CHECK(!ParseByteUnits(" 123m", noop));
|
||||
|
||||
// no +-
|
||||
BOOST_CHECK(!ParseByteUnits("-123m", noop));
|
||||
BOOST_CHECK(!ParseByteUnits("+123m", noop));
|
||||
|
||||
// zero padding
|
||||
BOOST_CHECK_EQUAL(ParseByteUnits("020M", noop).value(), 20ULL << 20);
|
||||
|
||||
// fractions not allowed
|
||||
BOOST_CHECK(!ParseByteUnits("0.5T", noop));
|
||||
|
||||
// overflow
|
||||
BOOST_CHECK(!ParseByteUnits("18446744073709551615g", noop));
|
||||
|
||||
// invalid unit
|
||||
BOOST_CHECK(!ParseByteUnits("1x", noop));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
|
@ -526,3 +527,48 @@ std::string HexStr(const Span<const uint8_t> s)
|
|||
assert(it == rv.end());
|
||||
return rv;
|
||||
}
|
||||
|
||||
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier)
|
||||
{
|
||||
if (str.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto multiplier = default_multiplier;
|
||||
char unit = str.back();
|
||||
switch (unit) {
|
||||
case 'k':
|
||||
multiplier = ByteUnit::k;
|
||||
break;
|
||||
case 'K':
|
||||
multiplier = ByteUnit::K;
|
||||
break;
|
||||
case 'm':
|
||||
multiplier = ByteUnit::m;
|
||||
break;
|
||||
case 'M':
|
||||
multiplier = ByteUnit::M;
|
||||
break;
|
||||
case 'g':
|
||||
multiplier = ByteUnit::g;
|
||||
break;
|
||||
case 'G':
|
||||
multiplier = ByteUnit::G;
|
||||
break;
|
||||
case 't':
|
||||
multiplier = ByteUnit::t;
|
||||
break;
|
||||
case 'T':
|
||||
multiplier = ByteUnit::T;
|
||||
break;
|
||||
default:
|
||||
unit = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
uint64_t unit_amount = static_cast<uint64_t>(multiplier);
|
||||
auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
|
||||
if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow
|
||||
return std::nullopt;
|
||||
}
|
||||
return *parsed_num * unit_amount;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,23 @@ enum SafeChars
|
|||
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
|
||||
};
|
||||
|
||||
/**
|
||||
* Used by ParseByteUnits()
|
||||
* Lowercase base 1000
|
||||
* Uppercase base 1024
|
||||
*/
|
||||
enum class ByteUnit : uint64_t {
|
||||
NOOP = 1ULL,
|
||||
k = 1000ULL,
|
||||
K = 1024ULL,
|
||||
m = 1'000'000ULL,
|
||||
M = 1ULL << 20,
|
||||
g = 1'000'000'000ULL,
|
||||
G = 1ULL << 30,
|
||||
t = 1'000'000'000'000ULL,
|
||||
T = 1ULL << 40,
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
|
||||
* addresses, but avoid anything even possibly remotely dangerous like & or >
|
||||
|
@ -305,4 +322,17 @@ std::string ToUpper(const std::string& str);
|
|||
*/
|
||||
std::string Capitalize(std::string str);
|
||||
|
||||
/**
|
||||
* Parse a string with suffix unit [k|K|m|M|g|G|t|T].
|
||||
* Must be a whole integer, fractions not allowed (0.5t), no whitespace or +-
|
||||
* Lowercase units are 1000 base. Uppercase units are 1024 base.
|
||||
* Examples: 2m,27M,19g,41T
|
||||
*
|
||||
* @param[in] str the string to convert into bytes
|
||||
* @param[in] default_multiplier if no unit is found in str use this unit
|
||||
* @returns optional uint64_t bytes from str or nullopt
|
||||
* if ToIntegral is false, str is empty, trailing whitespace or overflow
|
||||
*/
|
||||
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier);
|
||||
|
||||
#endif // BITCOIN_UTIL_STRENCODINGS_H
|
||||
|
|
Loading…
Reference in a new issue