Skip to content

Commit fadf0a7

Browse files
author
MarcoFalke
committed
refactor: Remove Span operator==, Use std::ranges::equal
1 parent 1873e41 commit fadf0a7

20 files changed

+71
-69
lines changed

Diff for: src/external_signer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
8080
// Check if signer fingerprint matches any input master key fingerprint
8181
auto matches_signer_fingerprint = [&](const PSBTInput& input) {
8282
for (const auto& entry : input.hd_keypaths) {
83-
if (parsed_m_fingerprint == MakeUCharSpan(entry.second.fingerprint)) return true;
83+
if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;
8484
}
8585
for (const auto& entry : input.m_tap_bip32_paths) {
86-
if (parsed_m_fingerprint == MakeUCharSpan(entry.second.second.fingerprint)) return true;
86+
if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;
8787
}
8888
return false;
8989
};

Diff for: src/net.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@
4646

4747
#include <algorithm>
4848
#include <array>
49+
#include <cmath>
4950
#include <cstdint>
5051
#include <functional>
5152
#include <optional>
5253
#include <unordered_map>
5354

54-
#include <math.h>
55-
5655
/** Maximum number of block-relay-only anchor connections */
5756
static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2;
5857
static_assert (MAX_BLOCK_RELAY_ONLY_ANCHORS <= static_cast<size_t>(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
11521151
Assume(m_recv_state == RecvState::GARB_GARBTERM);
11531152
Assume(m_recv_buffer.size() <= MAX_GARBAGE_LEN + BIP324Cipher::GARBAGE_TERMINATOR_LEN);
11541153
if (m_recv_buffer.size() >= BIP324Cipher::GARBAGE_TERMINATOR_LEN) {
1155-
if (MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN) == m_cipher.GetReceiveGarbageTerminator()) {
1154+
if (std::ranges::equal(MakeByteSpan(m_recv_buffer).last(BIP324Cipher::GARBAGE_TERMINATOR_LEN), m_cipher.GetReceiveGarbageTerminator())) {
11561155
// Garbage terminator received. Store garbage to authenticate it as AAD later.
11571156
m_recv_aad = std::move(m_recv_buffer);
11581157
m_recv_aad.resize(m_recv_aad.size() - BIP324Cipher::GARBAGE_TERMINATOR_LEN);

Diff for: src/netaddress.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,14 @@ bool CNetAddr::SetTor(const std::string& addr)
245245
Span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
246246
Span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
247247

248-
if (input_version != torv3::VERSION) {
248+
if (!std::ranges::equal(input_version, torv3::VERSION)) {
249249
return false;
250250
}
251251

252252
uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
253253
torv3::Checksum(input_pubkey, calculated_checksum);
254254

255-
if (input_checksum != calculated_checksum) {
255+
if (!std::ranges::equal(input_checksum, calculated_checksum)) {
256256
return false;
257257
}
258258

Diff for: src/script/descriptor.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <util/strencodings.h>
2222
#include <util/vector.h>
2323

24+
#include <algorithm>
2425
#include <memory>
2526
#include <numeric>
2627
#include <optional>
@@ -1405,11 +1406,11 @@ std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const S
14051406
}
14061407
KeyPath path;
14071408
DeriveType type = DeriveType::NO;
1408-
if (split.back() == Span{"*"}.first(1)) {
1409+
if (std::ranges::equal(split.back(), Span{"*"}.first(1))) {
14091410
split.pop_back();
14101411
type = DeriveType::UNHARDENED;
1411-
} else if (split.back() == Span{"*'"}.first(2) || split.back() == Span{"*h"}.first(2)) {
1412-
apostrophe = split.back() == Span{"*'"}.first(2);
1412+
} else if (std::ranges::equal(split.back(), Span{"*'"}.first(2)) || std::ranges::equal(split.back(), Span{"*h"}.first(2))) {
1413+
apostrophe = std::ranges::equal(split.back(), Span{"*'"}.first(2));
14131414
split.pop_back();
14141415
type = DeriveType::HARDENED;
14151416
}

Diff for: src/signet.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
#include <signet.h>
66

7-
#include <array>
8-
#include <cstdint>
9-
#include <vector>
10-
117
#include <common/system.h>
128
#include <consensus/merkle.h>
139
#include <consensus/params.h>
@@ -23,6 +19,11 @@
2319
#include <uint256.h>
2420
#include <util/strencodings.h>
2521

22+
#include <algorithm>
23+
#include <array>
24+
#include <cstdint>
25+
#include <vector>
26+
2627
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
2728

2829
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<const uint8_t> header, CSc
3839
std::vector<uint8_t> pushdata;
3940
while (witness_commitment.GetOp(pc, opcode, pushdata)) {
4041
if (pushdata.size() > 0) {
41-
if (!found_header && pushdata.size() > (size_t)header.size() && Span{pushdata}.first(header.size()) == header) {
42+
if (!found_header && pushdata.size() > header.size() && std::ranges::equal(Span{pushdata}.first(header.size()), header)) {
4243
// pushdata only counts if it has the header _and_ some data
4344
result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
4445
pushdata.erase(pushdata.begin() + header.size(), pushdata.end());

Diff for: src/span.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
#ifndef BITCOIN_SPAN_H
66
#define BITCOIN_SPAN_H
77

8-
#include <algorithm>
98
#include <cassert>
109
#include <cstddef>
1110
#include <span>
1211
#include <type_traits>
12+
#include <utility>
1313

1414
#ifdef DEBUG
1515
#define CONSTEXPR_IF_NOT_DEBUG
@@ -213,13 +213,6 @@ class Span
213213
return Span<C>(m_data + m_size - count, count);
214214
}
215215

216-
friend constexpr bool operator==(const Span& a, const Span& b) noexcept { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); }
217-
friend constexpr bool operator!=(const Span& a, const Span& b) noexcept { return !(a == b); }
218-
friend constexpr bool operator<(const Span& a, const Span& b) noexcept { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
219-
friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); }
220-
friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); }
221-
friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); }
222-
223216
template <typename O> friend class Span;
224217
};
225218

Diff for: src/test/base32_tests.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <util/strencodings.h>
66

77
#include <boost/test/unit_test.hpp>
8+
9+
#include <algorithm>
810
#include <string>
911

1012
using namespace std::literals;
@@ -24,7 +26,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
2426
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
2527
auto dec = DecodeBase32(vstrOut[i]);
2628
BOOST_REQUIRE(dec);
27-
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
29+
BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
2830
}
2931

3032
// Decoding strings with embedded NUL characters should fail

Diff for: src/test/base64_tests.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <util/strencodings.h>
66

77
#include <boost/test/unit_test.hpp>
8+
9+
#include <algorithm>
810
#include <string>
911

1012
using namespace std::literals;
@@ -21,7 +23,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
2123
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
2224
auto dec = DecodeBase64(strEnc);
2325
BOOST_REQUIRE(dec);
24-
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
26+
BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
2527
}
2628

