Merge bitcoin/bitcoin#34483: refactor: Use SpanReader over DataStream

fa0677d131 refactor: Use SpanReader over DataStream (MarcoFalke)
fad3eb3956 refactor: Use SpanReader over DataStream (MarcoFalke)
fa06e26764 refactor: [qt] Use SpanReader to avoid two vector copies (MarcoFalke)
fabd4d2e2e refactor: Avoid UB in SpanReader::ignore (MarcoFalke)
fa20bc2ec2 refactor: Use empty() over eof() in the streams interface (MarcoFalke)
fa879db735 test: Read debug log for self-checking comment (MarcoFalke)

Pull request description:

  This changes all places, where possible, to use SpanReader over DataStream. This makes the code easier to read and reason about, because `SpanReader` can never write data. Also, the code should be minimally faster, because it avoids a full redundant copy of the whole vector of bytes.

ACKs for top commit:
  stickies-v:
    re-ACK fa0677d131
  achow101:
    ACK fa0677d131
  janb84:
    re ACK fa0677d131
  sipa:
    crACK fa0677d131

Tree-SHA512: 1d9f43fc6e71d481cf7b8f8457f479745ee331734649e9e2c2ab00ce5d317112796c77afc328612ed004e65ac5c16fc92279d760cfb012cfddce9098c4af810f
This commit is contained in:
Ava Chow committed 2026-02-06 18:00:18 -08:00
commit 3b39a8aeb4
30 files changed
+49 -71

No files matched your search

