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:
Lőrinc
2026-01-28 14:38:24 +01:00
parent b3edd30aa2
commit af0ee28eb6
36 changed files with 138 additions and 82 deletions

View File

@@ -18,6 +18,7 @@
#include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <uint256.h>
#include <util/byte_units.h>
#include <util/result.h>
#include <util/vector.h>
#include <validation.h>
@@ -72,10 +73,10 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
const uint256 snapshot_blockhash = active_tip->GetBlockHash();
Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, snapshot_blockhash)))};
c2.InitCoinsDB(
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
/*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
{
LOCK(::cs_main);
c2.InitCoinsCache(1 << 23);
c2.InitCoinsCache(8_MiB);
c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
for (const auto& cs : manager.m_chainstates) {
cs->ClearBlockIndexCandidates();
@@ -133,7 +134,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
chainstates.push_back(&c1);
{
LOCK(::cs_main);
c1.InitCoinsCache(1 << 23);
c1.InitCoinsCache(8_MiB);
manager.MaybeRebalanceCaches();
}
@@ -146,7 +147,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, *snapshot_base->phashBlock)))};
chainstates.push_back(&c2);
c2.InitCoinsDB(
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
/*cache_size_bytes=*/8_MiB, /*in_memory=*/true, /*should_wipe=*/false);
// Reset IBD state so IsInitialBlockDownload() returns true and causes
// MaybeRebalanceCaches() to prioritize the snapshot chainstate, giving it
@@ -159,7 +160,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
{
LOCK(::cs_main);
c2.InitCoinsCache(1 << 23);
c2.InitCoinsCache(8_MiB);
manager.MaybeRebalanceCaches();
}