mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
refactor: Drop CDataStream constructors in favor of one taking a Span of bytes
This commit is contained in:
@@ -8,9 +8,10 @@
|
|||||||
#include <clientversion.h>
|
#include <clientversion.h>
|
||||||
#include <fs.h>
|
#include <fs.h>
|
||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
|
#include <span.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <util/system.h>
|
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
|
#include <util/system.h>
|
||||||
|
|
||||||
#include <leveldb/db.h>
|
#include <leveldb/db.h>
|
||||||
#include <leveldb/write_batch.h>
|
#include <leveldb/write_batch.h>
|
||||||
@@ -146,7 +147,7 @@ public:
|
|||||||
template<typename K> bool GetKey(K& key) {
|
template<typename K> bool GetKey(K& key) {
|
||||||
leveldb::Slice slKey = piter->key();
|
leveldb::Slice slKey = piter->key();
|
||||||
try {
|
try {
|
||||||
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
|
CDataStream ssKey(MakeUCharSpan(slKey), SER_DISK, CLIENT_VERSION);
|
||||||
ssKey >> key;
|
ssKey >> key;
|
||||||
} catch (const std::exception&) {
|
} catch (const std::exception&) {
|
||||||
return false;
|
return false;
|
||||||
@@ -157,7 +158,7 @@ public:
|
|||||||
template<typename V> bool GetValue(V& value) {
|
template<typename V> bool GetValue(V& value) {
|
||||||
leveldb::Slice slValue = piter->value();
|
leveldb::Slice slValue = piter->value();
|
||||||
try {
|
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.Xor(dbwrapper_private::GetObfuscateKey(parent));
|
||||||
ssValue >> value;
|
ssValue >> value;
|
||||||
} catch (const std::exception&) {
|
} catch (const std::exception&) {
|
||||||
@@ -243,7 +244,7 @@ public:
|
|||||||
dbwrapper_private::HandleError(status);
|
dbwrapper_private::HandleError(status);
|
||||||
}
|
}
|
||||||
try {
|
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.Xor(obfuscate_key);
|
||||||
ssValue >> value;
|
ssValue >> value;
|
||||||
} catch (const std::exception&) {
|
} catch (const std::exception&) {
|
||||||
@@ -333,7 +334,6 @@ public:
|
|||||||
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
|
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
|
||||||
pdb->CompactRange(&slKey1, &slKey2);
|
pdb->CompactRange(&slKey1, &slKey2);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_DBWRAPPER_H
|
#endif // BITCOIN_DBWRAPPER_H
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6
|
|||||||
|
|
||||||
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error)
|
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 {
|
try {
|
||||||
ss_data >> psbt;
|
ss_data >> psbt;
|
||||||
if (!ss_data.empty()) {
|
if (!ss_data.empty()) {
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient
|
|||||||
// called from ctor when loading from wallet
|
// called from ctor when loading from wallet
|
||||||
void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
|
void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
|
||||||
{
|
{
|
||||||
std::vector<char> data(recipient.begin(), recipient.end());
|
std::vector<uint8_t> data(recipient.begin(), recipient.end());
|
||||||
CDataStream ss(data, SER_DISK, CLIENT_VERSION);
|
CDataStream ss(data, SER_DISK, CLIENT_VERSION);
|
||||||
|
|
||||||
RecentRequestEntry entry;
|
RecentRequestEntry entry;
|
||||||
|
|||||||
@@ -6,8 +6,9 @@
|
|||||||
#ifndef BITCOIN_STREAMS_H
|
#ifndef BITCOIN_STREAMS_H
|
||||||
#define BITCOIN_STREAMS_H
|
#define BITCOIN_STREAMS_H
|
||||||
|
|
||||||
#include <support/allocators/zeroafterfree.h>
|
|
||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
|
#include <span.h>
|
||||||
|
#include <support/allocators/zeroafterfree.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -15,8 +16,8 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -225,27 +226,8 @@ public:
|
|||||||
Init(nTypeIn, nVersionIn);
|
Init(nTypeIn, nVersionIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) : vch(pbegin, pend)
|
explicit CDataStream(Span<const uint8_t> sp, int nTypeIn, int nVersionIn)
|
||||||
{
|
: vch(sp.data(), sp.data() + sp.size())
|
||||||
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<char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
|
|
||||||
{
|
|
||||||
Init(nTypeIn, nVersionIn);
|
|
||||||
}
|
|
||||||
|
|
||||||
CDataStream(const std::vector<unsigned char>& vchIn, int nTypeIn, int nVersionIn) : vch(vchIn.begin(), vchIn.end())
|
|
||||||
{
|
{
|
||||||
Init(nTypeIn, nVersionIn);
|
Init(nTypeIn, nVersionIn);
|
||||||
}
|
}
|
||||||
@@ -289,7 +271,7 @@ public:
|
|||||||
value_type* data() { return vch.data() + nReadPos; }
|
value_type* data() { return vch.data() + nReadPos; }
|
||||||
const value_type* data() const { return vch.data() + nReadPos; }
|
const value_type* data() const { return vch.data() + nReadPos; }
|
||||||
|
|
||||||
void insert(iterator it, std::vector<char>::const_iterator first, std::vector<char>::const_iterator last)
|
void insert(iterator it, std::vector<uint8_t>::const_iterator first, std::vector<uint8_t>::const_iterator last)
|
||||||
{
|
{
|
||||||
if (last == first) return;
|
if (last == first) return;
|
||||||
assert(last - first > 0);
|
assert(last - first > 0);
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ NODISCARD inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvid
|
|||||||
|
|
||||||
NODISCARD inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
|
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<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept
|
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
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
|
|||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
|
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
|
||||||
{
|
{
|
||||||
std::vector<char> in;
|
std::vector<uint8_t> in;
|
||||||
std::vector<char> expected_xor;
|
std::vector<char> expected_xor;
|
||||||
std::vector<unsigned char> key;
|
std::vector<unsigned char> key;
|
||||||
CDataStream ds(in, 0, 0);
|
CDataStream ds(in, 0, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user