Merge bitcoin/bitcoin#28912: refactor: VectorWriter and SpanReader without nVersion

fae76a1f2a scripted-diff: Use DataStream in most places (MarcoFalke)
fac39b56b7 refactor: SpanReader without nVersion (MarcoFalke)

Pull request description:

  The serialize version is unused, so remove it. This also allows to remove `GCS_SER_VERSION` and allows a scripted-diff to remove most of `CDataStream`.

ACKs for top commit:
  ajtowns:
    ACK fae76a1f2a
  ryanofsky:
    Code review ACK fae76a1f2a

Tree-SHA512: 3b487dba8ea380f1eacff9fdfb9197f025dbc30906813d3f4c3e6f1e9e4d9f2a169c6f163f51d135e18af538be78e2d2b13d694073ad25c5762980ae971a4c83
This commit is contained in:
fanquake 2023-11-28 17:25:05 +00:00
commit fe4e83f50d
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
21 changed files with 78 additions and 87 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

@ -74,7 +74,7 @@ UniValue ExternalSigner::GetDescriptors(const int account)
bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error)
{
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
// parse ExternalSigner master fingerprint
std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);

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

@ -128,14 +128,14 @@ void PSBTOperationsDialog::broadcastTransaction()
}
void PSBTOperationsDialog::copyToClipboard() {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << m_transaction_data;
GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
showStatus(tr("PSBT copied to clipboard."), StatusLevel::INFO);
}
void PSBTOperationsDialog::saveTransaction() {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << m_transaction_data;
QString selected_filter;

View file

@ -397,7 +397,7 @@ bool SendCoinsDialog::PrepareSendText(QString& question_string, QString& informa
void SendCoinsDialog::presentPSBT(PartiallySignedTransaction& psbtx)
{
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
QMessageBox msgBox(this);

View file

@ -543,7 +543,7 @@ bool WalletModel::bumpFee(uint256 hash, uint256& new_hash)
return false;
}
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
GUIUtil::setClipboard(EncodeBase64(ssTx.str()).c_str());
Q_EMIT message(tr("PSBT copied"), tr("Copied to clipboard", "Fee-bump PSBT saved"), CClientUIInterface::MSG_INFORMATION);

View file

@ -1493,7 +1493,7 @@ static RPCHelpMan combinepsbt()
throw JSONRPCTransactionError(error);
}
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << merged_psbt;
return EncodeBase64(ssTx);
},
@ -1538,7 +1538,7 @@ static RPCHelpMan finalizepsbt()
bool complete = FinalizeAndExtractPSBT(psbtx, mtx);
UniValue result(UniValue::VOBJ);
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
std::string result_str;
if (complete && extract) {
@ -1589,7 +1589,7 @@ static RPCHelpMan createpsbt()
}
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
return EncodeBase64(ssTx);
@ -1656,7 +1656,7 @@ static RPCHelpMan converttopsbt()
}
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
return EncodeBase64(ssTx);
@ -1703,7 +1703,7 @@ static RPCHelpMan utxoupdatepsbt()
/*sighash_type=*/SIGHASH_ALL,
/*finalize=*/false);
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
return EncodeBase64(ssTx);
},
@ -1804,7 +1804,7 @@ static RPCHelpMan joinpsbts()
}
shuffled_psbt.unknown.insert(merged_psbt.unknown.begin(), merged_psbt.unknown.end());
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << shuffled_psbt;
return EncodeBase64(ssTx);
},
@ -1984,7 +1984,7 @@ RPCHelpMan descriptorprocesspsbt()
complete &= PSBTInputSigned(input);
}
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
UniValue result(UniValue::VOBJ);

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,7 +68,7 @@ BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
{
CBlockHeaderAndShortTxIDs shortIDs{block};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
@ -119,7 +119,7 @@ public:
std::vector<PrefilledTransaction> prefilledtxn;
explicit TestHeaderAndShortIDs(const CBlockHeaderAndShortTxIDs& orig) {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << orig;
stream >> *this;
}
@ -127,7 +127,7 @@ public:
TestHeaderAndShortIDs(CBlockHeaderAndShortTxIDs{block}) {}
uint64_t GetShortID(const uint256& txhash) const {
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << *this;
CBlockHeaderAndShortTxIDs base;
stream >> base;
@ -158,7 +158,7 @@ BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[0]->GetHash());
shortIDs.shorttxids[1] = shortIDs.GetShortID(block.vtx[2]->GetHash());
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
shortIDs.shorttxids.resize(1);
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[1]->GetHash());
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;
@ -283,7 +283,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
{
CBlockHeaderAndShortTxIDs shortIDs{block};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
DataStream stream{};
stream << shortIDs;
CBlockHeaderAndShortTxIDs shortIDs2;

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

@ -1126,7 +1126,7 @@ public:
void SendV1Version(const MessageStartChars& magic)
{
CMessageHeader hdr(magic, "version", 126 + InsecureRandRange(11));
CDataStream ser(SER_NETWORK, CLIENT_VERSION);
DataStream ser{};
ser << hdr;
m_to_send.insert(m_to_send.end(), UCharCast(ser.data()), UCharCast(ser.data() + ser.size()));
}

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

@ -67,7 +67,7 @@ public:
ssKey.reserve(1000);
ssKey << key;
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
DataStream ssValue{};
if (!ReadKey(std::move(ssKey), ssValue)) return false;
try {
ssValue >> value;
@ -84,7 +84,7 @@ public:
ssKey.reserve(1000);
ssKey << key;
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
DataStream ssValue{};
ssValue.reserve(10000);
ssValue << value;

View file

@ -100,7 +100,7 @@ static UniValue FinishTransaction(const std::shared_ptr<CWallet> pwallet, const
bool add_to_wallet{options.exists("add_to_wallet") ? options["add_to_wallet"].get_bool() : true};
if (psbt_opt_in || !complete || !add_to_wallet) {
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
result.pushKV("psbt", EncodeBase64(ssTx.str()));
}
@ -1165,7 +1165,7 @@ static RPCHelpMan bumpfee_helper(std::string method_name)
const TransactionError err = pwallet->FillPSBT(psbtx, complete, SIGHASH_DEFAULT, /*sign=*/false, /*bip32derivs=*/true);
CHECK_NONFATAL(err == TransactionError::OK);
CHECK_NONFATAL(!complete);
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
result.pushKV("psbt", EncodeBase64(ssTx.str()));
}
@ -1610,7 +1610,7 @@ RPCHelpMan walletprocesspsbt()
}
UniValue result(UniValue::VOBJ);
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
result.pushKV("psbt", EncodeBase64(ssTx.str()));
result.pushKV("complete", complete);
@ -1737,7 +1737,7 @@ RPCHelpMan walletcreatefundedpsbt()
}
// Serialize the PSBT
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
UniValue result(UniValue::VOBJ);

