diff --git a/doc/developer-notes.md b/doc/developer-notes.md index c8851a8dd09..cbe874312fd 100644 --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -856,14 +856,14 @@ class A - *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those that are not language lawyers. -- Use `Span` as function argument when it can operate on any range-like container. +- Use `std::span` as function argument when it can operate on any range-like container. - *Rationale*: Compared to `Foo(const vector&)` this avoids the need for a (potentially expensive) conversion to vector if the caller happens to have the input stored in another type of container. However, be aware of the pitfalls documented in [span.h](../src/span.h). ```cpp -void Foo(Span data); +void Foo(std::span data); std::vector vec{1,2,3}; Foo(vec); diff --git a/src/base58.cpp b/src/base58.cpp index cab99f0cb5d..d0e1ab465e4 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2022 The Bitcoin Core developers +// Copyright (c) 2014-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -86,7 +86,7 @@ static const int8_t mapBase58[256] = { return true; } -std::string EncodeBase58(Span input) +std::string EncodeBase58(std::span input) { // Skip & count leading zeroes. int zeroes = 0; @@ -134,7 +134,7 @@ bool DecodeBase58(const std::string& str, std::vector& vchRet, in return DecodeBase58(str.c_str(), vchRet, max_ret_len); } -std::string EncodeBase58Check(Span input) +std::string EncodeBase58Check(std::span input) { // add 4-byte hash check to the end std::vector vch(input.begin(), input.end()); @@ -151,7 +151,7 @@ std::string EncodeBase58Check(Span input) return false; } // re-calculate the checksum, ensure it matches the included 4-byte checksum - uint256 hash = Hash(Span{vchRet}.first(vchRet.size() - 4)); + uint256 hash = Hash(std::span{vchRet}.first(vchRet.size() - 4)); if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { vchRet.clear(); return false; diff --git a/src/base58.h b/src/base58.h index 2f4d0b74b1b..f258163bc3d 100644 --- a/src/base58.h +++ b/src/base58.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -22,7 +22,7 @@ /** * Encode a byte span as a base58-encoded string */ -std::string EncodeBase58(Span input); +std::string EncodeBase58(std::span input); /** * Decode a base58-encoded string (str) into a byte vector (vchRet). @@ -33,7 +33,7 @@ std::string EncodeBase58(Span input); /** * Encode a byte span into a base58-encoded string, including checksum */ -std::string EncodeBase58Check(Span input); +std::string EncodeBase58Check(std::span input); /** * Decode a base58-encoded string (str) that includes a checksum into a byte diff --git a/src/bench/load_external.cpp b/src/bench/load_external.cpp index 8f9399c60d5..18f21be4c48 100644 --- a/src/bench/load_external.cpp +++ b/src/bench/load_external.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2022 The Bitcoin Core developers +// Copyright (c) 2022-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php. @@ -44,9 +44,8 @@ static void LoadExternalBlockFile(benchmark::Bench& bench) auto params{testing_setup->m_node.chainman->GetParams()}; ss << params.MessageStart(); ss << static_cast(benchmark::data::block413567.size()); - // We can't use the streaming serialization (ss << benchmark::data::block413567) - // because that first writes a compact size. - ss << Span{benchmark::data::block413567}; + // Use span-serialization to avoid writing the size first. + ss << std::span{benchmark::data::block413567}; // Create the test file. { diff --git a/src/bip324.cpp b/src/bip324.cpp index f579a25193a..73f51a195ee 100644 --- a/src/bip324.cpp +++ b/src/bip324.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -22,8 +22,8 @@ #include #include -BIP324Cipher::BIP324Cipher(const CKey& key, Span ent32) noexcept : - m_key(key) +BIP324Cipher::BIP324Cipher(const CKey& key, std::span ent32) noexcept + : m_key(key) { m_our_pubkey = m_key.EllSwiftCreate(ent32); } @@ -70,7 +70,7 @@ void BIP324Cipher::Initialize(const EllSwiftPubKey& their_pubkey, bool initiator m_key = CKey(); } -void BIP324Cipher::Encrypt(Span contents, Span aad, bool ignore, Span output) noexcept +void BIP324Cipher::Encrypt(std::span contents, std::span aad, bool ignore, std::span output) noexcept { assert(output.size() == contents.size() + EXPANSION); @@ -86,7 +86,7 @@ void BIP324Cipher::Encrypt(Span contents, Span m_send_p_cipher->Encrypt(header, contents, aad, output.subspan(LENGTH_LEN)); } -uint32_t BIP324Cipher::DecryptLength(Span input) noexcept +uint32_t BIP324Cipher::DecryptLength(std::span input) noexcept { assert(input.size() == LENGTH_LEN); @@ -97,7 +97,7 @@ uint32_t BIP324Cipher::DecryptLength(Span input) noexcept return uint32_t(buf[0]) + (uint32_t(buf[1]) << 8) + (uint32_t(buf[2]) << 16); } -bool BIP324Cipher::Decrypt(Span input, Span aad, bool& ignore, Span contents) noexcept +bool BIP324Cipher::Decrypt(std::span input, std::span aad, bool& ignore, std::span contents) noexcept { assert(input.size() + LENGTH_LEN == contents.size() + EXPANSION); diff --git a/src/bip324.h b/src/bip324.h index 28e7c411eaa..396a28a4489 100644 --- a/src/bip324.h +++ b/src/bip324.h @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -45,7 +45,7 @@ public: BIP324Cipher() = delete; /** Initialize a BIP324 cipher with specified key and encoding entropy (testing only). */ - BIP324Cipher(const CKey& key, Span ent32) noexcept; + BIP324Cipher(const CKey& key, std::span ent32) noexcept; /** Initialize a BIP324 cipher with specified key (testing only). */ BIP324Cipher(const CKey& key, const EllSwiftPubKey& pubkey) noexcept; @@ -68,29 +68,29 @@ public: * * It must hold that output.size() == contents.size() + EXPANSION. */ - void Encrypt(Span contents, Span aad, bool ignore, Span output) noexcept; + void Encrypt(std::span contents, std::span aad, bool ignore, std::span output) noexcept; /** Decrypt the length of a packet. Only after Initialize(). * * It must hold that input.size() == LENGTH_LEN. */ - unsigned DecryptLength(Span input) noexcept; + unsigned DecryptLength(std::span input) noexcept; /** Decrypt a packet. Only after Initialize(). * * It must hold that input.size() + LENGTH_LEN == contents.size() + EXPANSION. * Contents.size() must equal the length returned by DecryptLength. */ - bool Decrypt(Span input, Span aad, bool& ignore, Span contents) noexcept; + bool Decrypt(std::span input, std::span aad, bool& ignore, std::span contents) noexcept; /** Get the Session ID. Only after Initialize(). */ - Span GetSessionID() const noexcept { return m_session_id; } + std::span GetSessionID() const noexcept { return m_session_id; } /** Get the Garbage Terminator to send. Only after Initialize(). */ - Span GetSendGarbageTerminator() const noexcept { return m_send_garbage_terminator; } + std::span GetSendGarbageTerminator() const noexcept { return m_send_garbage_terminator; } /** Get the expected Garbage Terminator to receive. Only after Initialize(). */ - Span GetReceiveGarbageTerminator() const noexcept { return m_recv_garbage_terminator; } + std::span GetReceiveGarbageTerminator() const noexcept { return m_recv_garbage_terminator; } }; #endif // BITCOIN_BIP324_H diff --git a/src/cluster_linearize.h b/src/cluster_linearize.h index 50b121d9e4c..7c7401706f6 100644 --- a/src/cluster_linearize.h +++ b/src/cluster_linearize.h @@ -75,7 +75,7 @@ public: * * @param depgraph The original DepGraph that is being remapped. * - * @param mapping A Span such that mapping[i] gives the position in the new DepGraph + * @param mapping A span such that mapping[i] gives the position in the new DepGraph * for position i in the old depgraph. Its size must be equal to * depgraph.PositionRange(). The value of mapping[i] is ignored if * position i is a hole in depgraph (i.e., if !depgraph.Positions()[i]). @@ -86,7 +86,7 @@ public: * * Complexity: O(N^2) where N=depgraph.TxCount(). */ - DepGraph(const DepGraph& depgraph, Span mapping, ClusterIndex pos_range) noexcept : entries(pos_range) + DepGraph(const DepGraph& depgraph, std::span mapping, ClusterIndex pos_range) noexcept : entries(pos_range) { Assume(mapping.size() == depgraph.PositionRange()); Assume((pos_range == 0) == (depgraph.TxCount() == 0)); @@ -371,7 +371,7 @@ struct SetInfo /** Compute the feerates of the chunks of linearization. */ template -std::vector ChunkLinearization(const DepGraph& depgraph, Span linearization) noexcept +std::vector ChunkLinearization(const DepGraph& depgraph, std::span linearization) noexcept { std::vector ret; for (ClusterIndex i : linearization) { @@ -396,7 +396,7 @@ class LinearizationChunking const DepGraph& m_depgraph; /** The linearization we started from, possibly with removed prefix stripped. */ - Span m_linearization; + std::span m_linearization; /** Chunk sets and their feerates, of what remains of the linearization. */ std::vector> m_chunks; @@ -437,7 +437,7 @@ class LinearizationChunking public: /** Initialize a LinearizationSubset object for a given length of linearization. */ - explicit LinearizationChunking(const DepGraph& depgraph LIFETIMEBOUND, Span lin LIFETIMEBOUND) noexcept : + explicit LinearizationChunking(const DepGraph& depgraph LIFETIMEBOUND, std::span lin LIFETIMEBOUND) noexcept : m_depgraph(depgraph), m_linearization(lin) { // Mark everything in lin as todo still. @@ -1016,7 +1016,7 @@ public: * Complexity: possibly O(N * min(max_iterations + N, sqrt(2^N))) where N=depgraph.TxCount(). */ template -std::pair, bool> Linearize(const DepGraph& depgraph, uint64_t max_iterations, uint64_t rng_seed, Span old_linearization = {}) noexcept +std::pair, bool> Linearize(const DepGraph& depgraph, uint64_t max_iterations, uint64_t rng_seed, std::span old_linearization = {}) noexcept { Assume(old_linearization.empty() || old_linearization.size() == depgraph.TxCount()); if (depgraph.TxCount() == 0) return {{}, true}; @@ -1110,7 +1110,7 @@ std::pair, bool> Linearize(const DepGraph& de * postlinearize" process. */ template -void PostLinearize(const DepGraph& depgraph, Span linearization) +void PostLinearize(const DepGraph& depgraph, std::span linearization) { // This algorithm performs a number of passes (currently 2); the even ones operate from back to // front, the odd ones from front to back. Each results in an equal-or-better linearization @@ -1299,7 +1299,7 @@ void PostLinearize(const DepGraph& depgraph, Span lineari * Complexity: O(N^2) where N=depgraph.TxCount(); O(N) if both inputs are identical. */ template -std::vector MergeLinearizations(const DepGraph& depgraph, Span lin1, Span lin2) +std::vector MergeLinearizations(const DepGraph& depgraph, std::span lin1, std::span lin2) { Assume(lin1.size() == depgraph.TxCount()); Assume(lin2.size() == depgraph.TxCount()); diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp index 076ee406354..efb4178cab5 100644 --- a/src/common/bloom.cpp +++ b/src/common/bloom.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -40,13 +40,13 @@ CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, c { } -inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, Span vDataToHash) const +inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, std::span vDataToHash) const { // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values. return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8); } -void CBloomFilter::insert(Span vKey) +void CBloomFilter::insert(std::span vKey) { if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700) return; @@ -65,7 +65,7 @@ void CBloomFilter::insert(const COutPoint& outpoint) insert(MakeUCharSpan(stream)); } -bool CBloomFilter::contains(Span vKey) const +bool CBloomFilter::contains(std::span vKey) const { if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700) return true; @@ -187,12 +187,12 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou } /* Similar to CBloomFilter::Hash */ -static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, Span vDataToHash) +static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, std::span vDataToHash) { return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash); } -void CRollingBloomFilter::insert(Span vKey) +void CRollingBloomFilter::insert(std::span vKey) { if (nEntriesThisGeneration == nEntriesPerGeneration) { nEntriesThisGeneration = 0; @@ -223,7 +223,7 @@ void CRollingBloomFilter::insert(Span vKey) } } -bool CRollingBloomFilter::contains(Span vKey) const +bool CRollingBloomFilter::contains(std::span vKey) const { for (int n = 0; n < nHashFuncs; n++) { uint32_t h = RollingBloomHash(n, nTweak, vKey); diff --git a/src/common/bloom.h b/src/common/bloom.h index 4f7473d8ffc..bff96a23a37 100644 --- a/src/common/bloom.h +++ b/src/common/bloom.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -49,7 +49,7 @@ private: unsigned int nTweak; unsigned char nFlags; - unsigned int Hash(unsigned int nHashNum, Span vDataToHash) const; + unsigned int Hash(unsigned int nHashNum, std::span vDataToHash) const; public: /** @@ -66,10 +66,10 @@ public: SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); } - void insert(Span vKey); + void insert(std::span vKey); void insert(const COutPoint& outpoint); - bool contains(Span vKey) const; + bool contains(std::span vKey) const; bool contains(const COutPoint& outpoint) const; //! 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 public: CRollingBloomFilter(const unsigned int nElements, const double nFPRate); - void insert(Span vKey); - bool contains(Span vKey) const; + void insert(std::span vKey); + bool contains(std::span vKey) const; void reset(); diff --git a/src/common/pcp.cpp b/src/common/pcp.cpp index d0d4955470f..8ababab32f3 100644 --- a/src/common/pcp.cpp +++ b/src/common/pcp.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 The Bitcoin Core developers +// Copyright (c) 2024-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or https://www.opensource.org/licenses/mit-license.php. @@ -179,7 +179,7 @@ std::string PCPResultString(uint8_t result_code) } //! Wrap address in IPv6 according to RFC6887. wrapped_addr needs to be able to store 16 bytes. -[[nodiscard]] bool PCPWrapAddress(Span wrapped_addr, const CNetAddr &addr) +[[nodiscard]] bool PCPWrapAddress(std::span wrapped_addr, const CNetAddr &addr) { Assume(wrapped_addr.size() == ADDR_IPV6_SIZE); if (addr.IsIPv4()) { @@ -200,7 +200,7 @@ std::string PCPResultString(uint8_t result_code) } //! Unwrap PCP-encoded address according to RFC6887. -CNetAddr PCPUnwrapAddress(Span wrapped_addr) +CNetAddr PCPUnwrapAddress(std::span wrapped_addr) { Assume(wrapped_addr.size() == ADDR_IPV6_SIZE); if (util::HasPrefix(wrapped_addr, IPV4_IN_IPV6_PREFIX)) { @@ -215,9 +215,9 @@ CNetAddr PCPUnwrapAddress(Span wrapped_addr) } //! PCP or NAT-PMP send-receive loop. -std::optional> PCPSendRecv(Sock &sock, const std::string &protocol, Span request, int num_tries, +std::optional> PCPSendRecv(Sock &sock, const std::string &protocol, std::span request, int num_tries, std::chrono::milliseconds timeout_per_try, - std::function)> check_packet) + std::function)> check_packet) { using namespace std::chrono; // UDP is a potentially lossy protocol, so we try to send again a few times. @@ -254,9 +254,9 @@ std::optional> PCPSendRecv(Sock &sock, const std::string &p LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "%s: Could not receive response: %s\n", protocol, NetworkErrorString(WSAGetLastError())); return std::nullopt; // Network-level error, probably no use retrying. } - LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Received response of %d bytes: %s\n", protocol, recvsz, HexStr(Span(response, recvsz))); + LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Received response of %d bytes: %s\n", protocol, recvsz, HexStr(std::span(response, recvsz))); - if (check_packet(Span(response, recvsz))) { + if (check_packet(std::span(response, recvsz))) { got_response = true; // Got expected response, break from receive loop as well as from retry loop. break; } @@ -309,7 +309,7 @@ std::variant NATPMPRequestPortMap(const CNetAddr &g request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_GETEXTERNAL; auto recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try, - [&](const Span response) -> bool { + [&](const std::span response) -> bool { if (response.size() < NATPMP_GETEXTERNAL_RESPONSE_SIZE) { LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n"); return false; // Wasn't response to what we expected, try receiving next packet. @@ -346,7 +346,7 @@ std::variant NATPMPRequestPortMap(const CNetAddr &g WriteBE32(request.data() + NATPMP_MAP_REQUEST_LIFETIME_OFS, lifetime); recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try, - [&](const Span response) -> bool { + [&](const std::span response) -> bool { if (response.size() < NATPMP_MAP_RESPONSE_SIZE) { LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n"); return false; // Wasn't response to what we expected, try receiving next packet. @@ -438,7 +438,7 @@ std::variant PCPRequestPortMap(const PCPMappingNonc request[ofs + PCP_HDR_VERSION_OFS] = PCP_VERSION; request[ofs + PCP_HDR_OP_OFS] = PCP_REQUEST | PCP_OP_MAP; WriteBE32(request.data() + ofs + PCP_HDR_LIFETIME_OFS, lifetime); - if (!PCPWrapAddress(Span(request).subspan(ofs + PCP_REQUEST_HDR_IP_OFS, ADDR_IPV6_SIZE), internal)) return MappingError::NETWORK_ERROR; + if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_REQUEST_HDR_IP_OFS, ADDR_IPV6_SIZE), internal)) return MappingError::NETWORK_ERROR; ofs += PCP_HDR_SIZE; @@ -449,7 +449,7 @@ std::variant PCPRequestPortMap(const PCPMappingNonc request[ofs + PCP_MAP_PROTOCOL_OFS] = PCP_PROTOCOL_TCP; WriteBE16(request.data() + ofs + PCP_MAP_INTERNAL_PORT_OFS, port); WriteBE16(request.data() + ofs + PCP_MAP_EXTERNAL_PORT_OFS, port); - if (!PCPWrapAddress(Span(request).subspan(ofs + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE), bind)) return MappingError::NETWORK_ERROR; + if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE), bind)) return MappingError::NETWORK_ERROR; ofs += PCP_MAP_SIZE; Assume(ofs == request.size()); @@ -457,7 +457,7 @@ std::variant PCPRequestPortMap(const PCPMappingNonc // Receive loop. bool is_natpmp = false; auto recv_res = PCPSendRecv(*sock, "pcp", request, num_tries, timeout_per_try, - [&](const Span response) -> bool { + [&](const std::span response) -> bool { // Unsupported version according to RFC6887 appendix A and RFC6886 section 3.5, can fall back to NAT-PMP. if (response.size() == NATPMP_RESPONSE_HDR_SIZE && response[PCP_HDR_VERSION_OFS] == NATPMP_VERSION && response[PCP_RESPONSE_HDR_RESULT_OFS] == NATPMP_RESULT_UNSUPP_VERSION) { is_natpmp = true; diff --git a/src/compressor.h b/src/compressor.h index a0970c595e2..95490b7bd23 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2021 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -65,12 +65,12 @@ struct ScriptCompression void Ser(Stream &s, const CScript& script) { CompressedScript compr; if (CompressScript(script, compr)) { - s << Span{compr}; + s << std::span{compr}; return; } unsigned int nSize = script.size() + nSpecialScripts; s << VARINT(nSize); - s << Span{script}; + s << std::span{script}; } template @@ -79,7 +79,7 @@ struct ScriptCompression s >> VARINT(nSize); if (nSize < nSpecialScripts) { CompressedScript vch(GetSpecialScriptSize(nSize), 0x00); - s >> Span{vch}; + s >> std::span{vch}; DecompressScript(script, nSize, vch); return; } @@ -90,7 +90,7 @@ struct ScriptCompression s.ignore(nSize); } else { script.resize(nSize); - s >> Span{script}; + s >> std::span{script}; } } }; diff --git a/src/crypto/chacha20.cpp b/src/crypto/chacha20.cpp index b1bd3c54907..acb860e9b46 100644 --- a/src/crypto/chacha20.cpp +++ b/src/crypto/chacha20.cpp @@ -22,7 +22,7 @@ #define REPEAT10(a) do { {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; {a}; } while(0) -void ChaCha20Aligned::SetKey(Span key) noexcept +void ChaCha20Aligned::SetKey(std::span key) noexcept { assert(key.size() == KEYLEN); input[0] = ReadLE32(key.data() + 0); @@ -44,7 +44,7 @@ ChaCha20Aligned::~ChaCha20Aligned() memory_cleanse(input, sizeof(input)); } -ChaCha20Aligned::ChaCha20Aligned(Span key) noexcept +ChaCha20Aligned::ChaCha20Aligned(std::span key) noexcept { SetKey(key); } @@ -57,7 +57,7 @@ void ChaCha20Aligned::Seek(Nonce96 nonce, uint32_t block_counter) noexcept input[11] = nonce.second >> 32; } -inline void ChaCha20Aligned::Keystream(Span output) noexcept +inline void ChaCha20Aligned::Keystream(std::span output) noexcept { std::byte* c = output.data(); size_t blocks = output.size() / BLOCKLEN; @@ -158,7 +158,7 @@ inline void ChaCha20Aligned::Keystream(Span output) noexcept } } -inline void ChaCha20Aligned::Crypt(Span in_bytes, Span out_bytes) noexcept +inline void ChaCha20Aligned::Crypt(std::span in_bytes, std::span out_bytes) noexcept { assert(in_bytes.size() == out_bytes.size()); const std::byte* m = in_bytes.data(); @@ -279,7 +279,7 @@ inline void ChaCha20Aligned::Crypt(Span in_bytes, Span out) noexcept +void ChaCha20::Keystream(std::span out) noexcept { if (out.empty()) return; if (m_bufleft) { @@ -300,7 +300,7 @@ void ChaCha20::Keystream(Span out) noexcept } } -void ChaCha20::Crypt(Span input, Span output) noexcept +void ChaCha20::Crypt(std::span input, std::span output) noexcept { assert(input.size() == output.size()); @@ -334,20 +334,20 @@ ChaCha20::~ChaCha20() memory_cleanse(m_buffer.data(), m_buffer.size()); } -void ChaCha20::SetKey(Span key) noexcept +void ChaCha20::SetKey(std::span key) noexcept { m_aligned.SetKey(key); m_bufleft = 0; memory_cleanse(m_buffer.data(), m_buffer.size()); } -FSChaCha20::FSChaCha20(Span key, uint32_t rekey_interval) noexcept : +FSChaCha20::FSChaCha20(std::span key, uint32_t rekey_interval) noexcept : m_chacha20(key), m_rekey_interval(rekey_interval) { assert(key.size() == KEYLEN); } -void FSChaCha20::Crypt(Span input, Span output) noexcept +void FSChaCha20::Crypt(std::span input, std::span output) noexcept { assert(input.size() == output.size()); diff --git a/src/crypto/chacha20.h b/src/crypto/chacha20.h index 5f0f1ff64ba..06eb10781c5 100644 --- a/src/crypto/chacha20.h +++ b/src/crypto/chacha20.h @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022 The Bitcoin Core developers +// Copyright (c) 2017-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -38,13 +38,13 @@ public: ChaCha20Aligned() noexcept = delete; /** Initialize a cipher with specified 32-byte key. */ - ChaCha20Aligned(Span key) noexcept; + ChaCha20Aligned(std::span key) noexcept; /** Destructor to clean up private memory. */ ~ChaCha20Aligned(); /** Set 32-byte key, and seek to nonce 0 and block position 0. */ - void SetKey(Span key) noexcept; + void SetKey(std::span key) noexcept; /** Type for 96-bit nonces used by the Set function below. * @@ -64,13 +64,13 @@ public: void Seek(Nonce96 nonce, uint32_t block_counter) noexcept; /** outputs the keystream into out, whose length must be a multiple of BLOCKLEN. */ - void Keystream(Span out) noexcept; + void Keystream(std::span out) noexcept; /** en/deciphers the message and write the result into * * The size of input and output must be equal, and be a multiple of BLOCKLEN. */ - void Crypt(Span input, Span output) noexcept; + void Crypt(std::span input, std::span output) noexcept; }; /** Unrestricted ChaCha20 cipher. */ @@ -89,13 +89,13 @@ public: ChaCha20() noexcept = delete; /** Initialize a cipher with specified 32-byte key. */ - ChaCha20(Span key) noexcept : m_aligned(key) {} + ChaCha20(std::span key) noexcept : m_aligned(key) {} /** Destructor to clean up private memory. */ ~ChaCha20(); /** Set 32-byte key, and seek to nonce 0 and block position 0. */ - void SetKey(Span key) noexcept; + void SetKey(std::span key) noexcept; /** 96-bit nonce type. */ using Nonce96 = ChaCha20Aligned::Nonce96; @@ -111,10 +111,10 @@ public: * * The size of in_bytes and out_bytes must be equal. */ - void Crypt(Span in_bytes, Span out_bytes) noexcept; + void Crypt(std::span in_bytes, std::span out_bytes) noexcept; /** outputs the keystream to out. */ - void Keystream(Span out) noexcept; + void Keystream(std::span out) noexcept; }; /** Forward-secure ChaCha20 @@ -150,10 +150,10 @@ public: FSChaCha20& operator=(FSChaCha20&&) = delete; /** Construct an FSChaCha20 cipher that rekeys every rekey_interval Crypt() calls. */ - FSChaCha20(Span key, uint32_t rekey_interval) noexcept; + FSChaCha20(std::span key, uint32_t rekey_interval) noexcept; /** Encrypt or decrypt a chunk. */ - void Crypt(Span input, Span output) noexcept; + void Crypt(std::span input, std::span output) noexcept; }; #endif // BITCOIN_CRYPTO_CHACHA20_H diff --git a/src/crypto/chacha20poly1305.cpp b/src/crypto/chacha20poly1305.cpp index 282f32a5e42..4c7da07d7ec 100644 --- a/src/crypto/chacha20poly1305.cpp +++ b/src/crypto/chacha20poly1305.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -13,12 +13,12 @@ #include #include -AEADChaCha20Poly1305::AEADChaCha20Poly1305(Span key) noexcept : m_chacha20(key) +AEADChaCha20Poly1305::AEADChaCha20Poly1305(std::span key) noexcept : m_chacha20(key) { assert(key.size() == KEYLEN); } -void AEADChaCha20Poly1305::SetKey(Span key) noexcept +void AEADChaCha20Poly1305::SetKey(std::span key) noexcept { assert(key.size() == KEYLEN); m_chacha20.SetKey(key); @@ -36,7 +36,7 @@ int timingsafe_bcmp_internal(const unsigned char* b1, const unsigned char* b2, s } /** Compute poly1305 tag. chacha20 must be set to the right nonce, block 0. Will be at block 1 after. */ -void ComputeTag(ChaCha20& chacha20, Span aad, Span cipher, Span tag) noexcept +void ComputeTag(ChaCha20& chacha20, std::span aad, std::span cipher, std::span tag) noexcept { static const std::byte PADDING[16] = {{}}; @@ -45,15 +45,15 @@ void ComputeTag(ChaCha20& chacha20, Span aad, Span aad, Span plain1, Span plain2, Span aad, Nonce96 nonce, Span cipher) noexcept +void AEADChaCha20Poly1305::Encrypt(std::span plain1, std::span plain2, std::span aad, Nonce96 nonce, std::span cipher) noexcept { assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION); @@ -80,7 +80,7 @@ void AEADChaCha20Poly1305::Encrypt(Span plain1, Span cipher, Span aad, Nonce96 nonce, Span plain1, Span plain2) noexcept +bool AEADChaCha20Poly1305::Decrypt(std::span cipher, std::span aad, Nonce96 nonce, std::span plain1, std::span plain2) noexcept { assert(cipher.size() == plain1.size() + plain2.size() + EXPANSION); @@ -96,7 +96,7 @@ bool AEADChaCha20Poly1305::Decrypt(Span cipher, Span keystream) noexcept +void AEADChaCha20Poly1305::Keystream(Nonce96 nonce, std::span keystream) noexcept { // Skip the first output block, as it's used for generating the poly1305 key. m_chacha20.Seek(nonce, 1); @@ -111,7 +111,7 @@ void FSChaCha20Poly1305::NextPacket() noexcept std::byte one_block[ChaCha20Aligned::BLOCKLEN]; m_aead.Keystream({0xFFFFFFFF, m_rekey_counter}, one_block); // Switch keys. - m_aead.SetKey(Span{one_block}.first(KEYLEN)); + m_aead.SetKey(std::span{one_block}.first(KEYLEN)); // Wipe the generated keystream (a copy remains inside m_aead, which will be cleaned up // once it cycles again, or is destroyed). memory_cleanse(one_block, sizeof(one_block)); @@ -121,13 +121,13 @@ void FSChaCha20Poly1305::NextPacket() noexcept } } -void FSChaCha20Poly1305::Encrypt(Span plain1, Span plain2, Span aad, Span cipher) noexcept +void FSChaCha20Poly1305::Encrypt(std::span plain1, std::span plain2, std::span aad, std::span cipher) noexcept { m_aead.Encrypt(plain1, plain2, aad, {m_packet_counter, m_rekey_counter}, cipher); NextPacket(); } -bool FSChaCha20Poly1305::Decrypt(Span cipher, Span aad, Span plain1, Span plain2) noexcept +bool FSChaCha20Poly1305::Decrypt(std::span cipher, std::span aad, std::span plain1, std::span plain2) noexcept { bool ret = m_aead.Decrypt(cipher, aad, {m_packet_counter, m_rekey_counter}, plain1, plain2); NextPacket(); diff --git a/src/crypto/chacha20poly1305.h b/src/crypto/chacha20poly1305.h index a847c258ef1..75404ca5ffd 100644 --- a/src/crypto/chacha20poly1305.h +++ b/src/crypto/chacha20poly1305.h @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -26,10 +26,10 @@ public: static constexpr unsigned EXPANSION = Poly1305::TAGLEN; /** Initialize an AEAD instance with a specified 32-byte key. */ - AEADChaCha20Poly1305(Span key) noexcept; + AEADChaCha20Poly1305(std::span key) noexcept; /** Switch to another 32-byte key. */ - void SetKey(Span key) noexcept; + void SetKey(std::span key) noexcept; /** 96-bit nonce type. */ using Nonce96 = ChaCha20::Nonce96; @@ -38,7 +38,7 @@ public: * * Requires cipher.size() = plain.size() + EXPANSION. */ - void Encrypt(Span plain, Span aad, Nonce96 nonce, Span cipher) noexcept + void Encrypt(std::span plain, std::span aad, Nonce96 nonce, std::span cipher) noexcept { Encrypt(plain, {}, aad, nonce, cipher); } @@ -47,13 +47,13 @@ public: * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ - void Encrypt(Span plain1, Span plain2, Span aad, Nonce96 nonce, Span cipher) noexcept; + void Encrypt(std::span plain1, std::span plain2, std::span aad, Nonce96 nonce, std::span cipher) noexcept; /** Decrypt a message with a specified 96-bit nonce and aad. Returns true if valid. * * Requires cipher.size() = plain.size() + EXPANSION. */ - bool Decrypt(Span cipher, Span aad, Nonce96 nonce, Span plain) noexcept + bool Decrypt(std::span cipher, std::span aad, Nonce96 nonce, std::span plain) noexcept { return Decrypt(cipher, aad, nonce, plain, {}); } @@ -62,14 +62,14 @@ public: * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ - bool Decrypt(Span cipher, Span aad, Nonce96 nonce, Span plain1, Span plain2) noexcept; + bool Decrypt(std::span cipher, std::span aad, Nonce96 nonce, std::span plain1, std::span plain2) noexcept; /** Get a number of keystream bytes from the underlying stream cipher. * * This is equivalent to Encrypt() with plain set to that many zero bytes, and dropping the * last EXPANSION bytes off the result. */ - void Keystream(Nonce96 nonce, Span keystream) noexcept; + void Keystream(Nonce96 nonce, std::span keystream) noexcept; }; /** Forward-secure wrapper around AEADChaCha20Poly1305. @@ -111,14 +111,14 @@ public: FSChaCha20Poly1305& operator=(FSChaCha20Poly1305&&) = delete; /** Construct an FSChaCha20Poly1305 cipher that rekeys every rekey_interval operations. */ - FSChaCha20Poly1305(Span key, uint32_t rekey_interval) noexcept : + FSChaCha20Poly1305(std::span key, uint32_t rekey_interval) noexcept : m_aead(key), m_rekey_interval(rekey_interval) {} /** Encrypt a message with a specified aad. * * Requires cipher.size() = plain.size() + EXPANSION. */ - void Encrypt(Span plain, Span aad, Span cipher) noexcept + void Encrypt(std::span plain, std::span aad, std::span cipher) noexcept { Encrypt(plain, {}, aad, cipher); } @@ -127,13 +127,13 @@ public: * * Requires cipher.size() = plain.size() + EXPANSION. */ - void Encrypt(Span plain1, Span plain2, Span aad, Span cipher) noexcept; + void Encrypt(std::span plain1, std::span plain2, std::span aad, std::span cipher) noexcept; /** Decrypt a message with a specified aad. Returns true if valid. * * Requires cipher.size() = plain.size() + EXPANSION. */ - bool Decrypt(Span cipher, Span aad, Span plain) noexcept + bool Decrypt(std::span cipher, std::span aad, std::span plain) noexcept { return Decrypt(cipher, aad, plain, {}); } @@ -142,7 +142,7 @@ public: * * Requires cipher.size() = plain1.size() + plain2.size() + EXPANSION. */ - bool Decrypt(Span cipher, Span aad, Span plain1, Span plain2) noexcept; + bool Decrypt(std::span cipher, std::span aad, std::span plain1, std::span plain2) noexcept; }; #endif // BITCOIN_CRYPTO_CHACHA20POLY1305_H diff --git a/src/crypto/hex_base.cpp b/src/crypto/hex_base.cpp index 67d691b63e1..e2eccf5509b 100644 --- a/src/crypto/hex_base.cpp +++ b/src/crypto/hex_base.cpp @@ -26,7 +26,7 @@ constexpr std::array CreateByteToHexMap() } // namespace -std::string HexStr(const Span s) +std::string HexStr(const std::span s) { std::string rv(s.size() * 2, '\0'); static constexpr auto byte_to_hex = CreateByteToHexMap(); diff --git a/src/crypto/hex_base.h b/src/crypto/hex_base.h index cdfea68c29c..704b19f8864 100644 --- a/src/crypto/hex_base.h +++ b/src/crypto/hex_base.h @@ -14,9 +14,9 @@ /** * Convert a span of bytes to a lower-case hexadecimal string. */ -std::string HexStr(const Span s); -inline std::string HexStr(const Span s) { return HexStr(MakeUCharSpan(s)); } -inline std::string HexStr(const Span s) { return HexStr(MakeUCharSpan(s)); } +std::string HexStr(const std::span s); +inline std::string HexStr(const std::span s) { return HexStr(MakeUCharSpan(s)); } +inline std::string HexStr(const std::span s) { return HexStr(MakeUCharSpan(s)); } signed char HexDigit(char c); diff --git a/src/crypto/muhash.cpp b/src/crypto/muhash.cpp index 4f502269f52..02c38c44427 100644 --- a/src/crypto/muhash.cpp +++ b/src/crypto/muhash.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022 The Bitcoin Core developers +// Copyright (c) 2017-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -532,7 +532,7 @@ void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) { } } -Num3072 MuHash3072::ToNum3072(Span in) { +Num3072 MuHash3072::ToNum3072(std::span in) { unsigned char tmp[Num3072::BYTE_SIZE]; uint256 hashed_in{(HashWriter{} << in).GetSHA256()}; @@ -543,7 +543,7 @@ Num3072 MuHash3072::ToNum3072(Span in) { return out; } -MuHash3072::MuHash3072(Span in) noexcept +MuHash3072::MuHash3072(std::span in) noexcept { m_numerator = ToNum3072(in); } @@ -573,12 +573,12 @@ MuHash3072& MuHash3072::operator/=(const MuHash3072& div) noexcept return *this; } -MuHash3072& MuHash3072::Insert(Span in) noexcept { +MuHash3072& MuHash3072::Insert(std::span in) noexcept { m_numerator.Multiply(ToNum3072(in)); return *this; } -MuHash3072& MuHash3072::Remove(Span in) noexcept { +MuHash3072& MuHash3072::Remove(std::span in) noexcept { m_denominator.Multiply(ToNum3072(in)); return *this; } diff --git a/src/crypto/muhash.h b/src/crypto/muhash.h index 798df573725..5137c8e2400 100644 --- a/src/crypto/muhash.h +++ b/src/crypto/muhash.h @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2021 The Bitcoin Core developers +// Copyright (c) 2017-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -102,20 +102,20 @@ private: Num3072 m_numerator; Num3072 m_denominator; - Num3072 ToNum3072(Span in); + Num3072 ToNum3072(std::span in); public: /* The empty set. */ MuHash3072() noexcept = default; /* A singleton with variable sized data in it. */ - explicit MuHash3072(Span in) noexcept; + explicit MuHash3072(std::span in) noexcept; /* Insert a single piece of data into the set. */ - MuHash3072& Insert(Span in) noexcept; + MuHash3072& Insert(std::span in) noexcept; /* Remove a single piece of data from the set. */ - MuHash3072& Remove(Span in) noexcept; + MuHash3072& Remove(std::span in) noexcept; /* Multiply (resulting in a hash for the union of the sets) */ MuHash3072& operator*=(const MuHash3072& mul) noexcept; diff --git a/src/crypto/poly1305.h b/src/crypto/poly1305.h index 592c090f571..94ea69be87e 100644 --- a/src/crypto/poly1305.h +++ b/src/crypto/poly1305.h @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -33,7 +33,7 @@ void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept; } // namespace poly1305_donna -/** C++ wrapper with std::byte Span interface around poly1305_donna code. */ +/** C++ wrapper with std::byte span interface around poly1305_donna code. */ class Poly1305 { poly1305_donna::poly1305_context m_ctx; @@ -46,21 +46,21 @@ public: static constexpr unsigned KEYLEN{32}; /** Construct a Poly1305 object with a given 32-byte key. */ - Poly1305(Span key) noexcept + Poly1305(std::span key) noexcept { assert(key.size() == KEYLEN); poly1305_donna::poly1305_init(&m_ctx, UCharCast(key.data())); } /** Process message bytes. */ - Poly1305& Update(Span msg) noexcept + Poly1305& Update(std::span msg) noexcept { poly1305_donna::poly1305_update(&m_ctx, UCharCast(msg.data()), msg.size()); return *this; } /** Write authentication tag to 16-byte out. */ - void Finalize(Span out) noexcept + void Finalize(std::span out) noexcept { assert(out.size() == TAGLEN); poly1305_donna::poly1305_finish(&m_ctx, UCharCast(out.data())); diff --git a/src/crypto/sha3.cpp b/src/crypto/sha3.cpp index 56aaa4615e7..77ea2a8ebfb 100644 --- a/src/crypto/sha3.cpp +++ b/src/crypto/sha3.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -103,7 +103,7 @@ void KeccakF(uint64_t (&st)[25]) } } -SHA3_256& SHA3_256::Write(Span data) +SHA3_256& SHA3_256::Write(std::span data) { if (m_bufsize && data.size() >= sizeof(m_buffer) - m_bufsize) { // Fill the buffer and process it. @@ -133,7 +133,7 @@ SHA3_256& SHA3_256::Write(Span data) return *this; } -SHA3_256& SHA3_256::Finalize(Span output) +SHA3_256& SHA3_256::Finalize(std::span output) { assert(output.size() == OUTPUT_SIZE); std::fill(m_buffer + m_bufsize, m_buffer + sizeof(m_buffer), 0); diff --git a/src/crypto/sha3.h b/src/crypto/sha3.h index a28c5311ff9..685586af63b 100644 --- a/src/crypto/sha3.h +++ b/src/crypto/sha3.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -33,8 +33,8 @@ public: static constexpr size_t OUTPUT_SIZE = 32; SHA3_256() = default; - SHA3_256& Write(Span data); - SHA3_256& Finalize(Span output); + SHA3_256& Write(std::span data); + SHA3_256& Finalize(std::span output); SHA3_256& Reset(); }; diff --git a/src/crypto/siphash.cpp b/src/crypto/siphash.cpp index 8004a0548ec..dda28a6ced4 100644 --- a/src/crypto/siphash.cpp +++ b/src/crypto/siphash.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2020 The Bitcoin Core developers +// Copyright (c) 2016-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -45,7 +45,7 @@ CSipHasher& CSipHasher::Write(uint64_t data) return *this; } -CSipHasher& CSipHasher::Write(Span data) +CSipHasher& CSipHasher::Write(std::span data) { uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; uint64_t t = tmp; diff --git a/src/crypto/siphash.h b/src/crypto/siphash.h index 4fb3dc2f258..c388c94951b 100644 --- a/src/crypto/siphash.h +++ b/src/crypto/siphash.h @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2020 The Bitcoin Core developers +// Copyright (c) 2016-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -27,7 +27,7 @@ public: */ CSipHasher& Write(uint64_t data); /** Hash arbitrary bytes. */ - CSipHasher& Write(Span data); + CSipHasher& Write(std::span data); /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */ uint64_t Finalize() const; }; diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 8fb366515af..b3faff10cea 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -168,7 +168,7 @@ void CDBBatch::Clear() size_estimate = 0; } -void CDBBatch::WriteImpl(Span key, DataStream& ssValue) +void CDBBatch::WriteImpl(std::span key, DataStream& ssValue) { leveldb::Slice slKey(CharCast(key.data()), key.size()); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); @@ -184,7 +184,7 @@ void CDBBatch::WriteImpl(Span key, DataStream& ssValue) size_estimate += 3 + (slKey.size() > 127) + slKey.size() + (slValue.size() > 127) + slValue.size(); } -void CDBBatch::EraseImpl(Span key) +void CDBBatch::EraseImpl(std::span key) { leveldb::Slice slKey(CharCast(key.data()), key.size()); m_impl_batch->batch.Delete(slKey); @@ -336,7 +336,7 @@ std::vector CDBWrapper::CreateObfuscateKey() const return ret; } -std::optional CDBWrapper::ReadImpl(Span key) const +std::optional CDBWrapper::ReadImpl(std::span key) const { leveldb::Slice slKey(CharCast(key.data()), key.size()); std::string strValue; @@ -350,7 +350,7 @@ std::optional CDBWrapper::ReadImpl(Span key) const return strValue; } -bool CDBWrapper::ExistsImpl(Span key) const +bool CDBWrapper::ExistsImpl(std::span key) const { leveldb::Slice slKey(CharCast(key.data()), key.size()); @@ -365,7 +365,7 @@ bool CDBWrapper::ExistsImpl(Span key) const return true; } -size_t CDBWrapper::EstimateSizeImpl(Span key1, Span key2) const +size_t CDBWrapper::EstimateSizeImpl(std::span key1, std::span key2) const { leveldb::Slice slKey1(CharCast(key1.data()), key1.size()); leveldb::Slice slKey2(CharCast(key2.data()), key2.size()); @@ -396,18 +396,18 @@ CDBIterator* CDBWrapper::NewIterator() return new CDBIterator{*this, std::make_unique(DBContext().pdb->NewIterator(DBContext().iteroptions))}; } -void CDBIterator::SeekImpl(Span key) +void CDBIterator::SeekImpl(std::span key) { leveldb::Slice slKey(CharCast(key.data()), key.size()); m_impl_iter->iter->Seek(slKey); } -Span CDBIterator::GetKeyImpl() const +std::span CDBIterator::GetKeyImpl() const { return MakeByteSpan(m_impl_iter->iter->key()); } -Span CDBIterator::GetValueImpl() const +std::span CDBIterator::GetValueImpl() const { return MakeByteSpan(m_impl_iter->iter->value()); } diff --git a/src/dbwrapper.h b/src/dbwrapper.h index dd5daa7a1fc..98446361e4e 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -85,8 +85,8 @@ private: size_t size_estimate{0}; - void WriteImpl(Span key, DataStream& ssValue); - void EraseImpl(Span key); + void WriteImpl(std::span key, DataStream& ssValue); + void EraseImpl(std::span key); public: /** @@ -129,9 +129,9 @@ private: const CDBWrapper &parent; const std::unique_ptr m_impl_iter; - void SeekImpl(Span key); - Span GetKeyImpl() const; - Span GetValueImpl() const; + void SeekImpl(std::span key); + std::span GetKeyImpl() const; + std::span GetValueImpl() const; public: @@ -206,9 +206,9 @@ private: //! whether or not the database resides in memory bool m_is_memory; - std::optional ReadImpl(Span key) const; - bool ExistsImpl(Span key) const; - size_t EstimateSizeImpl(Span key1, Span key2) const; + std::optional ReadImpl(std::span key) const; + bool ExistsImpl(std::span key) const; + size_t EstimateSizeImpl(std::span key1, std::span key2) const; auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); } public: diff --git a/src/hash.cpp b/src/hash.cpp index 78a969ecd38..8e6cb5c5445 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2022 The Bitcoin Core developers +// Copyright (c) 2013-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -10,7 +10,7 @@ #include #include -unsigned int MurmurHash3(unsigned int nHashSeed, Span vDataToHash) +unsigned int MurmurHash3(unsigned int nHashSeed, std::span vDataToHash) { // The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp uint32_t h1 = nHashSeed; diff --git a/src/hash.h b/src/hash.h index 52babf8b1d9..34486af64a1 100644 --- a/src/hash.h +++ b/src/hash.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -27,14 +27,14 @@ private: public: static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE; - void Finalize(Span output) { + void Finalize(std::span output) { assert(output.size() == OUTPUT_SIZE); unsigned char buf[CSHA256::OUTPUT_SIZE]; sha.Finalize(buf); sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); } - CHash256& Write(Span input) { + CHash256& Write(std::span input) { sha.Write(input.data(), input.size()); return *this; } @@ -52,14 +52,14 @@ private: public: static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE; - void Finalize(Span output) { + void Finalize(std::span output) { assert(output.size() == OUTPUT_SIZE); unsigned char buf[CSHA256::OUTPUT_SIZE]; sha.Finalize(buf); CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data()); } - CHash160& Write(Span input) { + CHash160& Write(std::span input) { sha.Write(input.data(), input.size()); return *this; } @@ -103,7 +103,7 @@ private: CSHA256 ctx; public: - void write(Span src) + void write(std::span src) { ctx.Write(UCharCast(src.data()), src.size()); } @@ -155,7 +155,7 @@ private: public: explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} - void read(Span dst) + void read(std::span dst) { m_source.read(dst); this->write(dst); @@ -189,7 +189,7 @@ private: public: explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {} - void write(Span src) + void write(std::span src) { m_source.write(src); HashWriter::write(src); @@ -206,7 +206,7 @@ public: /** Single-SHA256 a 32-byte input (represented as uint256). */ [[nodiscard]] uint256 SHA256Uint256(const uint256& input); -unsigned int MurmurHash3(unsigned int nHashSeed, Span vDataToHash); +unsigned int MurmurHash3(unsigned int nHashSeed, std::span vDataToHash); void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]); @@ -219,7 +219,7 @@ void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char he HashWriter TaggedHash(const std::string& tag); /** Compute the 160-bit RIPEMD-160 hash of an array. */ -inline uint160 RIPEMD160(Span data) +inline uint160 RIPEMD160(std::span data) { uint160 result; CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin()); diff --git a/src/i2p.cpp b/src/i2p.cpp index 0420bc92382..156c8024339 100644 --- a/src/i2p.cpp +++ b/src/i2p.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -309,7 +310,7 @@ Session::Reply Session::SendRequestAndGetReply(const Sock& sock, reply.full = sock.RecvUntilTerminator('\n', recv_timeout, *m_interrupt, MAX_MSG_SIZE); for (const auto& kv : Split(reply.full, ' ')) { - const auto& pos = std::find(kv.begin(), kv.end(), '='); + const auto pos{std::ranges::find(kv, '=')}; if (pos != kv.end()) { reply.keys.emplace(std::string{kv.begin(), pos}, std::string{pos + 1, kv.end()}); } else { diff --git a/src/key.cpp b/src/key.cpp index 360a1f46d3f..01fa3d270e3 100644 --- a/src/key.cpp +++ b/src/key.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Copyright (c) 2017 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -269,7 +269,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector& vchSig) return true; } -bool CKey::SignSchnorr(const uint256& hash, Span sig, const uint256* merkle_root, const uint256& aux) const +bool CKey::SignSchnorr(const uint256& hash, std::span sig, const uint256* merkle_root, const uint256& aux) const { KeyPair kp = ComputeKeyPair(merkle_root); return kp.SignSchnorr(hash, sig, aux); @@ -308,7 +308,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const return ret; } -EllSwiftPubKey CKey::EllSwiftCreate(Span ent32) const +EllSwiftPubKey CKey::EllSwiftCreate(std::span ent32) const { assert(keydata); assert(ent32.size() == 32); @@ -365,7 +365,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const { return key.Derive(out.key, out.chaincode, _nChild, chaincode); } -void CExtKey::SetSeed(Span seed) +void CExtKey::SetSeed(std::span seed) { static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'}; std::vector> vout(64); @@ -423,7 +423,7 @@ KeyPair::KeyPair(const CKey& key, const uint256* merkle_root) if (!success) ClearKeyPairData(); } -bool KeyPair::SignSchnorr(const uint256& hash, Span sig, const uint256& aux) const +bool KeyPair::SignSchnorr(const uint256& hash, std::span sig, const uint256& aux) const { assert(sig.size() == 64); if (!IsValid()) return false; diff --git a/src/key.h b/src/key.h index 6ed6fd0fc3d..22f96880b7b 100644 --- a/src/key.h +++ b/src/key.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Copyright (c) 2017 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -170,7 +170,7 @@ public: * (this is used for key path spending, with specific * Merkle root of the script tree). */ - bool SignSchnorr(const uint256& hash, Span sig, const uint256* merkle_root, const uint256& aux) const; + bool SignSchnorr(const uint256& hash, std::span sig, const uint256* merkle_root, const uint256& aux) const; //! Derive BIP32 child key. [[nodiscard]] bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const; @@ -192,7 +192,7 @@ public: * resulting encoding will be indistinguishable from uniform to any adversary who does not * know the private key (because the private key itself is always used as entropy as well). */ - EllSwiftPubKey EllSwiftCreate(Span entropy) const; + EllSwiftPubKey EllSwiftCreate(std::span entropy) const; /** Compute a BIP324-style ECDH shared secret. * @@ -250,7 +250,7 @@ struct CExtKey { void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); [[nodiscard]] bool Derive(CExtKey& out, unsigned int nChild) const; CExtPubKey Neuter() const; - void SetSeed(Span seed); + void SetSeed(std::span seed); }; /** KeyPair @@ -286,7 +286,7 @@ public: KeyPair(const KeyPair& other) { *this = other; } friend KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const; - [[nodiscard]] bool SignSchnorr(const uint256& hash, Span sig, const uint256& aux) const; + [[nodiscard]] bool SignSchnorr(const uint256& hash, std::span sig, const uint256& aux) const; //! Check whether this keypair is valid. bool IsValid() const { return !!m_keypair; } diff --git a/src/net.cpp b/src/net.cpp index 735985a8414..0bab3dcd27f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -663,7 +663,7 @@ void CNode::CopyStats(CNodeStats& stats) } #undef X -bool CNode::ReceiveMsgBytes(Span msg_bytes, bool& complete) +bool CNode::ReceiveMsgBytes(std::span msg_bytes, bool& complete) { complete = false; const auto time = GetTime(); @@ -731,7 +731,7 @@ Transport::Info V1Transport::GetInfo() const noexcept return {.transport_type = TransportProtocolType::V1, .session_id = {}}; } -int V1Transport::readHeader(Span msg_bytes) +int V1Transport::readHeader(std::span msg_bytes) { AssertLockHeld(m_recv_mutex); // copy data to temporary parsing buffer @@ -772,7 +772,7 @@ int V1Transport::readHeader(Span msg_bytes) return nCopy; } -int V1Transport::readData(Span msg_bytes) +int V1Transport::readData(std::span msg_bytes) { AssertLockHeld(m_recv_mutex); unsigned int nRemaining = hdr.nMessageSize - nDataPos; @@ -823,7 +823,7 @@ CNetMessage V1Transport::GetReceivedMessage(const std::chrono::microseconds time if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) { LogDebug(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n", SanitizeString(msg.m_type), msg.m_message_size, - HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)), + HexStr(std::span{hash}.first(CMessageHeader::CHECKSUM_SIZE)), HexStr(hdr.pchChecksum), m_node_id); reject_message = true; @@ -868,14 +868,14 @@ Transport::BytesToSend V1Transport::GetBytesToSend(bool have_next_message) const AssertLockNotHeld(m_send_mutex); LOCK(m_send_mutex); if (m_sending_header) { - return {Span{m_header_to_send}.subspan(m_bytes_sent), + return {std::span{m_header_to_send}.subspan(m_bytes_sent), // We have more to send after the header if the message has payload, or if there // is a next message after that. have_next_message || !m_message_to_send.data.empty(), m_message_to_send.m_type }; } else { - return {Span{m_message_to_send.data}.subspan(m_bytes_sent), + return {std::span{m_message_to_send.data}.subspan(m_bytes_sent), // We only have more to send after this message's payload if there is another // message. have_next_message, @@ -997,7 +997,7 @@ void V2Transport::StartSendingHandshake() noexcept // We cannot wipe m_send_garbage as it will still be used as AAD later in the handshake. } -V2Transport::V2Transport(NodeId nodeid, bool initiating, const CKey& key, Span ent32, std::vector garbage) noexcept +V2Transport::V2Transport(NodeId nodeid, bool initiating, const CKey& key, std::span ent32, std::vector garbage) noexcept : m_cipher{key, ent32}, m_initiating{initiating}, m_nodeid{nodeid}, m_v1_fallback{nodeid}, m_recv_state{initiating ? RecvState::KEY : RecvState::KEY_MAYBE_V1}, @@ -1098,7 +1098,7 @@ void V2Transport::ProcessReceivedMaybeV1Bytes() noexcept } else if (m_recv_buffer.size() == v1_prefix.size()) { // Full match with the v1 prefix, so fall back to v1 behavior. LOCK(m_send_mutex); - Span feedback{m_recv_buffer}; + std::span feedback{m_recv_buffer}; // Feed already received bytes to v1 transport. It should always accept these, because it's // less than the size of a v1 header, and these are the first bytes fed to m_v1_fallback. bool ret = m_v1_fallback.ReceivedBytes(feedback); @@ -1132,7 +1132,7 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept if (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) { if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) { LogDebug(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n", - HexStr(Span(m_recv_buffer).first(OFFSET))); + HexStr(std::span(m_recv_buffer).first(OFFSET))); return false; } } @@ -1319,7 +1319,7 @@ size_t V2Transport::GetMaxBytesToProcess() noexcept return 0; } -bool V2Transport::ReceivedBytes(Span& msg_bytes) noexcept +bool V2Transport::ReceivedBytes(std::span& msg_bytes) noexcept { AssertLockNotHeld(m_recv_mutex); /** How many bytes to allocate in the receive buffer at most above what is received so far. */ @@ -1409,7 +1409,7 @@ bool V2Transport::ReceivedBytes(Span& msg_bytes) noexcept return true; } -std::optional V2Transport::GetMessageType(Span& contents) noexcept +std::optional V2Transport::GetMessageType(std::span& contents) noexcept { if (contents.size() == 0) return std::nullopt; // Empty contents uint8_t first_byte = contents[0]; @@ -1456,7 +1456,7 @@ CNetMessage V2Transport::GetReceivedMessage(std::chrono::microseconds time, bool if (m_recv_state == RecvState::V1) return m_v1_fallback.GetReceivedMessage(time, reject_message); Assume(m_recv_state == RecvState::APP_READY); - Span contents{m_recv_decode_buffer}; + std::span contents{m_recv_decode_buffer}; auto msg_type = GetMessageType(contents); CNetMessage msg{DataStream{}}; // Note that BIP324Cipher::EXPANSION also includes the length descriptor size. @@ -1519,7 +1519,7 @@ Transport::BytesToSend V2Transport::GetBytesToSend(bool have_next_message) const if (m_send_state == SendState::MAYBE_V1) Assume(m_send_buffer.empty()); Assume(m_send_pos <= m_send_buffer.size()); return { - Span{m_send_buffer}.subspan(m_send_pos), + std::span{m_send_buffer}.subspan(m_send_pos), // We only have more to send after the current m_send_buffer if there is a (next) // message to be sent, and we're capable of sending packets. */ have_next_message && m_send_state == SendState::READY, @@ -2050,7 +2050,7 @@ bool CConnman::InactivityCheck(const CNode& node) const return false; } -Sock::EventsPerSock CConnman::GenerateWaitSockets(Span nodes) +Sock::EventsPerSock CConnman::GenerateWaitSockets(std::span nodes) { Sock::EventsPerSock events_per_sock; @@ -2511,7 +2511,7 @@ bool CConnman::MaybePickPreferredNetwork(std::optional& network) return false; } -void CConnman::ThreadOpenConnections(const std::vector connect, Span seed_nodes) +void CConnman::ThreadOpenConnections(const std::vector connect, std::span seed_nodes) { AssertLockNotHeld(m_unused_i2p_sessions_mutex); AssertLockNotHeld(m_reconnections_mutex); @@ -3982,7 +3982,7 @@ void CConnman::ASMapHealthCheck() // Dump binary message to file, with timestamp. static void CaptureMessageToFile(const CAddress& addr, const std::string& msg_type, - Span data, + std::span data, bool is_incoming) { // Note: This function captures the message at the time of processing, @@ -4002,7 +4002,7 @@ static void CaptureMessageToFile(const CAddress& addr, AutoFile f{fsbridge::fopen(path, "ab")}; ser_writedata64(f, now.count()); - f << Span{msg_type}; + f << std::span{msg_type}; for (auto i = msg_type.length(); i < CMessageHeader::MESSAGE_TYPE_SIZE; ++i) { f << uint8_t{'\0'}; } @@ -4013,6 +4013,6 @@ static void CaptureMessageToFile(const CAddress& addr, std::function data, + std::span data, bool is_incoming)> CaptureMessage = CaptureMessageToFile; diff --git a/src/net.h b/src/net.h index e64d9a67f46..9fdec52115e 100644 --- a/src/net.h +++ b/src/net.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -276,7 +276,7 @@ public: * * Consumed bytes are chopped off the front of msg_bytes. */ - virtual bool ReceivedBytes(Span& msg_bytes) = 0; + virtual bool ReceivedBytes(std::span& msg_bytes) = 0; /** Retrieve a completed message from transport. * @@ -298,14 +298,14 @@ public: virtual bool SetMessageToSend(CSerializedNetMsg& msg) noexcept = 0; /** Return type for GetBytesToSend, consisting of: - * - Span to_send: span of bytes to be sent over the wire (possibly empty). + * - std::span to_send: span of bytes to be sent over the wire (possibly empty). * - bool more: whether there will be more bytes to be sent after the ones in to_send are * all sent (as signaled by MarkBytesSent()). * - const std::string& m_type: message type on behalf of which this is being sent * ("" for bytes that are not on behalf of any message). */ using BytesToSend = std::tuple< - Span /*to_send*/, + std::span /*to_send*/, bool /*more*/, const std::string& /*m_type*/ >; @@ -380,8 +380,8 @@ private: unsigned int nDataPos GUARDED_BY(m_recv_mutex); const uint256& GetMessageHash() const EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); - int readHeader(Span msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); - int readData(Span msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); + int readHeader(std::span msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); + int readData(std::span msg_bytes) EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); void Reset() EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex) { AssertLockHeld(m_recv_mutex); @@ -424,7 +424,7 @@ public: Info GetInfo() const noexcept override; - bool ReceivedBytes(Span& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex) + bool ReceivedBytes(std::span& msg_bytes) override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex) { AssertLockNotHeld(m_recv_mutex); LOCK(m_recv_mutex); @@ -616,7 +616,7 @@ private: /** Change the send state. */ void SetSendState(SendState send_state) noexcept EXCLUSIVE_LOCKS_REQUIRED(m_send_mutex); /** Given a packet's contents, find the message type (if valid), and strip it from contents. */ - static std::optional GetMessageType(Span& contents) noexcept; + static std::optional GetMessageType(std::span& contents) noexcept; /** Determine how many received bytes can be processed in one go (not allowed in V1 state). */ size_t GetMaxBytesToProcess() noexcept EXCLUSIVE_LOCKS_REQUIRED(m_recv_mutex); /** Put our public key + garbage in the send buffer. */ @@ -641,11 +641,11 @@ public: V2Transport(NodeId nodeid, bool initiating) noexcept; /** Construct a V2 transport with specified keys and garbage (test use only). */ - V2Transport(NodeId nodeid, bool initiating, const CKey& key, Span ent32, std::vector garbage) noexcept; + V2Transport(NodeId nodeid, bool initiating, const CKey& key, std::span ent32, std::vector garbage) noexcept; // Receive side functions. bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); - bool ReceivedBytes(Span& msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex, !m_send_mutex); + bool ReceivedBytes(std::span& msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex, !m_send_mutex); CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool& reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex); // Send side functions. @@ -914,7 +914,7 @@ public: * @return True if the peer should stay connected, * False if the peer should be disconnected from. */ - bool ReceiveMsgBytes(Span msg_bytes, bool& complete) EXCLUSIVE_LOCKS_REQUIRED(!cs_vRecv); + bool ReceiveMsgBytes(std::span msg_bytes, bool& complete) EXCLUSIVE_LOCKS_REQUIRED(!cs_vRecv); void SetCommonVersion(int greatest_common_version) { @@ -1297,7 +1297,7 @@ private: void ThreadOpenAddedConnections() EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_unused_i2p_sessions_mutex, !m_reconnections_mutex); void AddAddrFetch(const std::string& strDest) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex); void ProcessAddrFetch() EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex, !m_unused_i2p_sessions_mutex); - void ThreadOpenConnections(std::vector connect, Span seed_nodes) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex, !m_added_nodes_mutex, !m_nodes_mutex, !m_unused_i2p_sessions_mutex, !m_reconnections_mutex); + void ThreadOpenConnections(std::vector connect, std::span seed_nodes) EXCLUSIVE_LOCKS_REQUIRED(!m_addr_fetches_mutex, !m_added_nodes_mutex, !m_nodes_mutex, !m_unused_i2p_sessions_mutex, !m_reconnections_mutex); void ThreadMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc); void ThreadI2PAcceptIncoming(); void AcceptConnection(const ListenSocket& hListenSocket); @@ -1325,7 +1325,7 @@ private: * @param[in] nodes Select from these nodes' sockets. * @return sockets to check for readiness */ - Sock::EventsPerSock GenerateWaitSockets(Span nodes); + Sock::EventsPerSock GenerateWaitSockets(std::span nodes); /** * Check connected and listening sockets for IO readiness and process them accordingly. @@ -1679,7 +1679,7 @@ private: /** Defaults to `CaptureMessageToFile()`, but can be overridden by unit tests. */ extern std::function data, + std::span data, bool is_incoming)> CaptureMessage; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 3f6d0f3e42b..05fa7119b9e 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -2281,7 +2281,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& pfrom.fDisconnect = true; return; } - MakeAndPushMessage(pfrom, NetMsgType::BLOCK, Span{block_data}); + MakeAndPushMessage(pfrom, NetMsgType::BLOCK, std::span{block_data}); // Don't set pblock as we've sent the block } else { // Send block from disk diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 5ad0da7792e..ca562a96cae 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -134,7 +134,7 @@ void CNetAddr::SetIP(const CNetAddr& ipIn) m_addr = ipIn.m_addr; } -void CNetAddr::SetLegacyIPv6(Span ipv6) +void CNetAddr::SetLegacyIPv6(std::span ipv6) { assert(ipv6.size() == ADDR_IPV6_SIZE); @@ -187,7 +187,7 @@ static constexpr size_t CHECKSUM_LEN = 2; static const unsigned char VERSION[] = {3}; static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION); -static void Checksum(Span addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN]) +static void Checksum(std::span addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN]) { // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2] static const unsigned char prefix[] = ".onion checksum"; @@ -195,7 +195,7 @@ static void Checksum(Span addr_pubkey, uint8_t (&checksum)[CHECKS SHA3_256 hasher; - hasher.Write(Span{prefix}.first(prefix_len)); + hasher.Write(std::span{prefix}.first(prefix_len)); hasher.Write(addr_pubkey); hasher.Write(VERSION); @@ -241,9 +241,9 @@ bool CNetAddr::SetTor(const std::string& addr) } if (input->size() == torv3::TOTAL_LEN) { - Span input_pubkey{input->data(), ADDR_TORV3_SIZE}; - 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)}; + std::span input_pubkey{input->data(), ADDR_TORV3_SIZE}; + std::span input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN}; + std::span input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)}; if (!std::ranges::equal(input_version, torv3::VERSION)) { return false; @@ -508,14 +508,14 @@ enum Network CNetAddr::GetNetwork() const return m_net; } -static std::string IPv4ToString(Span a) +static std::string IPv4ToString(std::span a) { return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]); } // Return an IPv6 address text representation with zero compression as described in RFC 5952 // ("A Recommendation for IPv6 Address Text Representation"). -static std::string IPv6ToString(Span a, uint32_t scope_id) +static std::string IPv6ToString(std::span a, uint32_t scope_id) { assert(a.size() == ADDR_IPV6_SIZE); const std::array groups{ @@ -570,7 +570,7 @@ static std::string IPv6ToString(Span a, uint32_t scope_id) return r; } -std::string OnionToString(Span addr) +std::string OnionToString(std::span addr) { uint8_t checksum[torv3::CHECKSUM_LEN]; torv3::Checksum(addr, checksum); @@ -664,13 +664,13 @@ uint32_t CNetAddr::GetLinkedIPv4() const return ReadBE32(m_addr.data()); } else if (IsRFC6052() || IsRFC6145()) { // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address - return ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data()); + return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data()); } else if (IsRFC3964()) { // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6 - return ReadBE32(Span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data()); + return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data()); } else if (IsRFC4380()) { // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped - return ~ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data()); + return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data()); } assert(false); } diff --git a/src/netaddress.h b/src/netaddress.h index ad83c5381c3..7c311e2f3ab 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -103,7 +103,7 @@ static constexpr size_t ADDR_INTERNAL_SIZE = 10; /// SAM 3.1 and earlier do not support specifying ports and force the port to 0. static constexpr uint16_t I2P_SAM31_PORT{0}; -std::string OnionToString(Span addr); +std::string OnionToString(std::span addr); /** * Network address. @@ -139,7 +139,7 @@ public: * (e.g. IPv4) disguised as IPv6. This encoding is used in the legacy * `addr` encoding. */ - void SetLegacyIPv6(Span ipv6); + void SetLegacyIPv6(std::span ipv6); bool SetInternal(const std::string& name); @@ -437,7 +437,7 @@ private: if (SetNetFromBIP155Network(bip155_net, address_size)) { m_addr.resize(address_size); - s >> Span{m_addr}; + s >> std::span{m_addr}; if (m_net != NET_IPV6) { return; diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index ed336928235..47840fcb0a0 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -281,7 +281,7 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) // - No annexes if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) { // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341) - Span stack{tx.vin[i].scriptWitness.stack}; + std::span stack{tx.vin[i].scriptWitness.stack}; if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) { // Annexes are nonstandard as long as no semantics are defined for them. return false; diff --git a/src/psbt.cpp b/src/psbt.cpp index 19d855e4c78..9369cfc334d 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -543,7 +543,7 @@ bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base6 return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error); } -bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span tx_data, std::string& error) +bool DecodeRawPSBT(PartiallySignedTransaction& psbt, std::span tx_data, std::string& error) { DataStream ss_data{tx_data}; try { diff --git a/src/psbt.h b/src/psbt.h index 6d49864b3cd..a53767ab54b 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -188,7 +188,7 @@ void SerializeHDKeypaths(Stream& s, const std::map& hd_k if (!keypath_pair.first.IsValid()) { throw std::ios_base::failure("Invalid CPubKey being serialized"); } - SerializeToVector(s, type, Span{keypath_pair.first}); + SerializeToVector(s, type, std::span{keypath_pair.first}); SerializeHDKeypath(s, keypath_pair.second); } } @@ -242,7 +242,7 @@ struct PSBTInput if (final_script_sig.empty() && final_script_witness.IsNull()) { // Write any partial signatures for (const auto& sig_pair : partial_sigs) { - SerializeToVector(s, CompactSizeWriter(PSBT_IN_PARTIAL_SIG), Span{sig_pair.second.first}); + SerializeToVector(s, CompactSizeWriter(PSBT_IN_PARTIAL_SIG), std::span{sig_pair.second.first}); s << sig_pair.second.second; } @@ -269,25 +269,25 @@ struct PSBTInput // Write any ripemd160 preimage for (const auto& [hash, preimage] : ripemd160_preimages) { - SerializeToVector(s, CompactSizeWriter(PSBT_IN_RIPEMD160), Span{hash}); + SerializeToVector(s, CompactSizeWriter(PSBT_IN_RIPEMD160), std::span{hash}); s << preimage; } // Write any sha256 preimage for (const auto& [hash, preimage] : sha256_preimages) { - SerializeToVector(s, CompactSizeWriter(PSBT_IN_SHA256), Span{hash}); + SerializeToVector(s, CompactSizeWriter(PSBT_IN_SHA256), std::span{hash}); s << preimage; } // Write any hash160 preimage for (const auto& [hash, preimage] : hash160_preimages) { - SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH160), Span{hash}); + SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH160), std::span{hash}); s << preimage; } // Write any hash256 preimage for (const auto& [hash, preimage] : hash256_preimages) { - SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH256), Span{hash}); + SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH256), std::span{hash}); s << preimage; } @@ -308,7 +308,7 @@ struct PSBTInput for (const auto& [leaf, control_blocks] : m_tap_scripts) { const auto& [script, leaf_ver] = leaf; for (const auto& control_block : control_blocks) { - SerializeToVector(s, PSBT_IN_TAP_LEAF_SCRIPT, Span{control_block}); + SerializeToVector(s, PSBT_IN_TAP_LEAF_SCRIPT, std::span{control_block}); std::vector value_v(script.begin(), script.end()); value_v.push_back((uint8_t)leaf_ver); s << value_v; @@ -594,7 +594,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{Span{key}.subspan(1)}; + SpanReader s_key{std::span{key}.subspan(1)}; XOnlyPubKey xonly; uint256 hash; s_key >> xonly; @@ -636,7 +636,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{Span{key}.subspan(1)}; + SpanReader s_key{std::span{key}.subspan(1)}; XOnlyPubKey xonly; s_key >> xonly; std::set leaf_hashes; @@ -893,7 +893,7 @@ struct PSBTOutput } else if (key.size() != 33) { throw std::ios_base::failure("Output Taproot BIP32 keypath key is not at 33 bytes"); } - XOnlyPubKey xonly(uint256(Span(key).last(32))); + XOnlyPubKey xonly(uint256(std::span(key).last(32))); std::set leaf_hashes; uint64_t value_len = ReadCompactSize(s); size_t before_hashes = s.size(); @@ -1279,6 +1279,6 @@ bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransacti //! Decode a base64ed PSBT into a PartiallySignedTransaction [[nodiscard]] bool DecodeBase64PSBT(PartiallySignedTransaction& decoded_psbt, const std::string& base64_psbt, std::string& error); //! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction -[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, Span raw_psbt, std::string& error); +[[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, std::span raw_psbt, std::string& error); #endif // BITCOIN_PSBT_H diff --git a/src/pubkey.cpp b/src/pubkey.cpp index fb25ebd4ca7..a4ca9a170a9 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Copyright (c) 2017 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -227,7 +227,7 @@ bool XOnlyPubKey::IsFullyValid() const return secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey, m_keydata.data()); } -bool XOnlyPubKey::VerifySchnorr(const uint256& msg, Span sigbytes) const +bool XOnlyPubKey::VerifySchnorr(const uint256& msg, std::span sigbytes) const { assert(sigbytes.size() == 64); secp256k1_xonly_pubkey pubkey; @@ -353,7 +353,7 @@ bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChi return true; } -EllSwiftPubKey::EllSwiftPubKey(Span ellswift) noexcept +EllSwiftPubKey::EllSwiftPubKey(std::span ellswift) noexcept { assert(ellswift.size() == SIZE); std::copy(ellswift.begin(), ellswift.end(), m_pubkey.begin()); diff --git a/src/pubkey.h b/src/pubkey.h index b4666aad228..cbc827dc606 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Copyright (c) 2017 The Zcash developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -103,7 +103,7 @@ public: } //! Construct a public key from a byte vector. - explicit CPubKey(Span _vch) + explicit CPubKey(std::span _vch) { Set(_vch.begin(), _vch.end()); } @@ -142,14 +142,14 @@ public: { unsigned int len = size(); ::WriteCompactSize(s, len); - s << Span{vch, len}; + s << std::span{vch, len}; } template void Unserialize(Stream& s) { const unsigned int len(::ReadCompactSize(s)); if (len <= SIZE) { - s >> Span{vch, len}; + s >> std::span{vch, len}; if (len != size()) { Invalidate(); } @@ -163,13 +163,13 @@ public: //! Get the KeyID of this public key (hash of its serialization) CKeyID GetID() const { - return CKeyID(Hash160(Span{vch}.first(size()))); + return CKeyID(Hash160(std::span{vch}.first(size()))); } //! Get the 256-bit hash of this public key. uint256 GetHash() const { - return Hash(Span{vch}.first(size())); + return Hash(std::span{vch}.first(size())); } /* @@ -257,13 +257,13 @@ public: constexpr explicit XOnlyPubKey(std::span bytes) : m_keydata{bytes} {} /** Construct an x-only pubkey from a normal pubkey. */ - explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span{pubkey}.subspan(1, 32)) {} + explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(std::span{pubkey}.subspan(1, 32)) {} /** Verify a Schnorr signature against this public key. * * sigbytes must be exactly 64 bytes. */ - bool VerifySchnorr(const uint256& msg, Span sigbytes) const; + bool VerifySchnorr(const uint256& msg, std::span sigbytes) const; /** Compute the Taproot tweak as specified in BIP341, with *this as internal * key: @@ -317,7 +317,7 @@ public: EllSwiftPubKey() noexcept = default; /** Construct a new ellswift public key from a given serialization. */ - EllSwiftPubKey(Span ellswift) noexcept; + EllSwiftPubKey(std::span ellswift) noexcept; /** Decode to normal compressed CPubKey (for debugging purposes). */ CPubKey Decode() const; diff --git a/src/random.cpp b/src/random.cpp index 5b605e988d0..5da053f74e4 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -505,7 +505,7 @@ public: // Handle requests for deterministic randomness. if (!always_use_real_rng && m_deterministic_prng.has_value()) [[unlikely]] { // Overwrite the beginning of buf, which will be used for output. - m_deterministic_prng->Keystream(AsWritableBytes(Span{buf, num})); + m_deterministic_prng->Keystream(std::as_writable_bytes(std::span{buf, num})); // Do not require strong seeding for deterministic output. ret = true; } @@ -673,13 +673,13 @@ void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept } std::atomic g_used_g_prng{false}; // Only accessed from tests -void GetRandBytes(Span bytes) noexcept +void GetRandBytes(std::span bytes) noexcept { g_used_g_prng = true; ProcRand(bytes.data(), bytes.size(), RNGLevel::FAST, /*always_use_real_rng=*/false); } -void GetStrongRandBytes(Span bytes) noexcept +void GetStrongRandBytes(std::span bytes) noexcept { ProcRand(bytes.data(), bytes.size(), RNGLevel::SLOW, /*always_use_real_rng=*/true); } @@ -698,7 +698,7 @@ void FastRandomContext::RandomSeed() noexcept requires_seed = false; } -void FastRandomContext::fillrand(Span output) noexcept +void FastRandomContext::fillrand(std::span output) noexcept { if (requires_seed) RandomSeed(); rng.Keystream(output); diff --git a/src/random.h b/src/random.h index 39bcd086f75..c702309d0c3 100644 --- a/src/random.h +++ b/src/random.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -114,7 +114,7 @@ void RandAddEvent(const uint32_t event_info) noexcept; * * Thread-safe. */ -void GetRandBytes(Span bytes) noexcept; +void GetRandBytes(std::span bytes) noexcept; /** * Gather entropy from various sources, feed it into the internal PRNG, and @@ -126,7 +126,7 @@ void GetRandBytes(Span bytes) noexcept; * * Thread-safe. */ -void GetStrongRandBytes(Span bytes) noexcept; +void GetStrongRandBytes(std::span bytes) noexcept; /* ============================= RANDOM NUMBER GENERATION CLASSES ============================= @@ -144,7 +144,7 @@ class RandomMixin; /** A concept for RandomMixin-based random number generators. */ template -concept RandomNumberGenerator = requires(T& rng, Span s) { +concept RandomNumberGenerator = requires(T& rng, std::span s) { // A random number generator must provide rand64(). { rng.rand64() } noexcept -> std::same_as; // A random number generator must derive from RandomMixin, which adds other rand* functions. @@ -263,8 +263,8 @@ public: } } - /** Fill a Span with random bytes. */ - void fillrand(Span span) noexcept + /** Fill a span with random bytes. */ + void fillrand(std::span span) noexcept { while (span.size() >= 8) { uint64_t gen = Impl().rand64(); @@ -400,8 +400,8 @@ public: return ReadLE64(buf.data()); } - /** Fill a byte Span with random bytes. This overrides the RandomMixin version. */ - void fillrand(Span output) noexcept; + /** Fill a byte span with random bytes. This overrides the RandomMixin version. */ + void fillrand(std::span output) noexcept; }; /** xoroshiro128++ PRNG. Extremely fast, not appropriate for cryptographic purposes. diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 421656152cb..950c7e04004 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -1074,7 +1074,7 @@ static RPCHelpMan decodepsbt() UniValue keypath(UniValue::VOBJ); keypath.pushKV("xpub", EncodeBase58Check(ser_xpub)); - keypath.pushKV("master_fingerprint", HexStr(Span(xpub_pair.first.fingerprint, xpub_pair.first.fingerprint + 4))); + keypath.pushKV("master_fingerprint", HexStr(std::span(xpub_pair.first.fingerprint, xpub_pair.first.fingerprint + 4))); keypath.pushKV("path", WriteHDKeypath(xpub_pair.first.path)); global_xpubs.push_back(std::move(keypath)); } diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index c8802d2bf89..3e42a4a7d67 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -102,7 +102,7 @@ uint64_t PolyMod(uint64_t c, int val) return c; } -std::string DescriptorChecksum(const Span& span) +std::string DescriptorChecksum(const std::span& span) { /** A character set designed such that: * - The most common 'unprotected' descriptor characters (hex, keypaths) are in the first group of 32. @@ -595,7 +595,7 @@ protected: * The origin info of the provided pubkeys is automatically added. * @return A vector with scriptPubKeys for this descriptor. */ - virtual std::vector MakeScripts(const std::vector& pubkeys, Span scripts, FlatSigningProvider& out) const = 0; + virtual std::vector MakeScripts(const std::vector& pubkeys, std::span scripts, FlatSigningProvider& out) const = 0; public: DescriptorImpl(std::vector> pubkeys, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args() {} @@ -725,7 +725,7 @@ public: out.origins.emplace(entry.first.GetID(), std::make_pair(CPubKey(entry.first), std::move(entry.second))); } - output_scripts = MakeScripts(pubkeys, Span{subscripts}, out); + output_scripts = MakeScripts(pubkeys, std::span{subscripts}, out); return true; } @@ -790,7 +790,7 @@ class AddressDescriptor final : public DescriptorImpl const CTxDestination m_destination; protected: std::string ToStringExtra() const override { return EncodeDestination(m_destination); } - std::vector MakeScripts(const std::vector&, Span, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(m_destination)); } + std::vector MakeScripts(const std::vector&, std::span, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(m_destination)); } public: AddressDescriptor(CTxDestination destination) : DescriptorImpl({}, "addr"), m_destination(std::move(destination)) {} bool IsSolvable() const final { return false; } @@ -815,7 +815,7 @@ class RawDescriptor final : public DescriptorImpl const CScript m_script; protected: std::string ToStringExtra() const override { return HexStr(m_script); } - std::vector MakeScripts(const std::vector&, Span, FlatSigningProvider&) const override { return Vector(m_script); } + std::vector MakeScripts(const std::vector&, std::span, FlatSigningProvider&) const override { return Vector(m_script); } public: RawDescriptor(CScript script) : DescriptorImpl({}, "raw"), m_script(std::move(script)) {} bool IsSolvable() const final { return false; } @@ -843,7 +843,7 @@ class PKDescriptor final : public DescriptorImpl private: const bool m_xonly; protected: - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider&) const override + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider&) const override { if (m_xonly) { CScript script = CScript() << ToByteVector(XOnlyPubKey(keys[0])) << OP_CHECKSIG; @@ -881,7 +881,7 @@ public: class PKHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider& out) const override { CKeyID id = keys[0].GetID(); out.pubkeys.emplace(id, keys[0]); @@ -915,7 +915,7 @@ public: class WPKHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider& out) const override { CKeyID id = keys[0].GetID(); out.pubkeys.emplace(id, keys[0]); @@ -949,7 +949,7 @@ public: class ComboDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider& out) const override { std::vector ret; CKeyID id = keys[0].GetID(); @@ -980,7 +980,7 @@ class MultisigDescriptor final : public DescriptorImpl const bool m_sorted; protected: std::string ToStringExtra() const override { return strprintf("%i", m_threshold); } - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider&) const override { + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider&) const override { if (m_sorted) { std::vector sorted_keys(keys); std::sort(sorted_keys.begin(), sorted_keys.end()); @@ -1026,7 +1026,7 @@ class MultiADescriptor final : public DescriptorImpl const bool m_sorted; protected: std::string ToStringExtra() const override { return strprintf("%i", m_threshold); } - std::vector MakeScripts(const std::vector& keys, Span, FlatSigningProvider&) const override { + std::vector MakeScripts(const std::vector& keys, std::span, FlatSigningProvider&) const override { CScript ret; std::vector xkeys; xkeys.reserve(keys.size()); @@ -1069,7 +1069,7 @@ public: class SHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector&, Span scripts, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector&, std::span scripts, FlatSigningProvider& out) const override { auto ret = Vector(GetScriptForDestination(ScriptHash(scripts[0]))); if (ret.size()) out.scripts.emplace(CScriptID(scripts[0]), scripts[0]); @@ -1119,7 +1119,7 @@ public: class WSHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector&, Span scripts, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector&, std::span scripts, FlatSigningProvider& out) const override { auto ret = Vector(GetScriptForDestination(WitnessV0ScriptHash(scripts[0]))); if (ret.size()) out.scripts.emplace(CScriptID(scripts[0]), scripts[0]); @@ -1161,7 +1161,7 @@ class TRDescriptor final : public DescriptorImpl { std::vector m_depths; protected: - std::vector MakeScripts(const std::vector& keys, Span scripts, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector& keys, std::span scripts, FlatSigningProvider& out) const override { TaprootBuilder builder; assert(m_depths.size() == scripts.size()); @@ -1304,7 +1304,7 @@ private: miniscript::NodeRef m_node; protected: - std::vector MakeScripts(const std::vector& keys, Span scripts, + std::vector MakeScripts(const std::vector& keys, std::span scripts, FlatSigningProvider& provider) const override { const auto script_ctx{m_node->GetMsCtx()}; @@ -1361,7 +1361,7 @@ public: class RawTRDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector& keys, Span scripts, FlatSigningProvider& out) const override + std::vector MakeScripts(const std::vector& keys, std::span scripts, FlatSigningProvider& out) const override { assert(keys.size() == 1); XOnlyPubKey xpk(keys[0]); @@ -1404,7 +1404,7 @@ enum class ParseScriptContext { P2TR, //!< Inside tr() (either internal key, or BIP342 script leaf) }; -std::optional ParseKeyPathNum(Span elem, bool& apostrophe, std::string& error) +std::optional ParseKeyPathNum(std::span elem, bool& apostrophe, std::string& error) { bool hardened = false; if (elem.size() > 0) { @@ -1437,7 +1437,7 @@ std::optional ParseKeyPathNum(Span elem, bool& apostrophe, * @param[in] allow_multipath Allows the parsed path to use the multipath specifier * @returns false if parsing failed **/ -[[nodiscard]] bool ParseKeyPath(const std::vector>& split, std::vector& out, bool& apostrophe, std::string& error, bool allow_multipath) +[[nodiscard]] bool ParseKeyPath(const std::vector>& split, std::vector& out, bool& apostrophe, std::string& error, bool allow_multipath) { KeyPath path; std::optional multipath_segment_index; @@ -1445,7 +1445,7 @@ std::optional ParseKeyPathNum(Span elem, bool& apostrophe, std::unordered_set seen_multipath; for (size_t i = 1; i < split.size(); ++i) { - const Span& elem = split[i]; + const std::span& elem = split[i]; // Check if element contain multipath specifier if (!elem.empty() && elem.front() == '<' && elem.back() == '>') { @@ -1459,7 +1459,7 @@ std::optional ParseKeyPathNum(Span elem, bool& apostrophe, } // Parse each possible value - std::vector> nums = Split(Span(elem.begin()+1, elem.end()-1), ";"); + std::vector> nums = Split(std::span(elem.begin()+1, elem.end()-1), ";"); if (nums.size() < 2) { error = "Multipath key path specifiers must have at least two items"; return false; @@ -1499,7 +1499,7 @@ std::optional ParseKeyPathNum(Span elem, bool& apostrophe, } /** Parse a public key that excludes origin information. */ -std::vector> ParsePubkeyInner(uint32_t key_exp_index, const Span& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error) +std::vector> ParsePubkeyInner(uint32_t key_exp_index, const std::span& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error) { std::vector> ret; bool permit_uncompressed = ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH; @@ -1558,11 +1558,11 @@ std::vector> ParsePubkeyInner(uint32_t key_exp_i } std::vector paths; DeriveType type = DeriveType::NO; - if (std::ranges::equal(split.back(), Span{"*"}.first(1))) { + if (std::ranges::equal(split.back(), std::span{"*"}.first(1))) { split.pop_back(); type = DeriveType::UNHARDENED; - } 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)); + } else if (std::ranges::equal(split.back(), std::span{"*'"}.first(2)) || std::ranges::equal(split.back(), std::span{"*h"}.first(2))) { + apostrophe = std::ranges::equal(split.back(), std::span{"*'"}.first(2)); split.pop_back(); type = DeriveType::HARDENED; } @@ -1578,7 +1578,7 @@ std::vector> ParsePubkeyInner(uint32_t key_exp_i } /** Parse a public key including origin information (if enabled). */ -std::vector> ParsePubkey(uint32_t key_exp_index, const Span& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error) +std::vector> ParsePubkey(uint32_t key_exp_index, const std::span& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error) { std::vector> ret; auto origin_split = Split(sp, ']'); @@ -1751,7 +1751,7 @@ struct KeyParser { /** Parse a script in a particular context. */ // NOLINTNEXTLINE(misc-no-recursion) -std::vector> ParseScript(uint32_t& key_exp_index, Span& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error) +std::vector> ParseScript(uint32_t& key_exp_index, std::span& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error) { using namespace script; Assume(ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH || ctx == ParseScriptContext::P2TR); @@ -2178,7 +2178,7 @@ std::unique_ptr InferMultiA(const CScript& script, ParseScriptCo std::unique_ptr InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider) { if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) { - XOnlyPubKey key{Span{script}.subspan(1, 32)}; + XOnlyPubKey key{std::span{script}.subspan(1, 32)}; return std::make_unique(InferXOnlyPubkey(key, ctx, provider), true); } @@ -2321,7 +2321,7 @@ std::unique_ptr InferScript(const CScript& script, ParseScriptCo } // namespace /** Check a descriptor checksum, and update desc to be the checksum-less part. */ -bool CheckChecksum(Span& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr) +bool CheckChecksum(std::span& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr) { auto check_split = Split(sp, '#'); if (check_split.size() > 2) { @@ -2356,7 +2356,7 @@ bool CheckChecksum(Span& sp, bool require_checksum, std::string& err std::vector> Parse(const std::string& descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum) { - Span sp{descriptor}; + std::span sp{descriptor}; if (!CheckChecksum(sp, require_checksum, error)) return {}; uint32_t key_exp_index = 0; auto ret = ParseScript(key_exp_index, sp, ParseScriptContext::TOP, out, error); @@ -2375,7 +2375,7 @@ std::string GetDescriptorChecksum(const std::string& descriptor) { std::string ret; std::string error; - Span sp{descriptor}; + std::span sp{descriptor}; if (!CheckChecksum(sp, false, error, &ret)) return ""; return ret; } diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index a35306b6935..61ea7f4503c 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -1278,12 +1278,12 @@ public: it = itBegin; while (scriptCode.GetOp(it, opcode)) { if (opcode == OP_CODESEPARATOR) { - s.write(AsBytes(Span{&itBegin[0], size_t(it - itBegin - 1)})); + s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin - 1)})); itBegin = it; } } if (itBegin != scriptCode.end()) - s.write(AsBytes(Span{&itBegin[0], size_t(it - itBegin)})); + s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin)})); } /** Serialize an input of txTo */ @@ -1639,7 +1639,7 @@ bool GenericTransactionSignatureChecker::VerifyECDSASignature(const std::vect } template -bool GenericTransactionSignatureChecker::VerifySchnorrSignature(Span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const +bool GenericTransactionSignatureChecker::VerifySchnorrSignature(std::span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const { return pubkey.VerifySchnorr(sighash, sig); } @@ -1670,7 +1670,7 @@ bool GenericTransactionSignatureChecker::CheckECDSASignature(const std::vecto } template -bool GenericTransactionSignatureChecker::CheckSchnorrSignature(Span sig, Span pubkey_in, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const +bool GenericTransactionSignatureChecker::CheckSchnorrSignature(std::span sig, std::span pubkey_in, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const { assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); // Schnorr signatures have 32-byte public keys. The caller is responsible for enforcing this. @@ -1785,7 +1785,7 @@ bool GenericTransactionSignatureChecker::CheckSequence(const CScriptNum& nSeq template class GenericTransactionSignatureChecker; template class GenericTransactionSignatureChecker; -static bool ExecuteWitnessScript(const Span& stack_span, const CScript& exec_script, unsigned int flags, SigVersion sigversion, const BaseSignatureChecker& checker, ScriptExecutionData& execdata, ScriptError* serror) +static bool ExecuteWitnessScript(const std::span& stack_span, const CScript& exec_script, unsigned int flags, SigVersion sigversion, const BaseSignatureChecker& checker, ScriptExecutionData& execdata, ScriptError* serror) { std::vector stack{stack_span.begin(), stack_span.end()}; @@ -1825,12 +1825,12 @@ static bool ExecuteWitnessScript(const Span& stack_span, const CS return true; } -uint256 ComputeTapleafHash(uint8_t leaf_version, Span script) +uint256 ComputeTapleafHash(uint8_t leaf_version, std::span script) { return (HashWriter{HASHER_TAPLEAF} << leaf_version << CompactSizeWriter(script.size()) << script).GetSHA256(); } -uint256 ComputeTapbranchHash(Span a, Span b) +uint256 ComputeTapbranchHash(std::span a, std::span b) { HashWriter ss_branch{HASHER_TAPBRANCH}; if (std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())) { @@ -1841,7 +1841,7 @@ uint256 ComputeTapbranchHash(Span a, Span control, const uint256& tapleaf_hash) +uint256 ComputeTaprootMerkleRoot(std::span control, const uint256& tapleaf_hash) { assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); assert(control.size() <= TAPROOT_CONTROL_MAX_SIZE); @@ -1850,7 +1850,7 @@ uint256 ComputeTaprootMerkleRoot(Span control, const uint25 const int path_len = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE; uint256 k = tapleaf_hash; for (int i = 0; i < path_len; ++i) { - Span node{Span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)}; + std::span node{std::span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)}; k = ComputeTapbranchHash(k, node); } return k; @@ -1861,7 +1861,7 @@ static bool VerifyTaprootCommitment(const std::vector& control, c assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); assert(program.size() >= uint256::size()); //! The internal pubkey (x-only, so no Y coordinate parity). - const XOnlyPubKey p{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)}; + const XOnlyPubKey p{std::span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)}; //! The output pubkey (taken from the scriptPubKey). const XOnlyPubKey q{program}; // Compute the Merkle root from the leaf and the provided path. @@ -1873,7 +1873,7 @@ static bool VerifyTaprootCommitment(const std::vector& control, c static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion, const std::vector& program, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror, bool is_p2sh) { CScript exec_script; //!< Actually executed script (last stack item in P2WSH; implied P2PKH script in P2WPKH; leaf script in P2TR) - Span stack{witness.stack}; + std::span stack{witness.stack}; ScriptExecutionData execdata; if (witversion == 0) { diff --git a/src/script/interpreter.h b/src/script/interpreter.h index e2fb1998f0b..e8c5b09045f 100644 --- a/src/script/interpreter.h +++ b/src/script/interpreter.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -250,7 +250,7 @@ public: return false; } - virtual bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const + virtual bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const { return false; } @@ -292,13 +292,13 @@ private: protected: virtual bool VerifyECDSASignature(const std::vector& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const; - virtual bool VerifySchnorrSignature(Span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const; + virtual bool VerifySchnorrSignature(std::span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const; public: GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(nullptr) {} GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {} bool CheckECDSASignature(const std::vector& scriptSig, const std::vector& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override; - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override; + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override; bool CheckLockTime(const CScriptNum& nLockTime) const override; bool CheckSequence(const CScriptNum& nSequence) const override; }; @@ -319,7 +319,7 @@ public: return m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion); } - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override { return m_checker.CheckSchnorrSignature(sig, pubkey, sigversion, execdata, serror); } @@ -335,13 +335,13 @@ public: }; /** Compute the BIP341 tapleaf hash from leaf version & script. */ -uint256 ComputeTapleafHash(uint8_t leaf_version, Span script); +uint256 ComputeTapleafHash(uint8_t leaf_version, std::span script); /** Compute the BIP341 tapbranch hash from two branches. * Spans must be 32 bytes each. */ -uint256 ComputeTapbranchHash(Span a, Span b); +uint256 ComputeTapbranchHash(std::span a, std::span b); /** Compute the BIP341 taproot script tree Merkle root from control block and leaf hash. * Requires control block to have valid length (33 + k*32, with k in {0,1,..,128}). */ -uint256 ComputeTaprootMerkleRoot(Span control, const uint256& tapleaf_hash); +uint256 ComputeTaprootMerkleRoot(std::span control, const uint256& tapleaf_hash); bool EvalScript(std::vector >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* error = nullptr); bool EvalScript(std::vector >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr); diff --git a/src/script/miniscript.cpp b/src/script/miniscript.cpp index 4b8d3673f95..29cae1ec535 100644 --- a/src/script/miniscript.cpp +++ b/src/script/miniscript.cpp @@ -419,7 +419,7 @@ std::optional ParseScriptNumber(const Opcode& in) { return {}; } -int FindNextChar(Span sp, const char m) +int FindNextChar(std::span sp, const char m) { for (int i = 0; i < (int)sp.size(); ++i) { if (sp[i] == m) return i; diff --git a/src/script/miniscript.h b/src/script/miniscript.h index 03f0a7c3654..450a3d56451 100644 --- a/src/script/miniscript.h +++ b/src/script/miniscript.h @@ -531,7 +531,7 @@ struct Node { NodeRef Clone() const { // Use TreeEval() to avoid a stack-overflow due to recursion - auto upfn = [](const Node& node, Span> children) { + auto upfn = [](const Node& node, std::span> children) { std::vector> new_subs; for (auto child = children.begin(); child != children.end(); ++child) { new_subs.emplace_back(std::move(*child)); @@ -592,8 +592,8 @@ private: * node, its state, and an index of one of its children, computes the state of that * child. It can modify the state. Children of a given node will have downfn() * called in order. - * - upfn is a callable (State&&, const Node&, Span) -> std::optional, - * which given a node, its state, and a Span of the results of its children, + * - upfn is a callable (State&&, const Node&, std::span) -> std::optional, + * which given a node, its state, and a span of the results of its children, * computes the result of the node. If std::nullopt is returned by upfn, * TreeEvalMaybe() immediately returns std::nullopt. * The return value of TreeEvalMaybe is the result of the root node. @@ -650,7 +650,7 @@ private: // Invoke upfn with the last node.subs.size() elements of results as input. assert(results.size() >= node.subs.size()); std::optional result{upfn(std::move(stack.back().state), node, - Span{results}.last(node.subs.size()))}; + std::span{results}.last(node.subs.size()))}; // If evaluation returns std::nullopt, abort immediately. if (!result) return {}; // Replace the last node.subs.size() elements of results with the new result. @@ -664,14 +664,14 @@ private: } /** Like TreeEvalMaybe, but without downfn or State type. - * upfn takes (const Node&, Span) and returns std::optional. */ + * upfn takes (const Node&, std::span) and returns std::optional. */ template std::optional TreeEvalMaybe(UpFn upfn) const { struct DummyState {}; return TreeEvalMaybe(DummyState{}, [](DummyState, const Node&, size_t) { return DummyState{}; }, - [&upfn](DummyState, const Node& node, Span subs) { + [&upfn](DummyState, const Node& node, std::span subs) { return upfn(node, subs); } ); @@ -685,7 +685,7 @@ private: // unconditionally dereference the result (it cannot be std::nullopt). return std::move(*TreeEvalMaybe(std::move(root_state), std::forward(downfn), - [&upfn](State&& state, const Node& node, Span subs) { + [&upfn](State&& state, const Node& node, std::span subs) { Result res{upfn(std::move(state), node, subs)}; return std::optional(std::move(res)); } @@ -693,14 +693,14 @@ private: } /** Like TreeEval, but without downfn or State type. - * upfn takes (const Node&, Span) and returns Result. */ + * upfn takes (const Node&, std::span) and returns Result. */ template Result TreeEval(UpFn upfn) const { struct DummyState {}; return std::move(*TreeEvalMaybe(DummyState{}, [](DummyState, const Node&, size_t) { return DummyState{}; }, - [&upfn](DummyState, const Node& node, Span subs) { + [&upfn](DummyState, const Node& node, std::span subs) { Result res{upfn(node, subs)}; return std::optional(std::move(res)); } @@ -764,7 +764,7 @@ public: // The upward function computes for a node, given its followed-by-OP_VERIFY status // and the CScripts of its child nodes, the CScript of the node. const bool is_tapscript{IsTapscript(m_script_ctx)}; - auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, Span subs) -> CScript { + auto upfn = [&ctx, is_tapscript](bool verify, const Node& node, std::span subs) -> CScript { switch (node.fragment) { case Fragment::PK_K: return BuildScript(ctx.ToPKBytes(node.keys[0])); case Fragment::PK_H: return BuildScript(OP_DUP, OP_HASH160, ctx.ToPKHBytes(node.keys[0]), OP_EQUALVERIFY); @@ -842,7 +842,7 @@ public: // The upward function computes for a node, given whether its parent is a wrapper, // and the string representations of its child nodes, the string representation of the node. const bool is_tapscript{IsTapscript(m_script_ctx)}; - auto upfn = [&ctx, is_tapscript](bool wrapped, const Node& node, Span subs) -> std::optional { + auto upfn = [&ctx, is_tapscript](bool wrapped, const Node& node, std::span subs) -> std::optional { std::string ret = wrapped ? ":" : ""; switch (node.fragment) { @@ -1189,7 +1189,7 @@ private: // Internal function which is invoked for every tree node, constructing satisfaction/dissatisfactions // given those of its subnodes. - auto helper = [&ctx](const Node& node, Span subres) -> InputResult { + auto helper = [&ctx](const Node& node, std::span subres) -> InputResult { switch (node.fragment) { case Fragment::PK_K: { std::vector sig; @@ -1385,7 +1385,7 @@ private: return {INVALID, INVALID}; }; - auto tester = [&helper](const Node& node, Span subres) -> InputResult { + auto tester = [&helper](const Node& node, std::span subres) -> InputResult { auto ret = helper(node, subres); // Do a consistency check between the satisfaction code and the type checker @@ -1453,7 +1453,7 @@ public: using keyset = std::set; using state = std::optional; - auto upfn = [&ctx](const Node& node, Span subs) -> state { + auto upfn = [&ctx](const Node& node, std::span subs) -> state { // If this node is already known to have duplicates, nothing left to do. if (node.has_duplicate_keys.has_value() && *node.has_duplicate_keys) return {}; @@ -1561,7 +1561,7 @@ public: //! Find an insane subnode which has no insane children. Nullptr if there is none. const Node* FindInsaneSub() const { - return TreeEval([](const Node& node, Span subs) -> const Node* { + return TreeEval([](const Node& node, std::span subs) -> const Node* { for (auto& sub: subs) if (sub) return sub; if (!node.IsSaneSubexpression()) return &node; return nullptr; @@ -1574,7 +1574,7 @@ public: bool IsSatisfiable(F fn) const { // TreeEval() doesn't support bool as NodeType, so use int instead. - return TreeEval([&fn](const Node& node, Span subs) -> bool { + return TreeEval([&fn](const Node& node, std::span subs) -> bool { switch (node.fragment) { case Fragment::JUST_0: return false; @@ -1744,11 +1744,11 @@ enum class ParseContext { CLOSE_BRACKET, }; -int FindNextChar(Span in, const char m); +int FindNextChar(std::span in, const char m); /** Parse a key string ending at the end of the fragment's text representation. */ template -std::optional> ParseKeyEnd(Span in, const Ctx& ctx) +std::optional> ParseKeyEnd(std::span in, const Ctx& ctx) { int key_size = FindNextChar(in, ')'); if (key_size < 1) return {}; @@ -1759,7 +1759,7 @@ std::optional> ParseKeyEnd(Span in, const Ctx& c /** Parse a hex string ending at the end of the fragment's text representation. */ template -std::optional, int>> ParseHexStrEnd(Span in, const size_t expected_size, +std::optional, int>> ParseHexStrEnd(std::span in, const size_t expected_size, const Ctx& ctx) { int hash_size = FindNextChar(in, ')'); @@ -1790,7 +1790,7 @@ void BuildBack(const MiniscriptContext script_ctx, Fragment nt, std::vector -inline NodeRef Parse(Span in, const Ctx& ctx) +inline NodeRef Parse(std::span in, const Ctx& ctx) { using namespace script; @@ -1814,7 +1814,7 @@ inline NodeRef Parse(Span in, const Ctx& ctx) to_parse.emplace_back(ParseContext::WRAPPED_EXPR, -1, -1); // Parses a multi() or multi_a() from its string representation. Returns false on parsing error. - const auto parse_multi_exp = [&](Span& in, const bool is_multi_a) -> bool { + const auto parse_multi_exp = [&](std::span& in, const bool is_multi_a) -> bool { const auto max_keys{is_multi_a ? MAX_PUBKEYS_PER_MULTI_A : MAX_PUBKEYS_PER_MULTISIG}; const auto required_ctx{is_multi_a ? MiniscriptContext::TAPSCRIPT : MiniscriptContext::P2WSH}; if (ctx.MsContext() != required_ctx) return false; diff --git a/src/script/parsing.cpp b/src/script/parsing.cpp index 3528ac9bfae..254ced6f0b0 100644 --- a/src/script/parsing.cpp +++ b/src/script/parsing.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -12,7 +12,7 @@ namespace script { -bool Const(const std::string& str, Span& sp) +bool Const(const std::string& str, std::span& sp) { if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) { sp = sp.subspan(str.size()); @@ -21,7 +21,7 @@ bool Const(const std::string& str, Span& sp) return false; } -bool Func(const std::string& str, Span& sp) +bool Func(const std::string& str, std::span& sp) { if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) { sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2); @@ -30,7 +30,7 @@ bool Func(const std::string& str, Span& sp) return false; } -Span Expr(Span& sp) +std::span Expr(std::span& sp) { int level = 0; auto it = sp.begin(); @@ -44,7 +44,7 @@ Span Expr(Span& sp) } ++it; } - Span ret = sp.first(it - sp.begin()); + std::span ret = sp.first(it - sp.begin()); sp = sp.subspan(it - sp.begin()); return ret; } diff --git a/src/script/parsing.h b/src/script/parsing.h index 850faea041a..8986aa57d07 100644 --- a/src/script/parsing.h +++ b/src/script/parsing.h @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -16,7 +16,7 @@ namespace script { * If sp's initial part matches str, sp is updated to skip that part, and true is returned. * Otherwise sp is unmodified and false is returned. */ -bool Const(const std::string& str, Span& sp); +bool Const(const std::string& str, std::span& sp); /** Parse a function call. * @@ -24,7 +24,7 @@ bool Const(const std::string& str, Span& sp); * section between the braces, and true is returned. Otherwise sp is unmodified and false * is returned. */ -bool Func(const std::string& str, Span& sp); +bool Func(const std::string& str, std::span& sp); /** Extract the expression that sp begins with. * @@ -33,7 +33,7 @@ bool Func(const std::string& str, Span& sp); * for "foo(bar(1),2),3" the initial part "foo(bar(1),2)" will be returned. sp will be * updated to skip the initial part that is returned. */ -Span Expr(Span& sp); +std::span Expr(std::span& sp); } // namespace script diff --git a/src/script/sigcache.cpp b/src/script/sigcache.cpp index 33531e6bf58..6b308258bcf 100644 --- a/src/script/sigcache.cpp +++ b/src/script/sigcache.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -42,7 +42,7 @@ void SignatureCache::ComputeEntryECDSA(uint256& entry, const uint256& hash, cons hasher.Write(hash.begin(), 32).Write(pubkey.data(), pubkey.size()).Write(vchSig.data(), vchSig.size()).Finalize(entry.begin()); } -void SignatureCache::ComputeEntrySchnorr(uint256& entry, const uint256& hash, Span sig, const XOnlyPubKey& pubkey) const +void SignatureCache::ComputeEntrySchnorr(uint256& entry, const uint256& hash, std::span sig, const XOnlyPubKey& pubkey) const { CSHA256 hasher = m_salted_hasher_schnorr; hasher.Write(hash.begin(), 32).Write(pubkey.data(), pubkey.size()).Write(sig.data(), sig.size()).Finalize(entry.begin()); @@ -73,7 +73,7 @@ bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector< return true; } -bool CachingTransactionSignatureChecker::VerifySchnorrSignature(Span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const +bool CachingTransactionSignatureChecker::VerifySchnorrSignature(std::span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const { uint256 entry; m_signature_cache.ComputeEntrySchnorr(entry, sighash, sig, pubkey); diff --git a/src/script/sigcache.h b/src/script/sigcache.h index 76802e6a7cc..ca4b44910e6 100644 --- a/src/script/sigcache.h +++ b/src/script/sigcache.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -53,7 +53,7 @@ public: void ComputeEntryECDSA(uint256& entry, const uint256 &hash, const std::vector& vchSig, const CPubKey& pubkey) const; - void ComputeEntrySchnorr(uint256& entry, const uint256 &hash, Span sig, const XOnlyPubKey& pubkey) const; + void ComputeEntrySchnorr(uint256& entry, const uint256 &hash, std::span sig, const XOnlyPubKey& pubkey) const; bool Get(const uint256& entry, const bool erase); @@ -70,7 +70,7 @@ public: CachingTransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, bool storeIn, SignatureCache& signature_cache, PrecomputedTransactionData& txdataIn) : TransactionSignatureChecker(txToIn, nInIn, amountIn, txdataIn, MissingDataBehavior::ASSERT_FAIL), store(storeIn), m_signature_cache(signature_cache) {} bool VerifyECDSASignature(const std::vector& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const override; - bool VerifySchnorrSignature(Span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override; + bool VerifySchnorrSignature(std::span sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override; }; #endif // BITCOIN_SCRIPT_SIGCACHE_H diff --git a/src/script/sign.cpp b/src/script/sign.cpp index 42db2513597..33cbc38be41 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -317,7 +317,7 @@ struct TapSatisfier: Satisfier { } }; -static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatureCreator& creator, SignatureData& sigdata, int leaf_version, Span script_bytes, std::vector& result) +static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatureCreator& creator, SignatureData& sigdata, int leaf_version, std::span script_bytes, std::vector& result) { // Only BIP342 tapscript signing is supported for now. if (leaf_version != TAPROOT_LEAF_TAPSCRIPT) return false; @@ -701,7 +701,7 @@ class DummySignatureChecker final : public BaseSignatureChecker public: DummySignatureChecker() = default; bool CheckECDSASignature(const std::vector& sig, const std::vector& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override { return sig.size() != 0; } - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const override { return sig.size() != 0; } + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const override { return sig.size() != 0; } bool CheckLockTime(const CScriptNum& nLockTime) const override { return true; } bool CheckSequence(const CScriptNum& nSequence) const override { return true; } }; diff --git a/src/script/signingprovider.cpp b/src/script/signingprovider.cpp index 597b1a1544b..07a1956eabc 100644 --- a/src/script/signingprovider.cpp +++ b/src/script/signingprovider.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -368,7 +368,7 @@ void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth) return branch.size() == 0 || (branch.size() == 1 && branch[0]); } -TaprootBuilder& TaprootBuilder::Add(int depth, Span script, int leaf_version, bool track) +TaprootBuilder& TaprootBuilder::Add(int depth, std::span script, int leaf_version, bool track) { assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0); if (!IsValid()) return *this; diff --git a/src/script/signingprovider.h b/src/script/signingprovider.h index 0fe34ecbcc5..f4c823be393 100644 --- a/src/script/signingprovider.h +++ b/src/script/signingprovider.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -115,7 +115,7 @@ public: /** Add a new script at a certain depth in the tree. Add() operations must be called * in depth-first traversal order of binary tree. If track is true, it will be included in * the GetSpendData() output. */ - TaprootBuilder& Add(int depth, Span script, int leaf_version, bool track = true); + TaprootBuilder& Add(int depth, std::span script, int leaf_version, bool track = true); /** Like Add(), but for a Merkle node with a given hash to the tree. */ TaprootBuilder& AddOmitted(int depth, const uint256& hash); /** Finalize the construction. Can only be called when IsComplete() is true. diff --git a/src/script/solver.cpp b/src/script/solver.cpp index bd3c5cdf724..783baf07089 100644 --- a/src/script/solver.cpp +++ b/src/script/solver.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -104,9 +104,9 @@ static bool MatchMultisig(const CScript& script, int& required_sigs, std::vector return (it + 1 == script.end()); } -std::optional>>> MatchMultiA(const CScript& script) +std::optional>>> MatchMultiA(const CScript& script) { - std::vector> keyspans; + std::vector> keyspans; // Redundant, but very fast and selective test. if (script.size() == 0 || script[0] != 32 || script.back() != OP_NUMEQUAL) return {}; diff --git a/src/script/solver.h b/src/script/solver.h index 5b945477c90..d2b7fb88814 100644 --- a/src/script/solver.h +++ b/src/script/solver.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -59,7 +59,7 @@ CScript GetScriptForRawPubKey(const CPubKey& pubkey); /** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise. * The keyspans refer to bytes in the passed script. */ -std::optional>>> MatchMultiA(const CScript& script LIFETIMEBOUND); +std::optional>>> MatchMultiA(const CScript& script LIFETIMEBOUND); /** Generate a multisig script. */ CScript GetScriptForMultisig(int nRequired, const std::vector& keys); diff --git a/src/serialize.h b/src/serialize.h index a1166b81a89..98851056bdb 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -53,67 +53,67 @@ constexpr deserialize_type deserialize {}; */ template inline void ser_writedata8(Stream &s, uint8_t obj) { - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline void ser_writedata16(Stream &s, uint16_t obj) { obj = htole16_internal(obj); - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline void ser_writedata16be(Stream &s, uint16_t obj) { obj = htobe16_internal(obj); - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline void ser_writedata32(Stream &s, uint32_t obj) { obj = htole32_internal(obj); - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline void ser_writedata32be(Stream &s, uint32_t obj) { obj = htobe32_internal(obj); - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline void ser_writedata64(Stream &s, uint64_t obj) { obj = htole64_internal(obj); - s.write(AsBytes(Span{&obj, 1})); + s.write(std::as_bytes(std::span{&obj, 1})); } template inline uint8_t ser_readdata8(Stream &s) { uint8_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return obj; } template inline uint16_t ser_readdata16(Stream &s) { uint16_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return le16toh_internal(obj); } template inline uint16_t ser_readdata16be(Stream &s) { uint16_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return be16toh_internal(obj); } template inline uint32_t ser_readdata32(Stream &s) { uint32_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return le32toh_internal(obj); } template inline uint32_t ser_readdata32be(Stream &s) { uint32_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return be32toh_internal(obj); } template inline uint64_t ser_readdata64(Stream &s) { uint64_t obj; - s.read(AsWritableBytes(Span{&obj, 1})); + s.read(std::as_writable_bytes(std::span{&obj, 1})); return le64toh_internal(obj); } @@ -242,7 +242,7 @@ const Out& AsBase(const In& x) FORMATTER_METHODS(cls, obj) // Templates for serializing to anything that looks like a stream, -// i.e. anything that supports .read(Span) and .write(Span) +// i.e. anything that supports .read(std::span) and .write(std::span) // // clang-format off @@ -265,7 +265,7 @@ template inline void Serialize(Stream& s, uint64_t a) { ser_wri template void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); } template void Serialize(Stream& s, const std::array& a) { s.write(MakeByteSpan(a)); } template void Serialize(Stream& s, std::span span) { s.write(std::as_bytes(span)); } -template void Serialize(Stream& s, Span span) { s.write(AsBytes(span)); } +template void Serialize(Stream& s, std::span span) { s.write(std::as_bytes(span)); } template void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t template void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; } @@ -280,7 +280,7 @@ template inline void Unserialize(Stream& s, uint64_t& a) { a = template void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); } template void Unserialize(Stream& s, std::array& a) { s.read(MakeWritableByteSpan(a)); } template void Unserialize(Stream& s, std::span span) { s.read(std::as_writable_bytes(span)); } -template void Unserialize(Stream& s, Span span) { s.read(AsWritableBytes(span)); } +template void Unserialize(Stream& s, std::span span) { s.read(std::as_writable_bytes(span)); } template inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); } template inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; } @@ -536,10 +536,10 @@ struct CustomUintFormatter if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range"); if (BigEndian) { uint64_t raw = htobe64_internal(v); - s.write(AsBytes(Span{&raw, 1}).last(Bytes)); + s.write(std::as_bytes(std::span{&raw, 1}).last(Bytes)); } else { uint64_t raw = htole64_internal(v); - s.write(AsBytes(Span{&raw, 1}).first(Bytes)); + s.write(std::as_bytes(std::span{&raw, 1}).first(Bytes)); } } @@ -549,10 +549,10 @@ struct CustomUintFormatter static_assert(std::numeric_limits::max() >= MAX && std::numeric_limits::min() <= 0, "Assigned type too small"); uint64_t raw = 0; if (BigEndian) { - s.read(AsWritableBytes(Span{&raw, 1}).last(Bytes)); + s.read(std::as_writable_bytes(std::span{&raw, 1}).last(Bytes)); v = static_cast(be64toh_internal(raw)); } else { - s.read(AsWritableBytes(Span{&raw, 1}).first(Bytes)); + s.read(std::as_writable_bytes(std::span{&raw, 1}).first(Bytes)); v = static_cast(le64toh_internal(raw)); } } @@ -829,7 +829,7 @@ void Unserialize(Stream& is, prevector& v) while (i < nSize) { unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); v.resize_uninitialized(i + blk); - is.read(AsWritableBytes(Span{&v[i], blk})); + is.read(std::as_writable_bytes(std::span{&v[i], blk})); i += blk; } } else { @@ -872,7 +872,7 @@ void Unserialize(Stream& is, std::vector& v) while (i < nSize) { unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); v.resize(i + blk); - is.read(AsWritableBytes(Span{&v[i], blk})); + is.read(std::as_writable_bytes(std::span{&v[i], blk})); i += blk; } } else { @@ -1065,7 +1065,7 @@ protected: public: SizeComputer() = default; - void write(Span src) + void write(std::span src) { this->nSize += src.size(); } @@ -1132,8 +1132,8 @@ public: template ParamsStream& operator<<(const U& obj) { ::Serialize(*this, obj); return *this; } template ParamsStream& operator>>(U&& obj) { ::Unserialize(*this, obj); return *this; } - void write(Span src) { GetStream().write(src); } - void read(Span dst) { GetStream().read(dst); } + void write(std::span src) { GetStream().write(src); } + void read(std::span dst) { GetStream().read(dst); } void ignore(size_t num) { GetStream().ignore(num); } bool eof() const { return GetStream().eof(); } size_t size() const { return GetStream().size(); } diff --git a/src/signet.cpp b/src/signet.cpp index 9b7ffd07cdf..45d279d2bf0 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2021 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -28,7 +28,7 @@ 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; -static bool FetchAndClearCommitmentSection(const Span header, CScript& witness_commitment, std::vector& result) +static bool FetchAndClearCommitmentSection(const std::span header, CScript& witness_commitment, std::vector& result) { CScript replacement; bool found_header = false; @@ -39,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() > header.size() && std::ranges::equal(Span{pushdata}.first(header.size()), header)) { + if (!found_header && pushdata.size() > header.size() && std::ranges::equal(std::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 93718f61895..5e34cd91909 100644 --- a/src/span.h +++ b/src/span.h @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -11,62 +11,38 @@ #include #include -#ifdef DEBUG -#define CONSTEXPR_IF_NOT_DEBUG -#define ASSERT_IF_DEBUG(x) assert((x)) -#else -#define CONSTEXPR_IF_NOT_DEBUG constexpr -#define ASSERT_IF_DEBUG(x) -#endif - -#if defined(__clang__) -#if __has_attribute(lifetimebound) -#define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]] -#else -#define SPAN_ATTR_LIFETIMEBOUND -#endif -#else -#define SPAN_ATTR_LIFETIMEBOUND -#endif - -/** A Span is an object that can refer to a contiguous sequence of objects. +/** A span is an object that can refer to a contiguous sequence of objects. * - * This file implements a subset of C++20's std::span. It can be considered - * temporary compatibility code until C++20 and is designed to be a - * self-contained abstraction without depending on other project files. For this - * reason, Clang lifetimebound is defined here instead of including - * , which also defines it. + * Things to be aware of when writing code that deals with spans: * - * Things to be aware of when writing code that deals with Spans: - * - * - Similar to references themselves, Spans are subject to reference lifetime + * - Similar to references themselves, spans are subject to reference lifetime * issues. The user is responsible for making sure the objects pointed to by - * a Span live as long as the Span is used. For example: + * a span live as long as the span is used. For example: * * std::vector vec{1,2,3,4}; - * Span sp(vec); + * std::span sp(vec); * vec.push_back(5); * printf("%i\n", sp.front()); // UB! * * may exhibit undefined behavior, as increasing the size of a vector may * invalidate references. * - * - One particular pitfall is that Spans can be constructed from temporaries, - * but this is unsafe when the Span is stored in a variable, outliving the + * - One particular pitfall is that spans can be constructed from temporaries, + * but this is unsafe when the span is stored in a variable, outliving the * temporary. For example, this will compile, but exhibits undefined behavior: * - * Span sp(std::vector{1, 2, 3}); + * std::span sp(std::vector{1, 2, 3}); * printf("%i\n", sp.front()); // UB! * * The lifetime of the vector ends when the statement it is created in ends. - * Thus the Span is left with a dangling reference, and using it is undefined. + * Thus the span is left with a dangling reference, and using it is undefined. * - * - Due to Span's automatic creation from range-like objects (arrays, and data + * - Due to spans automatic creation from range-like objects (arrays, and data * types that expose a data() and size() member function), functions that - * accept a Span as input parameter can be called with any compatible + * accept a span as input parameter can be called with any compatible * range-like object. For example, this works: * - * void Foo(Span arg); + * void Foo(std::span arg); * * Foo(std::vector{1, 2, 3}); // Works * @@ -74,10 +50,10 @@ * container, and only about having exactly a range of elements. However it * may also be surprising to see automatic conversions in this case. * - * When a function accepts a Span with a mutable element type, it will not + * When a function accepts a span with a mutable element type, it will not * accept temporaries; only variables or other references. For example: * - * void FooMut(Span arg); + * void FooMut(std::span arg); * * FooMut(std::vector{1, 2, 3}); // Does not compile * std::vector baz{1, 2, 3}; @@ -93,159 +69,10 @@ * result will be present in that variable after the call. Passing a temporary * is useless in that context. */ -template -class Span -{ - C* m_data; - std::size_t m_size{0}; - - template - struct is_Span_int : public std::false_type {}; - template - struct is_Span_int> : public std::true_type {}; - template - struct is_Span : public is_Span_int::type>{}; - - -public: - constexpr Span() noexcept : m_data(nullptr) {} - - /** Construct a span from a begin pointer and a size. - * - * This implements a subset of the iterator-based std::span constructor in C++20, - * which is hard to implement without std::address_of. - */ - template ::value, int>::type = 0> - constexpr Span(T* begin, std::size_t size) noexcept : m_data(begin), m_size(size) {} - - /** Construct a span from a begin and end pointer. - * - * This implements a subset of the iterator-based std::span constructor in C++20, - * which is hard to implement without std::address_of. - */ - template ::value, int>::type = 0> - CONSTEXPR_IF_NOT_DEBUG Span(T* begin, T* end) noexcept : m_data(begin), m_size(end - begin) - { - ASSERT_IF_DEBUG(end >= begin); - } - - /** Implicit conversion of spans between compatible types. - * - * Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type - * C, then permit implicit conversion of Span to Span. This matches the behavior of the corresponding - * C++20 std::span constructor. - * - * For example this means that a Span can be converted into a Span. - */ - template ::value, int>::type = 0> - constexpr Span(const Span& other) noexcept : m_data(other.m_data), m_size(other.m_size) {} - - /** Default copy constructor. */ - constexpr Span(const Span&) noexcept = default; - - /** Default assignment operator. */ - Span& operator=(const Span& other) noexcept = default; - - /** Construct a Span from an array. This matches the corresponding C++20 std::span constructor. */ - template - constexpr Span(C (&a)[N]) noexcept : m_data(a), m_size(N) {} - - /** Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector, ...). - * - * This implements a subset of the functionality provided by the C++20 std::span range-based constructor. - * - * To prevent surprises, only Spans for constant value types are supported when passing in temporaries. - * Note that this restriction does not exist when converting arrays or other Spans (see above). - */ - template - constexpr Span(V& other SPAN_ATTR_LIFETIMEBOUND, - typename std::enable_if::value && - std::is_convertible().data())>::type (*)[], C (*)[]>::value && - std::is_convertible().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) - : m_data(other.data()), m_size(other.size()){} - - template - constexpr Span(const V& other SPAN_ATTR_LIFETIMEBOUND, - typename std::enable_if::value && - std::is_convertible().data())>::type (*)[], C (*)[]>::value && - std::is_convertible().size()), std::size_t>::value, std::nullptr_t>::type = nullptr) - : m_data(other.data()), m_size(other.size()){} - - constexpr C* data() const noexcept { return m_data; } - constexpr C* begin() const noexcept { return m_data; } - constexpr C* end() const noexcept { return m_data + m_size; } - CONSTEXPR_IF_NOT_DEBUG C& front() const noexcept - { - ASSERT_IF_DEBUG(size() > 0); - return m_data[0]; - } - CONSTEXPR_IF_NOT_DEBUG C& back() const noexcept - { - ASSERT_IF_DEBUG(size() > 0); - return m_data[m_size - 1]; - } - constexpr std::size_t size() const noexcept { return m_size; } - constexpr std::size_t size_bytes() const noexcept { return sizeof(C) * m_size; } - constexpr bool empty() const noexcept { return size() == 0; } - CONSTEXPR_IF_NOT_DEBUG C& operator[](std::size_t pos) const noexcept - { - ASSERT_IF_DEBUG(size() > pos); - return m_data[pos]; - } - CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset) const noexcept - { - ASSERT_IF_DEBUG(size() >= offset); - return Span(m_data + offset, m_size - offset); - } - CONSTEXPR_IF_NOT_DEBUG Span subspan(std::size_t offset, std::size_t count) const noexcept - { - ASSERT_IF_DEBUG(size() >= offset + count); - return Span(m_data + offset, count); - } - CONSTEXPR_IF_NOT_DEBUG Span first(std::size_t count) const noexcept - { - ASSERT_IF_DEBUG(size() >= count); - return Span(m_data, count); - } - CONSTEXPR_IF_NOT_DEBUG Span last(std::size_t count) const noexcept - { - ASSERT_IF_DEBUG(size() >= count); - return Span(m_data + m_size - count, count); - } - - template friend class Span; -}; - -// Return result of calling .data() method on type T. This is used to be able to -// write template deduction guides for the single-parameter Span constructor -// below that will work if the value that is passed has a .data() method, and if -// the data method does not return a void pointer. -// -// It is important to check for the void type specifically below, so the -// deduction guides can be used in SFINAE contexts to check whether objects can -// be converted to spans. If the deduction guides did not explicitly check for -// void, and an object was passed that returned void* from data (like -// std::vector), the template deduction would succeed, but the Span -// object instantiation would fail, resulting in a hard error, rather than a -// SFINAE error. -// https://stackoverflow.com/questions/68759148/sfinae-to-detect-the-explicitness-of-a-ctad-deduction-guide -// https://stackoverflow.com/questions/16568986/what-happens-when-you-call-data-on-a-stdvectorbool -template -using DataResult = std::remove_pointer_t().data())>; - -// Deduction guides for Span -// For the pointer/size based and iterator based constructor: -template Span(T*, EndOrSize) -> Span; -// For the array constructor: -template Span(T (&)[N]) -> Span; -// For the temporaries/rvalue references constructor, only supporting const output. -template Span(T&&) -> Span && !std::is_void_v>, const DataResult>>; -// For (lvalue) references, supporting mutable output. -template Span(T&) -> Span>, DataResult>>; /** Pop the last element off a span, and return a reference to that element. */ template -T& SpanPopBack(Span& span) +T& SpanPopBack(std::span& span) { size_t size = span.size(); T& back = span.back(); @@ -253,27 +80,15 @@ T& SpanPopBack(Span& span) return back; } -// From C++20 as_bytes and as_writeable_bytes -template -Span AsBytes(Span s) noexcept -{ - return {reinterpret_cast(s.data()), s.size_bytes()}; -} -template -Span AsWritableBytes(Span s) noexcept -{ - return {reinterpret_cast(s.data()), s.size_bytes()}; -} - template -Span MakeByteSpan(V&& v) noexcept +auto MakeByteSpan(const V& v) noexcept { - return AsBytes(Span{std::forward(v)}); + return std::as_bytes(std::span{v}); } template -Span MakeWritableByteSpan(V&& v) noexcept +auto MakeWritableByteSpan(V&& v) noexcept { - return AsWritableBytes(Span{std::forward(v)}); + return std::as_writable_bytes(std::span{std::forward(v)}); } // Helper functions to safely cast basic byte pointers to unsigned char pointers. @@ -289,10 +104,11 @@ inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_c template concept BasicByte = requires { UCharCast(std::span{}.data()); }; -// Helper function to safely convert a Span to a Span<[const] unsigned char>. -template constexpr auto UCharSpanCast(Span s) -> Span::type> { return {UCharCast(s.data()), s.size()}; } +// Helper function to safely convert a span to a span<[const] unsigned char>. +template constexpr auto UCharSpanCast(std::span s) { return std::span, N>{UCharCast(s.data()), s.size()}; } -/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ -template constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward(v)})) { return UCharSpanCast(Span{std::forward(v)}); } +/** Like the std::span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ +template constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); } +template constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward(v)})) { return UCharSpanCast(std::span{std::forward(v)}); } #endif // BITCOIN_SPAN_H diff --git a/src/streams.cpp b/src/streams.cpp index cd496ee2be4..d82824ee583 100644 --- a/src/streams.cpp +++ b/src/streams.cpp @@ -18,7 +18,7 @@ AutoFile::AutoFile(std::FILE* file, std::vector data_xor) } } -std::size_t AutoFile::detail_fread(Span dst) +std::size_t AutoFile::detail_fread(std::span dst) { if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr"); size_t ret = std::fread(dst.data(), 1, dst.size(), m_file); @@ -57,7 +57,7 @@ int64_t AutoFile::tell() return *m_position; } -void AutoFile::read(Span dst) +void AutoFile::read(std::span dst) { if (detail_fread(dst) != dst.size()) { throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed"); @@ -78,7 +78,7 @@ void AutoFile::ignore(size_t nSize) } } -void AutoFile::write(Span src) +void AutoFile::write(std::span src) { if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr"); if (m_xor.empty()) { @@ -90,7 +90,7 @@ void AutoFile::write(Span src) if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::write: position unknown"); std::array buf; while (src.size() > 0) { - auto buf_now{Span{buf}.first(std::min(src.size(), buf.size()))}; + auto buf_now{std::span{buf}.first(std::min(src.size(), buf.size()))}; std::copy(src.begin(), src.begin() + buf_now.size(), buf_now.begin()); util::Xor(buf_now, m_xor, *m_position); if (std::fwrite(buf_now.data(), 1, buf_now.size(), m_file) != buf_now.size()) { diff --git a/src/streams.h b/src/streams.h index e5316da7e70..20bdaf2c060 100644 --- a/src/streams.h +++ b/src/streams.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -25,7 +25,7 @@ #include namespace util { -inline void Xor(Span write, Span key, size_t key_offset = 0) +inline void Xor(std::span write, std::span key, size_t key_offset = 0) { if (key.size() == 0) { return; @@ -71,7 +71,7 @@ public: { ::SerializeMany(*this, std::forward(args)...); } - void write(Span src) + void write(std::span src) { assert(nPos <= vchData.size()); size_t nOverwrite = std::min(src.size(), vchData.size() - nPos); @@ -95,18 +95,18 @@ private: size_t nPos; }; -/** Minimal stream for reading from an existing byte array by Span. +/** Minimal stream for reading from an existing byte array by std::span. */ class SpanReader { private: - Span m_data; + std::span m_data; public: /** * @param[in] data Referenced byte vector to overwrite/append */ - explicit SpanReader(Span data) : m_data{data} {} + explicit SpanReader(std::span data) : m_data{data} {} template SpanReader& operator>>(T&& obj) @@ -118,7 +118,7 @@ public: size_t size() const { return m_data.size(); } bool empty() const { return m_data.empty(); } - void read(Span dst) + void read(std::span dst) { if (dst.size() == 0) { return; @@ -162,8 +162,8 @@ public: typedef vector_type::reverse_iterator reverse_iterator; explicit DataStream() = default; - explicit DataStream(Span sp) : DataStream{AsBytes(sp)} {} - explicit DataStream(Span sp) : vch(sp.data(), sp.data() + sp.size()) {} + explicit DataStream(std::span sp) : DataStream{std::as_bytes(sp)} {} + explicit DataStream(std::span sp) : vch(sp.data(), sp.data() + sp.size()) {} std::string str() const { @@ -215,7 +215,7 @@ public: bool eof() const { return size() == 0; } int in_avail() const { return size(); } - void read(Span dst) + void read(std::span dst) { if (dst.size() == 0) return; @@ -248,7 +248,7 @@ public: m_read_pos = next_read_pos.value(); } - void write(Span src) + void write(std::span src) { // Write to the end of the buffer vch.insert(vch.end(), src.begin(), src.end()); @@ -431,7 +431,7 @@ public: void SetXor(std::vector data_xor) { m_xor = data_xor; } /** Implementation detail, only used internally. */ - std::size_t detail_fread(Span dst); + std::size_t detail_fread(std::span dst); /** Wrapper around fseek(). Will throw if seeking is not possible. */ void seek(int64_t offset, int origin); @@ -448,9 +448,9 @@ public: // // Stream subset // - void read(Span dst); + void read(std::span dst); void ignore(size_t nSize); - void write(Span src); + void write(std::span src); template AutoFile& operator<<(const T& obj) @@ -492,7 +492,7 @@ private: readNow = nAvail; if (readNow == 0) return false; - size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))}; + size_t nBytes{m_src.detail_fread(std::span{vchBuf}.subspan(pos, readNow))}; if (nBytes == 0) { throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"}; } @@ -536,7 +536,7 @@ public: } //! read a number of bytes - void read(Span dst) + void read(std::span dst) { while (dst.size() > 0) { auto [buffer_pointer, length]{AdvanceStream(dst.size())}; diff --git a/src/test/bip324_tests.cpp b/src/test/bip324_tests.cpp index adabb7ef57e..96392973b44 100644 --- a/src/test/bip324_tests.cpp +++ b/src/test/bip324_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -91,7 +91,7 @@ void TestBIP324PacketVector( BOOST_CHECK(out_ciphertext == ciphertext); } else { BOOST_CHECK(ciphertext.size() >= out_ciphertext_endswith.size()); - BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, Span{ciphertext}.last(out_ciphertext_endswith.size()))); + BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, std::span{ciphertext}.last(out_ciphertext_endswith.size()))); } for (unsigned error = 0; error <= 12; ++error) { @@ -121,8 +121,8 @@ void TestBIP324PacketVector( for (uint32_t i = 0; i < dec_idx; ++i) { unsigned use_idx = i < in_idx ? i : 0; bool dec_ignore{false}; - dec_cipher.DecryptLength(Span{dummies[use_idx]}.first(cipher.LENGTH_LEN)); - dec_cipher.Decrypt(Span{dummies[use_idx]}.subspan(cipher.LENGTH_LEN), {}, dec_ignore, {}); + dec_cipher.DecryptLength(std::span{dummies[use_idx]}.first(cipher.LENGTH_LEN)); + dec_cipher.Decrypt(std::span{dummies[use_idx]}.subspan(cipher.LENGTH_LEN), {}, dec_ignore, {}); } // Construct copied (and possibly damaged) copy of ciphertext. @@ -147,7 +147,7 @@ void TestBIP324PacketVector( // Decrypt contents. std::vector decrypted(dec_len); bool dec_ignore{false}; - bool dec_ok = dec_cipher.Decrypt(Span{to_decrypt}.subspan(cipher.LENGTH_LEN), dec_aad, dec_ignore, decrypted); + bool dec_ok = dec_cipher.Decrypt(std::span{to_decrypt}.subspan(cipher.LENGTH_LEN), dec_aad, dec_ignore, decrypted); // Verify result. BOOST_CHECK(dec_ok == !error); diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index e2ea7ed5fd2..5588d4cdbc6 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2021 The Bitcoin Core developers +// Copyright (c) 2014-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -176,9 +176,9 @@ void TestChaCha20(const std::string &hex_message, const std::string &hexkey, Cha size_t pos = 0; for (int j = 0; j < 3; ++j) { if (!hex_message.empty()) { - rng.Crypt(Span{m}.subspan(pos, lens[j]), Span{outres}.subspan(pos, lens[j])); + rng.Crypt(std::span{m}.subspan(pos, lens[j]), std::span{outres}.subspan(pos, lens[j])); } else { - rng.Keystream(Span{outres}.subspan(pos, lens[j])); + rng.Keystream(std::span{outres}.subspan(pos, lens[j])); } pos += lens[j]; } @@ -237,7 +237,7 @@ void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, cons // Test incremental interface for (int splits = 0; splits < 10; ++splits) { for (int iter = 0; iter < 10; ++iter) { - auto data = Span{m}; + auto data = std::span{m}; Poly1305 poly1305{key}; for (int chunk = 0; chunk < splits; ++chunk) { size_t now = m_rng.randrange(data.size() + 1); @@ -267,7 +267,7 @@ void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_h if (i == 0) { aead.Encrypt(plain, aad, nonce, cipher); } else { - aead.Encrypt(Span{plain}.first(prefix), Span{plain}.subspan(prefix), aad, nonce, cipher); + aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, nonce, cipher); } BOOST_CHECK(cipher == expected_cipher); @@ -277,7 +277,7 @@ void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_h if (i == 0) { ret = aead.Decrypt(cipher, aad, nonce, decipher); } else { - ret = aead.Decrypt(cipher, aad, nonce, Span{decipher}.first(prefix), Span{decipher}.subspan(prefix)); + ret = aead.Decrypt(cipher, aad, nonce, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix)); } BOOST_CHECK(ret); BOOST_CHECK(decipher == plain); @@ -308,21 +308,21 @@ void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad // Do msg_idx dummy encryptions to seek to the correct packet. FSChaCha20Poly1305 enc_aead{key, 224}; for (uint64_t i = 0; i < msg_idx; ++i) { - enc_aead.Encrypt(Span{dummy_tag}.first(0), Span{dummy_tag}.first(0), dummy_tag); + enc_aead.Encrypt(std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0), dummy_tag); } // Invoke single-plain or plain1/plain2 Encrypt. if (it == 0) { enc_aead.Encrypt(plain, aad, cipher); } else { - enc_aead.Encrypt(Span{plain}.first(prefix), Span{plain}.subspan(prefix), aad, cipher); + enc_aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, cipher); } BOOST_CHECK(cipher == expected_cipher); // Do msg_idx dummy decryptions to seek to the correct packet. FSChaCha20Poly1305 dec_aead{key, 224}; for (uint64_t i = 0; i < msg_idx; ++i) { - dec_aead.Decrypt(dummy_tag, Span{dummy_tag}.first(0), Span{dummy_tag}.first(0)); + dec_aead.Decrypt(dummy_tag, std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0)); } // Invoke single-plain or plain1/plain2 Decrypt. @@ -331,7 +331,7 @@ void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad if (it == 0) { ret = dec_aead.Decrypt(cipher, aad, decipher); } else { - ret = dec_aead.Decrypt(cipher, aad, Span{decipher}.first(prefix), Span{decipher}.subspan(prefix)); + ret = dec_aead.Decrypt(cipher, aad, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix)); } BOOST_CHECK(ret); BOOST_CHECK(decipher == plain); @@ -843,9 +843,9 @@ BOOST_AUTO_TEST_CASE(chacha20_midblock) c20.Keystream(b2); c20.Keystream(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_CHECK(std::ranges::equal(std::span{block}.first(5), b1)); + BOOST_CHECK(std::ranges::equal(std::span{block}.subspan(5, 7), b2)); + BOOST_CHECK(std::ranges::equal(std::span{block}.last(52), b3)); } BOOST_AUTO_TEST_CASE(poly1305_testvector) @@ -1103,8 +1103,8 @@ void CryptoTest::TestSHA3_256(const std::string& input, const std::string& outpu int s1 = m_rng.randrange(in_bytes.size() + 1); int s2 = m_rng.randrange(in_bytes.size() + 1 - s1); int s3 = in_bytes.size() - s1 - s2; - sha.Write(Span{in_bytes}.first(s1)).Write(Span{in_bytes}.subspan(s1, s2)); - sha.Write(Span{in_bytes}.last(s3)).Finalize(out); + sha.Write(std::span{in_bytes}.first(s1)).Write(std::span{in_bytes}.subspan(s1, s2)); + sha.Write(std::span{in_bytes}.last(s3)).Finalize(out); BOOST_CHECK(std::equal(std::begin(out_bytes), std::end(out_bytes), out)); } diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index 63c53a842c1..f7a4a5b8edb 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -481,13 +481,13 @@ void CheckInferDescriptor(const std::string& script_hex, const std::string& expe if (!origin_str.empty()) { KeyOriginInfo info; - Span origin_sp{origin_str}; - std::vector> origin_split = Split(origin_sp, "/"); + std::span origin_sp{origin_str}; + std::vector> origin_split = Split(origin_sp, "/"); std::string fpr_str(origin_split[0].begin(), origin_split[0].end()); auto fpr_bytes = ParseHex(fpr_str); std::copy(fpr_bytes.begin(), fpr_bytes.end(), info.fingerprint); for (size_t i = 1; i < origin_split.size(); ++i) { - Span elem = origin_split[i]; + std::span elem = origin_split[i]; bool hardened = false; if (elem.size() > 0) { const char last = elem[elem.size() - 1]; diff --git a/src/test/fuzz/bip324.cpp b/src/test/fuzz/bip324.cpp index f1fa15d8a3e..b787bd16687 100644 --- a/src/test/fuzz/bip324.cpp +++ b/src/test/fuzz/bip324.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -106,7 +106,7 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize) } // Decrypt length - uint32_t dec_length = receiver.DecryptLength(Span{ciphertext}.first(initiator.LENGTH_LEN)); + uint32_t dec_length = receiver.DecryptLength(std::span{ciphertext}.first(initiator.LENGTH_LEN)); if (!damage) { assert(dec_length == length); } else { @@ -119,7 +119,7 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize) // Decrypt std::vector decrypt(dec_length); bool dec_ignore{false}; - bool ok = receiver.Decrypt(Span{ciphertext}.subspan(initiator.LENGTH_LEN), aad, dec_ignore, decrypt); + bool ok = receiver.Decrypt(std::span{ciphertext}.subspan(initiator.LENGTH_LEN), aad, dec_ignore, decrypt); // Decryption *must* fail if the packet was damaged, and succeed if it wasn't. assert(!ok == damage); if (!ok) break; diff --git a/src/test/fuzz/crypto_chacha20.cpp b/src/test/fuzz/crypto_chacha20.cpp index fe47f18923c..15e45419933 100644 --- a/src/test/fuzz/crypto_chacha20.cpp +++ b/src/test/fuzz/crypto_chacha20.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -108,9 +108,9 @@ void ChaCha20SplitFuzz(FuzzedDataProvider& provider) // This tests that Keystream() has the same behavior as Crypt() applied // to 0x00 input bytes. if (UseCrypt || provider.ConsumeBool()) { - crypt2.Crypt(Span{data2}.subspan(bytes2, now), Span{data2}.subspan(bytes2, now)); + crypt2.Crypt(std::span{data2}.subspan(bytes2, now), std::span{data2}.subspan(bytes2, now)); } else { - crypt2.Keystream(Span{data2}.subspan(bytes2, now)); + crypt2.Keystream(std::span{data2}.subspan(bytes2, now)); } bytes2 += now; if (is_last) break; diff --git a/src/test/fuzz/crypto_chacha20poly1305.cpp b/src/test/fuzz/crypto_chacha20poly1305.cpp index 0700ba7fb6b..0f7228f5b4d 100644 --- a/src/test/fuzz/crypto_chacha20poly1305.cpp +++ b/src/test/fuzz/crypto_chacha20poly1305.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -18,9 +18,9 @@ constexpr static inline void crypt_till_rekey(FSChaCha20Poly1305& aead, int reke for (int i = 0; i < rekey_interval; ++i) { std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}}; if (encrypt) { - aead.Encrypt(Span{dummy_tag}.first(0), Span{dummy_tag}.first(0), dummy_tag); + aead.Encrypt(std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0), dummy_tag); } else { - aead.Decrypt(dummy_tag, Span{dummy_tag}.first(0), Span{dummy_tag}.first(0)); + aead.Decrypt(dummy_tag, std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0)); } } } @@ -62,7 +62,7 @@ FUZZ_TARGET(crypto_aeadchacha20poly1305) if (use_splits && length > 0) { size_t split_index = provider.ConsumeIntegralInRange(1, length); - aead.Encrypt(Span{plain}.first(split_index), Span{plain}.subspan(split_index), aad, nonce, cipher); + aead.Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad, nonce, cipher); } else { aead.Encrypt(plain, aad, nonce, cipher); } @@ -102,7 +102,7 @@ FUZZ_TARGET(crypto_aeadchacha20poly1305) if (use_splits && length > 0) { size_t split_index = provider.ConsumeIntegralInRange(1, length); - ok = aead.Decrypt(cipher, aad, nonce, Span{decrypted_contents}.first(split_index), Span{decrypted_contents}.subspan(split_index)); + ok = aead.Decrypt(cipher, aad, nonce, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index)); } else { ok = aead.Decrypt(cipher, aad, nonce, decrypted_contents); } @@ -152,7 +152,7 @@ FUZZ_TARGET(crypto_fschacha20poly1305) crypt_till_rekey(enc_aead, rekey_interval, true); if (use_splits && length > 0) { size_t split_index = provider.ConsumeIntegralInRange(1, length); - enc_aead.Encrypt(Span{plain}.first(split_index), Span{plain}.subspan(split_index), aad, cipher); + enc_aead.Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad, cipher); } else { enc_aead.Encrypt(plain, aad, cipher); } @@ -187,7 +187,7 @@ FUZZ_TARGET(crypto_fschacha20poly1305) crypt_till_rekey(dec_aead, rekey_interval, false); if (use_splits && length > 0) { size_t split_index = provider.ConsumeIntegralInRange(1, length); - ok = dec_aead.Decrypt(cipher, aad, Span{decrypted_contents}.first(split_index), Span{decrypted_contents}.subspan(split_index)); + ok = dec_aead.Decrypt(cipher, aad, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index)); } else { ok = dec_aead.Decrypt(cipher, aad, decrypted_contents); } diff --git a/src/test/fuzz/feeratediagram.cpp b/src/test/fuzz/feeratediagram.cpp index 1a9c5ee9464..1a700987d67 100644 --- a/src/test/fuzz/feeratediagram.cpp +++ b/src/test/fuzz/feeratediagram.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -17,7 +17,7 @@ namespace { /** Takes the pre-computed and topologically-valid chunks and generates a fee diagram which starts at FeeFrac of (0, 0) */ -std::vector BuildDiagramFromChunks(const Span chunks) +std::vector BuildDiagramFromChunks(const std::span chunks) { std::vector diagram; diagram.reserve(chunks.size() + 1); @@ -34,7 +34,7 @@ std::vector BuildDiagramFromChunks(const Span chunks) * * Fees in diagram cannot exceed 2^32, as the returned evaluation could overflow * the FeeFrac::fee field in the result. */ -FeeFrac EvaluateDiagram(int32_t size, Span diagram) +FeeFrac EvaluateDiagram(int32_t size, std::span diagram) { assert(diagram.size() > 0); unsigned not_above = 0; @@ -63,12 +63,12 @@ FeeFrac EvaluateDiagram(int32_t size, Span diagram) return {point_a.fee * dir_coef.size + dir_coef.fee * (size - point_a.size), dir_coef.size}; } -std::weak_ordering CompareFeeFracWithDiagram(const FeeFrac& ff, Span diagram) +std::weak_ordering CompareFeeFracWithDiagram(const FeeFrac& ff, std::span diagram) { return FeeRateCompare(FeeFrac{ff.fee, 1}, EvaluateDiagram(ff.size, diagram)); } -std::partial_ordering CompareDiagrams(Span dia1, Span dia2) +std::partial_ordering CompareDiagrams(std::span dia1, std::span dia2) { bool all_ge = true; bool all_le = true; diff --git a/src/test/fuzz/hex.cpp b/src/test/fuzz/hex.cpp index 3dcf1ed3d51..64c753045fc 100644 --- a/src/test/fuzz/hex.cpp +++ b/src/test/fuzz/hex.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2021 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -23,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(std::ranges::equal(AsBytes(Span{data}), bytes)); + assert(std::ranges::equal(std::as_bytes(std::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 60d096bb5a9..8984cbe50c0 100644 --- a/src/test/fuzz/miniscript.cpp +++ b/src/test/fuzz/miniscript.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2022 The Bitcoin Core developers +// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -129,7 +129,7 @@ struct ParserContext { auto it = TEST_DATA.dummy_key_idx_map.find(key); if (it == TEST_DATA.dummy_key_idx_map.end()) return {}; uint8_t idx = it->second; - return HexStr(Span{&idx, 1}); + return HexStr(std::span{&idx, 1}); } std::vector ToPKBytes(const Key& key) const { @@ -290,7 +290,7 @@ const struct CheckerContext: BaseSignatureChecker { if (it == TEST_DATA.dummy_sigs.end()) return false; return it->second.first == sig; } - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion, + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion, ScriptExecutionData&, ScriptError*) const override { XOnlyPubKey pk{pubkey}; auto it = TEST_DATA.schnorr_sigs.find(pk); diff --git a/src/test/fuzz/muhash.cpp b/src/test/fuzz/muhash.cpp index f1dd5ebaf9e..318a203699c 100644 --- a/src/test/fuzz/muhash.cpp +++ b/src/test/fuzz/muhash.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -25,7 +25,7 @@ public: /** Construct an arith_uint6144 from any multiple of 4 bytes in LE notation, * up to 768 bytes. */ - arith_uint6144(Span bytes) : base_uint{} + arith_uint6144(std::span bytes) : base_uint{} { assert(bytes.size() % 4 == 0); assert(bytes.size() <= 768); @@ -36,7 +36,7 @@ public: /** Serialize an arithm_uint6144 to any multiply of 4 bytes in LE notation, * on the condition that the represented number fits. */ - void Serialize(Span bytes) { + void Serialize(std::span bytes) { assert(bytes.size() % 4 == 0); assert(bytes.size() <= 768); for (unsigned i = 0; i * 4 < bytes.size(); ++i) { diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp index e377e23894e..88432ec1009 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -72,7 +72,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial } mutable_msg_bytes.insert(mutable_msg_bytes.end(), payload_bytes.begin(), payload_bytes.end()); - Span msg_bytes{mutable_msg_bytes}; + std::span msg_bytes{mutable_msg_bytes}; while (msg_bytes.size() > 0) { if (!recv_transport.ReceivedBytes(msg_bytes)) { break; @@ -87,7 +87,7 @@ FUZZ_TARGET(p2p_transport_serialization, .init = initialize_p2p_transport_serial assert(msg.m_time == m_time); std::vector header; - auto msg2 = NetMsg::Make(msg.m_type, Span{msg.m_recv}); + auto msg2 = NetMsg::Make(msg.m_type, std::span{msg.m_recv}); bool queued = send_transport.SetMessageToSend(msg2); assert(queued); std::optional known_more; @@ -191,7 +191,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa if (more_nonext) assert(more_next); // Compare with previously reported output. assert(to_send[side].size() <= bytes.size()); - assert(std::ranges::equal(to_send[side], Span{bytes}.first(to_send[side].size()))); + assert(std::ranges::equal(to_send[side], std::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. @@ -252,7 +252,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa // Decide span to receive size_t to_recv_len = in_flight[side].size(); if (!everything) to_recv_len = provider.ConsumeIntegralInRange(0, to_recv_len); - Span to_recv = Span{in_flight[side]}.first(to_recv_len); + std::span to_recv = std::span{in_flight[side]}.first(to_recv_len); // Process those bytes while (!to_recv.empty()) { size_t old_len = to_recv.size(); diff --git a/src/test/fuzz/poolresource.cpp b/src/test/fuzz/poolresource.cpp index dd8d5b07e5f..9217af1ff7c 100644 --- a/src/test/fuzz/poolresource.cpp +++ b/src/test/fuzz/poolresource.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2022 The Bitcoin Core developers +// Copyright (c) 2022-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -25,11 +25,11 @@ class PoolResourceFuzzer size_t m_total_allocated{}; struct Entry { - Span span; + std::span span; size_t alignment; uint64_t seed; - Entry(Span s, size_t a, uint64_t se) : span(s), alignment(a), seed(se) {} + Entry(std::span s, size_t a, uint64_t se) : span(s), alignment(a), seed(se) {} }; std::vector m_entries; @@ -48,7 +48,7 @@ public: assert((alignment & (alignment - 1)) == 0); // Alignment must be power of 2. assert((size & (alignment - 1)) == 0); // Size must be a multiple of alignment. - auto span = Span(static_cast(m_test_resource.Allocate(size, alignment)), size); + auto span = std::span(static_cast(m_test_resource.Allocate(size, alignment)), size); m_total_allocated += size; auto ptr_val = reinterpret_cast(span.data()); diff --git a/src/test/fuzz/script_parsing.cpp b/src/test/fuzz/script_parsing.cpp index d29a6ea90c4..1f32130d7cc 100644 --- a/src/test/fuzz/script_parsing.cpp +++ b/src/test/fuzz/script_parsing.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2020 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -15,9 +15,9 @@ FUZZ_TARGET(script_parsing) const size_t query_size = fuzzed_data_provider.ConsumeIntegral(); const std::string query = fuzzed_data_provider.ConsumeBytesAsString(std::min(query_size, 1024 * 1024)); const std::string span_str = fuzzed_data_provider.ConsumeRemainingBytesAsString(); - const Span const_span{span_str}; + const std::span const_span{span_str}; - Span mut_span = const_span; + std::span mut_span = const_span; (void)script::Const(query, mut_span); mut_span = const_span; diff --git a/src/test/fuzz/signature_checker.cpp b/src/test/fuzz/signature_checker.cpp index 59f4792961e..9c32b7a822b 100644 --- a/src/test/fuzz/signature_checker.cpp +++ b/src/test/fuzz/signature_checker.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -29,7 +29,7 @@ public: return m_fuzzed_data_provider.ConsumeBool(); } - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override { return m_fuzzed_data_provider.ConsumeBool(); } diff --git a/src/test/fuzz/span.cpp b/src/test/fuzz/span.cpp index cd436d582fc..c3853826169 100644 --- a/src/test/fuzz/span.cpp +++ b/src/test/fuzz/span.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -18,7 +18,7 @@ FUZZ_TARGET(span) FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); std::string str = fuzzed_data_provider.ConsumeBytesAsString(32); - const Span span{str}; + const std::span span{str}; (void)span.data(); (void)span.begin(); (void)span.end(); diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index d700e5ac975..a4a319e74b9 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -13,7 +13,7 @@ #include -std::vector ConstructPubKeyBytes(FuzzedDataProvider& fuzzed_data_provider, Span byte_data, const bool compressed) noexcept +std::vector ConstructPubKeyBytes(FuzzedDataProvider& fuzzed_data_provider, std::span byte_data, const bool compressed) noexcept { uint8_t pk_type; if (compressed) { diff --git a/src/test/fuzz/utxo_snapshot.cpp b/src/test/fuzz/utxo_snapshot.cpp index 5eb6a323202..ef66051efb0 100644 --- a/src/test/fuzz/utxo_snapshot.cpp +++ b/src/test/fuzz/utxo_snapshot.cpp @@ -87,7 +87,7 @@ void utxo_snapshot_fuzz(FuzzBufferType buffer) // Metadata if (fuzzed_data_provider.ConsumeBool()) { std::vector metadata{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - outfile << Span{metadata}; + outfile << std::span{metadata}; } else { auto msg_start = chainman.GetParams().MessageStart(); int base_blockheight{fuzzed_data_provider.ConsumeIntegralInRange(1, 2 * COINBASE_MATURITY)}; @@ -99,7 +99,7 @@ void utxo_snapshot_fuzz(FuzzBufferType buffer) // Coins if (fuzzed_data_provider.ConsumeBool()) { std::vector file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - outfile << Span{file_data}; + outfile << std::span{file_data}; } else { int height{0}; for (const auto& block : *g_chain) { diff --git a/src/test/fuzz/vecdeque.cpp b/src/test/fuzz/vecdeque.cpp index 3bb858ee8ae..9a71c43b987 100644 --- a/src/test/fuzz/vecdeque.cpp +++ b/src/test/fuzz/vecdeque.cpp @@ -24,7 +24,7 @@ static constexpr size_t MAX_OPERATIONS{1024}; * T must be constructible from a uint64_t seed, comparable to other T, copyable, and movable. */ template -void TestType(Span buffer, uint64_t rng_tweak) +void TestType(std::span buffer, uint64_t rng_tweak) { FuzzedDataProvider provider(buffer.data(), buffer.size()); // Local RNG, only used for the seeds to initialize T objects with. diff --git a/src/test/hash_tests.cpp b/src/test/hash_tests.cpp index f56f7232c3b..2fe44960f46 100644 --- a/src/test/hash_tests.cpp +++ b/src/test/hash_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2013-2021 The Bitcoin Core developers +// Copyright (c) 2013-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(siphash) for (uint8_t x=0; xsecond; } - bool CheckSchnorrSignature(Span sig, Span pubkey, SigVersion, + bool CheckSchnorrSignature(std::span sig, std::span pubkey, SigVersion, ScriptExecutionData&, ScriptError*) const override { XOnlyPubKey pk{pubkey}; auto it = g_testdata->schnorr_signatures.find(pk); diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 62e541b5b39..0036d94c2fa 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -861,7 +861,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message) const auto CaptureMessageOrig = CaptureMessage; CaptureMessage = [&sent, &expected](const CAddress& addr, const std::string& msg_type, - Span data, + std::span data, bool is_incoming) -> void { if (!is_incoming && msg_type == "addr") { DataStream s{data}; @@ -1054,7 +1054,7 @@ public: bool progress{false}; // Send bytes from m_to_send to the transport. if (!m_to_send.empty()) { - Span to_send = Span{m_to_send}.first(1 + m_rng.randrange(m_to_send.size())); + std::span to_send = std::span{m_to_send}.first(1 + m_rng.randrange(m_to_send.size())); size_t old_len = to_send.size(); if (!m_transport.ReceivedBytes(to_send)) { return std::nullopt; // transport error occurred @@ -1099,7 +1099,7 @@ public: BIP324Cipher& GetCipher() { return m_cipher; } /** Schedule bytes to be sent to the transport. */ - void Send(Span data) + void Send(std::span data) { m_to_send.insert(m_to_send.end(), data.begin(), data.end()); } @@ -1114,13 +1114,13 @@ public: } /** Schedule bytes to be sent to the transport. */ - void Send(Span data) { Send(MakeUCharSpan(data)); } + void Send(std::span data) { Send(MakeUCharSpan(data)); } /** Schedule our ellswift key to be sent to the transport. */ void SendKey() { Send(m_cipher.GetOurPubKey()); } /** Schedule specified garbage to be sent to the transport. */ - void SendGarbage(Span garbage) + void SendGarbage(std::span garbage) { // Remember the specified garbage (so we can use it as AAD). m_sent_garbage.assign(garbage.begin(), garbage.end()); @@ -1167,7 +1167,7 @@ public: /** Schedule an encrypted packet with specified content/aad/ignore to be sent to transport * (only after ReceiveKey). */ - void SendPacket(Span content, Span aad = {}, bool ignore = false) + void SendPacket(std::span content, std::span aad = {}, bool ignore = false) { // Use cipher to construct ciphertext. std::vector ciphertext; @@ -1189,9 +1189,9 @@ public: } /** Schedule version packet to be sent to the transport (only after ReceiveKey). */ - void SendVersion(Span version_data = {}, bool vers_ignore = false) + void SendVersion(std::span version_data = {}, bool vers_ignore = false) { - Span aad; + std::span aad; // Set AAD to garbage only for first packet. if (!m_sent_aad) aad = m_sent_garbage; SendPacket(/*content=*/version_data, /*aad=*/aad, /*ignore=*/vers_ignore); @@ -1201,7 +1201,7 @@ public: /** Expect a packet to have been received from transport, process it, and return its contents * (only after ReceiveKey). Decoys are skipped. Optional associated authenticated data (AAD) is * expected in the first received packet, no matter if that is a decoy or not. */ - std::vector ReceivePacket(Span aad = {}) + std::vector ReceivePacket(std::span aad = {}) { std::vector contents; // Loop as long as there are ignored packets that are to be skipped. @@ -1209,7 +1209,7 @@ public: // When processing a packet, at least enough bytes for its length descriptor must be received. BOOST_REQUIRE(m_received.size() >= BIP324Cipher::LENGTH_LEN); // Decrypt the content length. - size_t size = m_cipher.DecryptLength(MakeByteSpan(Span{m_received}.first(BIP324Cipher::LENGTH_LEN))); + size_t size = m_cipher.DecryptLength(MakeByteSpan(std::span{m_received}.first(BIP324Cipher::LENGTH_LEN))); // Check that the full packet is in the receive buffer. BOOST_REQUIRE(m_received.size() >= size + BIP324Cipher::EXPANSION); // Decrypt the packet contents. @@ -1217,7 +1217,7 @@ public: bool ignore{false}; bool ret = m_cipher.Decrypt( /*input=*/MakeByteSpan( - Span{m_received}.first(size + BIP324Cipher::EXPANSION).subspan(BIP324Cipher::LENGTH_LEN)), + std::span{m_received}.first(size + BIP324Cipher::EXPANSION).subspan(BIP324Cipher::LENGTH_LEN)), /*aad=*/aad, /*ignore=*/ignore, /*contents=*/MakeWritableByteSpan(contents)); @@ -1240,7 +1240,7 @@ public: size_t garblen; for (garblen = 0; garblen <= V2Transport::MAX_GARBAGE_LEN; ++garblen) { BOOST_REQUIRE(m_received.size() >= garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN); - auto term_span = MakeByteSpan(Span{m_received}.subspan(garblen, BIP324Cipher::GARBAGE_TERMINATOR_LEN)); + auto term_span = MakeByteSpan(std::span{m_received}.subspan(garblen, BIP324Cipher::GARBAGE_TERMINATOR_LEN)); if (std::ranges::equal(term_span, m_cipher.GetReceiveGarbageTerminator())) break; } // Copy the garbage to a buffer. @@ -1261,17 +1261,17 @@ public: /** Expect application packet to have been received, with specified short id and payload. * (only after ReceiveKey). */ - void ReceiveMessage(uint8_t short_id, Span payload) + void ReceiveMessage(uint8_t short_id, std::span payload) { auto ret = ReceivePacket(); BOOST_CHECK(ret.size() == payload.size() + 1); BOOST_CHECK(ret[0] == short_id); - BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1), payload)); + BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1), payload)); } /** Expect application packet to have been received, with specified 12-char message type and * payload (only after ReceiveKey). */ - void ReceiveMessage(const std::string& m_type, Span payload) + void ReceiveMessage(const std::string& m_type, std::span payload) { auto ret = ReceivePacket(); BOOST_REQUIRE(ret.size() == payload.size() + 1 + CMessageHeader::MESSAGE_TYPE_SIZE); @@ -1283,12 +1283,12 @@ public: BOOST_CHECK(ret[1 + i] == 0); } } - BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1 + CMessageHeader::MESSAGE_TYPE_SIZE), payload)); + BOOST_CHECK(std::ranges::equal(std::span{ret}.subspan(1 + CMessageHeader::MESSAGE_TYPE_SIZE), payload)); } /** Schedule an encrypted packet with specified message type and payload to be sent to * transport (only after ReceiveKey). */ - void SendMessage(std::string mtype, Span payload) + void SendMessage(std::string mtype, std::span payload) { // Construct contents consisting of 0x00 + 12-byte message type + payload. std::vector contents(1 + CMessageHeader::MESSAGE_TYPE_SIZE + payload.size()); @@ -1300,7 +1300,7 @@ public: /** Schedule an encrypted packet with specified short message id and payload to be sent to * transport (only after ReceiveKey). */ - void SendMessage(uint8_t short_id, Span payload) + void SendMessage(uint8_t short_id, std::span payload) { // Construct contents consisting of short_id + payload. std::vector contents(1 + payload.size()); diff --git a/src/test/pcp_tests.cpp b/src/test/pcp_tests.cpp index 967bef1946c..bb9a7fff03d 100644 --- a/src/test/pcp_tests.cpp +++ b/src/test/pcp_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2024 The Bitcoin Core developers +// Copyright (c) 2024-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -101,7 +101,7 @@ public: ssize_t Send(const void* data, size_t len, int) const override { if (!m_connected) return -1; - Span in_pkt = Span(static_cast(data), len); + std::span in_pkt = std::span(static_cast(data), len); if (AtEndOfScript() || CurOp().op != TestOp::SEND) { // Ignore sends after end of script, or sends when we expect a receive. FailScript(); diff --git a/src/test/pool_tests.cpp b/src/test/pool_tests.cpp index 9d15660126a..9965a520bf9 100644 --- a/src/test/pool_tests.cpp +++ b/src/test/pool_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2022 The Bitcoin Core developers +// Copyright (c) 2022-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(allocate_any_byte) uint8_t num_allocs = 200; - auto data = std::vector>(); + auto data = std::vector>(); // allocate an increasing number of bytes for (uint8_t num_bytes = 0; num_bytes < num_allocs; ++num_bytes) { diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index 8753ddee37f..4a5dc561cc3 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2022 The Bitcoin Core developers +// Copyright (c) 2011-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -258,7 +258,7 @@ public: CScript scriptPubKey = script; if (wm == WitnessMode::PKH) { uint160 hash; - CHash160().Write(Span{script}.subspan(1)).Finalize(hash); + CHash160().Write(std::span{script}.subspan(1)).Finalize(hash); script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG; scriptPubKey = CScript() << witnessversion << ToByteVector(hash); } else if (wm == WitnessMode::SH) { @@ -1719,8 +1719,8 @@ BOOST_AUTO_TEST_CASE(compute_tapleaf) constexpr uint256 tlc0{"edbc10c272a1215dcdcc11d605b9027b5ad6ed97cd45521203f136767b5b9c06"}; constexpr uint256 tlc2{"8b5c4f90ae6bf76e259dbef5d8a59df06359c391b59263741b25eca76451b27a"}; - BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, Span(script)), tlc0); - BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, Span(script)), tlc2); + BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc0, std::span(script)), tlc0); + BOOST_CHECK_EQUAL(ComputeTapleafHash(0xc2, std::span(script)), tlc2); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 7e403694e22..9c68c8bb964 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -211,32 +211,32 @@ BOOST_AUTO_TEST_CASE(noncanonical) std::vector::size_type n; // zero encoded with three bytes: - ss << Span{"\xfd\x00\x00"}.first(3); + ss << std::span{"\xfd\x00\x00"}.first(3); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); // 0xfc encoded with three bytes: - ss << Span{"\xfd\xfc\x00"}.first(3); + ss << std::span{"\xfd\xfc\x00"}.first(3); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); // 0xfd encoded with three bytes is OK: - ss << Span{"\xfd\xfd\x00"}.first(3); + ss << std::span{"\xfd\xfd\x00"}.first(3); n = ReadCompactSize(ss); BOOST_CHECK(n == 0xfd); // zero encoded with five bytes: - ss << Span{"\xfe\x00\x00\x00\x00"}.first(5); + ss << std::span{"\xfe\x00\x00\x00\x00"}.first(5); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); // 0xffff encoded with five bytes: - ss << Span{"\xfe\xff\xff\x00\x00"}.first(5); + ss << std::span{"\xfe\xff\xff\x00\x00"}.first(5); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); // zero encoded with nine bytes: - ss << Span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9); + ss << std::span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); // 0x01ffffff encoded with nine bytes: - ss << Span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9); + ss << std::span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9); BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException); } @@ -269,10 +269,10 @@ BOOST_AUTO_TEST_CASE(class_methods) { DataStream ds; const std::string in{"ab"}; - ds << Span{in} << std::byte{'c'}; + ds << std::span{in} << std::byte{'c'}; std::array out; std::byte out_3; - ds >> Span{out} >> out_3; + ds >> std::span{out} >> out_3; BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'}); BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'}); BOOST_CHECK_EQUAL(out_3, std::byte{'c'}); @@ -304,7 +304,7 @@ public: if (s.template GetParams().m_base_format == BaseFormat::RAW) { s << m_base_data; } else { - s << std::span{HexStr(Span{&m_base_data, 1})}; + s << std::span{HexStr(std::span{&m_base_data, 1})}; } } @@ -315,7 +315,7 @@ public: s >> m_base_data; } else { std::string hex{"aa"}; - s >> Span{hex}.first(hex.size()); + s >> std::span{hex}.first(hex.size()); m_base_data = TryParseHex(hex).value().at(0); } } diff --git a/src/test/span_tests.cpp b/src/test/span_tests.cpp index aae61990f79..42b62413aac 100644 --- a/src/test/span_tests.cpp +++ b/src/test/span_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -9,13 +9,13 @@ #include #include -namespace { +namespace spannable { struct Ignore { template Ignore(T&&) {} }; template -bool Spannable(T&& value, decltype(Span{value})* enable = nullptr) +bool Spannable(T&& value, decltype(std::span{value})* enable = nullptr) { return true; } @@ -24,47 +24,36 @@ bool Spannable(Ignore) return false; } -#if defined(__clang__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wunneeded-member-function" -# pragma clang diagnostic ignored "-Wunused-member-function" -#endif struct SpannableYes { int* data(); + int* begin(); + int* end(); size_t size(); }; struct SpannableNo { - void* data(); + void data(); size_t size(); }; -#if defined(__clang__) -# pragma clang diagnostic pop -#endif -} // namespace +} // namespace spannable + +using namespace spannable; BOOST_AUTO_TEST_SUITE(span_tests) -// Make sure template Span template deduction guides accurately enable calls to -// Span constructor overloads that work, and disable calls to constructor overloads that -// don't work. This makes it is possible to use the Span constructor in a SFINAE +// Make sure template std::span template deduction guides accurately enable calls to +// std::span constructor overloads that work, and disable calls to constructor overloads that +// don't work. This makes it possible to use the std::span constructor in a SFINAE // contexts like in the Spannable function above to detect whether types are or -// aren't compatible with Spans at compile time. -// -// Previously there was a bug where writing a SFINAE check for vector was -// not possible, because in libstdc++ vector has a data() member -// returning void*, and the Span template guide ignored the data() return value, -// so the template substitution would succeed, but the constructor would fail, -// resulting in a fatal compile error, rather than a SFINAE error that could be -// handled. +// aren't compatible with std::span at compile time. BOOST_AUTO_TEST_CASE(span_constructor_sfinae) { BOOST_CHECK(Spannable(std::vector{})); BOOST_CHECK(!Spannable(std::set{})); BOOST_CHECK(!Spannable(std::vector{})); BOOST_CHECK(Spannable(std::array{})); - BOOST_CHECK(Spannable(Span{})); + BOOST_CHECK(Spannable(std::span{})); BOOST_CHECK(Spannable("char array")); BOOST_CHECK(Spannable(SpannableYes{})); BOOST_CHECK(!Spannable(SpannableNo{})); diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 777122df6d0..1a44e66932c 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2022 The Bitcoin Core developers +// Copyright (c) 2012-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(xor_file) // Read raw from disk AutoFile non_xor_file{raw_file("rb")}; std::vector raw(7); - non_xor_file >> Span{raw}; + non_xor_file >> std::span{raw}; BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa"); // Check that no padding exists BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); diff --git a/src/test/util/cluster_linearize.h b/src/test/util/cluster_linearize.h index 871aa9d74ed..7ae56232bac 100644 --- a/src/test/util/cluster_linearize.h +++ b/src/test/util/cluster_linearize.h @@ -392,7 +392,7 @@ void SanityCheck(const DepGraph& depgraph) /** Perform a sanity check on a linearization. */ template -void SanityCheck(const DepGraph& depgraph, Span linearization) +void SanityCheck(const DepGraph& depgraph, std::span linearization) { // Check completeness. assert(linearization.size() == depgraph.TxCount()); diff --git a/src/test/util/net.cpp b/src/test/util/net.cpp index ddd96a50640..34d75fa8262 100644 --- a/src/test/util/net.cpp +++ b/src/test/util/net.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -71,7 +71,7 @@ void ConnmanTestMsg::Handshake(CNode& node, } } -void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, Span msg_bytes, bool& complete) const +void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, std::span msg_bytes, bool& complete) const { assert(node.ReceiveMsgBytes(msg_bytes, complete)); if (complete) { @@ -279,7 +279,7 @@ std::optional DynSock::Pipe::GetNetMsg() } for (;;) { - Span s{m_data}; + std::span s{m_data}; if (!transport.ReceivedBytes(s)) { // Consumed bytes are removed from the front of s. return std::nullopt; } diff --git a/src/test/util/net.h b/src/test/util/net.h index 3e717341d87..6bf2bf73007 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -81,7 +81,7 @@ struct ConnmanTestMsg : public CConnman { return m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); } - void NodeReceiveMsgBytes(CNode& node, Span msg_bytes, bool& complete) const; + void NodeReceiveMsgBytes(CNode& node, std::span msg_bytes, bool& complete) const; bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const; void FlushSendBuffer(CNode& node) const; diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 4cacbd1151f..0fa6f7d8e5a 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -160,8 +160,8 @@ BOOST_AUTO_TEST_CASE(parse_hex) BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); const std::vector hex_literal_vector{operator""_hex_v()}; - hex_literal_span = MakeUCharSpan(hex_literal_vector); - BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); + auto hex_literal_vec_span = MakeUCharSpan(hex_literal_vector); + BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_vec_span.begin(), hex_literal_vec_span.end(), expected.begin(), expected.end()); constexpr std::array hex_literal_array_uint8{operator""_hex_u8()}; BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end()); @@ -236,14 +236,14 @@ BOOST_AUTO_TEST_CASE(consteval_hex_digit) BOOST_AUTO_TEST_CASE(util_HexStr) { BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT); - BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), ""); - BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), ""); + BOOST_CHECK_EQUAL(HexStr(std::span{HEX_PARSE_OUTPUT}.last(0)), ""); + BOOST_CHECK_EQUAL(HexStr(std::span{HEX_PARSE_OUTPUT}.first(0)), ""); { constexpr std::string_view out_exp{"04678afdb0"}; constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2}; - const Span in_u{MakeUCharSpan(in_s)}; - const Span in_b{MakeByteSpan(in_s)}; + const std::span in_u{MakeUCharSpan(in_s)}; + const std::span in_b{MakeByteSpan(in_s)}; BOOST_CHECK_EQUAL(HexStr(in_u), out_exp); BOOST_CHECK_EQUAL(HexStr(in_s), out_exp); @@ -1335,7 +1335,7 @@ BOOST_AUTO_TEST_CASE(test_Capitalize) BOOST_CHECK_EQUAL(Capitalize("\x00\xfe\xff"), "\x00\xfe\xff"); } -static std::string SpanToStr(const Span& span) +static std::string SpanToStr(const std::span& span) { return std::string(span.begin(), span.end()); } @@ -1344,7 +1344,7 @@ BOOST_AUTO_TEST_CASE(test_script_parsing) { using namespace script; std::string input; - Span sp; + std::span sp; bool success; // Const(...): parse a constant, update span to skip it if successful @@ -1394,7 +1394,7 @@ BOOST_AUTO_TEST_CASE(test_script_parsing) BOOST_CHECK(!success); // Expr(...): return expression that span begins with, update span to skip it - Span result; + std::span result; input = "(n*(n-1))/2"; sp = input; @@ -1427,7 +1427,7 @@ BOOST_AUTO_TEST_CASE(test_script_parsing) BOOST_CHECK_EQUAL(SpanToStr(sp), ",xxx"); // Split(...): split a string on every instance of sep, return vector - std::vector> results; + std::vector> results; input = "xxx"; results = Split(input, 'x'); diff --git a/src/uint256.h b/src/uint256.h index 708f0c2ff1b..094db613b40 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -37,7 +37,7 @@ public: /* constructor for constants between 1 and 255 */ constexpr explicit base_blob(uint8_t v) : m_data{v} {} - constexpr explicit base_blob(Span vch) + constexpr explicit base_blob(std::span vch) { assert(vch.size() == WIDTH); std::copy(vch.begin(), vch.end(), m_data.begin()); @@ -125,7 +125,7 @@ public: template void Serialize(Stream& s) const { - s << Span(m_data); + s << std::span(m_data); } template @@ -190,7 +190,7 @@ class uint160 : public base_blob<160> { public: static std::optional FromHex(std::string_view str) { return detail::FromHex(str); } constexpr uint160() = default; - constexpr explicit uint160(Span vch) : base_blob<160>(vch) {} + constexpr explicit uint160(std::span vch) : base_blob<160>(vch) {} }; /** 256-bit opaque blob. @@ -205,7 +205,7 @@ public: constexpr uint256() = default; consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {} constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} - constexpr explicit uint256(Span vch) : base_blob<256>(vch) {} + constexpr explicit uint256(std::span vch) : base_blob<256>(vch) {} static const uint256 ZERO; static const uint256 ONE; }; diff --git a/src/util/feefrac.cpp b/src/util/feefrac.cpp index 5b6173835cb..68ba2b6665a 100644 --- a/src/util/feefrac.cpp +++ b/src/util/feefrac.cpp @@ -7,10 +7,10 @@ #include #include -std::partial_ordering CompareChunks(Span chunks0, Span chunks1) +std::partial_ordering CompareChunks(std::span chunks0, std::span chunks1) { /** Array to allow indexed access to input diagrams. */ - const std::array, 2> chunk = {chunks0, chunks1}; + const std::array, 2> chunk = {chunks0, chunks1}; /** How many elements we have processed in each input. */ size_t next_index[2] = {0, 0}; /** Accumulated fee/sizes in diagrams, up to next_index[i] - 1. */ diff --git a/src/util/feefrac.h b/src/util/feefrac.h index 161322b50a4..6097d1ec638 100644 --- a/src/util/feefrac.h +++ b/src/util/feefrac.h @@ -154,6 +154,6 @@ struct FeeFrac * The caller must guarantee that the sum of the FeeFracs in either of the chunks' data set do not * overflow (so sum fees < 2^63, and sum sizes < 2^31). */ -std::partial_ordering CompareChunks(Span chunks0, Span chunks1); +std::partial_ordering CompareChunks(std::span chunks0, std::span chunks1); #endif // BITCOIN_UTIL_FEEFRAC_H diff --git a/src/util/hasher.cpp b/src/util/hasher.cpp index 3109ba02a8d..117cfe8dcd1 100644 --- a/src/util/hasher.cpp +++ b/src/util/hasher.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -20,7 +20,7 @@ SaltedSipHasher::SaltedSipHasher() : m_k0{FastRandomContext().rand64()}, m_k1{FastRandomContext().rand64()} {} -size_t SaltedSipHasher::operator()(const Span& script) const +size_t SaltedSipHasher::operator()(const std::span& script) const { return CSipHasher(m_k0, m_k1).Write(script).Finalize(); } diff --git a/src/util/hasher.h b/src/util/hasher.h index e4594c7ddaf..3a75c91a3ef 100644 --- a/src/util/hasher.h +++ b/src/util/hasher.h @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022 The Bitcoin Core developers +// Copyright (c) 2019-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -94,7 +94,7 @@ private: public: SaltedSipHasher(); - size_t operator()(const Span& script) const; + size_t operator()(const std::span& script) const; }; #endif // BITCOIN_UTIL_HASHER_H diff --git a/src/util/sock.cpp b/src/util/sock.cpp index e896b871608..9c318067bd4 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -242,7 +242,7 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per #endif /* USE_POLL */ } -void Sock::SendComplete(Span data, +void Sock::SendComplete(std::span data, std::chrono::milliseconds timeout, CThreadInterrupt& interrupt) const { @@ -283,7 +283,7 @@ void Sock::SendComplete(Span data, } } -void Sock::SendComplete(Span data, +void Sock::SendComplete(std::span data, std::chrono::milliseconds timeout, CThreadInterrupt& interrupt) const { diff --git a/src/util/sock.h b/src/util/sock.h index 65e7ffc1650..69ec1b8e01d 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -228,14 +228,14 @@ public: * @throws std::runtime_error if the operation cannot be completed. In this case only some of * the data will be written to the socket. */ - virtual void SendComplete(Span data, + virtual void SendComplete(std::span data, std::chrono::milliseconds timeout, CThreadInterrupt& interrupt) const; /** * Convenience method, equivalent to `SendComplete(MakeUCharSpan(data), timeout, interrupt)`. */ - virtual void SendComplete(Span data, + virtual void SendComplete(std::span data, std::chrono::milliseconds timeout, CThreadInterrupt& interrupt) const; diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index eed8bf81370..fbccebad942 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -96,7 +96,7 @@ bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut) return valid; } -std::string EncodeBase64(Span input) +std::string EncodeBase64(std::span input) { static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -142,7 +142,7 @@ std::optional> DecodeBase64(std::string_view str) return ret; } -std::string EncodeBase32(Span input, bool pad) +std::string EncodeBase32(std::span input, bool pad) { static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 75030983d21..fd713a555c7 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -73,8 +73,8 @@ std::vector ParseHex(std::string_view hex_str) * number of hex digits.*/ bool IsHex(std::string_view str); std::optional> DecodeBase64(std::string_view str); -std::string EncodeBase64(Span input); -inline std::string EncodeBase64(Span input) { return EncodeBase64(MakeUCharSpan(input)); } +std::string EncodeBase64(std::span input); +inline std::string EncodeBase64(std::span input) { return EncodeBase64(MakeUCharSpan(input)); } inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); } std::optional> DecodeBase32(std::string_view str); @@ -83,7 +83,7 @@ std::optional> DecodeBase32(std::string_view str); * If `pad` is true, then the output will be padded with '=' so that its length * is a multiple of 8. */ -std::string EncodeBase32(Span input, bool pad = true); +std::string EncodeBase32(std::span input, bool pad = true); /** * Base32 encode. diff --git a/src/util/string.h b/src/util/string.h index b523e6ef4e7..3075e46abbf 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -103,8 +103,8 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin * Note that this function does not care about braces, so splitting * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. */ -template > -std::vector Split(const Span& sp, std::string_view separators) +template > +std::vector Split(const std::span& sp, std::string_view separators) { std::vector ret; auto it = sp.begin(); @@ -127,8 +127,8 @@ std::vector Split(const Span& sp, std::string_view separators) * Note that this function does not care about braces, so splitting * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. */ -template > -std::vector Split(const Span& sp, char sep) +template > +std::vector Split(const std::span& sp, char sep) { return Split(sp, std::string_view{&sep, 1}); } diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index 79851dff33f..f6eee29ad1f 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -296,7 +296,7 @@ SafeDbt::operator Dbt*() return &m_dbt; } -static Span SpanFromDbt(const SafeDbt& dbt) +static std::span SpanFromDbt(const SafeDbt& dbt) { return {reinterpret_cast(dbt.get_data()), dbt.get_size()}; } @@ -726,7 +726,7 @@ void BerkeleyDatabase::ReloadDbEnv() env->ReloadDbEnv(); } -BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, Span prefix) +BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, std::span prefix) : m_key_prefix(prefix.begin(), prefix.end()) { if (!database.m_db.get()) { @@ -760,7 +760,7 @@ DatabaseCursor::Status BerkeleyCursor::Next(DataStream& ssKey, DataStream& ssVal return Status::FAIL; } - Span raw_key = SpanFromDbt(datKey); + std::span raw_key = SpanFromDbt(datKey); if (!m_key_prefix.empty() && std::mismatch(raw_key.begin(), raw_key.end(), m_key_prefix.begin(), m_key_prefix.end()).second != m_key_prefix.end()) { return Status::DONE; } @@ -786,7 +786,7 @@ std::unique_ptr BerkeleyBatch::GetNewCursor() return std::make_unique(m_database, *this); } -std::unique_ptr BerkeleyBatch::GetNewPrefixCursor(Span prefix) +std::unique_ptr BerkeleyBatch::GetNewPrefixCursor(std::span prefix) { if (!pdb) return nullptr; return std::make_unique(m_database, *this, prefix); @@ -899,7 +899,7 @@ bool BerkeleyBatch::HasKey(DataStream&& key) return ret == 0; } -bool BerkeleyBatch::ErasePrefix(Span prefix) +bool BerkeleyBatch::ErasePrefix(std::span prefix) { // Because this function erases records one by one, ensure that it is executed within a txn context. // Otherwise, consistency is at risk; it's possible that certain records are removed while others diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h index f3fe8a19c19..b8cfde60036 100644 --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -167,7 +167,7 @@ private: public: // Constructor for cursor for records matching the prefix // To match all records, an empty prefix may be provided. - explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, Span prefix = {}); + explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch, std::span prefix = {}); ~BerkeleyCursor() override; Status Next(DataStream& key, DataStream& value) override; @@ -182,7 +182,7 @@ private: bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override; bool EraseKey(DataStream&& key) override; bool HasKey(DataStream&& key) override; - bool ErasePrefix(Span prefix) override; + bool ErasePrefix(std::span prefix) override; protected: Db* pdb{nullptr}; @@ -204,7 +204,7 @@ public: void Close() override; std::unique_ptr GetNewCursor() override; - std::unique_ptr GetNewPrefixCursor(Span prefix) override; + std::unique_ptr GetNewPrefixCursor(std::span prefix) override; bool TxnBegin() override; bool TxnCommit() override; bool TxnAbort() override; diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 1c007ba949b..db4284595f8 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2021 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -17,8 +17,8 @@ #include namespace wallet { -bool operator<(BytePrefix a, Span b) { return std::ranges::lexicographical_compare(a.prefix, b.subspan(0, std::min(a.prefix.size(), b.size()))); } -bool operator<(Span a, BytePrefix b) { return std::ranges::lexicographical_compare(a.subspan(0, std::min(a.size(), b.prefix.size())), b.prefix); } +bool operator<(BytePrefix a, std::span b) { return std::ranges::lexicographical_compare(a.prefix, b.subspan(0, std::min(a.prefix.size(), b.size()))); } +bool operator<(std::span a, BytePrefix b) { return std::ranges::lexicographical_compare(a.subspan(0, std::min(a.size(), b.prefix.size())), b.prefix); } std::vector> ListDatabases(const fs::path& wallet_dir) { diff --git a/src/wallet/db.h b/src/wallet/db.h index e8790006a4d..570e9dd8c26 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2021 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -22,10 +22,10 @@ struct bilingual_str; namespace wallet { // BytePrefix compares equality with other byte spans that begin with the same prefix. struct BytePrefix { - Span prefix; + std::span prefix; }; -bool operator<(BytePrefix a, Span b); -bool operator<(Span a, BytePrefix b); +bool operator<(BytePrefix a, std::span b); +bool operator<(std::span a, BytePrefix b); class DatabaseCursor { @@ -115,10 +115,10 @@ public: return HasKey(std::move(ssKey)); } - virtual bool ErasePrefix(Span prefix) = 0; + virtual bool ErasePrefix(std::span prefix) = 0; virtual std::unique_ptr GetNewCursor() = 0; - virtual std::unique_ptr GetNewPrefixCursor(Span prefix) = 0; + virtual std::unique_ptr GetNewPrefixCursor(std::span prefix) = 0; virtual bool TxnBegin() = 0; virtual bool TxnCommit() = 0; virtual bool TxnAbort() = 0; diff --git a/src/wallet/dump.cpp b/src/wallet/dump.cpp index db2756e0ca8..c42ac068a3f 100644 --- a/src/wallet/dump.cpp +++ b/src/wallet/dump.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -57,7 +57,7 @@ bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& erro // Write out a magic string with version std::string line = strprintf("%s,%u\n", DUMP_MAGIC, DUMP_VERSION); dump_file.write(line.data(), line.size()); - hasher << Span{line}; + hasher << std::span{line}; // Write out the file format std::string format = db.Format(); @@ -68,7 +68,7 @@ bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& erro } line = strprintf("%s,%s\n", "format", format); dump_file.write(line.data(), line.size()); - hasher << Span{line}; + hasher << std::span{line}; if (ret) { @@ -89,7 +89,7 @@ bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& erro std::string value_str = HexStr(ss_value); line = strprintf("%s,%s\n", key_str, value_str); dump_file.write(line.data(), line.size()); - hasher << Span{line}; + hasher << std::span{line}; } } @@ -163,7 +163,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs:: return false; } std::string magic_hasher_line = strprintf("%s,%s\n", magic_key, version_value); - hasher << Span{magic_hasher_line}; + hasher << std::span{magic_hasher_line}; // Get the stored file format std::string format_key; @@ -196,7 +196,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs:: warnings.push_back(strprintf(_("Warning: Dumpfile wallet format \"%s\" does not match command line specified format \"%s\"."), format_value, file_format)); } std::string format_hasher_line = strprintf("%s,%s\n", format_key, format_value); - hasher << Span{format_hasher_line}; + hasher << std::span{format_hasher_line}; DatabaseOptions options; DatabaseStatus status; @@ -241,7 +241,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs:: } std::string line = strprintf("%s,%s\n", key, value); - hasher << Span{line}; + hasher << std::span{line}; if (key.empty() || value.empty()) { continue; @@ -260,7 +260,7 @@ bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs:: std::vector k = ParseHex(key); std::vector v = ParseHex(value); - if (!batch->Write(Span{k}, Span{v})) { + if (!batch->Write(std::span{k}, std::span{v})) { error = strprintf(_("Error: Unable to write record to new wallet")); ret = false; break; diff --git a/src/wallet/migrate.cpp b/src/wallet/migrate.cpp index d2c163027d2..f97ee0756c1 100644 --- a/src/wallet/migrate.cpp +++ b/src/wallet/migrate.cpp @@ -244,7 +244,7 @@ public: void Unserialize(Stream& s) { data.resize(m_header.len); - s.read(AsWritableBytes(Span(data.data(), data.size()))); + s.read(std::as_writable_bytes(std::span(data.data(), data.size()))); } }; @@ -272,7 +272,7 @@ public: s >> records; data.resize(m_header.len); - s.read(AsWritableBytes(Span(data.data(), data.size()))); + s.read(std::as_writable_bytes(std::span(data.data(), data.size()))); if (m_header.other_endian) { page_num = internal_bswap_32(page_num); @@ -456,7 +456,7 @@ public: void Unserialize(Stream& s) { data.resize(m_header.hf_offset); - s.read(AsWritableBytes(Span(data.data(), data.size()))); + s.read(std::as_writable_bytes(std::span(data.data(), data.size()))); } }; @@ -736,7 +736,7 @@ bool BerkeleyROBatch::ReadKey(DataStream&& key, DataStream& value) } auto val = it->second; value.clear(); - value.write(Span(val)); + value.write(std::span(val)); return true; } @@ -746,7 +746,7 @@ bool BerkeleyROBatch::HasKey(DataStream&& key) return m_database.m_records.count(key_data) > 0; } -BerkeleyROCursor::BerkeleyROCursor(const BerkeleyRODatabase& database, Span prefix) +BerkeleyROCursor::BerkeleyROCursor(const BerkeleyRODatabase& database, std::span prefix) : m_database(database) { std::tie(m_cursor, m_cursor_end) = m_database.m_records.equal_range(BytePrefix{prefix}); @@ -757,13 +757,13 @@ DatabaseCursor::Status BerkeleyROCursor::Next(DataStream& ssKey, DataStream& ssV if (m_cursor == m_cursor_end) { return DatabaseCursor::Status::DONE; } - ssKey.write(Span(m_cursor->first)); - ssValue.write(Span(m_cursor->second)); + ssKey.write(std::span(m_cursor->first)); + ssValue.write(std::span(m_cursor->second)); m_cursor++; return DatabaseCursor::Status::MORE; } -std::unique_ptr BerkeleyROBatch::GetNewPrefixCursor(Span prefix) +std::unique_ptr BerkeleyROBatch::GetNewPrefixCursor(std::span prefix) { return std::make_unique(m_database, prefix); } diff --git a/src/wallet/migrate.h b/src/wallet/migrate.h index 16eadeb019d..58f3e5701a5 100644 --- a/src/wallet/migrate.h +++ b/src/wallet/migrate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2021 The Bitcoin Core developers +// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -80,7 +80,7 @@ private: BerkeleyROData::const_iterator m_cursor_end; public: - explicit BerkeleyROCursor(const BerkeleyRODatabase& database, Span prefix = {}); + explicit BerkeleyROCursor(const BerkeleyRODatabase& database, std::span prefix = {}); ~BerkeleyROCursor() = default; Status Next(DataStream& key, DataStream& value) override; @@ -98,7 +98,7 @@ private: bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; } bool EraseKey(DataStream&& key) override { return false; } bool HasKey(DataStream&& key) override; - bool ErasePrefix(Span prefix) override { return false; } + bool ErasePrefix(std::span prefix) override { return false; } public: explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {} @@ -111,7 +111,7 @@ public: void Close() override {} std::unique_ptr GetNewCursor() override { return std::make_unique(m_database); } - std::unique_ptr GetNewPrefixCursor(Span prefix) override; + std::unique_ptr GetNewPrefixCursor(std::span prefix) override; bool TxnBegin() override { return false; } bool TxnCommit() override { return false; } bool TxnAbort() override { return false; } diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index 1efbbce41cb..63bd8703989 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -1102,7 +1102,7 @@ RPCHelpMan abandontransaction(); RPCHelpMan rescanblockchain(); RPCHelpMan abortrescan(); -Span GetWalletRPCCommands() +std::span GetWalletRPCCommands() { static const CRPCCommand commands[]{ {"rawtransactions", &fundrawtransaction}, diff --git a/src/wallet/rpc/wallet.h b/src/wallet/rpc/wallet.h index 423fc892b2c..c1958632725 100644 --- a/src/wallet/rpc/wallet.h +++ b/src/wallet/rpc/wallet.h @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2021 The Bitcoin Core developers +// Copyright (c) 2016-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -10,7 +10,7 @@ class CRPCCommand; namespace wallet { -Span GetWalletRPCCommands(); +std::span GetWalletRPCCommands(); } // namespace wallet #endif // BITCOIN_WALLET_RPC_WALLET_H diff --git a/src/wallet/salvage.cpp b/src/wallet/salvage.cpp index b924239073c..30a2dd6dea6 100644 --- a/src/wallet/salvage.cpp +++ b/src/wallet/salvage.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2021 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -33,14 +33,14 @@ private: bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override { return true; } bool EraseKey(DataStream&& key) override { return true; } bool HasKey(DataStream&& key) override { return true; } - bool ErasePrefix(Span prefix) override { return true; } + bool ErasePrefix(std::span prefix) override { return true; } public: void Flush() override {} void Close() override {} std::unique_ptr GetNewCursor() override { return std::make_unique(); } - std::unique_ptr GetNewPrefixCursor(Span prefix) override { return GetNewCursor(); } + std::unique_ptr GetNewPrefixCursor(std::span prefix) override { return GetNewCursor(); } bool TxnBegin() override { return true; } bool TxnCommit() override { return true; } bool TxnAbort() override { return true; } diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp index a8c9f8a8ab6..c202337d57c 100644 --- a/src/wallet/sqlite.cpp +++ b/src/wallet/sqlite.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -26,7 +26,7 @@ namespace wallet { static constexpr int32_t WALLET_SCHEMA_VERSION = 0; -static Span SpanFromBlob(sqlite3_stmt* stmt, int col) +static std::span SpanFromBlob(sqlite3_stmt* stmt, int col) { return {reinterpret_cast(sqlite3_column_blob(stmt, col)), static_cast(sqlite3_column_bytes(stmt, col))}; @@ -60,7 +60,7 @@ static int TraceSqlCallback(unsigned code, void* context, void* param1, void* pa static bool BindBlobToStatement(sqlite3_stmt* stmt, int index, - Span blob, + std::span blob, const std::string& description) { // Pass a pointer to the empty string "" below instead of passing the @@ -513,7 +513,7 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite) return res == SQLITE_DONE; } -bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, Span blob) +bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span blob) { if (!m_database.m_db) return false; assert(stmt); @@ -542,7 +542,7 @@ bool SQLiteBatch::EraseKey(DataStream&& key) return ExecStatement(m_delete_stmt, key); } -bool SQLiteBatch::ErasePrefix(Span prefix) +bool SQLiteBatch::ErasePrefix(std::span prefix) { return ExecStatement(m_delete_prefix_stmt, prefix); } @@ -606,7 +606,7 @@ std::unique_ptr SQLiteBatch::GetNewCursor() return cursor; } -std::unique_ptr SQLiteBatch::GetNewPrefixCursor(Span prefix) +std::unique_ptr SQLiteBatch::GetNewPrefixCursor(std::span prefix) { if (!m_database.m_db) return nullptr; diff --git a/src/wallet/sqlite.h b/src/wallet/sqlite.h index 78a3accf890..046875ec56c 100644 --- a/src/wallet/sqlite.h +++ b/src/wallet/sqlite.h @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 The Bitcoin Core developers +// Copyright (c) 2020-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -71,13 +71,13 @@ private: bool m_txn{false}; void SetupSQLStatements(); - bool ExecStatement(sqlite3_stmt* stmt, Span blob); + bool ExecStatement(sqlite3_stmt* stmt, std::span blob); bool ReadKey(DataStream&& key, DataStream& value) override; bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override; bool EraseKey(DataStream&& key) override; bool HasKey(DataStream&& key) override; - bool ErasePrefix(Span prefix) override; + bool ErasePrefix(std::span prefix) override; public: explicit SQLiteBatch(SQLiteDatabase& database); @@ -91,7 +91,7 @@ public: void Close() override; std::unique_ptr GetNewCursor() override; - std::unique_ptr GetNewPrefixCursor(Span prefix) override; + std::unique_ptr GetNewPrefixCursor(std::span prefix) override; bool TxnBegin() override; bool TxnCommit() override; bool TxnAbort() override; diff --git a/src/wallet/test/db_tests.cpp b/src/wallet/test/db_tests.cpp index 65f4a8e5f97..fc061b984a6 100644 --- a/src/wallet/test/db_tests.cpp +++ b/src/wallet/test/db_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2018-2022 The Bitcoin Core developers +// Copyright (c) 2018-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -24,7 +24,7 @@ inline std::ostream& operator<<(std::ostream& os, const std::pair& kv) { - Span key{kv.first}, value{kv.second}; + std::span key{kv.first}, value{kv.second}; os << "(\"" << std::string_view{reinterpret_cast(key.data()), key.size()} << "\", \"" << std::string_view{reinterpret_cast(value.data()), value.size()} << "\")"; return os; @@ -43,7 +43,7 @@ static SerializeData StringData(std::string_view str) return SerializeData{bytes.begin(), bytes.end()}; } -static void CheckPrefix(DatabaseBatch& batch, Span prefix, MockableData expected) +static void CheckPrefix(DatabaseBatch& batch, std::span prefix, MockableData expected) { std::unique_ptr cursor = batch.GetNewPrefixCursor(prefix); MockableData actual; @@ -212,7 +212,7 @@ BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test) } else { // Write elements to it if not berkeleyro for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) { - batch->Write(Span{k}, Span{v}); + batch->Write(std::span{k}, std::span{v}); } } diff --git a/src/wallet/test/fuzz/wallet_bdb_parser.cpp b/src/wallet/test/fuzz/wallet_bdb_parser.cpp index 6482b65d064..c9e9ed95b50 100644 --- a/src/wallet/test/fuzz/wallet_bdb_parser.cpp +++ b/src/wallet/test/fuzz/wallet_bdb_parser.cpp @@ -44,7 +44,7 @@ FUZZ_TARGET(wallet_bdb_parser, .init = initialize_wallet_bdb_parser) { AutoFile outfile{fsbridge::fopen(wallet_path, "wb")}; - outfile << Span{buffer}; + outfile << std::span{buffer}; } const DatabaseOptions options{}; diff --git a/src/wallet/test/util.cpp b/src/wallet/test/util.cpp index bc53510fe49..8cf58800514 100644 --- a/src/wallet/test/util.cpp +++ b/src/wallet/test/util.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2022 The Bitcoin Core developers +// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -94,7 +94,7 @@ CTxDestination getNewDestination(CWallet& w, OutputType output_type) return *Assert(w.GetNewDestination(output_type, "")); } -MockableCursor::MockableCursor(const MockableData& records, bool pass, Span prefix) +MockableCursor::MockableCursor(const MockableData& records, bool pass, std::span prefix) { m_pass = pass; std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix}); @@ -166,7 +166,7 @@ bool MockableBatch::HasKey(DataStream&& key) return m_records.count(key_data) > 0; } -bool MockableBatch::ErasePrefix(Span prefix) +bool MockableBatch::ErasePrefix(std::span prefix) { if (!m_pass) { return false; diff --git a/src/wallet/test/util.h b/src/wallet/test/util.h index 801dbacaf19..3acaba0202a 100644 --- a/src/wallet/test/util.h +++ b/src/wallet/test/util.h @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2022 The Bitcoin Core developers +// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -59,7 +59,7 @@ public: bool m_pass; explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {} - MockableCursor(const MockableData& records, bool pass, Span prefix); + MockableCursor(const MockableData& records, bool pass, std::span prefix); ~MockableCursor() = default; Status Next(DataStream& key, DataStream& value) override; @@ -75,7 +75,7 @@ private: bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override; bool EraseKey(DataStream&& key) override; bool HasKey(DataStream&& key) override; - bool ErasePrefix(Span prefix) override; + bool ErasePrefix(std::span prefix) override; public: explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {} @@ -88,7 +88,7 @@ public: { return std::make_unique(m_records, m_pass); } - std::unique_ptr GetNewPrefixCursor(Span prefix) override { + std::unique_ptr GetNewPrefixCursor(std::span prefix) override { return std::make_unique(m_records, m_pass, prefix); } bool TxnBegin() override { return m_pass; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 09eda0c28e4..4b22e3185d6 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -4065,7 +4065,7 @@ bool CWallet::MigrateToSQLite(bilingual_str& error) bool began = batch->TxnBegin(); assert(began); // This is a critical error, the new db could not be written to. The original db exists as a backup, but we should not continue execution. for (const auto& [key, value] : records) { - if (!batch->Write(Span{key}, Span{value})) { + if (!batch->Write(std::span{key}, std::span{value})) { batch->TxnAbort(); m_database->Close(); fs::remove(m_database->Filename());