Skip to content

Commit aa87e0b

Browse files
committed
Merge bitcoin#31519: refactor: Use std::span over Span
ffff4a2 bench: Update span-serialize comment (MarcoFalke) fa4d6ec refactor: Avoid false-positive gcc warning (MarcoFalke) fa94233 scripted-diff: Bump copyright headers after std::span changes (MarcoFalke) fa0c6b7 refactor: Remove unused Span alias (MarcoFalke) fade0b5 scripted-diff: Use std::span over Span (MarcoFalke) fadccc2 refactor: Make Span an alias of std::span (MarcoFalke) fa27e36 test: Fix broken span_tests (MarcoFalke) fadf02e refactor: Return std::span from MakeUCharSpan (MarcoFalke) fa720b9 refactor: Return std::span from MakeByteSpan (MarcoFalke) Pull request description: `Span` has some issues: * It does not support fixed-size spans, which are available through `std::span`. * It is confusing to have it available and in use at the same time with `std::span`. * It does not obey the standard library iterator build hardening flags. See bitcoin#31272 for a discussion. For example, this allows to catch issues like the one fixed in commit fabeca3. Both types are type-safe and can even implicitly convert into each other in most contexts. However, exclusively using `std::span` seems less confusing, so do it here with a scripted-diff. ACKs for top commit: l0rinc: reACK ffff4a2 theuni: ACK ffff4a2. Tree-SHA512: 9cc2f1f43551e2c07cc09f38b1f27d11e57e9e9bc0c6138c8fddd0cef54b91acd8b14711205ff949be874294a121910d0aceffe0e8914c4cff07f1e0e87ad5b8
2 parents ef525e8 + ffff4a2 commit aa87e0b

File tree

122 files changed

+695
-890
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+695
-890
lines changed

