Skip to content

Commit 5e8e0b3

Browse files
committed
Merge bitcoin/bitcoin#24253: Remove broken and unused CDataStream methods
fa1b227 Remove broken and unused CDataStream methods (MarcoFalke) faee5f8 test: Create fresh CDataStream each time (MarcoFalke) fa71114 test: Inline expected_xor (MarcoFalke) Pull request description: The `insert` and `erase` methods have many issues: * They are unused * They are confusing and hard to read, as they implement "special cases" for optimization, that isn't needed * They are broken (See bitcoin/bitcoin#24231) * Fixing them leads to mingw compile errors (See bitcoin/bitcoin#24231 (comment)) Fix all issues by removing them ACKs for top commit: laanwj: Code review ACK fa1b227 Tree-SHA512: 9d9e5d42e6ffc5ae82bdb67cfb5b50b45977ae674acee6ff99092560aebf2fc7e4584ded614e190db0663226fa198e34350517cd7ee57d518de22e9568bc349a
2 parents 6ac637f + fa1b227 commit 5e8e0b3

File tree

3 files changed

+17
-138
lines changed

3 files changed

+17
-138
lines changed

src/streams.h

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -240,76 +240,9 @@ class CDataStream
240240
const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
241241
reference operator[](size_type pos) { return vch[pos + nReadPos]; }
242242
void clear() { vch.clear(); nReadPos = 0; }
243-
iterator insert(iterator it, const value_type x) { return vch.insert(it, x); }
244-
void insert(iterator it, size_type n, const value_type x) { vch.insert(it, n, x); }
245243
value_type* data() { return vch.data() + nReadPos; }
246244
const value_type* data() const { return vch.data() + nReadPos; }
247245

248-
void insert(iterator it, std::vector<value_type>::const_iterator first, std::vector<value_type>::const_iterator last)
249-
{
250-
if (last == first) return;
251-
assert(last - first > 0);
252-
if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
253-
{
254-
// special case for inserting at the front when there's room
255-
nReadPos -= (last - first);
256-
memcpy(&vch[nReadPos], &first[0], last - first);
257-
}
258-
else
259-
vch.insert(it, first, last);
260-
}
261-
262-
void insert(iterator it, const value_type* first, const value_type* last)
263-
{
264-
if (last == first) return;
265-
assert(last - first > 0);
266-
if (it == vch.begin() + nReadPos && (unsigned int)(last - first) <= nReadPos)
267-
{
268-
// special case for inserting at the front when there's room
269-
nReadPos -= (last - first);
270-
memcpy(&vch[nReadPos], &first[0], last - first);
271-
}
272-
else
273-
vch.insert(it, first, last);
274-
}
275-
276-
iterator erase(iterator it)
277-
{
278-
if (it == vch.begin() + nReadPos)
279-
{
280-
// special case for erasing from the front
281-
if (++nReadPos >= vch.size())
282-
{
283-
// whenever we reach the end, we take the opportunity to clear the buffer
284-
nReadPos = 0;
285-
return vch.erase(vch.begin(), vch.end());
286-
}
287-
return vch.begin() + nReadPos;
288-
}
289-
else
290-
return vch.erase(it);
291-
}
292-
293-
iterator erase(iterator first, iterator last)
294-
{
295-
if (first == vch.begin() + nReadPos)
296-
{
297-
// special case for erasing from the front
298-
if (last == vch.end())
299-
{
300-
nReadPos = 0;
301-
return vch.erase(vch.begin(), vch.end());
302-
}
303-
else
304-
{
305-
nReadPos = (last - vch.begin());
306-
return last;
307-
}
308-
}
309-
else
310-
return vch.erase(first, last);
311-
}
312-
313246
inline void Compact()
314247
{
315248
vch.erase(vch.begin(), vch.begin() + nReadPos);

src/test/serialize_tests.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -215,51 +215,6 @@ BOOST_AUTO_TEST_CASE(noncanonical)
215215
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
216216
}
217217