2729
{

Diff for: src/test/bip324_tests.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <test/util/setup_common.h>
1212
#include <util/strencodings.h>
1313

14+
#include <algorithm>
1415
#include <array>
1516
#include <cstddef>
1617
#include <cstdint>
@@ -62,9 +63,9 @@ void TestBIP324PacketVector(
6263
BOOST_CHECK(cipher);
6364

6465
// Compare session variables.
65-
BOOST_CHECK(Span{out_session_id} == cipher.GetSessionID());
66-
BOOST_CHECK(Span{mid_send_garbage} == cipher.GetSendGarbageTerminator());
67-
BOOST_CHECK(Span{mid_recv_garbage} == cipher.GetReceiveGarbageTerminator());
66+
BOOST_CHECK(std::ranges::equal(out_session_id, cipher.GetSessionID()));
67+
BOOST_CHECK(std::ranges::equal(mid_send_garbage, cipher.GetSendGarbageTerminator()));
68+
BOOST_CHECK(std::ranges::equal(mid_recv_garbage, cipher.GetReceiveGarbageTerminator()));
6869

6970
// Vector of encrypted empty messages, encrypted in order to seek to the right position.
7071
std::vector<std::vector<std::byte>> dummies(in_idx);
@@ -89,7 +90,7 @@ void TestBIP324PacketVector(
8990
BOOST_CHECK(out_ciphertext == ciphertext);
9091
} else {
9192
BOOST_CHECK(ciphertext.size() >= out_ciphertext_endswith.size());
92-
BOOST_CHECK(Span{out_ciphertext_endswith} == Span{ciphertext}.last(out_ciphertext_endswith.size()));
93+
BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, Span{ciphertext}.last(out_ciphertext_endswith.size())));
9394
}
9495

