From fadf0a7e15d66ba3230153e789b785e6cf8ab84c Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Wed, 13 Dec 2023 12:24:21 +0100 Subject: [PATCH 1/2] refactor: Remove Span operator==, Use std::ranges::equal --- src/external_signer.cpp | 4 ++-- src/net.cpp | 5 ++--- src/netaddress.cpp | 4 ++-- src/script/descriptor.cpp | 7 ++++--- src/signet.cpp | 11 +++++----- src/span.h | 9 +-------- src/test/base32_tests.cpp | 4 +++- src/test/base64_tests.cpp | 4 +++- src/test/bip324_tests.cpp | 15 +++++++------- src/test/crypto_tests.cpp | 7 ++++--- src/test/fuzz/bip324.cpp | 7 ++++--- src/test/fuzz/hex.cpp | 3 ++- src/test/fuzz/miniscript.cpp | 4 +++- src/test/fuzz/p2p_transport_serialization.cpp | 7 ++++--- src/test/fuzz/poolresource.cpp | 2 +- src/test/fuzz/span.cpp | 6 ------ src/test/key_io_tests.cpp | 5 +++-- src/test/miniscript_tests.cpp | 11 +++++----- src/test/net_tests.cpp | 20 +++++++++---------- src/wallet/db.cpp | 5 +++-- 20 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/external_signer.cpp b/src/external_signer.cpp index ff159a2aa528f..1810f45bc862d 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -80,10 +80,10 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str // Check if signer fingerprint matches any input master key fingerprint auto matches_signer_fingerprint = [&](const PSBTInput& input) { for (const auto& entry : input.hd_keypaths) { - if (parsed_m_fingerprint == MakeUCharSpan(entry.second.fingerprint)) return true; + if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true; } for (const auto& entry : input.m_tap_bip32_paths) { - if (parsed_m_fingerprint == MakeUCharSpan(entry.second.second.fingerprint)) return true; + if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true; } return false; }; diff --git a/src/net.cpp b/src/net.cpp index acd78a3505a84..d296e1e532727 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -46,13 +46,12 @@ #include #include +#include #include #include #include #include -#include - /** Maximum number of block-relay-only anchor connections */ static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2; static_assert (MAX_BLOCK_RELAY_ONLY_ANCHORS <= static_cast(MAX_BLOCK_RELAY_ONLY_CONNECTIONS), "MAX_BLOCK_RELAY_ONLY_ANCHORS must not exceed MAX_BLOCK_RELAY_ONLY_CONNECTIONS."); @@ -1152,7 +1151,7 @@ bool V2Transport::ProcessReceivedGarbageBytes() noexcept Assume(m_recv_state == RecvState::GARB_GARBTERM); Assume(m_recv_buffer.size() <= MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN); if (m_recv_buffer.size() >= BIP324Cipher::GARBAGE_TERMINATOR_LEN) { - if (MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN) == m_cipher.GetReceiveGarbageTerminator()) { + if (std::ranges::equal(MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN), m_cipher.GetReceiveGarbageTerminator())) { // Garbage terminator received. Store garbage to authenticate it as AAD later. m_recv_aad = std::move(m_recv_buffer); m_recv_aad.resize(m_recv_aad.size() - BIP324Cipher::GARBAGE_TERMINATOR_LEN); diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 005346482291e..bd2353a712176 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -245,14 +245,14 @@ bool CNetAddr::SetTor(const std::string& addr) Span input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN}; Span input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)}; - if (input_version != torv3::VERSION) { + if (!std::ranges::equal(input_version, torv3::VERSION)) { return false; } uint8_t calculated_checksum[torv3::CHECKSUM_LEN]; torv3::Checksum(input_pubkey, calculated_checksum); - if (input_checksum != calculated_checksum) { + if (!std::ranges::equal(input_checksum, calculated_checksum)) { return false; } diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index ae9dba6a50268..83b07ae459bc4 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -1405,11 +1406,11 @@ std::unique_ptr ParsePubkeyInner(uint32_t key_exp_index, const S } KeyPath path; DeriveType type = DeriveType::NO; - if (split.back() == Span{"*"}.first(1)) { + if (std::ranges::equal(split.back(), Span{"*"}.first(1))) { split.pop_back(); type = DeriveType::UNHARDENED; - } else if (split.back() == Span{"*'"}.first(2) || split.back() == Span{"*h"}.first(2)) { - apostrophe = split.back() == Span{"*'"}.first(2); + } else if (std::ranges::equal(split.back(), Span{"*'"}.first(2)) || std::ranges::equal(split.back(), Span{"*h"}.first(2))) { + apostrophe = std::ranges::equal(split.back(), Span{"*'"}.first(2)); split.pop_back(); type = DeriveType::HARDENED; } diff --git a/src/signet.cpp b/src/signet.cpp index 7c193a1d77b46..11e89c7aadeba 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -4,10 +4,6 @@ #include -#include -#include -#include - #include #include #include @@ -23,6 +19,11 @@ #include #include +#include +#include +#include +#include + static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2}; static constexpr unsigned int BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY; @@ -38,7 +39,7 @@ static bool FetchAndClearCommitmentSection(const Span header, CSc std::vector pushdata; while (witness_commitment.GetOp(pc, opcode, pushdata)) { if (pushdata.size() > 0) { - if (!found_header && pushdata.size() > (size_t)header.size() && Span{pushdata}.first(header.size()) == header) { + if (!found_header && pushdata.size() > header.size() && std::ranges::equal(Span{pushdata}.first(header.size()), header)) { // pushdata only counts if it has the header _and_ some data result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end()); pushdata.erase(pushdata.begin() + header.size(), pushdata.end()); diff --git a/src/span.h b/src/span.h index c974c265ceccd..3c5028f0b79cb 100644 --- a/src/span.h +++ b/src/span.h @@ -5,11 +5,11 @@ #ifndef BITCOIN_SPAN_H #define BITCOIN_SPAN_H -#include #include #include #include #include +#include #ifdef DEBUG #define CONSTEXPR_IF_NOT_DEBUG @@ -213,13 +213,6 @@ class Span return Span(m_data + m_size - count, count); } - friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); } - friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); } - friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); } - friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); } - friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); } - friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); } - template friend class Span; }; diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp index 4617beecd999e..be3b0c2d1f380 100644 --- a/src/test/base32_tests.cpp +++ b/src/test/base32_tests.cpp @@ -5,6 +5,8 @@ #include #include + +#include #include using namespace std::literals; @@ -24,7 +26,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors) BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]); auto dec = DecodeBase32(vstrOut[i]); BOOST_REQUIRE(dec); - BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]); + BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]); } // Decoding strings with embedded NUL characters should fail diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp index 6462aa82fb66c..b9d0d2b24125a 100644 --- a/src/test/base64_tests.cpp +++ b/src/test/base64_tests.cpp @@ -5,6 +5,8 @@ #include #include + +#include #include using namespace std::literals; @@ -21,7 +23,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors) BOOST_CHECK_EQUAL(strEnc, vstrOut[i]); auto dec = DecodeBase64(strEnc); BOOST_REQUIRE(dec); - BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]); + BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]); } { diff --git a/src/test/bip324_tests.cpp b/src/test/bip324_tests.cpp index 1ed7e23bcf93f..1caea4000c306 100644 --- a/src/test/bip324_tests.cpp +++ b/src/test/bip324_tests.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -62,9 +63,9 @@ void TestBIP324PacketVector( BOOST_CHECK(cipher); // Compare session variables. - BOOST_CHECK(Span{out_session_id} == cipher.GetSessionID()); - BOOST_CHECK(Span{mid_send_garbage} == cipher.GetSendGarbageTerminator()); - BOOST_CHECK(Span{mid_recv_garbage} == cipher.GetReceiveGarbageTerminator()); + BOOST_CHECK(std::ranges::equal(out_session_id, cipher.GetSessionID())); + BOOST_CHECK(std::ranges::equal(mid_send_garbage, cipher.GetSendGarbageTerminator())); + BOOST_CHECK(std::ranges::equal(mid_recv_garbage, cipher.GetReceiveGarbageTerminator())); // Vector of encrypted empty messages, encrypted in order to seek to the right position. std::vector> dummies(in_idx); @@ -89,7 +90,7 @@ void TestBIP324PacketVector( BOOST_CHECK(out_ciphertext == ciphertext); } else { BOOST_CHECK(ciphertext.size() >= out_ciphertext_endswith.size()); - BOOST_CHECK(Span{out_ciphertext_endswith} == Span{ciphertext}.last(out_ciphertext_endswith.size())); + BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, Span{ciphertext}.last(out_ciphertext_endswith.size()))); } for (unsigned error = 0; error <= 12; ++error) { @@ -109,9 +110,9 @@ void TestBIP324PacketVector( BOOST_CHECK(dec_cipher); // Compare session variables. - BOOST_CHECK((Span{out_session_id} == dec_cipher.GetSessionID()) == (error != 1)); - BOOST_CHECK((Span{mid_send_garbage} == dec_cipher.GetSendGarbageTerminator()) == (error != 1)); - BOOST_CHECK((Span{mid_recv_garbage} == dec_cipher.GetReceiveGarbageTerminator()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(out_session_id, dec_cipher.GetSessionID()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(mid_send_garbage, dec_cipher.GetSendGarbageTerminator()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(mid_recv_garbage, dec_cipher.GetReceiveGarbageTerminator()) == (error != 1)); // Seek to the numbered packet. if (in_idx == 0 && error == 12) continue; diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index f84e04e819bdd..9b485a24e21f2 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -833,9 +834,9 @@ BOOST_AUTO_TEST_CASE(chacha20_midblock) c20.Keystream(b2); c20.Keystream(b3); - BOOST_CHECK(Span{block}.first(5) == Span{b1}); - BOOST_CHECK(Span{block}.subspan(5, 7) == Span{b2}); - BOOST_CHECK(Span{block}.last(52) == Span{b3}); + BOOST_CHECK(std::ranges::equal(Span{block}.first(5), b1)); + BOOST_CHECK(std::ranges::equal(Span{block}.subspan(5, 7), b2)); + BOOST_CHECK(std::ranges::equal(Span{block}.last(52), b3)); } BOOST_AUTO_TEST_CASE(poly1305_testvector) diff --git a/src/test/fuzz/bip324.cpp b/src/test/fuzz/bip324.cpp index 9892e7a81ce78..f1fa15d8a3ee1 100644 --- a/src/test/fuzz/bip324.cpp +++ b/src/test/fuzz/bip324.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -59,9 +60,9 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize) InsecureRandomContext rng(provider.ConsumeIntegral()); // Compare session IDs and garbage terminators. - assert(initiator.GetSessionID() == responder.GetSessionID()); - assert(initiator.GetSendGarbageTerminator() == responder.GetReceiveGarbageTerminator()); - assert(initiator.GetReceiveGarbageTerminator() == responder.GetSendGarbageTerminator()); + assert(std::ranges::equal(initiator.GetSessionID(), responder.GetSessionID())); + assert(std::ranges::equal(initiator.GetSendGarbageTerminator(), responder.GetReceiveGarbageTerminator())); + assert(std::ranges::equal(initiator.GetReceiveGarbageTerminator(), responder.GetSendGarbageTerminator())); LIMITED_WHILE(provider.remaining_bytes(), 1000) { // Mode: diff --git a/src/test/fuzz/hex.cpp b/src/test/fuzz/hex.cpp index ebe30c3c1aa8b..174d2a22899b2 100644 --- a/src/test/fuzz/hex.cpp +++ b/src/test/fuzz/hex.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -22,7 +23,7 @@ FUZZ_TARGET(hex) const std::string random_hex_string(buffer.begin(), buffer.end()); const std::vector data = ParseHex(random_hex_string); const std::vector bytes{ParseHex(random_hex_string)}; - assert(AsBytes(Span{data}) == Span{bytes}); + assert(std::ranges::equal(AsBytes(Span{data}), bytes)); const std::string hex_data = HexStr(data); if (IsHex(random_hex_string)) { assert(ToLower(random_hex_string) == hex_data); diff --git a/src/test/fuzz/miniscript.cpp b/src/test/fuzz/miniscript.cpp index 1f9ed9a0640f3..5b9e168856aed 100644 --- a/src/test/fuzz/miniscript.cpp +++ b/src/test/fuzz/miniscript.cpp @@ -13,6 +13,8 @@ #include #include +#include + namespace { using Fragment = miniscript::Fragment; @@ -293,7 +295,7 @@ const struct CheckerContext: BaseSignatureChecker { XOnlyPubKey pk{pubkey}; auto it = TEST_DATA.schnorr_sigs.find(pk); if (it == TEST_DATA.schnorr_sigs.end()) return false; - return it->second.first == sig; + return std::ranges::equal(it->second.first, sig); } bool CheckLockTime(const CScriptNum& nLockTime) const override { return nLockTime.GetInt64() & 1; } bool CheckSequence(const CScriptNum& nSequence) const override { return nSequence.GetInt64() & 1; } diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp index 93f77b6e5be12..cf3ef45c0a42b 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -185,12 +186,12 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa // Compare with expected more. if (expect_more[side].has_value()) assert(!bytes.empty() == *expect_more[side]); // Verify consistency between the two results. - assert(bytes == bytes_next); + assert(std::ranges::equal(bytes, bytes_next)); assert(msg_type == msg_type_next); if (more_nonext) assert(more_next); // Compare with previously reported output. assert(to_send[side].size() <= bytes.size()); - assert(to_send[side] == Span{bytes}.first(to_send[side].size())); + assert(std::ranges::equal(to_send[side], Span{bytes}.first(to_send[side].size()))); to_send[side].resize(bytes.size()); std::copy(bytes.begin(), bytes.end(), to_send[side].begin()); // Remember 'more' results. @@ -278,7 +279,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa // The m_type must match what is expected. assert(received.m_type == expected[side].front().m_type); // The data must match what is expected. - assert(MakeByteSpan(received.m_recv) == MakeByteSpan(expected[side].front().data)); + assert(std::ranges::equal(received.m_recv, MakeByteSpan(expected[side].front().data))); expected[side].pop_front(); progress = true; } diff --git a/src/test/fuzz/poolresource.cpp b/src/test/fuzz/poolresource.cpp index 28bf7175c0818..dd8d5b07e5f72 100644 --- a/src/test/fuzz/poolresource.cpp +++ b/src/test/fuzz/poolresource.cpp @@ -78,7 +78,7 @@ class PoolResourceFuzzer { std::vector expect(entry.span.size()); InsecureRandomContext(entry.seed).fillrand(expect); - assert(entry.span == expect); + assert(std::ranges::equal(entry.span, expect)); } void Deallocate(const Entry& entry) diff --git a/src/test/fuzz/span.cpp b/src/test/fuzz/span.cpp index 8f753948df3d9..cd436d582fca7 100644 --- a/src/test/fuzz/span.cpp +++ b/src/test/fuzz/span.cpp @@ -30,10 +30,4 @@ FUZZ_TARGET(span) (void)span.subspan(idx, span.size() - idx); (void)span[idx]; } - - std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32); - const Span another_span{another_str}; - assert((span <= another_span) != (span > another_span)); - assert((span == another_span) != (span != another_span)); - assert((span >= another_span) != (span < another_span)); } diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 66b4e09ebf80d..4dd77edc162a6 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -10,12 +10,13 @@ #include