Skip to content

Commit fa8bdb0

Browse files
author
MarcoFalke
committed
refactor: Drop CDataStream constructors in favor of one taking a Span of bytes
1 parent faa96f8 commit fa8bdb0

File tree

6 files changed

+15
-33
lines changed

6 files changed

+15
-33
lines changed

src/dbwrapper.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
#include <clientversion.h>
99
#include <fs.h>
1010
#include <serialize.h>
11+
#include <span.h>
1112
#include <streams.h>
12-
#include <util/system.h>
1313
#include <util/strencodings.h>
14+
#include <util/system.h>
1415

1516
#include <leveldb/db.h>
1617
#include <leveldb/write_batch.h>
@@ -146,7 +147,7 @@ class CDBIterator
146147
template<typename K> bool GetKey(K& key) {
147148
leveldb::Slice slKey = piter->key();
148149
try {
149-
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
150+
CDataStream ssKey(MakeUCharSpan(slKey), SER_DISK, CLIENT_VERSION);
150151
ssKey >> key;
151152
} catch (const std::exception&) {
152153
return false;
@@ -157,7 +158,7 @@ class CDBIterator
157158
template<typename V> bool GetValue(V& value) {
158159
leveldb::Slice slValue = piter->value();
159160
try {
160-
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
161+
CDataStream ssValue(MakeUCharSpan(slValue), SER_DISK, CLIENT_VERSION);
161162
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
162163
ssValue >> value;
163164
} catch (const std::exception&) {
@@ -243,7 +244,7 @@ class CDBWrapper
243244
dbwrapper_private::HandleError(status);
244245
}
245246
try {
246-
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION);
247+
CDataStream ssValue(MakeUCharSpan(strValue), SER_DISK, CLIENT_VERSION);
247248
ssValue.Xor(obfuscate_key);
248249
ssValue >> value;
249250
} catch (const std::exception&) {
@@ -333,7 +334,6 @@ class CDBWrapper
333334
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
334335
pdb->CompactRange(&slKey1, &slKey2);
335336
}
336-
337337
};
338338

339339
#endif // BITCOIN_DBWRAPPER_H

src/psbt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6
360360

361361
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error)
362362
{
363-
CDataStream ss_data(tx_data.data(), tx_data.data() + tx_data.size(), SER_NETWORK, PROTOCOL_VERSION);
363+
CDataStream ss_data(MakeUCharSpan(tx_data), SER_NETWORK, PROTOCOL_VERSION);
364364
try {
365365
ss_data >> psbt;
366366
if (!ss_data.empty()) {

src/qt/recentrequeststablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
179179
// called from ctor when loading from wallet
180180
void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
181181
{
182-
std::vector<char> data(recipient.begin(), recipient.end());
182+
std::vector<uint8_t> data(recipient.begin(), recipient.end());
183183
CDataStream ss(data, SER_DISK, CLIENT_VERSION);
184184

185185
RecentRequestEntry entry;

src/streams.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
#ifndef BITCOIN_STREAMS_H
77
#define BITCOIN_STREAMS_H
88

9-
#include <support/allocators/zeroafterfree.h>
109
#include <serialize.h>
10+
#include <span.h>
11+
#include <support/allocators/zeroafterfree.h>
1112

1213
#include <algorithm>
1314
#include <assert.h>
1415
#include <ios>
1516
#include <limits>
1617
#include <stdint.h>
1718
#include <stdio.h>
18-
#include <string>
1919
#include <string.h>
20+
#include <string>
2021
#include <utility>
2122
#include <vector>
2223

@@ -225,27 +226,8 @@ class CDataStream
225226
Init(nTypeIn, nVersionIn);
226227
}
227228

228-
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
229-
{
230-
Init(nTypeIn, nVersionIn);
231-
}
232-
233-
CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
234-
{
235-
Init(nTypeIn, nVersionIn);
236-
}
237-
238-
CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
239-
{
240-
Init(nTypeIn, nVersionIn);
241-
}
242-
243-
CDataStream(const std::vector<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
244-
{
245-
Init(nTypeIn, nVersionIn);
246-
}
247-
248-
CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
229+
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
230+
: vch(sp.data(), sp.data() + sp.size())
249231
{
250232
Init(nTypeIn, nVersionIn);
251233
}
@@ -289,7 +271,7 @@ class CDataStream
289271
value_type* data() { return vch.data() + nReadPos; }
290272
const value_type* data() const { return vch.data() + nReadPos; }
291273

292-
void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
274+
void insert(iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
293275
{
294276
if (last == first) return;
295277
assert(last - first > 0);

src/test/fuzz/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ NODISCARD inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvid
4848

4949
NODISCARD inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
5050
{
51-
return {ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION};
51+
return CDataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION};
5252
}
5353

5454
NODISCARD inline std::vector<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept

src/test/streams_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
149149

150150
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
151151
{
152-
std::vector<char> in;
152+
std::vector<uint8_t> in;
153153
std::vector<char> expected_xor;
154154
std::vector<unsigned char> key;
155155
CDataStream ds(in, 0, 0);

0 commit comments

Comments
 (0)