9596
for (unsigned error = 0; error <= 12; ++error) {
@@ -109,9 +110,9 @@ void TestBIP324PacketVector(
109110
BOOST_CHECK(dec_cipher);
110111

111112
// Compare session variables.
112-
BOOST_CHECK((Span{out_session_id} == dec_cipher.GetSessionID()) == (error != 1));
113-
BOOST_CHECK((Span{mid_send_garbage} == dec_cipher.GetSendGarbageTerminator()) == (error != 1));
114-
BOOST_CHECK((Span{mid_recv_garbage} == dec_cipher.GetReceiveGarbageTerminator()) == (error != 1));
113+
BOOST_CHECK(std::ranges::equal(out_session_id, dec_cipher.GetSessionID()) == (error != 1));
114+
BOOST_CHECK(std::ranges::equal(mid_send_garbage, dec_cipher.GetSendGarbageTerminator()) == (error != 1));
115+
BOOST_CHECK(std::ranges::equal(mid_recv_garbage, dec_cipher.GetReceiveGarbageTerminator()) == (error != 1));
115116

116117
// Seek to the numbered packet.
117118
if (in_idx == 0 && error == 12) continue;

Diff for: src/test/crypto_tests.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <test/util/setup_common.h>
2222
#include <util/strencodings.h>
2323

24+
#include <algorithm>
2425
#include <vector>
2526

2627
#include <boost/test/unit_test.hpp>
@@ -833,9 +834,9 @@ BOOST_AUTO_TEST_CASE(chacha20_midblock)
833834
c20.Keystream(b2);
834835
c20.Keystream(b3);
835836

836-
BOOST_CHECK(Span{block}.first(5) == Span{b1});
837-
BOOST_CHECK(Span{block}.subspan(5, 7) == Span{b2});
838-
BOOST_CHECK(Span{block}.last(52) == Span{b3});
837+
BOOST_CHECK(std::ranges::equal(Span{block}.first(5), b1));
838+
BOOST_CHECK(std::ranges::equal(Span{block}.subspan(5, 7), b2));
839+
BOOST_CHECK(std::ranges::equal(Span{block}.last(52), b3));
839840
}
840841

841842
BOOST_AUTO_TEST_CASE(poly1305_testvector)

Diff for: src/test/fuzz/bip324.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <test/fuzz/fuzz.h>
1111
#include <test/fuzz/util.h>
1212

13+
#include <algorithm>
1314
#include <cstdint>
1415
#include <vector>
1516

@@ -59,9 +60,9 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize)
5960
InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>());
6061

6162
// Compare session IDs and garbage terminators.
62-
assert(initiator.GetSessionID() == responder.GetSessionID());
63-
assert(initiator.GetSendGarbageTerminator() == responder.GetReceiveGarbageTerminator());
64-
assert(initiator.GetReceiveGarbageTerminator() == responder.GetSendGarbageTerminator());
63+
assert(std::ranges::equal(initiator.GetSessionID(), responder.GetSessionID()));
64+
assert(std::ranges::equal(initiator.GetSendGarbageTerminator(), responder.GetReceiveGarbageTerminator()));
65+
assert(std::ranges::equal(initiator.GetReceiveGarbageTerminator(), responder.GetSendGarbageTerminator()));
6566

