Skip to content

Commit 2c0fc85

Browse files
committed
Merge bitcoin#20464: refactor: Treat CDataStream bytes as uint8_t
fa29272 Remove redundant MakeUCharSpan wrappers (MarcoFalke) faf4aa2 Remove CDataStream::Init in favor of C++11 member initialization (MarcoFalke) fada14b Treat CDataStream bytes as uint8_t (MarcoFalke) fa8bdb0 refactor: Drop CDataStream constructors in favor of one taking a Span of bytes (MarcoFalke) faa96f8 Remove unused CDataStream methods (MarcoFalke) Pull request description: Using `uint8_t` for raw bytes has a style benefit: * The signedness is clear from reading the code, as it does not depend on the architecture Other clean-ups in this pull include: * Remove unused methods * Constructor is simplified with `Span` * Remove `Init()` member in favor of C++11 member initialization ACKs for top commit: laanwj: code review ACK fa29272 theStack: ACK fa29272 🍾 Tree-SHA512: 931ee28bd99843d7e894b48e90e1187ffb0278677c267044b3c0c255069d9bbd9298ab2e539b1002a30b543d240450eaec718ef4ee95a7fd4be0a295e926343f
2 parents 56fcf93 + fa29272 commit 2c0fc85

13 files changed

+63
-122
lines changed

src/bench/prevector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void PrevectorDeserialize(benchmark::Bench& bench)
7676
for (auto x = 0; x < 1000; ++x) {
7777
s0 >> t1;
7878
}
79-
s0.Init(SER_NETWORK, 0);
79+
s0.Rewind();
8080
});
8181
}
8282

src/dbwrapper.h

+15-15
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>
@@ -73,12 +74,12 @@ class CDBBatch
7374
{
7475
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
7576
ssKey << key;
76-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
77+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
7778

7879
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
7980
ssValue << value;
8081
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
81-
leveldb::Slice slValue(ssValue.data(), ssValue.size());
82+
leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size());
8283

8384
batch.Put(slKey, slValue);
8485
// LevelDB serializes writes as:
@@ -98,7 +99,7 @@ class CDBBatch
9899
{
99100
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
100101
ssKey << key;
101-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
102+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
102103

103104
batch.Delete(slKey);
104105
// LevelDB serializes erases as:
@@ -137,7 +138,7 @@ class CDBIterator
137138
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
138139
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
139140
ssKey << key;
140-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
141+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
141142
piter->Seek(slKey);
142143
}
143144

@@ -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&) {
@@ -232,7 +233,7 @@ class CDBWrapper
232233
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
233234
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
234235
ssKey << key;
235-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
236+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
236237

237238
std::string strValue;
238239
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -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&) {
@@ -266,7 +267,7 @@ class CDBWrapper
266267
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
267268
ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
268269
ssKey << key;
269-
leveldb::Slice slKey(ssKey.data(), ssKey.size());
270+
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size());
270271

271272
std::string strValue;
272273
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@@ -310,8 +311,8 @@ class CDBWrapper
310311
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
311312
ssKey1 << key_begin;
312313
ssKey2 << key_end;
313-
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
314-
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
314+
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
315+
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
315316
uint64_t size = 0;
316317
leveldb::Range range(slKey1, slKey2);
317318
pdb->GetApproximateSizes(&range, 1, &size);
@@ -329,11 +330,10 @@ class CDBWrapper
329330
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
330331
ssKey1 << key_begin;
331332
ssKey2 << key_end;
332-
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
333-
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
333+
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size());
334+
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size());
334335
pdb->CompactRange(&slKey1, &slKey2);
335336
}
336-
337337
};
338338

339339
#endif // BITCOIN_DBWRAPPER_H

src/psbt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6
363363

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

src/qt/recentrequeststablemodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
181181
// called from ctor when loading from wallet
182182
void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
183183
{
184-
std::vector<char> data(recipient.begin(), recipient.end());
184+
std::vector<uint8_t> data(recipient.begin(), recipient.end());
185185
CDataStream ss(data, SER_DISK, CLIENT_VERSION);
186186

187187
RecentRequestEntry entry;

src/qt/walletmodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
245245

246246
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
247247
ssTx << *newTx;
248-
transaction_array.append(&(ssTx[0]), ssTx.size());
248+
transaction_array.append((const char*)&(ssTx[0]), ssTx.size());
249249
}
250250

251251
// Add addresses / update labels that we've sent to the address book,

src/rpc/rawtransaction.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ static RPCHelpMan combinepsbt()
13451345

13461346
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
13471347
ssTx << merged_psbt;
1348-
return EncodeBase64(MakeUCharSpan(ssTx));
1348+
return EncodeBase64(ssTx);
13491349
},
13501350
};
13511351
}
@@ -1484,7 +1484,7 @@ static RPCHelpMan createpsbt()
14841484
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
14851485
ssTx << psbtx;
14861486

1487-
return EncodeBase64(MakeUCharSpan(ssTx));
1487+
return EncodeBase64(ssTx);
14881488
},
14891489
};
14901490
}
@@ -1553,7 +1553,7 @@ static RPCHelpMan converttopsbt()
15531553
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
15541554
ssTx << psbtx;
15551555

1556-
return EncodeBase64(MakeUCharSpan(ssTx));
1556+
return EncodeBase64(ssTx);
15571557
},
15581558
};
15591559
}
@@ -1644,7 +1644,7 @@ static RPCHelpMan utxoupdatepsbt()
16441644

