From fa8bdb048e65cae2d26bea3f991717a856e2fb39 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Mon, 23 Nov 2020 18:53:44 +0100 Subject: [PATCH] refactor: Drop CDataStream constructors in favor of one taking a Span of bytes --- src/dbwrapper.h | 10 +++++----- src/psbt.cpp | 2 +- src/qt/recentrequeststablemodel.cpp | 2 +- src/streams.h | 30 ++++++----------------------- src/test/fuzz/util.h | 2 +- src/test/streams_tests.cpp | 2 +- 6 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 215b033708b..356111b6755 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -8,9 +8,10 @@ #include #include #include +#include #include -#include #include +#include #include #include @@ -146,7 +147,7 @@ public: template bool GetKey(K& key) { leveldb::Slice slKey = piter->key(); try { - CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION); + CDataStream ssKey(MakeUCharSpan(slKey), SER_DISK, CLIENT_VERSION); ssKey >> key; } catch (const std::exception&) { return false; @@ -157,7 +158,7 @@ public: template bool GetValue(V& value) { leveldb::Slice slValue = piter->value(); try { - CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION); + CDataStream ssValue(MakeUCharSpan(slValue), SER_DISK, CLIENT_VERSION); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue >> value; } catch (const std::exception&) { @@ -243,7 +244,7 @@ public: dbwrapper_private::HandleError(status); } try { - CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(), SER_DISK, CLIENT_VERSION); + CDataStream ssValue(MakeUCharSpan(strValue), SER_DISK, CLIENT_VERSION); ssValue.Xor(obfuscate_key); ssValue >> value; } catch (const std::exception&) { @@ -333,7 +334,6 @@ public: leveldb::Slice slKey2(ssKey2.data(), ssKey2.size()); pdb->CompactRange(&slKey1, &slKey2); } - }; #endif // BITCOIN_DBWRAPPER_H diff --git a/src/psbt.cpp b/src/psbt.cpp index 3fb743e5db2..f51fab6225d 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -360,7 +360,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6 bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error) { - CDataStream ss_data(tx_data.data(), tx_data.data() + tx_data.size(), SER_NETWORK, PROTOCOL_VERSION); + CDataStream ss_data(MakeUCharSpan(tx_data), SER_NETWORK, PROTOCOL_VERSION); try { ss_data >> psbt; if (!ss_data.empty()) { diff --git a/src/qt/recentrequeststablemodel.cpp b/src/qt/recentrequeststablemodel.cpp index 3e20368a36d..b11a1ced00b 100644 --- a/src/qt/recentrequeststablemodel.cpp +++ b/src/qt/recentrequeststablemodel.cpp @@ -179,7 +179,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient // called from ctor when loading from wallet void RecentRequestsTableModel::addNewRequest(const std::string &recipient) { - std::vector data(recipient.begin(), recipient.end()); + std::vector data(recipient.begin(), recipient.end()); CDataStream ss(data, SER_DISK, CLIENT_VERSION); RecentRequestEntry entry; diff --git a/src/streams.h b/src/streams.h index 793c06b5c58..53924d76ed2 100644 --- a/src/streams.h +++ b/src/streams.h @@ -6,8 +6,9 @@ #ifndef BITCOIN_STREAMS_H #define BITCOIN_STREAMS_H -#include #include +#include +#include #include #include @@ -15,8 +16,8 @@ #include #include #include -#include #include +#include #include #include @@ -225,27 +226,8 @@ public: Init(nTypeIn, nVersionIn); } - CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend) - { - Init(nTypeIn, nVersionIn); - } - - CDataStream(const char* pbegin, const char* pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend) - { - Init(nTypeIn, nVersionIn); - } - - CDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end()) - { - Init(nTypeIn, nVersionIn); - } - - CDataStream(const std::vector& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end()) - { - Init(nTypeIn, nVersionIn); - } - - CDataStream(const std::vector& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end()) + explicit CDataStream(Span sp, int nTypeIn, int nVersionIn) + : vch(sp.data(), sp.data() + sp.size()) { Init(nTypeIn, nVersionIn); } @@ -289,7 +271,7 @@ public: value_type* data() { return vch.data() + nReadPos; } const value_type* data() const { return vch.data() + nReadPos; } - void insert(iterator it, std::vector::const_iterator first, std::vector::const_iterator last) + void insert(iterator it, std::vector::const_iterator first, std::vector::const_iterator last) { if (last == first) return; assert(last - first > 0); diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index e99ed8d72d3..481ffa3da46 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -48,7 +48,7 @@ NODISCARD inline std::vector ConsumeRandomLengthBitVector(FuzzedDataProvid NODISCARD inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept { - return {ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION}; + return CDataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION}; } NODISCARD inline std::vector ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index c509a252e05..26e24d836a1 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(bitstream_reader_writer) BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { - std::vector in; + std::vector in; std::vector expected_xor; std::vector key; CDataStream ds(in, 0, 0);