refactor: SpanReader without nVersion

The field is unused, so remove it.

This is also required for future commits.
This commit is contained in:
MarcoFalke 2023-11-17 16:32:29 +01:00
parent c252a0fc0f
commit fac39b56b7
No known key found for this signature in database
9 changed files with 25 additions and 34 deletions

View File

@ -16,9 +16,6 @@
#include <util/golombrice.h>
#include <util/string.h>
/// Protocol version used to serialize parameters in GCS filter encoding.
static constexpr int GCS_SER_VERSION = 0;
static const std::map<BlockFilterType, std::string> g_filter_types = {
{BlockFilterType::BASIC, "basic"},
};
@ -49,7 +46,7 @@ GCSFilter::GCSFilter(const Params& params)
GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
: m_params(params), m_encoded(std::move(encoded_filter))
{
SpanReader stream{GCS_SER_VERSION, m_encoded};
SpanReader stream{m_encoded};
uint64_t N = ReadCompactSize(stream);
m_N = static_cast<uint32_t>(N);
@ -62,7 +59,7 @@ GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_fi
// Verify that the encoded filter contains exactly N elements. If it has too much or too little
// data, a std::ios_base::failure exception will be raised.
BitStreamReader<SpanReader> bitreader{stream};
BitStreamReader bitreader{stream};
for (uint64_t i = 0; i < m_N; ++i) {
GolombRiceDecode(bitreader, m_params.m_P);
}
@ -103,13 +100,13 @@ GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
{
SpanReader stream{GCS_SER_VERSION, m_encoded};
SpanReader stream{m_encoded};
// Seek forward by size of N
uint64_t N = ReadCompactSize(stream);
assert(N == m_N);
BitStreamReader<SpanReader> bitreader{stream};
BitStreamReader bitreader{stream};
uint64_t value = 0;
size_t hashes_index = 0;

View File

@ -382,7 +382,7 @@ struct PSBTInput
}
// Type is compact size uint at beginning of key
SpanReader skey{s.GetVersion(), key};
SpanReader skey{key};
uint64_t type = ReadCompactSize(skey);
// Do stuff based on type
@ -590,7 +590,7 @@ struct PSBTInput
} else if (key.size() != 65) {
throw std::ios_base::failure("Input Taproot script signature key is not 65 bytes");
}
SpanReader s_key{s.GetVersion(), Span{key}.subspan(1)};
SpanReader s_key{Span{key}.subspan(1)};
XOnlyPubKey xonly;
uint256 hash;
s_key >> xonly;
@ -632,7 +632,7 @@ struct PSBTInput
} else if (key.size() != 33) {
throw std::ios_base::failure("Input Taproot BIP32 keypath key is not at 33 bytes");
}
SpanReader s_key{s.GetVersion(), Span{key}.subspan(1)};
SpanReader s_key{Span{key}.subspan(1)};
XOnlyPubKey xonly;
s_key >> xonly;
std::set<uint256> leaf_hashes;
@ -807,7 +807,7 @@ struct PSBTOutput
}
// Type is compact size uint at beginning of key
SpanReader skey{s.GetVersion(), key};
SpanReader skey{key};
uint64_t type = ReadCompactSize(skey);
// Do stuff based on type
@ -856,7 +856,7 @@ struct PSBTOutput
}
std::vector<unsigned char> tree_v;
s >> tree_v;
SpanReader s_tree{s.GetVersion(), tree_v};
SpanReader s_tree{tree_v};
if (s_tree.empty()) {
throw std::ios_base::failure("Output Taproot tree must not be empty");
}
@ -1060,7 +1060,7 @@ struct PartiallySignedTransaction
}
// Type is compact size uint at beginning of key
SpanReader skey{s.GetVersion(), key};
SpanReader skey{key};
uint64_t type = ReadCompactSize(skey);
// Do stuff based on type

View File

@ -22,7 +22,6 @@
#include <streams.h>
#include <uint256.h>
#include <util/strencodings.h>
#include <version.h>
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
@ -99,7 +98,7 @@ std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& c
// no signet solution -- allow this to support OP_TRUE as trivial block challenge
} else {
try {
SpanReader v{INIT_PROTO_VERSION, signet_solution};
SpanReader v{signet_solution};
v >> tx_spending.vin[0].scriptSig;
v >> tx_spending.vin[0].scriptWitness.stack;
if (!v.empty()) return std::nullopt; // extraneous data encountered

View File

@ -100,16 +100,13 @@ private:
class SpanReader
{
private:
const int m_version;
Span<const unsigned char> m_data;
public:
/**
* @param[in] version Serialization Version (including any flags)
* @param[in] data Referenced byte vector to overwrite/append
*/
SpanReader(int version, Span<const unsigned char> data)
: m_version{version}, m_data{data} {}
explicit SpanReader(Span<const unsigned char> data) : m_data{data} {}
template<typename T>
SpanReader& operator>>(T&& obj)
@ -118,8 +115,6 @@ public:
return (*this);
}
int GetVersion() const { return m_version; }
size_t size() const { return m_data.size(); }
bool empty() const { return m_data.empty(); }

View File

@ -68,8 +68,8 @@ FUZZ_TARGET(golomb_rice)
std::vector<uint64_t> decoded_deltas;
{
SpanReader stream{0, golomb_rice_data};
BitStreamReader<SpanReader> bitreader{stream};
SpanReader stream{golomb_rice_data};
BitStreamReader bitreader{stream};
const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
for (uint32_t i = 0; i < n; ++i) {
decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P));
@ -80,14 +80,14 @@ FUZZ_TARGET(golomb_rice)
{
const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);
SpanReader stream{0, random_bytes};
SpanReader stream{random_bytes};
uint32_t n;
try {
n = static_cast<uint32_t>(ReadCompactSize(stream));
} catch (const std::ios_base::failure&) {
return;
}
BitStreamReader<SpanReader> bitreader{stream};
BitStreamReader bitreader{stream};
for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {
try {
(void)GolombRiceDecode(bitreader, BASIC_FILTER_P);

View File

@ -54,7 +54,7 @@ CMutableTransaction TxFromHex(const std::string& str)
{
CMutableTransaction tx;
try {
SpanReader{0, CheckedParseHex(str)} >> TX_NO_WITNESS(tx);
SpanReader{CheckedParseHex(str)} >> TX_NO_WITNESS(tx);
} catch (const std::ios_base::failure&) {
throw std::runtime_error("Tx deserialization failure");
}
@ -68,7 +68,7 @@ std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
for (size_t i = 0; i < univalue.size(); ++i) {
CTxOut txout;
try {
SpanReader{0, CheckedParseHex(univalue[i].get_str())} >> txout;
SpanReader{CheckedParseHex(univalue[i].get_str())} >> txout;
} catch (const std::ios_base::failure&) {
throw std::runtime_error("Prevout invalid format");
}

View File

@ -1470,7 +1470,7 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
static CMutableTransaction TxFromHex(const std::string& str)
{
CMutableTransaction tx;
SpanReader{0, ParseHex(str)} >> TX_NO_WITNESS(tx);
SpanReader{ParseHex(str)} >> TX_NO_WITNESS(tx);
return tx;
}
@ -1480,7 +1480,7 @@ static std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
std::vector<CTxOut> prevouts;
for (size_t i = 0; i < univalue.size(); ++i) {
CTxOut txout;
SpanReader{0, ParseHex(univalue[i].get_str())} >> txout;
SpanReader{ParseHex(univalue[i].get_str())} >> txout;
prevouts.push_back(std::move(txout));
}
return prevouts;
@ -1816,7 +1816,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
for (const auto& vec : vectors.getValues()) {
auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
CMutableTransaction tx;
SpanReader{PROTOCOL_VERSION, txhex} >> TX_WITH_WITNESS(tx);
SpanReader{txhex} >> TX_WITH_WITNESS(tx);
std::vector<CTxOut> utxos;
for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());

View File

@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
{
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
SpanReader reader{INIT_PROTO_VERSION, vch};
SpanReader reader{vch};
BOOST_CHECK_EQUAL(reader.size(), 6U);
BOOST_CHECK(!reader.empty());
@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
// Read a 4 bytes as a signed int from the beginning of the buffer.
SpanReader new_reader{INIT_PROTO_VERSION, vch};
SpanReader new_reader{vch};
new_reader >> d;
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
BOOST_CHECK_EQUAL(new_reader.size(), 2U);
@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
{
std::vector<uint8_t> data{0x82, 0xa7, 0x31};
SpanReader reader{INIT_PROTO_VERSION, data};
SpanReader reader{data};
uint32_t varint = 0;
// Deserialize into r-value
reader >> VARINT(varint);

View File

@ -759,7 +759,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
vw << int32_t{0};
vw << int32_t{1};
SpanReader vr{0, malformed_record};
SpanReader vr{malformed_record};
WalletDescriptor w_desc;
BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
}