16451645
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
16461646
ssTx << psbtx;
1647-
return EncodeBase64(MakeUCharSpan(ssTx));
1647+
return EncodeBase64(ssTx);
16481648
},
16491649
};
16501650
}
@@ -1740,7 +1740,7 @@ static RPCHelpMan joinpsbts()
17401740

17411741
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
17421742
ssTx << shuffled_psbt;
1743-
return EncodeBase64(MakeUCharSpan(ssTx));
1743+
return EncodeBase64(ssTx);
17441744
},
17451745
};
17461746
}

src/streams.h

+26-64
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
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>
17+
#include <optional>
1618
#include <stdint.h>
1719
#include <stdio.h>
18-
#include <string>
1920
#include <string.h>
21+
#include <string>
2022
#include <utility>
2123
#include <vector>
2224

@@ -202,14 +204,14 @@ class VectorReader
202204
class CDataStream
203205
{
204206
protected:
205-
typedef CSerializeData vector_type;
207+
using vector_type = SerializeData;
206208
vector_type vch;
207-
unsigned int nReadPos;
209+
unsigned int nReadPos{0};
208210

209211
int nType;
210212
int nVersion;
211-
public:
212213

214+
public:
213215
typedef vector_type::allocator_type allocator_type;
214216
typedef vector_type::size_type size_type;
215217
typedef vector_type::difference_type difference_type;
@@ -221,62 +223,22 @@ class CDataStream
221223
typedef vector_type::reverse_iterator reverse_iterator;
222224

223225
explicit CDataStream(int nTypeIn, int nVersionIn)
224-
{
225-
Init(nTypeIn, nVersionIn);
226-
}
227-
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-
}
226+
: nType{nTypeIn},
227+
nVersion{nVersionIn} {}
247228

248-
CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
249-
{
250-
Init(nTypeIn, nVersionIn);
251-
}
229+
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
230+
: vch(sp.data(), sp.data() + sp.size()),
231+
nType{nTypeIn},
232+
nVersion{nVersionIn} {}
252233

253234
template <typename... Args>
254235
CDataStream(int nTypeIn, int nVersionIn, Args&&... args)
236+
: nType{nTypeIn},
237+
nVersion{nVersionIn}
255238
{
256-
Init(nTypeIn, nVersionIn);
257239
::SerializeMany(*this, std::forward<Args>(args)...);
258240
}
259241

260-
void Init(int nTypeIn, int nVersionIn)
261-
{
262-
nReadPos = 0;
263-
nType = nTypeIn;
264-
nVersion = nVersionIn;
265-
}
266-
267-
CDataStream& operator+=(const CDataStream& b)
268-
{
269-
vch.insert(vch.end(), b.begin(), b.end());
270-
return *this;
271-
}
272-
273-
friend CDataStream operator+(const CDataStream& a, const CDataStream& b)
274-
{
275-
CDataStream ret = a;
276-
ret += b;
277-
return (ret);
278-
}
279-
280242
std::string str() const
281243
{
282244
return (std::string(begin(), end()));
@@ -297,12 +259,12 @@ class CDataStream
297259
const_reference operator[](size_type pos) const { return vch[pos + nReadPos]; }
298260
reference operator[](size_type pos) { return vch[pos + nReadPos]; }
299261
void clear() { vch.clear(); nReadPos = 0; }
300-
iterator insert(iterator it, const char x=char()) { return vch.insert(it, x); }
301-
void insert(iterator it, size_type n, const char x) { vch.insert(it, n, x); }
262+
iterator insert(iterator it, const uint8_t x) { return vch.insert(it, x); }
263+
void insert(iterator it, size_type n, const uint8_t x) { vch.insert(it, n, x); }
302264
value_type* data() { return vch.data() + nReadPos; }
303265
const value_type* data() const { return vch.data() + nReadPos; }
304266

305-
void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
267+
void insert(iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
306268
{
307269
if (last == first) return;
308270
assert(last - first > 0);
@@ -373,12 +335,17 @@ class CDataStream
373335
nReadPos = 0;
374336
}
375337

376-
bool Rewind(size_type n)
338+
bool Rewind(std::optional<size_type> n = std::nullopt)
377339
{
340+
// Total rewind if no size is passed
341+
if (!n) {
342+
nReadPos = 0;
343+
return true;
344+
}
378345
// Rewind by n characters if the buffer hasn't been compacted yet
379-
if (n > nReadPos)
346+
if (*n > nReadPos)
380347
return false;
381-
nReadPos -= n;
348+
nReadPos -= *n;
382349
return true;
383350
}
384351

@@ -462,11 +429,6 @@ class CDataStream
462429
return (*this);
463430
}
464431

465-
void GetAndClear(CSerializeData &d) {
466-
d.insert(d.end(), begin(), end());
467-
clear();
468-
}
469-
470432
/**
471433
* XOR the contents of this stream with a certain key.
472434
*

src/support/allocators/zeroafterfree.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct zero_after_free_allocator : public std::allocator<T> {
4242
}
4343
};
4444

45-
// Byte-vector that clears its contents before deletion.
46-
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;
45+
/** Byte-vector that clears its contents before deletion. */
46+
using SerializeData = std::vector<uint8_t, zero_after_free_allocator<uint8_t>>;
4747

4848
#endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H

0 commit comments

Comments
 (0)