Skip to content

Commit b7e6330

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23687: Remove unused (and broken) functionality in SpanReader
31ba1af Remove unused (and broken) functionality in SpanReader (Pieter Wuille) Pull request description: This removes the ability to set an offset in the `SpanReader::SpanReader` constructor, as the current code is broken since #23653. All call sites use `pos=0`, so it is actually unused. If future call sites need it, `SpanReader{a, b, c, d}` is equivalent to `SpanReader{a, b, c.subspan(d)}`. It also removes the ability to deserialize from `SpanReader` directly from the constructor. This too is unused, and can be more idiomatically simulated using `(SpanReader{a, b, c} >> x >> y >> z)` instead of `SpanReader{a, b, c, x, y, z}`. This was pointed out by achow101 in bitcoin/bitcoin#23653 (comment). ACKs for top commit: jb55: crACK 31ba1af achow101: ACK 31ba1af Tree-SHA512: 700ebcd74147628488c39168dbf3a00f8ed41709a26711695f4bf036250a9b115574923bbf96040ec7b7fee4132d6dbbcb5c6e5a2977c4beb521dc1500e6ed53
2 parents 9a53ba4 + 31ba1af commit b7e6330

File tree

8 files changed

+16
-35
lines changed

8 files changed

+16
-35
lines changed

src/blockfilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ GCSFilter::GCSFilter(const Params& params)
8181
GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter)
8282
: m_params(params), m_encoded(std::move(encoded_filter))
8383
{
84-
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0};
84+
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded};
8585

8686
uint64_t N = ReadCompactSize(stream);
8787
m_N = static_cast<uint32_t>(N);
@@ -133,7 +133,7 @@ GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
133133

134134
bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
135135
{
136-
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0};
136+
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded};
137137

138138
// Seek forward by size of N
139139
uint64_t N = ReadCompactSize(stream);

