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.
1752// Basic operations1753BOOST_CHECK_EQUAL(CheckedLeftShift<T>(0, 1), 0);1754BOOST_CHECK_EQUAL(CheckedLeftShift<T>(0, 127), 0);1755BOOST_CHECK_EQUAL(CheckedLeftShift<T>(1, 1), 2);1756BOOST_CHECK_EQUAL(CheckedLeftShift<T>(2, 2), 8);1757BOOST_CHECK_EQUAL(CheckedLeftShift<T>(MAX >> 1, 1), MAX - 1);1759// Max left shift1760BOOST_CHECK_EQUAL(CheckedLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1);1762// Overflow cases1763BOOST_CHECK(!CheckedLeftShift<T>((MAX >> 1) + 1, 1));1764BOOST_CHECK(!CheckedLeftShift<T>(MAX, 1));1765BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits));1766BOOST_CHECK(!CheckedLeftShift<T>(1, std::numeric_limits<T>::digits + 1));1768if constexpr (std::is_signed_v<T>) {1769constexpr auto MIN{std::numeric_limits<T>::min()};1770// Negative input1771BOOST_CHECK_EQUAL(CheckedLeftShift<T>(-1, 1), -2);1772BOOST_CHECK_EQUAL(CheckedLeftShift<T>((MIN >> 2), 1), MIN / 2);1773BOOST_CHECK_EQUAL(CheckedLeftShift<T>((MIN >> 1) + 1, 1), MIN + 2);1774BOOST_CHECK_EQUAL(CheckedLeftShift<T>(MIN >> 1, 1), MIN);1775// Overflow negative1776BOOST_CHECK(!CheckedLeftShift<T>((MIN >> 1) - 1, 1));1777BOOST_CHECK(!CheckedLeftShift<T>(MIN >> 1, 2));1778BOOST_CHECK(!CheckedLeftShift<T>(-1, 100));1779}1780}1782template <typename T>1783void TestSaturatingLeftShift()1784{1785constexpr auto MAX{std::numeric_limits<T>::max()};1787// Basic operations1788BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(0, 1), 0);1789BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(0, 127), 0);1790BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, 1), 2);1791BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(2, 2), 8);1792BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MAX >> 1, 1), MAX - 1);1794// Max left shift1795BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits - 1), MAX / 2 + 1);1797// Saturation cases1798BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MAX >> 1) + 1, 1), MAX);1799BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MAX, 1), MAX);1800BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits), MAX);1801BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(1, std::numeric_limits<T>::digits + 1), MAX);1803if constexpr (std::is_signed_v<T>) {1804constexpr auto MIN{std::numeric_limits<T>::min()};1805// Negative input1806BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(-1, 1), -2);1807BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 2), 1), MIN / 2);1808BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 1) + 1, 1), MIN + 2);1809BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MIN >> 1, 1), MIN);1810// Saturation negative1811BOOST_CHECK_EQUAL(SaturatingLeftShift<T>((MIN >> 1) - 1, 1), MIN);1812BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(MIN >> 1, 2), MIN);1813BOOST_CHECK_EQUAL(SaturatingLeftShift<T>(-1, 100), MIN);1814}1815}1817BOOST_AUTO_TEST_CASE(checked_left_shift_test)1818{1819TestCheckedLeftShift<uint8_t>();1820TestCheckedLeftShift<int8_t>();1821TestCheckedLeftShift<size_t>();1822TestCheckedLeftShift<uint64_t>();1823TestCheckedLeftShift<int64_t>();1824}1826BOOST_AUTO_TEST_CASE(saturating_left_shift_test)1827{1828TestSaturatingLeftShift<uint8_t>();1829TestSaturatingLeftShift<int8_t>();1830TestSaturatingLeftShift<size_t>();1831TestSaturatingLeftShift<uint64_t>();1832TestSaturatingLeftShift<int64_t>();1833}1835template <class Int, auto bytes>1836concept BraceInitializesTo = requires { Int{bytes}; };1838BOOST_AUTO_TEST_CASE(mib_string_literal_test)1839{1840// Basic equivalences and simple arithmetic operations1841BOOST_CHECK_EQUAL(0_MiB, 0);1842BOOST_CHECK_EQUAL(1_MiB, 1 << 20);1843BOOST_CHECK_EQUAL(1_MiB, 1024 * 1024);1844BOOST_CHECK_EQUAL(1_MiB, 0x100000U);1845BOOST_CHECK_EQUAL(1_MiB, 1048576U);1846BOOST_CHECK_EQUAL(2ULL * 1_MiB, 2ULL << 20);1847BOOST_CHECK_EQUAL((3_MiB + 123) / double(1_MiB), (3_MiB + 123) / 1024.0 / 1024.0);1849// Specific codebase values1850BOOST_CHECK_EQUAL(4_MiB, 1 << 22);1851BOOST_CHECK_EQUAL(8_MiB, 1 << 23);1852BOOST_CHECK_EQUAL(16_MiB, 0x1000000U);1853BOOST_CHECK_EQUAL(16_MiB, 1 << 24);1854BOOST_CHECK_EQUAL(32_MiB, 0x2000000U);1855BOOST_CHECK_EQUAL(32_MiB, 32U << 20);1856BOOST_CHECK_EQUAL(50_MiB / 1_MiB, 50U);1857BOOST_CHECK_EQUAL(50_MiB, 52428800U);1858BOOST_CHECK_EQUAL(128_MiB, 0x8000000U);1859BOOST_CHECK_EQUAL(550_MiB, 550ULL * 1024 * 1024);1861// 4095 MiB fits in uint32_t bytes. 4096 MiB requires the uint64_t return type.1862static_assert(BraceInitializesTo<uint32_t, 4095_MiB>);1863static_assert(!BraceInitializesTo<uint32_t, 4096_MiB>);1864static_assert(BraceInitializesTo<uint64_t, 4096_MiB>);1865BOOST_CHECK_EQUAL(4095_MiB, uint32_t{4095} << 20);1866BOOST_CHECK_EQUAL(4096_MiB, uint64_t{4096} << 20);1867}1869BOOST_AUTO_TEST_CASE(ceil_div_test)1870{1871// Type combinations used by current CeilDiv callsites.1872BOOST_CHECK((std::is_same_v<decltype(CeilDiv(uint32_t{0}, 8u)), uint32_t>));1873BOOST_CHECK((std::is_same_v<decltype(CeilDiv(size_t{0}, 8u)), size_t>));1874BOOST_CHECK((std::is_same_v<decltype(CeilDiv(unsigned{0}, size_t{1})), size_t>));1876// `common/bloom.cpp` and `cuckoocache.h` patterns.1877BOOST_CHECK_EQUAL(CeilDiv(uint32_t{3}, 2u), uint32_t{2});1878BOOST_CHECK_EQUAL(CeilDiv(uint32_t{65}, 64u), uint32_t{2});1879BOOST_CHECK_EQUAL(CeilDiv(uint32_t{9}, 8u), uint32_t{2});1881// `key_io.cpp`, `rest.cpp`, `merkleblock.cpp`, `strencodings.cpp` patterns.1882BOOST_CHECK_EQUAL(CeilDiv(size_t{9}, 8u), size_t{2});1883BOOST_CHECK_EQUAL(CeilDiv(size_t{10}, 3u), size_t{4});1884BOOST_CHECK_EQUAL(CeilDiv(size_t{11}, 5u), size_t{3});1885BOOST_CHECK_EQUAL(CeilDiv(size_t{41} * 8, 5u), size_t{66});1887// `flatfile.cpp` mixed unsigned/size_t pattern.1888BOOST_CHECK_EQUAL(CeilDiv(unsigned{10}, size_t{4}), size_t{3});1890// `util/feefrac.h` fast-path rounding-up pattern.1891constexpr int64_t fee{12345};1892constexpr int32_t at_size{67};1893constexpr int32_t size{10};1894BOOST_CHECK_EQUAL(CeilDiv(uint64_t(fee) * at_size, uint32_t(size)),1895(uint64_t(fee) * at_size + uint32_t(size) - 1) / uint32_t(size));1897// `bitset.h` template parameter pattern.1898constexpr unsigned bits{129};1899constexpr size_t digits{std::numeric_limits<size_t>::digits};1900BOOST_CHECK_EQUAL(CeilDiv(bits, digits), (bits + digits - 1) / digits);1902// `serialize.h` varint scratch-buffer pattern.1903BOOST_CHECK_EQUAL(CeilDiv(sizeof(uint64_t) * 8, 7u), (sizeof(uint64_t) * 8 + 6) / 7);1904}1906BOOST_AUTO_TEST_CASE(gib_string_literal_test)1907{1908// Basic equivalences and simple arithmetic operations1909BOOST_CHECK_EQUAL(0_GiB, 0);1910BOOST_CHECK_EQUAL(1_GiB, 1 << 30);1911BOOST_CHECK_EQUAL(1_GiB, 1024 * 1024 * 1024);1912BOOST_CHECK_EQUAL(1_GiB, 0x40000000U);1913BOOST_CHECK_EQUAL(1_GiB, 1073741824U);1914BOOST_CHECK_EQUAL(1_GiB, 1_MiB * 1024);1915BOOST_CHECK_EQUAL(1_GiB, 1024_MiB);1916BOOST_CHECK_EQUAL((1_GiB + 123) / double(1_GiB), (1_GiB + 123) / 1024.0 / 1024.0 / 1024.0);1917BOOST_CHECK_EQUAL(2ULL * 1_GiB, 2ULL << 30);1918BOOST_CHECK_EQUAL(4 * uint64_t{1_GiB}, uint64_t{4} << 30);1919BOOST_CHECK_EQUAL(2_GiB, 2048_MiB);1920BOOST_CHECK_EQUAL(3_GiB / 1_GiB, 3U);1921BOOST_CHECK_EQUAL(3_GiB, 3U << 30);1923// 3 GiB fits in uint32_t bytes. 4 GiB requires the uint64_t return type.1924static_assert(BraceInitializesTo<uint32_t, 3_GiB>);1925static_assert(!BraceInitializesTo<uint32_t, 4_GiB>);1926static_assert(BraceInitializesTo<uint64_t, 4_GiB>);1927BOOST_CHECK_EQUAL(3_GiB, uint32_t{3} << 30);1928BOOST_CHECK_EQUAL(4_GiB, uint64_t{4} << 30);1930// Specific codebase values1931BOOST_CHECK_EQUAL(4_GiB, 4096_MiB);1932BOOST_CHECK_EQUAL(8_GiB, 8192_MiB);1933BOOST_CHECK_EQUAL(16_GiB, 16384_MiB);1934BOOST_CHECK_EQUAL(32_GiB, 32768_MiB);1935}1937BOOST_AUTO_TEST_CASE(token_bucket_initial_value)1938{1939// Initial value is clamped to cap1940util::TokenBucket<NodeClock> b1(/*rate=*/1, /*value=*/100, /*cap=*/10);1941BOOST_CHECK_EQUAL(b1.value(), 10);1943// Initial value below cap is kept as-is1944util::TokenBucket<NodeClock> b2(/*rate=*/1, /*value=*/5, /*cap=*/10);1945BOOST_CHECK_EQUAL(b2.value(), 5);1946}1948BOOST_AUTO_TEST_CASE(token_bucket_first_increment)1949{1950// First increment establishes the time baseline but does not refill1951util::TokenBucket<NodeClock> b(/*rate=*/100, /*value=*/0, /*cap=*/1000);1952b.increment(NodeClock::time_point{10s});1953BOOST_CHECK_EQUAL(b.value(), 0);1955// Second increment refills based on elapsed time1956b.increment(NodeClock::time_point{15s});1957BOOST_CHECK_EQUAL(b.value(), 500); // 100/s * 5s1958}1960BOOST_AUTO_TEST_CASE(token_bucket_refill_caps)1961{1962util::TokenBucket<NodeClock> b(/*rate=*/10, /*value=*/90, /*cap=*/100);1963b.increment(NodeClock::time_point{1s});1964b.increment(NodeClock::time_point{100s}); // would add 990, but cap is 1001965BOOST_CHECK_EQUAL(b.value(), 100);1966}1968BOOST_AUTO_TEST_CASE(token_bucket_time_backwards)1969{1970util::TokenBucket<NodeClock> b(/*rate=*/10, /*value=*/50, /*cap=*/200);1971b.increment(NodeClock::time_point{10s});1972b.increment(NodeClock::time_point{5s}); // backwards, no change1973BOOST_CHECK_EQUAL(b.value(), 50);1974b.increment(NodeClock::time_point{15s}); // forwards takes backwards into account1975BOOST_CHECK_EQUAL(b.value(), 150);1976}1978BOOST_AUTO_TEST_CASE(token_bucket_decrement_no_debt)1979{1980// Default debt=0: returns false at exactly 01981util::TokenBucket<NodeClock> b(/*rate=*/1, /*value=*/3, /*cap=*/10);1982BOOST_CHECK(b.decrement(1)); // 3 -> 21983BOOST_CHECK(b.decrement(1)); // 2 -> 11984BOOST_CHECK(!b.decrement(1)); // 1 -> 0, at floor1985BOOST_CHECK_EQUAL(b.value(), 0);1986BOOST_CHECK(!b.decrement(1)); // 0 -> -1, despite being at floor1987BOOST_CHECK_EQUAL(b.value(), -1);1988}1990BOOST_AUTO_TEST_CASE(token_bucket_decrement_with_debt)1991{1992util::TokenBucket<NodeClock> b(/*rate=*/1, /*value=*/2, /*cap=*/10);1993BOOST_CHECK(b.decrement(1, -3)); // 2 -> 11994BOOST_CHECK(b.decrement(1, -3)); // 1 -> 01995BOOST_CHECK(b.decrement(1, -3)); // 0 -> -1, still above -31996BOOST_CHECK(b.decrement(1, -3)); // -1 -> -2, still above -31997BOOST_CHECK(!b.decrement(1, -3)); // -2 -> -3, at floor1998BOOST_CHECK_EQUAL(b.value(), -3);1999}
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.