refactor: Use SpanReader over DataStream

This refactor does not change behavior. However, it avoids a vector
copy, which can lead to a minimal speed-up of 1%-5%, depending on the
call-site. This is mostly relevant for the fuzz tests and utils that
read large blobs of data (like a full block).
This commit is contained in:
MarcoFalke 2026-02-02 16:55:11 +01:00
parent fad3eb3956
commit fa0677d131
No known key found for this signature in database
23 changed files with 37 additions and 60 deletions

View File

@ -21,9 +21,8 @@
static CBlock CreateTestBlock()
{
DataStream stream{benchmark::data::block413567};
CBlock block;
stream >> TX_WITH_WITNESS(block);
SpanReader{benchmark::data::block413567} >> TX_WITH_WITNESS(block);
return block;
}

View File

@ -174,7 +174,7 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
// Try decoding with extended serialization support, and remember if the result successfully
// consumes the entire input.
if (try_witness) {
DataStream ssData(tx_data);
SpanReader ssData{tx_data};
try {
ssData >> TX_WITH_WITNESS(tx_extended);
if (ssData.empty()) ok_extended = true;
@ -192,7 +192,7 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
// Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
if (try_no_witness) {
DataStream ssData(tx_data);
SpanReader ssData{tx_data};
try {
ssData >> TX_NO_WITNESS(tx_legacy);
if (ssData.empty()) ok_legacy = true;
@ -239,9 +239,8 @@ bool DecodeHexBlockHeader(CBlockHeader& header, const std::string& hex_header)
if (!IsHex(hex_header)) return false;
const std::vector<unsigned char> header_data{ParseHex(hex_header)};
DataStream ser_header{header_data};
try {
ser_header >> header;
SpanReader{header_data} >> header;
} catch (const std::exception&) {
return false;
}
@ -254,9 +253,8 @@ bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
return false;
std::vector<unsigned char> blockData(ParseHex(strHexBlk));
DataStream ssBlock(blockData);
try {
ssBlock >> TX_WITH_WITNESS(block);
SpanReader{blockData} >> TX_WITH_WITNESS(block);
}
catch (const std::exception&) {
return false;

View File

@ -504,7 +504,7 @@ btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t ra
return nullptr;
}
try {
DataStream stream{std::span{reinterpret_cast<const std::byte*>(raw_transaction), raw_transaction_len}};
SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_transaction), raw_transaction_len}};
return btck_Transaction::create(std::make_shared<const CTransaction>(deserialize, TX_WITH_WITNESS, stream));
} catch (...) {
return nullptr;
@ -1093,7 +1093,7 @@ btck_Block* btck_block_create(const void* raw_block, size_t raw_block_length)
}
auto block{std::make_shared<CBlock>()};
DataStream stream{std::span{reinterpret_cast<const std::byte*>(raw_block), raw_block_length}};
SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_block), raw_block_length}};
try {
stream >> TX_WITH_WITNESS(*block);
@ -1344,7 +1344,7 @@ btck_BlockHeader* btck_block_header_create(const void* raw_block_header, size_t
return nullptr;
}
auto header{std::make_unique<CBlockHeader>()};
DataStream stream{std::span{reinterpret_cast<const std::byte*>(raw_block_header), raw_block_header_len}};
SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_block_header), raw_block_header_len}};
try {
stream >> *header;

View File

@ -201,7 +201,7 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
const auto one_week{7 * 24h};
std::vector<CAddress> vSeedsOut;
FastRandomContext rng;
ParamsStream s{DataStream{vSeedsIn}, CAddress::V2_NETWORK};
ParamsStream s{SpanReader{vSeedsIn}, CAddress::V2_NETWORK};
while (!s.empty()) {
CService endpoint;
s >> endpoint;

View File

@ -617,7 +617,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, std::span<const std::byte> tx_data, std::string& error)
{
DataStream ss_data{tx_data};
SpanReader ss_data{tx_data};
try {
ss_data >> psbt;
if (!ss_data.empty()) {

View File

@ -365,7 +365,7 @@ void TestGUI(interfaces::Node& node, const std::shared_ptr<CWallet>& wallet)
std::vector<std::string> requests = walletModel.wallet().getAddressReceiveRequests();
QCOMPARE(requests.size(), size_t{1});
RecentRequestEntry entry;
DataStream{MakeUCharSpan(requests[0])} >> entry;
SpanReader{MakeByteSpan(requests[0])} >> entry;
QCOMPARE(entry.nVersion, int{1});
QCOMPARE(entry.id, int64_t{1});
QVERIFY(entry.date.isValid());

View File

@ -451,8 +451,7 @@ static bool rest_block(const std::any& context,
case RESTResponseFormat::JSON: {
if (tx_verbosity) {
CBlock block{};
DataStream block_stream{*block_data};
block_stream >> TX_WITH_WITNESS(block);
SpanReader{*block_data} >> TX_WITH_WITNESS(block);
UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, *tx_verbosity, chainman.GetConsensus().powLimit);
std::string strJSON = objBlock.write() + "\n";
req->WriteHeader("Content-Type", "application/json");

View File

@ -832,9 +832,8 @@ static RPCHelpMan getblock()
return HexStr(block_data);
}
DataStream block_stream{block_data};
CBlock block{};
block_stream >> TX_WITH_WITNESS(block);
SpanReader{block_data} >> TX_WITH_WITNESS(block);
TxVerbosity tx_verbosity;
if (verbosity == 1) {

View File

@ -144,9 +144,8 @@ static RPCHelpMan verifytxoutproof()
RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
DataStream ssMB{ParseHexV(request.params[0], "proof")};
CMerkleBlock merkleBlock;
ssMB >> merkleBlock;
SpanReader{ParseHexV(request.params[0], "proof")} >> merkleBlock;
UniValue res(UniValue::VARR);

View File

@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE(bloom_match)
// and one which spends it (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 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 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
DataStream spendStream{vch};
SpanReader spendStream{vch};
CTransaction spendingTx(deserialize, TX_WITH_WITNESS, spendStream);
CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL);

View File

@ -517,37 +517,33 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
BOOST_AUTO_TEST_CASE(ccoins_serialization)
{
// Good example
DataStream ss1{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
Coin cc1;
ss1 >> cc1;
SpanReader{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex} >> cc1;
BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
// Good example
DataStream ss2{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
Coin cc2;
ss2 >> cc2;
SpanReader{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex} >> cc2;
BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
// Smallest possible example
DataStream ss3{"000006"_hex};
Coin cc3;
ss3 >> cc3;
SpanReader{"000006"_hex} >> cc3;
BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
// scriptPubKey that ends beyond the end of the stream
DataStream ss4{"000007"_hex};
try {
Coin cc4;
ss4 >> cc4;
SpanReader{"000007"_hex} >> cc4;
BOOST_CHECK_MESSAGE(false, "We should have thrown");
} catch (const std::ios_base::failure&) {
}
@ -557,10 +553,9 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
uint64_t x = 3000000000ULL;
tmp << VARINT(x);
BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
DataStream ss5{"00008a95c0bb00"_hex};
try {
Coin cc5;
ss5 >> cc5;
SpanReader{"00008a95c0bb00"_hex} >> cc5;
BOOST_CHECK_MESSAGE(false, "We should have thrown");
} catch (const std::ios_base::failure&) {
}

View File

@ -24,10 +24,9 @@ void initialize_block()
FUZZ_TARGET(block, .init = initialize_block)
{
DataStream ds{buffer};
CBlock block;
try {
ds >> TX_WITH_WITNESS(block);
SpanReader{buffer} >> TX_WITH_WITNESS(block);
} catch (const std::ios_base::failure&) {
return;
}

View File

@ -81,9 +81,8 @@ T Deserialize(DataStream&& ds, const P& params)
template <typename T, typename P>
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const P& params)
{
DataStream ds{buffer};
try {
ds >> params(obj);
SpanReader{buffer} >> params(obj);
} catch (const std::ios_base::failure&) {
throw invalid_fuzzing_input_exception();
}
@ -109,9 +108,8 @@ T Deserialize(DataStream ds)
template <typename T>
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj)
{
DataStream ds{buffer};
try {
ds >> obj;
SpanReader{buffer} >> obj;
} catch (const std::ios_base::failure&) {
throw invalid_fuzzing_input_exception();
}

View File

@ -15,7 +15,7 @@
#include <utility>
#include <vector>
static DataStream& operator>>(DataStream& ds, script_verify_flags& f)
static SpanReader& operator>>(SpanReader& ds, script_verify_flags& f)
{
script_verify_flags::value_type n{0};
ds >> n;
@ -27,7 +27,7 @@ static DataStream& operator>>(DataStream& ds, script_verify_flags& f)
FUZZ_TARGET(script_flags)
{
if (buffer.size() > 100'000) return;
DataStream ds{buffer};
SpanReader ds{buffer};
try {
const CTransaction tx(deserialize, TX_WITH_WITNESS, ds);

View File

@ -30,7 +30,7 @@ void initialize_transaction()
FUZZ_TARGET(transaction, .init = initialize_transaction)
{
SeedRandomStateForTest(SeedRand::ZEROS);
DataStream ds{buffer};
SpanReader ds{buffer};
bool valid_tx = true;
const CTransaction tx = [&] {
try {
@ -41,10 +41,9 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
}
}();
bool valid_mutable_tx = true;
DataStream ds_mtx{buffer};
CMutableTransaction mutable_tx;
try {
ds_mtx >> TX_WITH_WITNESS(mutable_tx);
SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx);
} catch (const std::ios_base::failure&) {
valid_mutable_tx = false;
}

View File

@ -13,10 +13,9 @@
FUZZ_TARGET(tx_in)
{
DataStream ds{buffer};
CTxIn tx_in;
try {
ds >> tx_in;
SpanReader{buffer} >> tx_in;
} catch (const std::ios_base::failure&) {
return;
}

View File

@ -12,10 +12,9 @@
FUZZ_TARGET(tx_out)
{
DataStream ds{buffer};
CTxOut tx_out;
try {
ds >> tx_out;
SpanReader{buffer} >> tx_out;
} catch (const std::ios_base::failure&) {
return;
}

View File

@ -105,7 +105,7 @@ template <typename T, typename P>
[[nodiscard]] std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const P& params, const std::optional<size_t>& max_length = std::nullopt) noexcept
{
const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
DataStream ds{buffer};
SpanReader ds{buffer};
T obj;
try {
ds >> params(obj);
@ -119,7 +119,7 @@ template <typename T>
[[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
{
const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
DataStream ds{buffer};
SpanReader ds{buffer};
T obj;
try {
ds >> obj;

View File

@ -872,10 +872,9 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
std::span<const unsigned char> data,
bool is_incoming) -> void {
if (!is_incoming && msg_type == "addr") {
DataStream s{data};
std::vector<CAddress> addresses;
s >> CAddress::V1_NETWORK(addresses);
SpanReader{data} >> CAddress::V1_NETWORK(addresses);
for (const auto& addr : addresses) {
if (addr == expected) {

View File

@ -575,10 +575,9 @@ BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
{
DataStream s{ParseHex(stream_addrv1_hex)};
std::vector<CAddress> addresses_unserialized;
s >> CAddress::V1_NETWORK(addresses_unserialized);
SpanReader{ParseHex(stream_addrv1_hex)} >> CAddress::V1_NETWORK(addresses_unserialized);
BOOST_CHECK(fixture_addresses == addresses_unserialized);
}
@ -592,10 +591,9 @@ BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
{
DataStream s{ParseHex(stream_addrv2_hex)};
std::vector<CAddress> addresses_unserialized;
s >> CAddress::V2_NETWORK(addresses_unserialized);
SpanReader{ParseHex(stream_addrv2_hex)} >> CAddress::V2_NETWORK(addresses_unserialized);
BOOST_CHECK(fixture_addresses == addresses_unserialized);
}

View File

@ -189,8 +189,7 @@ BOOST_AUTO_TEST_CASE(sighash_from_data)
nHashType = test[3].getInt<int>();
sigHashHex = test[4].get_str();
DataStream stream(ParseHex(raw_tx));
stream >> TX_WITH_WITNESS(tx);
SpanReader{ParseHex(raw_tx)} >> TX_WITH_WITNESS(tx);
TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);

View File

@ -377,9 +377,8 @@ BOOST_AUTO_TEST_CASE(basic_transaction_tests)
// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
DataStream stream(vch);
CMutableTransaction tx;
stream >> TX_WITH_WITNESS(tx);
SpanReader{vch} >> TX_WITH_WITNESS(tx);
TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");

View File

@ -57,9 +57,8 @@ RPCHelpMan importprunedfunds()
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
}
DataStream ssMB{ParseHexV(request.params[1], "proof")};
CMerkleBlock merkleBlock;
ssMB >> merkleBlock;
SpanReader{ParseHexV(request.params[1], "proof")} >> merkleBlock;
//Search partial merkle tree in proof for our transaction and index in valid block
std::vector<Txid> vMatch;