6667
LIMITED_WHILE(provider.remaining_bytes(), 1000) {
6768
// Mode:

Diff for: src/test/fuzz/hex.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <util/strencodings.h>
1313
#include <util/transaction_identifier.h>
1414

15+
#include <algorithm>
1516
#include <cassert>
1617
#include <cstdint>
1718
#include <string>
@@ -22,7 +23,7 @@ FUZZ_TARGET(hex)
2223
const std::string random_hex_string(buffer.begin(), buffer.end());
2324
const std::vector<unsigned char> data = ParseHex(random_hex_string);
2425
const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)};
25-
assert(AsBytes(Span{data}) == Span{bytes});
26+
assert(std::ranges::equal(AsBytes(Span{data}), bytes));
2627
const std::string hex_data = HexStr(data);
2728
if (IsHex(random_hex_string)) {
2829
assert(ToLower(random_hex_string) == hex_data);

Diff for: src/test/fuzz/miniscript.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <test/fuzz/util.h>
1414
#include <util/strencodings.h>
1515

16+
#include <algorithm>
17+
1618
namespace {
1719

1820
using Fragment = miniscript::Fragment;
@@ -293,7 +295,7 @@ const struct CheckerContext: BaseSignatureChecker {
293295
XOnlyPubKey pk{pubkey};
294296
auto it = TEST_DATA.schnorr_sigs.find(pk);
295297
if (it == TEST_DATA.schnorr_sigs.end()) return false;
296-
return it->second.first == sig;
298+
return std::ranges::equal(it->second.first, sig);
297299
}
298300
bool CheckLockTime(const CScriptNum& nLockTime) const override { return nLockTime.GetInt64() & 1; }
299301
bool CheckSequence(const CScriptNum& nSequence) const override { return nSequence.GetInt64() & 1; }

Diff for: src/test/fuzz/p2p_transport_serialization.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <test/fuzz/util.h>
1313
#include <util/chaintype.h>
1414

15+
#include <algorithm>
1516
#include <cassert>
1617
#include <cstdint>
1718
#include <limits>
@@ -185,12 +186,12 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
185186
// Compare with expected more.
186187
if (expect_more[side].has_value()) assert(!bytes.empty() == *expect_more[side]);
187188
// Verify consistency between the two results.
188-
assert(bytes == bytes_next);
189+
assert(std::ranges::equal(bytes, bytes_next));
189190
assert(msg_type == msg_type_next);
190191
if (more_nonext) assert(more_next);
191192
// Compare with previously reported output.
192193
assert(to_send[side].size() <= bytes.size());
193-
assert(to_send[side] == Span{bytes}.first(to_send[side].size()));
194+
assert(std::ranges::equal(to_send[side], Span{bytes}.first(to_send[side].size())));
194195
to_send[side].resize(bytes.size());
195196
std::copy(bytes.begin(), bytes.end(), to_send[side].begin());
196197
// Remember 'more' results.
@@ -278,7 +279,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa
278279
// The m_type must match what is expected.
279280
assert(received.m_type == expected[side].front().m_type);
280281
// The data must match what is expected.
281-
assert(MakeByteSpan(received.m_recv) == MakeByteSpan(expected[side].front().data));
282+
assert(std::ranges::equal(received.m_recv, MakeByteSpan(expected[side].front().data)));
282283
expected[side].pop_front();
283284
progress = true;
284285
}

Diff for: src/test/fuzz/poolresource.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class PoolResourceFuzzer
7878
{
7979
std::vector<std::byte> expect(entry.span.size());
8080
InsecureRandomContext(entry.seed).fillrand(expect);
81-
assert(entry.span == expect);
81+
assert(std::ranges::equal(entry.span, expect));
8282
}
8383

8484
void Deallocate(const Entry& entry)

Diff for: src/test/fuzz/span.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,4 @@ FUZZ_TARGET(span)
3030
(void)span.subspan(idx, span.size() - idx);
3131
(void)span[idx];
3232
}
33-
34-
std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32);
35-
const Span<const char> another_span{another_str};
36-
assert((span <= another_span) != (span > another_span));
37-
assert((span == another_span) != (span != another_span));
38-
assert((span >= another_span) != (span < another_span));
3933
}

Diff for: src/test/key_io_tests.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#include <script/script.h>
1111
#include <test/util/json.h>
1212
#include <test/util/setup_common.h>
13+
#include <univalue.h>
1314
#include <util/chaintype.h>
1415
#include <util/strencodings.h>
1516

1617
#include <boost/test/unit_test.hpp>
1718

18-
#include <univalue.h>
19+
#include <algorithm>
1920

2021
BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup)
2122

@@ -46,7 +47,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
4647
privkey = DecodeSecret(exp_base58string);
4748
BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
4849
BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
49-
BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest);
50+
BOOST_CHECK_MESSAGE(std::ranges::equal(privkey, exp_payload), "key mismatch:" + strTest);
5051

5152
// Private key must be invalid public key
5253
destination = DecodeDestination(exp_base58string);

Diff for: src/test/miniscript_tests.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <stdint.h>
6-
#include <string>
7-
#include <vector>
8-
95
#include <test/util/random.h>
106
#include <test/util/setup_common.h>
117
#include <boost/test/unit_test.hpp>
@@ -22,6 +18,11 @@
2218
#include <script/script_error.h>
2319
#include <script/signingprovider.h>
2420

21+
#include <algorithm>
22+
#include <cstdint>
23+
#include <string>
24+
#include <vector>
25+
2526
namespace {
2627

2728
/** TestData groups various kinds of precomputed data necessary in this test. */
@@ -274,7 +275,7 @@ class TestSignatureChecker : public BaseSignatureChecker {
274275
XOnlyPubKey pk{pubkey};
275276
auto it = g_testdata->schnorr_signatures.find(pk);
276277
if (it == g_testdata->schnorr_signatures.end()) return false;
277-
return sig == it->second;
278+
return std::ranges::equal(sig, it->second);
278279
}
279280

280281
bool CheckLockTime(const CScriptNum& locktime) const override {

0 commit comments

Comments
 (0)