View file

@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
BOOST_REQUIRE_EQUAL(TransactionError::OK, m_wallet.FillPSBT(psbtx, complete, SIGHASH_ALL, false, true));
// Get the final tx
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
DataStream ssTx{};
ssTx << psbtx;
std::string final_hex = HexStr(ssTx);
BOOST_CHECK_EQUAL(final_hex, "70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000104475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae2206029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f10d90c6a4f000000800000008000000080220602dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d710d90c6a4f0000008000000080010000800001008a020000000158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8876500000001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88701042200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903010547522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae2206023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7310d90c6a4f000000800000008003000080220603089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc10d90c6a4f00000080000000800200008000220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000");

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);
}

View file

@ -24,7 +24,7 @@ BOOST_AUTO_TEST_CASE(walletdb_readkeyvalue)
* silently fail. The test here makes sure the type of exception thrown from CDataStream::read()
* matches the type we expect, otherwise we need to update the "key"/"wkey" exception type caught.
*/
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
DataStream ssValue{};
uint256 dummy;
BOOST_CHECK_THROW(ssValue >> dummy, std::ios_base::failure);
}

View file

@ -478,12 +478,12 @@ struct LoadResult
int m_records{0};
};
using LoadFunc = std::function<DBErrors(CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err)>;
using LoadFunc = std::function<DBErrors(CWallet* pwallet, DataStream& key, DataStream& value, std::string& err)>;
static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std::string& key, DataStream& prefix, LoadFunc load_func)
{
LoadResult result;
DataStream ssKey;
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
DataStream ssValue{};
Assume(!prefix.empty());
std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
@ -532,7 +532,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
for (const auto& type : DBKeys::LEGACY_TYPES) {
DataStream key;
CDataStream value(SER_DISK, CLIENT_VERSION);
DataStream value{};
DataStream prefix;
prefix << type;
@ -555,28 +555,28 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// Load HD Chain
// Note: There should only be one HDCHAIN record with no data following the type
LoadResult hd_chain_res = LoadRecords(pwallet, batch, DBKeys::HDCHAIN,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
return LoadHDChain(pwallet, value, err) ? DBErrors:: LOAD_OK : DBErrors::CORRUPT;
});
result = std::max(result, hd_chain_res.m_result);
// Load unencrypted keys
LoadResult key_res = LoadRecords(pwallet, batch, DBKeys::KEY,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
return LoadKey(pwallet, key, value, err) ? DBErrors::LOAD_OK : DBErrors::CORRUPT;
});
result = std::max(result, key_res.m_result);
// Load encrypted keys
LoadResult ckey_res = LoadRecords(pwallet, batch, DBKeys::CRYPTED_KEY,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
return LoadCryptedKey(pwallet, key, value, err) ? DBErrors::LOAD_OK : DBErrors::CORRUPT;
});
result = std::max(result, ckey_res.m_result);
// Load scripts
LoadResult script_res = LoadRecords(pwallet, batch, DBKeys::CSCRIPT,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& strErr) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
uint160 hash;
key >> hash;
CScript script;
@ -599,7 +599,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// Load keymeta
std::map<uint160, CHDChain> hd_chains;
LoadResult keymeta_res = LoadRecords(pwallet, batch, DBKeys::KEYMETA,
[&hd_chains] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& strErr) {
[&hd_chains] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
CPubKey vchPubKey;
key >> vchPubKey;
CKeyMetadata keyMeta;
@ -686,7 +686,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// Load watchonly scripts
LoadResult watch_script_res = LoadRecords(pwallet, batch, DBKeys::WATCHS,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
CScript script;
key >> script;
uint8_t fYes;
@ -700,7 +700,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// Load watchonly meta
LoadResult watch_meta_res = LoadRecords(pwallet, batch, DBKeys::WATCHMETA,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
CScript script;
key >> script;
CKeyMetadata keyMeta;
@ -712,7 +712,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// Load keypool
LoadResult pool_res = LoadRecords(pwallet, batch, DBKeys::POOL,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
int64_t nIndex;
key >> nIndex;
CKeyPool keypool;
@ -729,7 +729,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// we want to make sure that it is valid so that we can detect corruption
// Note: There should only be one DEFAULTKEY with nothing trailing the type
LoadResult default_key_res = LoadRecords(pwallet, batch, DBKeys::DEFAULTKEY,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
CPubKey default_pubkey;
try {
value >> default_pubkey;
@ -747,7 +747,7 @@ static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch,
// "wkey" records are unsupported, if we see any, throw an error
LoadResult wkey_res = LoadRecords(pwallet, batch, DBKeys::OLD_KEY,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
err = "Found unsupported 'wkey' record, try loading with version 0.18";
return DBErrors::LOAD_FAIL;
});
@ -787,7 +787,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
int num_keys = 0;
int num_ckeys= 0;
LoadResult desc_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTOR,
[&batch, &num_keys, &num_ckeys, &last_client] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& strErr) {
[&batch, &num_keys, &num_ckeys, &last_client] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
DBErrors result = DBErrors::LOAD_OK;
uint256 id;
@ -817,7 +817,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
// Get key cache for this descriptor
DataStream prefix = PrefixStream(DBKeys::WALLETDESCRIPTORCACHE, id);
LoadResult key_cache_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORCACHE, prefix,
[&id, &cache] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[&id, &cache] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
bool parent = true;
uint256 desc_id;
uint32_t key_exp_index;
@ -850,7 +850,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
// Get last hardened cache for this descriptor
prefix = PrefixStream(DBKeys::WALLETDESCRIPTORLHCACHE, id);
LoadResult lh_cache_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORLHCACHE, prefix,
[&id, &cache] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[&id, &cache] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
uint256 desc_id;
uint32_t key_exp_index;
key >> desc_id;
@ -874,7 +874,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
// Get unencrypted keys
prefix = PrefixStream(DBKeys::WALLETDESCRIPTORKEY, id);
LoadResult key_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORKEY, prefix,
[&id, &spk_man] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& strErr) {
[&id, &spk_man] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
uint256 desc_id;
CPubKey pubkey;
key >> desc_id;
@ -918,7 +918,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
// Get encrypted keys
prefix = PrefixStream(DBKeys::WALLETDESCRIPTORCKEY, id);
LoadResult ckey_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORCKEY, prefix,
[&id, &spk_man] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[&id, &spk_man] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
uint256 desc_id;
CPubKey pubkey;
key >> desc_id;
@ -957,7 +957,7 @@ static DBErrors LoadAddressBookRecords(CWallet* pwallet, DatabaseBatch& batch) E
// Load name record
LoadResult name_res = LoadRecords(pwallet, batch, DBKeys::NAME,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
std::string strAddress;
key >> strAddress;
std::string label;
@ -969,7 +969,7 @@ static DBErrors LoadAddressBookRecords(CWallet* pwallet, DatabaseBatch& batch) E
// Load purpose record
LoadResult purpose_res = LoadRecords(pwallet, batch, DBKeys::PURPOSE,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
std::string strAddress;
key >> strAddress;
std::string purpose_str;
@ -985,7 +985,7 @@ static DBErrors LoadAddressBookRecords(CWallet* pwallet, DatabaseBatch& batch) E
// Load destination data record
LoadResult dest_res = LoadRecords(pwallet, batch, DBKeys::DESTDATA,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
std::string strAddress, strKey, strValue;
key >> strAddress;
key >> strKey;
@ -1019,7 +1019,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
// Load tx record
any_unordered = false;
LoadResult tx_res = LoadRecords(pwallet, batch, DBKeys::TX,
[&any_unordered, &upgraded_txs] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[&any_unordered, &upgraded_txs] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
DBErrors result = DBErrors::LOAD_OK;
uint256 hash;
key >> hash;
@ -1072,7 +1072,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
// Load locked utxo record
LoadResult locked_utxo_res = LoadRecords(pwallet, batch, DBKeys::LOCKED_UTXO,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
Txid hash;
uint32_t n;
key >> hash;
@ -1085,7 +1085,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
// Load orderposnext record
// Note: There should only be one ORDERPOSNEXT record with nothing trailing the type
LoadResult order_pos_res = LoadRecords(pwallet, batch, DBKeys::ORDERPOSNEXT,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
try {
value >> pwallet->nOrderPosNext;
} catch (const std::exception& e) {
@ -1108,7 +1108,7 @@ static DBErrors LoadActiveSPKMs(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIV
std::set<std::pair<OutputType, bool>> seen_spks;
for (const auto& spk_key : {DBKeys::ACTIVEEXTERNALSPK, DBKeys::ACTIVEINTERNALSPK}) {
LoadResult spkm_res = LoadRecords(pwallet, batch, spk_key,
[&seen_spks, &spk_key] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& strErr) {
[&seen_spks, &spk_key] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
uint8_t output_type;
key >> output_type;
uint256 id;
@ -1134,7 +1134,7 @@ static DBErrors LoadDecryptionKeys(CWallet* pwallet, DatabaseBatch& batch) EXCLU
// Load decryption key (mkey) records
LoadResult mkey_res = LoadRecords(pwallet, batch, DBKeys::MASTER_KEY,
[] (CWallet* pwallet, DataStream& key, CDataStream& value, std::string& err) {
[] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
if (!LoadEncryptionKey(pwallet, key, value, err)) {
return DBErrors::CORRUPT;
}