mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
refactor: SpanReader without nVersion
The field is unused, so remove it. This is also required for future commits.
This commit is contained in:
parent
c252a0fc0f
commit
fac39b56b7
9 changed files with 25 additions and 34 deletions
|
@ -16,9 +16,6 @@
|
||||||
#include <util/golombrice.h>
|
#include <util/golombrice.h>
|
||||||
#include <util/string.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 = {
|
static const std::map<BlockFilterType, std::string> g_filter_types = {
|
||||||
{BlockFilterType::BASIC, "basic"},
|
{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)
|
GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
|
||||||
: m_params(params), m_encoded(std::move(encoded_filter))
|
: 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);
|
uint64_t N = ReadCompactSize(stream);
|
||||||
m_N = static_cast<uint32_t>(N);
|
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
|
// 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.
|
// 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) {
|
for (uint64_t i = 0; i < m_N; ++i) {
|
||||||
GolombRiceDecode(bitreader, m_params.m_P);
|
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
|
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
|
// Seek forward by size of N
|
||||||
uint64_t N = ReadCompactSize(stream);
|
uint64_t N = ReadCompactSize(stream);
|
||||||
assert(N == m_N);
|
assert(N == m_N);
|
||||||
|
|
||||||
BitStreamReader<SpanReader> bitreader{stream};
|
BitStreamReader bitreader{stream};
|
||||||
|
|
||||||
uint64_t value = 0;
|
uint64_t value = 0;
|
||||||
size_t hashes_index = 0;
|
size_t hashes_index = 0;
|
||||||
|
|
12
src/psbt.h
12
src/psbt.h
|
@ -382,7 +382,7 @@ struct PSBTInput
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type is compact size uint at beginning of key
|
// Type is compact size uint at beginning of key
|
||||||
SpanReader skey{s.GetVersion(), key};
|
SpanReader skey{key};
|
||||||
uint64_t type = ReadCompactSize(skey);
|
uint64_t type = ReadCompactSize(skey);
|
||||||
|
|
||||||
// Do stuff based on type
|
// Do stuff based on type
|
||||||
|
@ -590,7 +590,7 @@ struct PSBTInput
|
||||||
} else if (key.size() != 65) {
|
} else if (key.size() != 65) {
|
||||||
throw std::ios_base::failure("Input Taproot script signature key is not 65 bytes");
|
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;
|
XOnlyPubKey xonly;
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
s_key >> xonly;
|
s_key >> xonly;
|
||||||
|
@ -632,7 +632,7 @@ struct PSBTInput
|
||||||
} else if (key.size() != 33) {
|
} else if (key.size() != 33) {
|
||||||
throw std::ios_base::failure("Input Taproot BIP32 keypath key is not at 33 bytes");
|
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;
|
XOnlyPubKey xonly;
|
||||||
s_key >> xonly;
|
s_key >> xonly;
|
||||||
std::set<uint256> leaf_hashes;
|
std::set<uint256> leaf_hashes;
|
||||||
|
@ -807,7 +807,7 @@ struct PSBTOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type is compact size uint at beginning of key
|
// Type is compact size uint at beginning of key
|
||||||
SpanReader skey{s.GetVersion(), key};
|
SpanReader skey{key};
|
||||||
uint64_t type = ReadCompactSize(skey);
|
uint64_t type = ReadCompactSize(skey);
|
||||||
|
|
||||||
// Do stuff based on type
|
// Do stuff based on type
|
||||||
|
@ -856,7 +856,7 @@ struct PSBTOutput
|
||||||
}
|
}
|
||||||
std::vector<unsigned char> tree_v;
|
std::vector<unsigned char> tree_v;
|
||||||
s >> tree_v;
|
s >> tree_v;
|
||||||
SpanReader s_tree{s.GetVersion(), tree_v};
|
SpanReader s_tree{tree_v};
|
||||||
if (s_tree.empty()) {
|
if (s_tree.empty()) {
|
||||||
throw std::ios_base::failure("Output Taproot tree must not be 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
|
// Type is compact size uint at beginning of key
|
||||||
SpanReader skey{s.GetVersion(), key};
|
SpanReader skey{key};
|
||||||
uint64_t type = ReadCompactSize(skey);
|
uint64_t type = ReadCompactSize(skey);
|
||||||
|
|
||||||
// Do stuff based on type
|
// Do stuff based on type
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <version.h>
|
|
||||||
|
|
||||||
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
|
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
|
// no signet solution -- allow this to support OP_TRUE as trivial block challenge
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
SpanReader v{INIT_PROTO_VERSION, signet_solution};
|
SpanReader v{signet_solution};
|
||||||
v >> tx_spending.vin[0].scriptSig;
|
v >> tx_spending.vin[0].scriptSig;
|
||||||
v >> tx_spending.vin[0].scriptWitness.stack;
|
v >> tx_spending.vin[0].scriptWitness.stack;
|
||||||
if (!v.empty()) return std::nullopt; // extraneous data encountered
|
if (!v.empty()) return std::nullopt; // extraneous data encountered
|
||||||
|
|
|
@ -100,16 +100,13 @@ private:
|
||||||
class SpanReader
|
class SpanReader
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const int m_version;
|
|
||||||
Span<const unsigned char> m_data;
|
Span<const unsigned char> m_data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @param[in] version Serialization Version (including any flags)
|
|
||||||
* @param[in] data Referenced byte vector to overwrite/append
|
* @param[in] data Referenced byte vector to overwrite/append
|
||||||
*/
|
*/
|
||||||
SpanReader(int version, Span<const unsigned char> data)
|
explicit SpanReader(Span<const unsigned char> data) : m_data{data} {}
|
||||||
: m_version{version}, m_data{data} {}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
SpanReader& operator>>(T&& obj)
|
SpanReader& operator>>(T&& obj)
|
||||||
|
@ -118,8 +115,6 @@ public:
|
||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetVersion() const { return m_version; }
|
|
||||||
|
|
||||||
size_t size() const { return m_data.size(); }
|
size_t size() const { return m_data.size(); }
|
||||||
bool empty() const { return m_data.empty(); }
|
bool empty() const { return m_data.empty(); }
|
||||||
|
|
||||||
|
|
|
@ -68,8 +68,8 @@ FUZZ_TARGET(golomb_rice)
|
||||||
|
|
||||||
std::vector<uint64_t> decoded_deltas;
|
std::vector<uint64_t> decoded_deltas;
|
||||||
{
|
{
|
||||||
SpanReader stream{0, golomb_rice_data};
|
SpanReader stream{golomb_rice_data};
|
||||||
BitStreamReader<SpanReader> bitreader{stream};
|
BitStreamReader bitreader{stream};
|
||||||
const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
|
const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
|
||||||
for (uint32_t i = 0; i < n; ++i) {
|
for (uint32_t i = 0; i < n; ++i) {
|
||||||
decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P));
|
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);
|
const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);
|
||||||
SpanReader stream{0, random_bytes};
|
SpanReader stream{random_bytes};
|
||||||
uint32_t n;
|
uint32_t n;
|
||||||
try {
|
try {
|
||||||
n = static_cast<uint32_t>(ReadCompactSize(stream));
|
n = static_cast<uint32_t>(ReadCompactSize(stream));
|
||||||
} catch (const std::ios_base::failure&) {
|
} catch (const std::ios_base::failure&) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BitStreamReader<SpanReader> bitreader{stream};
|
BitStreamReader bitreader{stream};
|
||||||
for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {
|
for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {
|
||||||
try {
|
try {
|
||||||
(void)GolombRiceDecode(bitreader, BASIC_FILTER_P);
|
(void)GolombRiceDecode(bitreader, BASIC_FILTER_P);
|
||||||
|
|
|
@ -54,7 +54,7 @@ CMutableTransaction TxFromHex(const std::string& str)
|
||||||
{
|
{
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
try {
|
try {
|
||||||
SpanReader{0, CheckedParseHex(str)} >> TX_NO_WITNESS(tx);
|
SpanReader{CheckedParseHex(str)} >> TX_NO_WITNESS(tx);
|
||||||
} catch (const std::ios_base::failure&) {
|
} catch (const std::ios_base::failure&) {
|
||||||
throw std::runtime_error("Tx deserialization 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) {
|
for (size_t i = 0; i < univalue.size(); ++i) {
|
||||||
CTxOut txout;
|
CTxOut txout;
|
||||||
try {
|
try {
|
||||||
SpanReader{0, CheckedParseHex(univalue[i].get_str())} >> txout;
|
SpanReader{CheckedParseHex(univalue[i].get_str())} >> txout;
|
||||||
} catch (const std::ios_base::failure&) {
|
} catch (const std::ios_base::failure&) {
|
||||||
throw std::runtime_error("Prevout invalid format");
|
throw std::runtime_error("Prevout invalid format");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1470,7 +1470,7 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
|
||||||
static CMutableTransaction TxFromHex(const std::string& str)
|
static CMutableTransaction TxFromHex(const std::string& str)
|
||||||
{
|
{
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
SpanReader{0, ParseHex(str)} >> TX_NO_WITNESS(tx);
|
SpanReader{ParseHex(str)} >> TX_NO_WITNESS(tx);
|
||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1480,7 +1480,7 @@ static std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
|
||||||
std::vector<CTxOut> prevouts;
|
std::vector<CTxOut> prevouts;
|
||||||
for (size_t i = 0; i < univalue.size(); ++i) {
|
for (size_t i = 0; i < univalue.size(); ++i) {
|
||||||
CTxOut txout;
|
CTxOut txout;
|
||||||
SpanReader{0, ParseHex(univalue[i].get_str())} >> txout;
|
SpanReader{ParseHex(univalue[i].get_str())} >> txout;
|
||||||
prevouts.push_back(std::move(txout));
|
prevouts.push_back(std::move(txout));
|
||||||
}
|
}
|
||||||
return prevouts;
|
return prevouts;
|
||||||
|
@ -1816,7 +1816,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
|
||||||
for (const auto& vec : vectors.getValues()) {
|
for (const auto& vec : vectors.getValues()) {
|
||||||
auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
|
auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
SpanReader{PROTOCOL_VERSION, txhex} >> TX_WITH_WITNESS(tx);
|
SpanReader{txhex} >> TX_WITH_WITNESS(tx);
|
||||||
std::vector<CTxOut> utxos;
|
std::vector<CTxOut> utxos;
|
||||||
for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
|
for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
|
||||||
auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());
|
auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());
|
||||||
|
|
|
@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
|
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_EQUAL(reader.size(), 6U);
|
||||||
BOOST_CHECK(!reader.empty());
|
BOOST_CHECK(!reader.empty());
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
|
||||||
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
|
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
|
||||||
|
|
||||||
// Read a 4 bytes as a signed int from the beginning of the buffer.
|
// 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;
|
new_reader >> d;
|
||||||
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
|
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
|
||||||
BOOST_CHECK_EQUAL(new_reader.size(), 2U);
|
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)
|
BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> data{0x82, 0xa7, 0x31};
|
std::vector<uint8_t> data{0x82, 0xa7, 0x31};
|
||||||
SpanReader reader{INIT_PROTO_VERSION, data};
|
SpanReader reader{data};
|
||||||
uint32_t varint = 0;
|
uint32_t varint = 0;
|
||||||
// Deserialize into r-value
|
// Deserialize into r-value
|
||||||
reader >> VARINT(varint);
|
reader >> VARINT(varint);
|
||||||
|
|
|
@ -759,7 +759,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_descriptor_test, BasicTestingSetup)
|
||||||
vw << int32_t{0};
|
vw << int32_t{0};
|
||||||
vw << int32_t{1};
|
vw << int32_t{1};
|
||||||
|
|
||||||
SpanReader vr{0, malformed_record};
|
SpanReader vr{malformed_record};
|
||||||
WalletDescriptor w_desc;
|
WalletDescriptor w_desc;
|
||||||
BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
|
BOOST_CHECK_EXCEPTION(vr >> w_desc, std::ios_base::failure, malformed_descriptor);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue