From fac39b56b723f79ba0aa9570708a1d4a600f4e12 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 17 Nov 2023 16:32:29 +0100 Subject: [PATCH 1/2] refactor: SpanReader without nVersion The field is unused, so remove it. This is also required for future commits. --- src/blockfilter.cpp | 11 ++++------- src/psbt.h | 12 ++++++------ src/signet.cpp | 3 +-- src/streams.h | 7 +------ src/test/fuzz/golomb_rice.cpp | 8 ++++---- src/test/fuzz/script_assets_test_minimizer.cpp | 4 ++-- src/test/script_tests.cpp | 6 +++--- src/test/streams_tests.cpp | 6 +++--- src/wallet/test/wallet_tests.cpp | 2 +- 9 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp index 404c9ecc4f..e045b88513 100644 --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -16,9 +16,6 @@ #include #include -/// Protocol version used to serialize parameters in GCS filter encoding. -static constexpr int GCS_SER_VERSION = 0; - static const std::map g_filter_types = { {BlockFilterType::BASIC, "basic"}, }; @@ -49,7 +46,7 @@ GCSFilter::GCSFilter(const Params& params) GCSFilter::GCSFilter(const Params& params, std::vector 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(N); @@ -62,7 +59,7 @@ GCSFilter::GCSFilter(const Params& params, std::vector 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 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 bitreader{stream}; + BitStreamReader bitreader{stream}; uint64_t value = 0; size_t hashes_index = 0; diff --git a/src/psbt.h b/src/psbt.h index ac2d246a27..3f74083717 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -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 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 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 diff --git a/src/signet.cpp b/src/signet.cpp index ef9913218a..ebf0de09d3 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -22,7 +22,6 @@ #include #include #include -#include static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2}; @@ -99,7 +98,7 @@ std::optional 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 diff --git a/src/streams.h b/src/streams.h index 5fec289e34..39e9a56f11 100644 --- a/src/streams.h +++ b/src/streams.h @@ -100,16 +100,13 @@ private: class SpanReader { private: - const int m_version; Span m_data; public: /** - * @param[in] version Serialization Version (including any flags) * @param[in] data Referenced byte vector to overwrite/append */ - SpanReader(int version, Span data) - : m_version{version}, m_data{data} {} + explicit SpanReader(Span data) : m_data{data} {} template 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(); } diff --git a/src/test/fuzz/golomb_rice.cpp b/src/test/fuzz/golomb_rice.cpp index 8b395f3ea9..92e49445ba 100644 --- a/src/test/fuzz/golomb_rice.cpp +++ b/src/test/fuzz/golomb_rice.cpp @@ -68,8 +68,8 @@ FUZZ_TARGET(golomb_rice) std::vector decoded_deltas; { - SpanReader stream{0, golomb_rice_data}; - BitStreamReader bitreader{stream}; + SpanReader stream{golomb_rice_data}; + BitStreamReader bitreader{stream}; const uint32_t n = static_cast(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 random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024); - SpanReader stream{0, random_bytes}; + SpanReader stream{random_bytes}; uint32_t n; try { n = static_cast(ReadCompactSize(stream)); } catch (const std::ios_base::failure&) { return; } - BitStreamReader bitreader{stream}; + BitStreamReader bitreader{stream}; for (uint32_t i = 0; i < std::min(n, 1024); ++i) { try { (void)GolombRiceDecode(bitreader, BASIC_FILTER_P); diff --git a/src/test/fuzz/script_assets_test_minimizer.cpp b/src/test/fuzz/script_assets_test_minimizer.cpp index 55a4fbc75c..511b581f60 100644 --- a/src/test/fuzz/script_assets_test_minimizer.cpp +++ b/src/test/fuzz/script_assets_test_minimizer.cpp @@ -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 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"); } diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index 874f3e51f7..e988ce2194 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -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 TxOutsFromJSON(const UniValue& univalue) std::vector 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 utxos; for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) { auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str()); diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 693247b8d4..7d1ac5a19a 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader) { std::vector 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 data{0x82, 0xa7, 0x31}; - SpanReader reader{INIT_PROTO_VERSION, data}; + SpanReader reader{data}; uint32_t varint = 0; // Deserialize into r-value reader >> VARINT(varint); diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 6d7b9100b5..dd43705a84 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -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); } From fae76a1f2ac0b352cdd708e6d57f8605bf40112e Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Sun, 19 Nov 2023 14:58:00 +0100 Subject: [PATCH 2/2] scripted-diff: Use DataStream in most places The remaining places are handled easier outside a scripted-diff. -BEGIN VERIFY SCRIPT- sed --regexp-extended -i 's/CDataStream ([0-9a-zA-Z_]+)\(SER_[A-Z]+, [A-Z_]+_VERSION\);/DataStream \1{};/g' $( git grep -l CDataStream) sed -i 's/, CDataStream/, DataStream/g' src/wallet/walletdb.cpp -END VERIFY SCRIPT- --- src/external_signer.cpp | 2 +- src/qt/psbtoperationsdialog.cpp | 4 +-- src/qt/sendcoinsdialog.cpp | 2 +- src/qt/walletmodel.cpp | 2 +- src/rpc/rawtransaction.cpp | 14 ++++---- src/test/blockencodings_tests.cpp | 12 +++---- src/test/net_tests.cpp | 2 +- src/wallet/db.h | 4 +-- src/wallet/rpc/spend.cpp | 8 ++--- src/wallet/test/psbt_wallet_tests.cpp | 2 +- src/wallet/test/walletdb_tests.cpp | 2 +- src/wallet/walletdb.cpp | 52 +++++++++++++-------------- 12 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/external_signer.cpp b/src/external_signer.cpp index 15024f6e0f..8cf57cde92 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -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 parsed_m_fingerprint = ParseHex(m_fingerprint); diff --git a/src/qt/psbtoperationsdialog.cpp b/src/qt/psbtoperationsdialog.cpp index 3c2c7fe435..353709c7f5 100644 --- a/src/qt/psbtoperationsdialog.cpp +++ b/src/qt/psbtoperationsdialog.cpp @@ -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; diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 6614ed91a7..3c331c70ef 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -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); diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 445495897d..9821f8504a 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -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); diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index e8bcbd8a2b..56ef8a5efc 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -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); diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index 7968966303..763f0f897e 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -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 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; diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 508c42cabc..32002624fd 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -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())); } diff --git a/src/wallet/db.h b/src/wallet/db.h index 9d684225c3..c263f54144 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -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; diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index 84e617c140..aa6db11d1c 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -100,7 +100,7 @@ static UniValue FinishTransaction(const std::shared_ptr 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); diff --git a/src/wallet/test/psbt_wallet_tests.cpp b/src/wallet/test/psbt_wallet_tests.cpp index b46f361363..5c754ff26b 100644 --- a/src/wallet/test/psbt_wallet_tests.cpp +++ b/src/wallet/test/psbt_wallet_tests.cpp @@ -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"); diff --git a/src/wallet/test/walletdb_tests.cpp b/src/wallet/test/walletdb_tests.cpp index 17b6c4f7ed..0d27f99d37 100644 --- a/src/wallet/test/walletdb_tests.cpp +++ b/src/wallet/test/walletdb_tests.cpp @@ -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); } diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 66da5c1dc4..96999de97b 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -478,12 +478,12 @@ struct LoadResult int m_records{0}; }; -using LoadFunc = std::function; +using LoadFunc = std::function; 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 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 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> 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; }