+1 -2
View File
@@ -21,9 +21,8 @@
static CBlock CreateTestBlock() static CBlock CreateTestBlock()
{ {
DataStream stream{benchmark::data::block413567};
CBlock block; CBlock block;
stream >> TX_WITH_WITNESS(block); SpanReader{benchmark::data::block413567} >> TX_WITH_WITNESS(block);
return block; return block;
} }
+4 -6
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 // Try decoding with extended serialization support, and remember if the result successfully
// consumes the entire input. // consumes the entire input.
if (try_witness) { if (try_witness) {
DataStream ssData(tx_data); SpanReader ssData{tx_data};
try { try {
ssData >> TX_WITH_WITNESS(tx_extended); ssData >> TX_WITH_WITNESS(tx_extended);
if (ssData.empty()) ok_extended = true; 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. // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
if (try_no_witness) { if (try_no_witness) {
DataStream ssData(tx_data); SpanReader ssData{tx_data};
try { try {
ssData >> TX_NO_WITNESS(tx_legacy); ssData >> TX_NO_WITNESS(tx_legacy);
if (ssData.empty()) ok_legacy = true; 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; if (!IsHex(hex_header)) return false;
const std::vector<unsigned char> header_data{ParseHex(hex_header)}; const std::vector<unsigned char> header_data{ParseHex(hex_header)};
DataStream ser_header{header_data};
try { try {
ser_header >> header; SpanReader{header_data} >> header;
} catch (const std::exception&) { } catch (const std::exception&) {
return false; return false;
} }
@@ -254,9 +253,8 @@ bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
return false; return false;
std::vector<unsigned char> blockData(ParseHex(strHexBlk)); std::vector<unsigned char> blockData(ParseHex(strHexBlk));
DataStream ssBlock(blockData);
try { try {
ssBlock >> TX_WITH_WITNESS(block); SpanReader{blockData} >> TX_WITH_WITNESS(block);
} }
catch (const std::exception&) { catch (const std::exception&) {
return false; return false;
+2 -2
View File
@@ -214,9 +214,9 @@ public:
return false; return false;
} }
try { try {
DataStream ssValue{MakeByteSpan(*strValue)}; std::span ssValue{MakeWritableByteSpan(*strValue)};
m_obfuscation(ssValue); m_obfuscation(ssValue);
ssValue >> value; SpanReader{ssValue} >> value;
} catch (const std::exception&) { } catch (const std::exception&) {
return false; return false;
} }
+3 -3
View File
@@ -503,7 +503,7 @@ btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t ra
return nullptr; return nullptr;
} }
try { 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)); return btck_Transaction::create(std::make_shared<const CTransaction>(deserialize, TX_WITH_WITNESS, stream));
} catch (...) { } catch (...) {
return nullptr; return nullptr;
@@ -1092,7 +1092,7 @@ btck_Block* btck_block_create(const void* raw_block, size_t raw_block_length)
} }
auto block{std::make_shared<CBlock>()}; 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 { try {
stream >> TX_WITH_WITNESS(*block); stream >> TX_WITH_WITNESS(*block);
@@ -1343,7 +1343,7 @@ btck_BlockHeader* btck_block_header_create(const void* raw_block_header, size_t
return nullptr; return nullptr;
} }
auto header{std::make_unique<CBlockHeader>()}; 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 { try {
stream >> *header; stream >> *header;
+2 -2
View File
@@ -201,8 +201,8 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
const auto one_week{7 * 24h}; const auto one_week{7 * 24h};
std::vector<CAddress> vSeedsOut; std::vector<CAddress> vSeedsOut;
FastRandomContext rng; FastRandomContext rng;
ParamsStream s{DataStream{vSeedsIn}, CAddress::V2_NETWORK}; ParamsStream s{SpanReader{vSeedsIn}, CAddress::V2_NETWORK};
while (!s.eof()) { while (!s.empty()) {
CService endpoint; CService endpoint;
s >> endpoint; s >> endpoint;
CAddress addr{endpoint, SeedsServiceFlags()}; CAddress addr{endpoint, SeedsServiceFlags()};
+1 -1
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) 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 { try {
ss_data >> psbt; ss_data >> psbt;
if (!ss_data.empty()) { if (!ss_data.empty()) {
+1 -2
View File
@@ -189,8 +189,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
// called from ctor when loading from wallet // called from ctor when loading from wallet
void RecentRequestsTableModel::addNewRequest(const std::string &recipient) void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
{ {
std::vector<uint8_t> data(recipient.begin(), recipient.end()); SpanReader ss{MakeByteSpan(recipient)};
DataStream ss{data};
RecentRequestEntry entry; RecentRequestEntry entry;
ss >> entry; ss >> entry;
+1 -1
View File
@@ -364,7 +364,7 @@ void TestGUI(interfaces::Node& node, const std::shared_ptr<CWallet>& wallet)
std::vector<std::string> requests = walletModel.wallet().getAddressReceiveRequests(); std::vector<std::string> requests = walletModel.wallet().getAddressReceiveRequests();
QCOMPARE(requests.size(), size_t{1}); QCOMPARE(requests.size(), size_t{1});
RecentRequestEntry entry; RecentRequestEntry entry;
DataStream{MakeUCharSpan(requests[0])} >> entry; SpanReader{MakeByteSpan(requests[0])} >> entry;
QCOMPARE(entry.nVersion, int{1}); QCOMPARE(entry.nVersion, int{1});
QCOMPARE(entry.id, int64_t{1}); QCOMPARE(entry.id, int64_t{1});
QVERIFY(entry.date.isValid()); QVERIFY(entry.date.isValid());
+1 -2
View File
@@ -451,8 +451,7 @@ static bool rest_block(const std::any& context,
case RESTResponseFormat::JSON: { case RESTResponseFormat::JSON: {
if (tx_verbosity) { if (tx_verbosity) {
CBlock block{}; CBlock block{};
DataStream block_stream{*block_data}; SpanReader{*block_data} >> TX_WITH_WITNESS(block);
block_stream >> TX_WITH_WITNESS(block);
UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, *tx_verbosity, chainman.GetConsensus().powLimit); UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, *tx_verbosity, chainman.GetConsensus().powLimit);
std::string strJSON = objBlock.write() + "\n"; std::string strJSON = objBlock.write() + "\n";
req->WriteHeader("Content-Type", "application/json"); req->WriteHeader("Content-Type", "application/json");
+1 -2
View File
@@ -832,9 +832,8 @@ static RPCHelpMan getblock()
return HexStr(block_data); return HexStr(block_data);
} }
DataStream block_stream{block_data};
CBlock block{}; CBlock block{};
block_stream >> TX_WITH_WITNESS(block); SpanReader{block_data} >> TX_WITH_WITNESS(block);
TxVerbosity tx_verbosity; TxVerbosity tx_verbosity;
if (verbosity == 1) { if (verbosity == 1) {
+1 -2
View File
@@ -144,9 +144,8 @@ static RPCHelpMan verifytxoutproof()
RPCExamples{""}, RPCExamples{""},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
DataStream ssMB{ParseHexV(request.params[0], "proof")};
CMerkleBlock merkleBlock; CMerkleBlock merkleBlock;
ssMB >> merkleBlock; SpanReader{ParseHexV(request.params[0], "proof")} >> merkleBlock;
UniValue res(UniValue::VARR); UniValue res(UniValue::VARR);
+1 -1
View File
@@ -1127,7 +1127,7 @@ public:
void write(std::span<const std::byte> src) { GetStream().write(src); } void write(std::span<const std::byte> src) { GetStream().write(src); }
void read(std::span<std::byte> dst) { GetStream().read(dst); } void read(std::span<std::byte> dst) { GetStream().read(dst); }
void ignore(size_t num) { GetStream().ignore(num); } void ignore(size_t num) { GetStream().ignore(num); }
bool eof() const { return GetStream().eof(); } bool empty() const { return GetStream().empty(); }
size_t size() const { return GetStream().size(); } size_t size() const { return GetStream().size(); }
//! Get reference to stream parameters. //! Get reference to stream parameters.
+3 -1
View File
@@ -117,6 +117,9 @@ public:
void ignore(size_t n) void ignore(size_t n)
{ {
if (n > m_data.size()) {
throw std::ios_base::failure("SpanReader::ignore(): end of data");
}
m_data = m_data.subspan(n); m_data = m_data.subspan(n);
} }
}; };
@@ -195,7 +198,6 @@ public:
// //
// Stream subset // Stream subset
// //
bool eof() const { return size() == 0; }
int in_avail() const { return size(); } int in_avail() const { return size(); }
void read(std::span<value_type> dst) void read(std::span<value_type> dst)
+1 -1
View File
@@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE(bloom_match)
// and one which spends it (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) // 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, 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}; 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); std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
DataStream spendStream{vch}; SpanReader spendStream{vch};
CTransaction spendingTx(deserialize, TX_WITH_WITNESS, spendStream); CTransaction spendingTx(deserialize, TX_WITH_WITNESS, spendStream);
CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL); CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL);
+5 -10
View File
@@ -517,37 +517,33 @@ BOOST_FIXTURE_TEST_CASE(updatecoins_simulation_test, UpdateTest)
BOOST_AUTO_TEST_CASE(ccoins_serialization) BOOST_AUTO_TEST_CASE(ccoins_serialization)
{ {
// Good example // Good example
DataStream ss1{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
Coin cc1; Coin cc1;
ss1 >> cc1; SpanReader{"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex} >> cc1;
BOOST_CHECK_EQUAL(cc1.fCoinBase, false); BOOST_CHECK_EQUAL(cc1.fCoinBase, false);
BOOST_CHECK_EQUAL(cc1.nHeight, 203998U); BOOST_CHECK_EQUAL(cc1.nHeight, 203998U);
BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000}); BOOST_CHECK_EQUAL(cc1.out.nValue, CAmount{60000000000});
BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8))))); BOOST_CHECK_EQUAL(HexStr(cc1.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("816115944e077fe7c803cfa57f29b36bf87c1d35"_hex_u8)))));
// Good example // Good example
DataStream ss2{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
Coin cc2; Coin cc2;
ss2 >> cc2; SpanReader{"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex} >> cc2;
BOOST_CHECK_EQUAL(cc2.fCoinBase, true); BOOST_CHECK_EQUAL(cc2.fCoinBase, true);
BOOST_CHECK_EQUAL(cc2.nHeight, 120891U); BOOST_CHECK_EQUAL(cc2.nHeight, 120891U);
BOOST_CHECK_EQUAL(cc2.out.nValue, 110397); BOOST_CHECK_EQUAL(cc2.out.nValue, 110397);
BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8))))); BOOST_CHECK_EQUAL(HexStr(cc2.out.scriptPubKey), HexStr(GetScriptForDestination(PKHash(uint160("8c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex_u8)))));
// Smallest possible example // Smallest possible example
DataStream ss3{"000006"_hex};
Coin cc3; Coin cc3;
ss3 >> cc3; SpanReader{"000006"_hex} >> cc3;
BOOST_CHECK_EQUAL(cc3.fCoinBase, false); BOOST_CHECK_EQUAL(cc3.fCoinBase, false);
BOOST_CHECK_EQUAL(cc3.nHeight, 0U); BOOST_CHECK_EQUAL(cc3.nHeight, 0U);
BOOST_CHECK_EQUAL(cc3.out.nValue, 0); BOOST_CHECK_EQUAL(cc3.out.nValue, 0);
BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U); BOOST_CHECK_EQUAL(cc3.out.scriptPubKey.size(), 0U);
// scriptPubKey that ends beyond the end of the stream // scriptPubKey that ends beyond the end of the stream
DataStream ss4{"000007"_hex};
try { try {
Coin cc4; Coin cc4;
ss4 >> cc4; SpanReader{"000007"_hex} >> cc4;
BOOST_CHECK_MESSAGE(false, "We should have thrown"); BOOST_CHECK_MESSAGE(false, "We should have thrown");
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
} }
@@ -557,10 +553,9 @@ BOOST_AUTO_TEST_CASE(ccoins_serialization)
uint64_t x = 3000000000ULL; uint64_t x = 3000000000ULL;
tmp << VARINT(x); tmp << VARINT(x);
BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00"); BOOST_CHECK_EQUAL(HexStr(tmp), "8a95c0bb00");
DataStream ss5{"00008a95c0bb00"_hex};
try { try {
Coin cc5; Coin cc5;
ss5 >> cc5; SpanReader{"00008a95c0bb00"_hex} >> cc5;
BOOST_CHECK_MESSAGE(false, "We should have thrown"); BOOST_CHECK_MESSAGE(false, "We should have thrown");
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
} }
+1 -1
View File
@@ -364,7 +364,7 @@ struct StringContentsSerializer {
{ {
str.clear(); str.clear();
uint8_t c{0}; uint8_t c{0};
while (!s.eof()) { while (!s.empty()) {
s >> c; s >> c;
str.push_back(c); str.push_back(c);
} }
+1 -2
View File
@@ -24,10 +24,9 @@ void initialize_block()
FUZZ_TARGET(block, .init = initialize_block) FUZZ_TARGET(block, .init = initialize_block)
{ {
DataStream ds{buffer};
CBlock block; CBlock block;
try { try {
ds >> TX_WITH_WITNESS(block); SpanReader{buffer} >> TX_WITH_WITNESS(block);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
return; return;
} }
+2 -4
View File
@@ -81,9 +81,8 @@ T Deserialize(DataStream&& ds, const P& params)
template <typename T, typename P> template <typename T, typename P>
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const P& params) void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj, const P& params)
{ {
DataStream ds{buffer};
try { try {
ds >> params(obj); SpanReader{buffer} >> params(obj);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
throw invalid_fuzzing_input_exception(); throw invalid_fuzzing_input_exception();
} }
@@ -109,9 +108,8 @@ T Deserialize(DataStream ds)
template <typename T> template <typename T>
void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj) void DeserializeFromFuzzingInput(FuzzBufferType buffer, T&& obj)
{ {
DataStream ds{buffer};
try { try {
ds >> obj; SpanReader{buffer} >> obj;
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
throw invalid_fuzzing_input_exception(); throw invalid_fuzzing_input_exception();
} }
+2 -2
View File
@@ -15,7 +15,7 @@
#include <utility> #include <utility>
#include <vector> #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}; script_verify_flags::value_type n{0};
ds >> n; ds >> n;
@@ -27,7 +27,7 @@ static DataStream& operator>>(DataStream& ds, script_verify_flags& f)
FUZZ_TARGET(script_flags) FUZZ_TARGET(script_flags)
{ {
if (buffer.size() > 100'000) return; if (buffer.size() > 100'000) return;
DataStream ds{buffer}; SpanReader ds{buffer};
try { try {
const CTransaction tx(deserialize, TX_WITH_WITNESS, ds); const CTransaction tx(deserialize, TX_WITH_WITNESS, ds);
+2 -3
View File
@@ -30,7 +30,7 @@ void initialize_transaction()
FUZZ_TARGET(transaction, .init = initialize_transaction) FUZZ_TARGET(transaction, .init = initialize_transaction)
{ {
SeedRandomStateForTest(SeedRand::ZEROS); SeedRandomStateForTest(SeedRand::ZEROS);
DataStream ds{buffer}; SpanReader ds{buffer};
bool valid_tx = true; bool valid_tx = true;
const CTransaction tx = [&] { const CTransaction tx = [&] {
try { try {
@@ -41,10 +41,9 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
} }
}(); }();
bool valid_mutable_tx = true; bool valid_mutable_tx = true;
DataStream ds_mtx{buffer};
CMutableTransaction mutable_tx; CMutableTransaction mutable_tx;
try { try {
ds_mtx >> TX_WITH_WITNESS(mutable_tx); SpanReader{buffer} >> TX_WITH_WITNESS(mutable_tx);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
valid_mutable_tx = false; valid_mutable_tx = false;
} }
+1 -2
View File
@@ -13,10 +13,9 @@
FUZZ_TARGET(tx_in) FUZZ_TARGET(tx_in)
{ {
DataStream ds{buffer};
CTxIn tx_in; CTxIn tx_in;
try { try {
ds >> tx_in; SpanReader{buffer} >> tx_in;
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
return; return;
} }
+1 -2
View File
@@ -12,10 +12,9 @@
FUZZ_TARGET(tx_out) FUZZ_TARGET(tx_out)
{ {
DataStream ds{buffer};
CTxOut tx_out; CTxOut tx_out;
try { try {
ds >> tx_out; SpanReader{buffer} >> tx_out;
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
return; return;
} }
+2 -2
View File
@@ -100,7 +100,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 [[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)}; const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
DataStream ds{buffer}; SpanReader ds{buffer};
T obj; T obj;
try { try {
ds >> params(obj); ds >> params(obj);
@@ -114,7 +114,7 @@ template <typename T>
[[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept [[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); const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
DataStream ds{buffer}; SpanReader ds{buffer};
T obj; T obj;
try { try {
ds >> obj; ds >> obj;
+1 -2
View File
@@ -876,10 +876,9 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
std::span<const unsigned char> data, std::span<const unsigned char> data,
bool is_incoming) -> void { bool is_incoming) -> void {
if (!is_incoming && msg_type == "addr") { if (!is_incoming && msg_type == "addr") {
DataStream s{data};
std::vector<CAddress> addresses; std::vector<CAddress> addresses;
s >> CAddress::V1_NETWORK(addresses); SpanReader{data} >> CAddress::V1_NETWORK(addresses);
for (const auto& addr : addresses) { for (const auto& addr : addresses) {
if (addr == expected) { if (addr == expected) {
+2 -4
View File
@@ -575,10 +575,9 @@ BOOST_AUTO_TEST_CASE(caddress_serialize_v1)
BOOST_AUTO_TEST_CASE(caddress_unserialize_v1) BOOST_AUTO_TEST_CASE(caddress_unserialize_v1)
{ {
DataStream s{ParseHex(stream_addrv1_hex)};
std::vector<CAddress> addresses_unserialized; 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); BOOST_CHECK(fixture_addresses == addresses_unserialized);
} }
@@ -592,10 +591,9 @@ BOOST_AUTO_TEST_CASE(caddress_serialize_v2)
BOOST_AUTO_TEST_CASE(caddress_unserialize_v2) BOOST_AUTO_TEST_CASE(caddress_unserialize_v2)
{ {
DataStream s{ParseHex(stream_addrv2_hex)};
std::vector<CAddress> addresses_unserialized; 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); BOOST_CHECK(fixture_addresses == addresses_unserialized);
} }
+1 -2
View File
@@ -189,8 +189,7 @@ BOOST_AUTO_TEST_CASE(sighash_from_data)
nHashType = test[3].getInt<int>(); nHashType = test[3].getInt<int>();
sigHashHex = test[4].get_str(); sigHashHex = test[4].get_str();
DataStream stream(ParseHex(raw_tx)); SpanReader{ParseHex(raw_tx)} >> TX_WITH_WITNESS(tx);
stream >> TX_WITH_WITNESS(tx);
TxValidationState state; TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest); BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
+1 -2
View File
@@ -377,9 +377,8 @@ BOOST_AUTO_TEST_CASE(basic_transaction_tests)
// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) // 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}; 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); std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
DataStream stream(vch);
CMutableTransaction tx; CMutableTransaction tx;
stream >> TX_WITH_WITNESS(tx); SpanReader{vch} >> TX_WITH_WITNESS(tx);
TxValidationState state; TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid."); BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
+1 -2
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."); 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; 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 //Search partial merkle tree in proof for our transaction and index in valid block
std::vector<Txid> vMatch; std::vector<Txid> vMatch;
+1 -1
View File
@@ -377,7 +377,7 @@ bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, st
// Get the checksum and check it // Get the checksum and check it
bool checksum_valid = false; bool checksum_valid = false;
if (!ssValue.eof()) { if (!ssValue.empty()) {
uint256 checksum; uint256 checksum;
ssValue >> checksum; ssValue >> checksum;
if (!(checksum_valid = Hash(vchPrivKey) == checksum)) { if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
+2 -2
View File
@@ -1198,8 +1198,8 @@ class SegWitTest(BitcoinTestFramework):
block.vtx = [block.vtx[0]] block.vtx = [block.vtx[0]]
self.update_witness_block_with_transactions(block, [tx2]) self.update_witness_block_with_transactions(block, [tx2])
# This block doesn't result in a specific reject reason, but an iostream exception: # This block doesn't result in a specific reject reason, but an iostream exception:
# "Exception 'CDataStream::read(): end of data: unspecified iostream_category error' (...) caught" with self.nodes[0].assert_debug_log(["Exception 'DataStream::read(): end of data"]):
test_witness_block(self.nodes[0], self.test_node, block, accepted=False) test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
# Now make one of the intermediate witnesses be incorrect # Now make one of the intermediate witnesses be incorrect
tx2.wit.vtxinwit.append(CTxInWitness()) tx2.wit.vtxinwit.append(CTxInWitness())