218-
BOOST_AUTO_TEST_CASE(insert_delete)
219-
{
220-
constexpr auto B2I{[](std::byte b) { return std::to_integer<uint8_t>(b); }};
221-
222-
// Test inserting/deleting bytes.
223-
CDataStream ss(SER_DISK, 0);
224-
BOOST_CHECK_EQUAL(ss.size(), 0U);
225-
226-
ss.write(MakeByteSpan("\x00\x01\x02\xff").first(4));
227-
BOOST_CHECK_EQUAL(ss.size(), 4U);
228-
229-
uint8_t c{11};
230-
231-
// Inserting at beginning/end/middle:
232-
ss.insert(ss.begin(), std::byte{c});
233-
BOOST_CHECK_EQUAL(ss.size(), 5U);
234-
BOOST_CHECK_EQUAL(B2I(ss[0]), c);
235-
BOOST_CHECK_EQUAL(B2I(ss[1]), 0);
236-
237-
ss.insert(ss.end(), std::byte{c});
238-
BOOST_CHECK_EQUAL(ss.size(), 6U);
239-
BOOST_CHECK_EQUAL(B2I(ss[4]), 0xff);
240-
BOOST_CHECK_EQUAL(B2I(ss[5]), c);
241-
242-
ss.insert(ss.begin() + 2, std::byte{c});
243-
BOOST_CHECK_EQUAL(ss.size(), 7U);
244-
BOOST_CHECK_EQUAL(B2I(ss[2]), c);
245-
246-
// Delete at beginning/end/middle
247-
ss.erase(ss.begin());
248-
BOOST_CHECK_EQUAL(ss.size(), 6U);
249-
BOOST_CHECK_EQUAL(B2I(ss[0]), 0);
250-
251-
ss.erase(ss.begin()+ss.size()-1);
252-
BOOST_CHECK_EQUAL(ss.size(), 5U);
253-
BOOST_CHECK_EQUAL(B2I(ss[4]), 0xff);
254-
255-
ss.erase(ss.begin()+1);
256-
BOOST_CHECK_EQUAL(ss.size(), 4U);
257-
BOOST_CHECK_EQUAL(B2I(ss[0]), 0);
258-
BOOST_CHECK_EQUAL(B2I(ss[1]), 1);
259-
BOOST_CHECK_EQUAL(B2I(ss[2]), 2);
260-
BOOST_CHECK_EQUAL(B2I(ss[3]), 0xff);
261-
}
262-
263218
BOOST_AUTO_TEST_CASE(class_methods)
264219
{
265220
int intval(100);

src/test/streams_tests.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <boost/test/unit_test.hpp>
1010

11+
using namespace std::string_literals;
12+
1113
BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
1214

1315
BOOST_AUTO_TEST_CASE(streams_vector_writer)
@@ -162,46 +164,35 @@ BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
162164
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
163165
{
164166
std::vector<std::byte> in;
165-
std::vector<char> expected_xor;
166-
CDataStream ds(in, 0, 0);
167167

168168
// Degenerate case
169-
ds.Xor({0x00, 0x00});
170-
BOOST_CHECK_EQUAL(
171-
std::string(expected_xor.begin(), expected_xor.end()),
172-
ds.str());
169+
{
170+
CDataStream ds{in, 0, 0};
171+
ds.Xor({0x00, 0x00});
172+
BOOST_CHECK_EQUAL(""s, ds.str());
173+
}
173174

174175
in.push_back(std::byte{0x0f});
175176
in.push_back(std::byte{0xf0});
176-
expected_xor.push_back('\xf0');
177-
expected_xor.push_back('\x0f');
178177

179178
// Single character key
180-
181-
ds.clear();
182-
ds.insert(ds.begin(), in.begin(), in.end());
183-
184-
ds.Xor({0xff});
185-
BOOST_CHECK_EQUAL(
186-
std::string(expected_xor.begin(), expected_xor.end()),
187-
ds.str());
179+
{
180+
CDataStream ds{in, 0, 0};
181+
ds.Xor({0xff});
182+
BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
183+
}
188184

189185
// Multi character key
190186

191187
in.clear();
192-
expected_xor.clear();
193188
in.push_back(std::byte{0xf0});
194189
in.push_back(std::byte{0x0f});
195-
expected_xor.push_back('\x0f');
196-
expected_xor.push_back('\x00');
197-
198-
ds.clear();
199-
ds.insert(ds.begin(), in.begin(), in.end());
200190

201-
ds.Xor({0xff, 0x0f});
202-
BOOST_CHECK_EQUAL(
203-
std::string(expected_xor.begin(), expected_xor.end()),
204-
ds.str());
191+
{
192+
CDataStream ds{in, 0, 0};
193+
ds.Xor({0xff, 0x0f});
194+
BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
195+
}
205196
}
206197

207198
BOOST_AUTO_TEST_CASE(streams_buffered_file)

0 commit comments

Comments
 (0)