src/signet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& c
9898
// no signet solution -- allow this to support OP_TRUE as trivial block challenge
9999
} else {
100100
try {
101-
SpanReader v{SER_NETWORK, INIT_PROTO_VERSION, signet_solution, 0};
101+
SpanReader v{SER_NETWORK, INIT_PROTO_VERSION, signet_solution};
102102
v >> tx_spending.vin[0].scriptSig;
103103
v >> tx_spending.vin[0].scriptWitness.stack;
104104
if (!v.empty()) return std::nullopt; // extraneous data encountered

src/streams.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,9 @@ class SpanReader
143143
* @param[in] type Serialization Type
144144
* @param[in] version Serialization Version (including any flags)
145145
* @param[in] data Referenced byte vector to overwrite/append
146-
* @param[in] pos Starting position. Vector index where reads should start.
147146
*/
148-
SpanReader(int type, int version, Span<const unsigned char> data, size_t pos)
149-
: m_type(type), m_version(version), m_data(data)
150-
{
151-
if (pos > m_data.size()) {
152-
throw std::ios_base::failure("SpanReader(...): end of data (pos > m_data.size())");
153-
}
154-
data = data.subspan(pos);
155-
}
156-
157-
/**
158-
* (other params same as above)
159-
* @param[in] args A list of items to deserialize starting at pos.
160-
*/
161-
template <typename... Args>
162-
SpanReader(int type, int version, Span<const unsigned char> data, size_t pos,
163-
Args&&... args)
164-
: SpanReader(type, version, data, pos)
165-
{
166-
::UnserializeMany(*this, std::forward<Args>(args)...);
167-
}
147+
SpanReader(int type, int version, Span<const unsigned char> data)
148+
: m_type(type), m_version(version), m_data(data) {}
168149

169150
template<typename T>
170151
SpanReader& operator>>(T&& obj)

src/test/fuzz/golomb_rice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ FUZZ_TARGET(golomb_rice)
8282

8383
std::vector<uint64_t> decoded_deltas;
8484
{
85-
SpanReader stream{SER_NETWORK, 0, golomb_rice_data, 0};
85+
SpanReader stream{SER_NETWORK, 0, golomb_rice_data};
8686
BitStreamReader<SpanReader> bitreader{stream};
8787
const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
8888
for (uint32_t i = 0; i < n; ++i) {
@@ -94,7 +94,7 @@ FUZZ_TARGET(golomb_rice)
9494

9595
{
9696
const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);
97-
SpanReader stream{SER_NETWORK, 0, random_bytes, 0};
97+
SpanReader stream{SER_NETWORK, 0, random_bytes};
9898
uint32_t n;
9999
try {
100100
n = static_cast<uint32_t>(ReadCompactSize(stream));

src/test/fuzz/script_assets_test_minimizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CMutableTransaction TxFromHex(const std::string& str)
5454
{
5555
CMutableTransaction tx;
5656
try {
57-
SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, CheckedParseHex(str), 0} >> tx;
57+
SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, CheckedParseHex(str)} >> tx;
5858
} catch (const std::ios_base::failure&) {
5959
throw std::runtime_error("Tx deserialization failure");
6060
}
@@ -68,7 +68,7 @@ std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
6868
for (size_t i = 0; i < univalue.size(); ++i) {
6969
CTxOut txout;
7070
try {
71-
SpanReader{SER_DISK, 0, CheckedParseHex(univalue[i].get_str()), 0} >> txout;
71+
SpanReader{SER_DISK, 0, CheckedParseHex(univalue[i].get_str())} >> txout;
7272
} catch (const std::ios_base::failure&) {
7373
throw std::runtime_error("Prevout invalid format");
7474
}

src/test/script_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
14731473
static CMutableTransaction TxFromHex(const std::string& str)
14741474
{
14751475
CMutableTransaction tx;
1476-
SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str), 0} >> tx;
1476+
SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str)} >> tx;
14771477
return tx;
14781478
}
14791479

@@ -1483,7 +1483,7 @@ static std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
14831483
std::vector<CTxOut> prevouts;
14841484
for (size_t i = 0; i < univalue.size(); ++i) {
14851485
CTxOut txout;
1486-
SpanReader{SER_DISK, 0, ParseHex(univalue[i].get_str()), 0} >> txout;
1486+
SpanReader{SER_DISK, 0, ParseHex(univalue[i].get_str())} >> txout;
14871487
prevouts.push_back(std::move(txout));
14881488
}
14891489
return prevouts;
@@ -1754,7 +1754,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
17541754
for (const auto& vec : vectors.getValues()) {
17551755
auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
17561756
CMutableTransaction tx;
1757-
SpanReader{SER_NETWORK, PROTOCOL_VERSION, txhex, 0} >> tx;
1757+
SpanReader{SER_NETWORK, PROTOCOL_VERSION, txhex} >> tx;
17581758
std::vector<CTxOut> utxos;
17591759
for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
17601760
auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());

src/test/streams_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
7171
{
7272
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
7373

74-
SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, vch, 0};
74+
SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, vch};
7575
BOOST_CHECK_EQUAL(reader.size(), 6U);
7676
BOOST_CHECK(!reader.empty());
7777

@@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
101101
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
102102

103103
// Read a 4 bytes as a signed int from the beginning of the buffer.
104-
SpanReader new_reader{SER_NETWORK, INIT_PROTO_VERSION, vch, 0};
104+
SpanReader new_reader{SER_NETWORK, INIT_PROTO_VERSION, vch};
105105
new_reader >> d;
106106
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
107107
BOOST_CHECK_EQUAL(new_reader.size(), 2U);
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
115115
BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
116116
{
117117
std::vector<uint8_t> data{0x82, 0xa7, 0x31};
118-
SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, data, /* pos= */ 0};
118+
SpanReader reader{SER_NETWORK, INIT_PROTO_VERSION, data};
119119
uint32_t varint = 0;
120120
// Deserialize into r-value
121121
reader >> VARINT(varint);

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
682682
vw << (int32_t)0;
683683
vw << (int32_t)1;
684684

685-
SpanReader vr{0, 0, malformed_record, 0};
685+
SpanReader vr{0, 0, malformed_record};
686686
WalletDescriptor w_desc;
687687
BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
688688
}

0 commit comments

Comments
 (0)