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.
1// Copyright (c) 2012-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <hash.h>6#include <serialize.h>7#include <streams.h>8#include <test/util/common.h>9#include <test/util/setup_common.h>10#include <util/strencodings.h>12#include <algorithm>13#include <cstdint>14#include <string>16#include <boost/test/unit_test.hpp>18BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)20// For testing move-semantics, declare a version of datastream that can be moved21// but is not copyable.22class UncopyableStream : public DataStream23{24public:25using DataStream::DataStream;26UncopyableStream(const UncopyableStream&) = delete;27UncopyableStream& operator=(const UncopyableStream&) = delete;28UncopyableStream(UncopyableStream&&) noexcept = default;29UncopyableStream& operator=(UncopyableStream&&) noexcept = default;30};32class CSerializeMethodsTestSingle33{34protected:35int intval;36bool boolval;37std::string stringval;38char charstrval[16];39CTransactionRef txval;40public:41CSerializeMethodsTestSingle() = default;42CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin)43{44memcpy(charstrval, charstrvalin, sizeof(charstrval));45}47SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)48{49READWRITE(obj.intval);50READWRITE(obj.boolval);51READWRITE(obj.stringval);52READWRITE(obj.charstrval);53READWRITE(TX_WITH_WITNESS(obj.txval));54}56bool operator==(const CSerializeMethodsTestSingle& rhs) const57{58return intval == rhs.intval &&59boolval == rhs.boolval &&60stringval == rhs.stringval &&61strcmp(charstrval, rhs.charstrval) == 0 &&62txval->Equals(*rhs.txval);63}64};66class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle67{68public:69using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;71SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)72{73READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));74}75};77BOOST_AUTO_TEST_CASE(sizes)78{79BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0));80BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0)));81BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0)));82BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0)));83BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0)));84BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0)));85BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0)));86BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0)));87BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0)));88// Bool is serialized as uint8_t89BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0)));91// Sanity-check GetSerializeSize and c++ type matching92BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0), 1U);93BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0)), 1U);94BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0)), 1U);95BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0)), 2U);96BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0)), 2U);97BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0)), 4U);98BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0)), 4U);99BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0)), 8U);100BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0)), 8U);101BOOST_CHECK_EQUAL(GetSerializeSize(bool(0)), 1U);102BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}), 1U);103BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}), 2U);104}106BOOST_AUTO_TEST_CASE(varints)107{108// encode110DataStream ss{};111DataStream::size_type size = 0;112for (int i = 0; i < 100000; i++) {113ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);114size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED));115BOOST_CHECK(size == ss.size());116}118for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {119ss << VARINT(i);120size += ::GetSerializeSize(VARINT(i));121BOOST_CHECK(size == ss.size());122}124// decode125for (int i = 0; i < 100000; i++) {126int j = -1;127ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);128BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);129}131for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {132uint64_t j = std::numeric_limits<uint64_t>::max();133ss >> VARINT(j);134BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);135}136}138BOOST_AUTO_TEST_CASE(varints_bitpatterns)139{140DataStream ss{};141ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();142ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();143ss << VARINT_MODE(int8_t{0x7f}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();144ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();145ss << VARINT(uint8_t{0x80}); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();146ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();147ss << VARINT_MODE(int16_t{0x1234}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();148ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();149ss << VARINT(uint16_t{0xffff}); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();150ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();151ss << VARINT_MODE(int32_t{0x123456}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();152ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();153ss << VARINT(uint32_t{0x80123456U}); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();154ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();155ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();156ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();157}159BOOST_AUTO_TEST_CASE(compactsize)160{161DataStream ss{};162std::vector<char>::size_type i, j;164for (i = 1; i <= MAX_SIZE; i *= 2)165{166WriteCompactSize(ss, i-1);167WriteCompactSize(ss, i);168}169for (i = 1; i <= MAX_SIZE; i *= 2)170{171j = ReadCompactSize(ss);172BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));173j = ReadCompactSize(ss);174BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);175}176}178static bool isCanonicalException(const std::ios_base::failure& ex)179{180std::ios_base::failure expectedException("non-canonical ReadCompactSize()");182// The string returned by what() can be different for different platforms.183// Instead of directly comparing the ex.what() with an expected string,184// create an instance of exception to see if ex.what() matches185// the expected explanatory string returned by the exception instance.186return strcmp(expectedException.what(), ex.what()) == 0;187}189BOOST_AUTO_TEST_CASE(vector_bool)190{191std::vector<uint8_t> vec1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};192std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};194BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));195BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());196}198BOOST_AUTO_TEST_CASE(array)199{200std::array<uint8_t, 32> array1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};201DataStream ds;202ds << array1;203std::array<uint8_t, 32> array2;204ds >> array2;205BOOST_CHECK(array1 == array2);206}208BOOST_AUTO_TEST_CASE(noncanonical)209{210// Write some non-canonical CompactSize encodings, and211// make sure an exception is thrown when read back.212DataStream ss{};213std::vector<char>::size_type n;215// zero encoded with three bytes:216ss << std::span{"\xfd\x00\x00"}.first(3);217BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);219// 0xfc encoded with three bytes:220ss << std::span{"\xfd\xfc\x00"}.first(3);221BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);223// 0xfd encoded with three bytes is OK:224ss << std::span{"\xfd\xfd\x00"}.first(3);225n = ReadCompactSize(ss);226BOOST_CHECK(n == 0xfd);228// zero encoded with five bytes:229ss << std::span{"\xfe\x00\x00\x00\x00"}.first(5);230BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);232// 0xffff encoded with five bytes:233ss << std::span{"\xfe\xff\xff\x00\x00"}.first(5);234BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);236// zero encoded with nine bytes:237ss << std::span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);238BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);240// 0x01ffffff encoded with nine bytes:241ss << std::span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);242BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);243}245BOOST_AUTO_TEST_CASE(string_view)246{247const std::string_view sv{"hello, world"};248DataStream ss;249ss << sv;250std::string s;
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.