Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
501BOOST_CHECK(!ParseMoney(" +1 .2 "));503// Attempted 63 bit overflow should fail504BOOST_CHECK(!ParseMoney("92233720368.54775808"));506// Parsing negative amounts must fail507BOOST_CHECK(!ParseMoney("-1"));509// Parsing strings with embedded NUL characters should fail510BOOST_CHECK(!ParseMoney("\0-1"s));511BOOST_CHECK(!ParseMoney(STRING_WITH_EMBEDDED_NULL_CHAR));512BOOST_CHECK(!ParseMoney("1\0"s));513}515BOOST_AUTO_TEST_CASE(util_IsHex)516{517BOOST_CHECK(IsHex("00"));518BOOST_CHECK(IsHex("00112233445566778899aabbccddeeffAABBCCDDEEFF"));519BOOST_CHECK(IsHex("ff"));520BOOST_CHECK(IsHex("FF"));522BOOST_CHECK(!IsHex(""));523BOOST_CHECK(!IsHex("0"));524BOOST_CHECK(!IsHex("a"));525BOOST_CHECK(!IsHex("eleven"));526BOOST_CHECK(!IsHex("00xx00"));527BOOST_CHECK(!IsHex("0x0000"));528}530BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)531{532SeedRandomForTest(SeedRand::ZEROS);533for (int mod=2;mod<11;mod++)534{535int mask = 1;536// Really rough binomial confidence approximation.537int err = 30*10000./mod*sqrt((1./mod*(1-1./mod))/10000.);538//mask is 2^ceil(log2(mod))-1539while(mask<mod-1)mask=(mask<<1)+1;541int count = 0;542//How often does it get a zero from the uniform range [0,mod)?543for (int i = 0; i < 10000; i++) {544uint32_t rval;545do{546rval=m_rng.rand32()&mask;547}while(rval>=(uint32_t)mod);548count += rval==0;549}550BOOST_CHECK(count<=10000/mod+err);551BOOST_CHECK(count>=10000/mod-err);552}553}555BOOST_AUTO_TEST_CASE(util_TimingResistantEqual)556{557BOOST_CHECK(TimingResistantEqual(std::string(""), std::string("")));558BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("")));559BOOST_CHECK(!TimingResistantEqual(std::string(""), std::string("abc")));560BOOST_CHECK(!TimingResistantEqual(std::string("a"), std::string("aa")));561BOOST_CHECK(!TimingResistantEqual(std::string("aa"), std::string("a")));562BOOST_CHECK(TimingResistantEqual(std::string("abc"), std::string("abc")));563BOOST_CHECK(!TimingResistantEqual(std::string("abc"), std::string("aba")));564}566/* Test strprintf formatting directives.567* Put a string before and after to ensure sanity of element sizes on stack. */568#define B "check_prefix"569#define E "check_postfix"570BOOST_AUTO_TEST_CASE(strprintf_numbers)571{572int64_t s64t = -9223372036854775807LL; /* signed 64 bit test value */573uint64_t u64t = 18446744073709551615ULL; /* unsigned 64 bit test value */574BOOST_CHECK(strprintf("%s %d %s", B, s64t, E) == B" -9223372036854775807 " E);575BOOST_CHECK(strprintf("%s %u %s", B, u64t, E) == B" 18446744073709551615 " E);576BOOST_CHECK(strprintf("%s %x %s", B, u64t, E) == B" ffffffffffffffff " E);578size_t st = 12345678; /* unsigned size_t test value */579ssize_t sst = -12345678; /* signed size_t test value */580BOOST_CHECK(strprintf("%s %d %s", B, sst, E) == B" -12345678 " E);581BOOST_CHECK(strprintf("%s %u %s", B, st, E) == B" 12345678 " E);582BOOST_CHECK(strprintf("%s %x %s", B, st, E) == B" bc614e " E);584ptrdiff_t pt = 87654321; /* positive ptrdiff_t test value */585ptrdiff_t spt = -87654321; /* negative ptrdiff_t test value */586BOOST_CHECK(strprintf("%s %d %s", B, spt, E) == B" -87654321 " E);587BOOST_CHECK(strprintf("%s %u %s", B, pt, E) == B" 87654321 " E);588BOOST_CHECK(strprintf("%s %x %s", B, pt, E) == B" 5397fb1 " E);589}590#undef B591#undef E593BOOST_AUTO_TEST_CASE(util_mocktime)594{595FakeNodeClock clock{111s};596// Check that mock time does not change after a sleep597for (const auto& num_sleep : {0ms, 1ms}) {598UninterruptibleSleep(num_sleep);599BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter600BOOST_CHECK_EQUAL(111, Now<NodeSeconds>().time_since_epoch().count());601BOOST_CHECK_EQUAL(111, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()));602BOOST_CHECK_EQUAL(111, TicksSinceEpoch<SecondsDouble>(Now<NodeSeconds>()));603BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());604BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());605BOOST_CHECK_EQUAL(111000, TicksSinceEpoch<std::chrono::milliseconds>(NodeClock::now()));606BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());607}608}610BOOST_AUTO_TEST_CASE(util_ticksseconds)611{612BOOST_CHECK_EQUAL(TicksSeconds(0s), 0);613BOOST_CHECK_EQUAL(TicksSeconds(1s), 1);614BOOST_CHECK_EQUAL(TicksSeconds(999ms), 0);615BOOST_CHECK_EQUAL(TicksSeconds(1000ms), 1);616BOOST_CHECK_EQUAL(TicksSeconds(1500ms), 1);617}619BOOST_AUTO_TEST_CASE(test_IsDigit)620{621BOOST_CHECK_EQUAL(IsDigit('0'), true);622BOOST_CHECK_EQUAL(IsDigit('1'), true);623BOOST_CHECK_EQUAL(IsDigit('8'), true);624BOOST_CHECK_EQUAL(IsDigit('9'), true);626BOOST_CHECK_EQUAL(IsDigit('0' - 1), false);627BOOST_CHECK_EQUAL(IsDigit('9' + 1), false);628BOOST_CHECK_EQUAL(IsDigit(0), false);629BOOST_CHECK_EQUAL(IsDigit(1), false);630BOOST_CHECK_EQUAL(IsDigit(8), false);631BOOST_CHECK_EQUAL(IsDigit(9), false);632}634/* Check for overflow */635template <typename T>636static void TestAddMatrixOverflow()637{638constexpr T MAXI{std::numeric_limits<T>::max()};639BOOST_CHECK(!CheckedAdd(T{1}, MAXI));640BOOST_CHECK(!CheckedAdd(MAXI, MAXI));641BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI));642BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(MAXI, MAXI));644BOOST_CHECK_EQUAL(0, CheckedAdd(T{0}, T{0}).value());645BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{0}, MAXI).value());646BOOST_CHECK_EQUAL(MAXI, CheckedAdd(T{1}, MAXI - 1).value());647BOOST_CHECK_EQUAL(MAXI - 1, CheckedAdd(T{1}, MAXI - 2).value());648BOOST_CHECK_EQUAL(0, SaturatingAdd(T{0}, T{0}));649BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{0}, MAXI));650BOOST_CHECK_EQUAL(MAXI, SaturatingAdd(T{1}, MAXI - 1));651BOOST_CHECK_EQUAL(MAXI - 1, SaturatingAdd(T{1}, MAXI - 2));652}654/* Check for overflow or underflow */655template <typename T>656static void TestAddMatrix()657{658TestAddMatrixOverflow<T>();659constexpr T MINI{std::numeric_limits<T>::min()};660constexpr T MAXI{std::numeric_limits<T>::max()};661BOOST_CHECK(!CheckedAdd(T{-1}, MINI));662BOOST_CHECK(!CheckedAdd(MINI, MINI));663BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI));664BOOST_CHECK_EQUAL(MINI, SaturatingAdd(MINI, MINI));666BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{0}, MINI).value());667BOOST_CHECK_EQUAL(MINI, CheckedAdd(T{-1}, MINI + 1).value());668BOOST_CHECK_EQUAL(-1, CheckedAdd(MINI, MAXI).value());669BOOST_CHECK_EQUAL(MINI + 1, CheckedAdd(T{-1}, MINI + 2).value());670BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{0}, MINI));671BOOST_CHECK_EQUAL(MINI, SaturatingAdd(T{-1}, MINI + 1));672BOOST_CHECK_EQUAL(MINI + 1, SaturatingAdd(T{-1}, MINI + 2));673BOOST_CHECK_EQUAL(-1, SaturatingAdd(MINI, MAXI));674}676BOOST_AUTO_TEST_CASE(util_overflow)677{678TestAddMatrixOverflow<unsigned>();679TestAddMatrix<signed>();680}682template <typename T>683static void RunToIntegralTests()684{685BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR));686BOOST_CHECK(!ToIntegral<T>(" 1"));687BOOST_CHECK(!ToIntegral<T>("1 "));688BOOST_CHECK(!ToIntegral<T>("1a"));689BOOST_CHECK(!ToIntegral<T>("1.1"));690BOOST_CHECK(!ToIntegral<T>("1.9"));691BOOST_CHECK(!ToIntegral<T>("+01.9"));692BOOST_CHECK(!ToIntegral<T>("-"));693BOOST_CHECK(!ToIntegral<T>("+"));694BOOST_CHECK(!ToIntegral<T>(" -1"));695BOOST_CHECK(!ToIntegral<T>("-1 "));696BOOST_CHECK(!ToIntegral<T>(" -1 "));697BOOST_CHECK(!ToIntegral<T>("+1"));698BOOST_CHECK(!ToIntegral<T>(" +1"));699BOOST_CHECK(!ToIntegral<T>(" +1 "));700BOOST_CHECK(!ToIntegral<T>("+-1"));701BOOST_CHECK(!ToIntegral<T>("-+1"));702BOOST_CHECK(!ToIntegral<T>("++1"));703BOOST_CHECK(!ToIntegral<T>("--1"));704BOOST_CHECK(!ToIntegral<T>(""));705BOOST_CHECK(!ToIntegral<T>("aap"));706BOOST_CHECK(!ToIntegral<T>("0x1"));707BOOST_CHECK(!ToIntegral<T>("-32482348723847471234"));708BOOST_CHECK(!ToIntegral<T>("32482348723847471234"));709}711BOOST_AUTO_TEST_CASE(test_ToIntegral)712{713BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);714BOOST_CHECK_EQUAL(ToIntegral<int32_t>("0").value(), 0);715BOOST_CHECK_EQUAL(ToIntegral<int32_t>("01234").value(), 1'234);716BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000001234").value(), 1'234);717BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-00000000000000001234").value(), -1'234);718BOOST_CHECK_EQUAL(ToIntegral<int32_t>("00000000000000000000").value(), 0);719BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-00000000000000000000").value(), 0);720BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);721BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);723RunToIntegralTests<uint64_t>();724RunToIntegralTests<int64_t>();725RunToIntegralTests<uint32_t>();726RunToIntegralTests<int32_t>();727RunToIntegralTests<uint16_t>();728RunToIntegralTests<int16_t>();729RunToIntegralTests<uint8_t>();730RunToIntegralTests<int8_t>();732BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));733BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);734BOOST_CHECK_EQUAL(ToIntegral<int64_t>("9223372036854775807").value(), 9'223'372'036'854'775'807);735BOOST_CHECK(!ToIntegral<int64_t>("9223372036854775808"));737BOOST_CHECK(!ToIntegral<uint64_t>("-1"));738BOOST_CHECK_EQUAL(ToIntegral<uint64_t>("0").value(), 0U);739BOOST_CHECK_EQUAL(ToIntegral<uint64_t>("18446744073709551615").value(), 18'446'744'073'709'551'615ULL);740BOOST_CHECK(!ToIntegral<uint64_t>("18446744073709551616"));742BOOST_CHECK(!ToIntegral<int32_t>("-2147483649"));743BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-2147483648").value(), -2'147'483'648LL);744BOOST_CHECK_EQUAL(ToIntegral<int32_t>("2147483647").value(), 2'147'483'647);745BOOST_CHECK(!ToIntegral<int32_t>("2147483648"));747BOOST_CHECK(!ToIntegral<uint32_t>("-1"));748BOOST_CHECK_EQUAL(ToIntegral<uint32_t>("0").value(), 0U);749BOOST_CHECK_EQUAL(ToIntegral<uint32_t>("4294967295").value(), 4'294'967'295U);750BOOST_CHECK(!ToIntegral<uint32_t>("4294967296"));
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.