doc/developer-notes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,14 @@ class A
856856
- *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those
857857
that are not language lawyers.
858858
859-
- Use `Span` as function argument when it can operate on any range-like container.
859+
- Use `std::span` as function argument when it can operate on any range-like container.
860860
861861
- *Rationale*: Compared to `Foo(const vector<int>&)` this avoids the need for a (potentially expensive)
862862
conversion to vector if the caller happens to have the input stored in another type of container.
863863
However, be aware of the pitfalls documented in [span.h](../src/span.h).
864864
865865
```cpp
866-
void Foo(Span<const int> data);
866+
void Foo(std::span<const int> data);
867867
868868
std::vector<int> vec{1,2,3};
869869
Foo(vec);

src/base58.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2022 The Bitcoin Core developers
1+
// Copyright (c) 2014-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -86,7 +86,7 @@ static const int8_t mapBase58[256] = {
8686
return true;
8787
}
8888

89-
std::string EncodeBase58(Span<const unsigned char> input)
89+
std::string EncodeBase58(std::span<const unsigned char> input)
9090
{
9191
// Skip & count leading zeroes.
9292
int zeroes = 0;
@@ -134,7 +134,7 @@ bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, in
134134
return DecodeBase58(str.c_str(), vchRet, max_ret_len);
135135
}
136136

137-
std::string EncodeBase58Check(Span<const unsigned char> input)
137+
std::string EncodeBase58Check(std::span<const unsigned char> input)
138138
{
139139
// add 4-byte hash check to the end
140140
std::vector<unsigned char> vch(input.begin(), input.end());
@@ -151,7 +151,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
151151
return false;
152152
}
153153
// re-calculate the checksum, ensure it matches the included 4-byte checksum
154-
uint256 hash = Hash(Span{vchRet}.first(vchRet.size() - 4));
154+
uint256 hash = Hash(std::span{vchRet}.first(vchRet.size() - 4));
155155
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
156156
vchRet.clear();
157157
return false;

src/base58.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -22,7 +22,7 @@
2222
/**
2323
* Encode a byte span as a base58-encoded string
2424
*/
25-
std::string EncodeBase58(Span<const unsigned char> input);
25+
std::string EncodeBase58(std::span<const unsigned char> input);
2626

2727
/**
2828
* Decode a base58-encoded string (str) into a byte vector (vchRet).
@@ -33,7 +33,7 @@ std::string EncodeBase58(Span<const unsigned char> input);
3333
/**
3434
* Encode a byte span into a base58-encoded string, including checksum
3535
*/
36-
std::string EncodeBase58Check(Span<const unsigned char> input);
36+
std::string EncodeBase58Check(std::span<const unsigned char> input);
3737

3838
/**
3939
* Decode a base58-encoded string (str) that includes a checksum into a byte

src/bench/load_external.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2022 The Bitcoin Core developers
1+
// Copyright (c) 2022-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
44

@@ -44,9 +44,8 @@ static void LoadExternalBlockFile(benchmark::Bench& bench)
4444
auto params{testing_setup->m_node.chainman->GetParams()};
4545
ss << params.MessageStart();
4646
ss << static_cast<uint32_t>(benchmark::data::block413567.size());
47-
// We can't use the streaming serialization (ss << benchmark::data::block413567)
48-
// because that first writes a compact size.
49-
ss << Span{benchmark::data::block413567};
47+
// Use span-serialization to avoid writing the size first.
48+
ss << std::span{benchmark::data::block413567};
5049

5150
// Create the test file.
5251
{

src/bip324.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 The Bitcoin Core developers
1+
// Copyright (c) 2023-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -22,8 +22,8 @@
2222
#include <iterator>
2323
#include <string>
2424

25-
BIP324Cipher::BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept :
26-
m_key(key)
25+
BIP324Cipher::BIP324Cipher(const CKey& key, std::span<const std::byte> ent32) noexcept
26+
: m_key(key)
2727
{
2828
m_our_pubkey = m_key.EllSwiftCreate(ent32);
2929
}
@@ -70,7 +70,7 @@ void BIP324Cipher::Initialize(const EllSwiftPubKey& their_pubkey, bool initiator
7070
m_key = CKey();
7171
}
7272

73-
void BIP324Cipher::Encrypt(Span<const std::byte> contents, Span<const std::byte> aad, bool ignore, Span<std::byte> output) noexcept
73+
void BIP324Cipher::Encrypt(std::span<const std::byte> contents, std::span<const std::byte> aad, bool ignore, std::span<std::byte> output) noexcept
7474
{
7575
assert(output.size() == contents.size() + EXPANSION);
7676

@@ -86,7 +86,7 @@ void BIP324Cipher::Encrypt(Span<const std::byte> contents, Span<const std::byte>
8686
m_send_p_cipher->Encrypt(header, contents, aad, output.subspan(LENGTH_LEN));
8787
}
8888

89-
uint32_t BIP324Cipher::DecryptLength(Span<const std::byte> input) noexcept
89+
uint32_t BIP324Cipher::DecryptLength(std::span<const std::byte> input) noexcept
9090
{
9191
assert(input.size() == LENGTH_LEN);
9292

@@ -97,7 +97,7 @@ uint32_t BIP324Cipher::DecryptLength(Span<const std::byte> input) noexcept
9797
return uint32_t(buf[0]) + (uint32_t(buf[1]) << 8) + (uint32_t(buf[2]) << 16);
9898
}
9999

100-
bool BIP324Cipher::Decrypt(Span<const std::byte> input, Span<const std::byte> aad, bool& ignore, Span<std::byte> contents) noexcept
100+
bool BIP324Cipher::Decrypt(std::span<const std::byte> input, std::span<const std::byte> aad, bool& ignore, std::span<std::byte> contents) noexcept
101101
{
102102
assert(input.size() + LENGTH_LEN == contents.size() + EXPANSION);
103103

src/bip324.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 The Bitcoin Core developers
1+
// Copyright (c) 2023-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -45,7 +45,7 @@ class BIP324Cipher
4545
BIP324Cipher() = delete;
4646

4747
/** Initialize a BIP324 cipher with specified key and encoding entropy (testing only). */
48-
BIP324Cipher(const CKey& key, Span<const std::byte> ent32) noexcept;
48+
BIP324Cipher(const CKey& key, std::span<const std::byte> ent32) noexcept;
4949

5050
/** Initialize a BIP324 cipher with specified key (testing only). */
5151
BIP324Cipher(const CKey& key, const EllSwiftPubKey& pubkey) noexcept;
@@ -68,29 +68,29 @@ class BIP324Cipher
6868
*
6969
* It must hold that output.size() == contents.size() + EXPANSION.
7070
*/
71-
void Encrypt(Span<const std::byte> contents, Span<const std::byte> aad, bool ignore, Span<std::byte> output) noexcept;
71+
void Encrypt(std::span<const std::byte> contents, std::span<const std::byte> aad, bool ignore, std::span<std::byte> output) noexcept;
7272

7373
/** Decrypt the length of a packet. Only after Initialize().
7474
*
7575
* It must hold that input.size() == LENGTH_LEN.
7676
*/
77-
unsigned DecryptLength(Span<const std::byte> input) noexcept;
77+
unsigned DecryptLength(std::span<const std::byte> input) noexcept;
7878

7979
/** Decrypt a packet. Only after Initialize().
8080
*
8181
* It must hold that input.size() + LENGTH_LEN == contents.size() + EXPANSION.
8282
* Contents.size() must equal the length returned by DecryptLength.
8383
*/
84-
bool Decrypt(Span<const std::byte> input, Span<const std::byte> aad, bool& ignore, Span<std::byte> contents) noexcept;
84+
bool Decrypt(std::span<const std::byte> input, std::span<const std::byte> aad, bool& ignore, std::span<std::byte> contents) noexcept;
8585

8686
/** Get the Session ID. Only after Initialize(). */
87-
Span<const std::byte> GetSessionID() const noexcept { return m_session_id; }
87+
std::span<const std::byte> GetSessionID() const noexcept { return m_session_id; }
8888

8989
/** Get the Garbage Terminator to send. Only after Initialize(). */
90-
Span<const std::byte> GetSendGarbageTerminator() const noexcept { return m_send_garbage_terminator; }
90+
std::span<const std::byte> GetSendGarbageTerminator() const noexcept { return m_send_garbage_terminator; }
9191

9292
/** Get the expected Garbage Terminator to receive. Only after Initialize(). */
93-
Span<const std::byte> GetReceiveGarbageTerminator() const noexcept { return m_recv_garbage_terminator; }
93+
std::span<const std::byte> GetReceiveGarbageTerminator() const noexcept { return m_recv_garbage_terminator; }
9494
};
9595

9696
#endif // BITCOIN_BIP324_H

src/cluster_linearize.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class DepGraph
7575
*
7676
* @param depgraph The original DepGraph that is being remapped.
7777
*
78-
* @param mapping A Span such that mapping[i] gives the position in the new DepGraph
78+
* @param mapping A span such that mapping[i] gives the position in the new DepGraph
7979
* for position i in the old depgraph. Its size must be equal to
8080
* depgraph.PositionRange(). The value of mapping[i] is ignored if
8181
* position i is a hole in depgraph (i.e., if !depgraph.Positions()[i]).
@@ -86,7 +86,7 @@ class DepGraph
8686
*
8787
* Complexity: O(N^2) where N=depgraph.TxCount().
8888
*/
89-
DepGraph(const DepGraph<SetType>& depgraph, Span<const ClusterIndex> mapping, ClusterIndex pos_range) noexcept : entries(pos_range)
89+
DepGraph(const DepGraph<SetType>& depgraph, std::span<const ClusterIndex> mapping, ClusterIndex pos_range) noexcept : entries(pos_range)
9090
{
9191
Assume(mapping.size() == depgraph.PositionRange());
9292
Assume((pos_range == 0) == (depgraph.TxCount() == 0));
@@ -371,7 +371,7 @@ struct SetInfo
371371

372372
/** Compute the feerates of the chunks of linearization. */
373373
template<typename SetType>
374-
std::vector<FeeFrac> ChunkLinearization(const DepGraph<SetType>& depgraph, Span<const ClusterIndex> linearization) noexcept
374+
std::vector<FeeFrac> ChunkLinearization(const DepGraph<SetType>& depgraph, std::span<const ClusterIndex> linearization) noexcept
375375
{
376376
std::vector<FeeFrac> ret;
377377
for (ClusterIndex i : linearization) {
@@ -396,7 +396,7 @@ class LinearizationChunking
396396
const DepGraph<SetType>& m_depgraph;
397397

398398
/** The linearization we started from, possibly with removed prefix stripped. */
399-
Span<const ClusterIndex> m_linearization;
399+
std::span<const ClusterIndex> m_linearization;
400400

401401
/** Chunk sets and their feerates, of what remains of the linearization. */
402402
std::vector<SetInfo<SetType>> m_chunks;
@@ -437,7 +437,7 @@ class LinearizationChunking
437437

438438
public:
439439
/** Initialize a LinearizationSubset object for a given length of linearization. */
440-
explicit LinearizationChunking(const DepGraph<SetType>& depgraph LIFETIMEBOUND, Span<const ClusterIndex> lin LIFETIMEBOUND) noexcept :
440+
explicit LinearizationChunking(const DepGraph<SetType>& depgraph LIFETIMEBOUND, std::span<const ClusterIndex> lin LIFETIMEBOUND) noexcept :
441441
m_depgraph(depgraph), m_linearization(lin)
442442
{
443443
// Mark everything in lin as todo still.
@@ -1016,7 +1016,7 @@ class SearchCandidateFinder
10161016
* Complexity: possibly O(N * min(max_iterations + N, sqrt(2^N))) where N=depgraph.TxCount().
10171017
*/
10181018
template<typename SetType>
1019-
std::pair<std::vector<ClusterIndex>, bool> Linearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations, uint64_t rng_seed, Span<const ClusterIndex> old_linearization = {}) noexcept
1019+
std::pair<std::vector<ClusterIndex>, bool> Linearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations, uint64_t rng_seed, std::span<const ClusterIndex> old_linearization = {}) noexcept
10201020
{
10211021
Assume(old_linearization.empty() || old_linearization.size() == depgraph.TxCount());
10221022
if (depgraph.TxCount() == 0) return {{}, true};
@@ -1110,7 +1110,7 @@ std::pair<std::vector<ClusterIndex>, bool> Linearize(const DepGraph<SetType>& de
11101110
* postlinearize" process.
11111111
*/
11121112
template<typename SetType>
1113-
void PostLinearize(const DepGraph<SetType>& depgraph, Span<ClusterIndex> linearization)
1113+
void PostLinearize(const DepGraph<SetType>& depgraph, std::span<ClusterIndex> linearization)
11141114
{
11151115
// This algorithm performs a number of passes (currently 2); the even ones operate from back to
11161116
// front, the odd ones from front to back. Each results in an equal-or-better linearization
@@ -1299,7 +1299,7 @@ void PostLinearize(const DepGraph<SetType>& depgraph, Span<ClusterIndex> lineari
12991299
* Complexity: O(N^2) where N=depgraph.TxCount(); O(N) if both inputs are identical.
13001300
*/
13011301
template<typename SetType>
1302-
std::vector<ClusterIndex> MergeLinearizations(const DepGraph<SetType>& depgraph, Span<const ClusterIndex> lin1, Span<const ClusterIndex> lin2)
1302+
std::vector<ClusterIndex> MergeLinearizations(const DepGraph<SetType>& depgraph, std::span<const ClusterIndex> lin1, std::span<const ClusterIndex> lin2)
13031303
{
13041304
Assume(lin1.size() == depgraph.TxCount());
13051305
Assume(lin2.size() == depgraph.TxCount());

src/common/bloom.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2012-2022 The Bitcoin Core developers
1+
// Copyright (c) 2012-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -40,13 +40,13 @@ CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, c
4040
{
4141
}
4242

43-
inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, Span<const unsigned char> vDataToHash) const
43+
inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, std::span<const unsigned char> vDataToHash) const
4444
{
4545
// 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
4646
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
4747
}
4848

49-
void CBloomFilter::insert(Span<const unsigned char> vKey)
49+
void CBloomFilter::insert(std::span<const unsigned char> vKey)
5050
{
5151
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
5252
return;
@@ -65,7 +65,7 @@ void CBloomFilter::insert(const COutPoint& outpoint)
6565
insert(MakeUCharSpan(stream));
6666
}
6767

68-
bool CBloomFilter::contains(Span<const unsigned char> vKey) const
68+
bool CBloomFilter::contains(std::span<const unsigned char> vKey) const
6969
{
7070
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
7171
return true;
@@ -187,12 +187,12 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou
187187
}
188188

189189
/* Similar to CBloomFilter::Hash */
190-
static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, Span<const unsigned char> vDataToHash)
190+
static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, std::span<const unsigned char> vDataToHash)
191191
{
192192
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
193193
}
194194

195-
void CRollingBloomFilter::insert(Span<const unsigned char> vKey)
195+
void CRollingBloomFilter::insert(std::span<const unsigned char> vKey)
196196
{
197197
if (nEntriesThisGeneration == nEntriesPerGeneration) {
198198
nEntriesThisGeneration = 0;
@@ -223,7 +223,7 @@ void CRollingBloomFilter::insert(Span<const unsigned char> vKey)
223223
}
224224
}
225225

226-
bool CRollingBloomFilter::contains(Span<const unsigned char> vKey) const
226+
bool CRollingBloomFilter::contains(std::span<const unsigned char> vKey) const
227227
{
228228
for (int n = 0; n < nHashFuncs; n++) {
229229
uint32_t h = RollingBloomHash(n, nTweak, vKey);

src/common/bloom.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2012-2021 The Bitcoin Core developers
1+
// Copyright (c) 2012-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -49,7 +49,7 @@ class CBloomFilter
4949
unsigned int nTweak;
5050
unsigned char nFlags;
5151

52-
unsigned int Hash(unsigned int nHashNum, Span<const unsigned char> vDataToHash) const;
52+
unsigned int Hash(unsigned int nHashNum, std::span<const unsigned char> vDataToHash) const;
5353

5454
public:
5555
/**
@@ -66,10 +66,10 @@ class CBloomFilter
6666

6767
SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }
6868

69-
void insert(Span<const unsigned char> vKey);
69+
void insert(std::span<const unsigned char> vKey);
7070
void insert(const COutPoint& outpoint);
7171

72-
bool contains(Span<const unsigned char> vKey) const;
72+
bool contains(std::span<const unsigned char> vKey) const;
7373
bool contains(const COutPoint& outpoint) const;
7474

7575
//! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS
@@ -110,8 +110,8 @@ class CRollingBloomFilter
110110
public:
111111
CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
112112

113-
void insert(Span<const unsigned char> vKey);
114-
bool contains(Span<const unsigned char> vKey) const;
113+
void insert(std::span<const unsigned char> vKey);
114+
bool contains(std::span<const unsigned char> vKey) const;
115115

116116
void reset();
117117

0 commit comments

Comments
 (0)