Skip to content

Commit e6f1424

Browse files
committed
Merge bitcoin/bitcoin#31540: refactor: std::span compat fixes
fa494a1 refactor: Specify const in std::span constructor, where needed (MarcoFalke) faaf480 Allow std::span in stream serialization (MarcoFalke) faa5391 refactor: test: Return std::span from StringBytes (MarcoFalke) fa86223 refactor: Avoid passing span iterators when data pointers are expected (MarcoFalke) faae6fa refactor: Simplify SpanPopBack (MarcoFalke) facc4f1 refactor: Replace fwd-decl with proper include (MarcoFalke) fac3a78 refactor: Avoid needless, unsafe c-style cast (MarcoFalke) Pull request description: The `std::span` type is already used in some parts of the codebase, and in most contexts can implicitly convert to and from `Span`. However, the two types are not identical in behavior and trying to use one over the other can result in compile failures in some contexts. Fix all those issues by allowing either `Span` or `std::span` in any part of the codebase. All of the changes are also required for the scripted-diff to replace `Span` with `std::span` in bitcoin/bitcoin#31519 ACKs for top commit: sipa: utACK fa494a1 fjahr: Code review ACK fa494a1 achow101: ACK fa494a1 theuni: utACK fa494a1. adamandrews1: utACK bitcoin/bitcoin@fa494a1 Tree-SHA512: 9440941823e884ff5d7ac161f58b9a0704d8e803b4c91c400bdb5f58f898e4637d63ae627cfc7330e98a721fc38285a04641175aa18d991bd35f8b69ed1d74c4
2 parents a137b0b + fa494a1 commit e6f1424

File tree

11 files changed

+17
-20
lines changed

11 files changed

+17
-20
lines changed

src/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
139139
// add 4-byte hash check to the end
140140
std::vector<unsigned char> vch(input.begin(), input.end());
141141
uint256 hash = Hash(vch);
142-
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
142+
vch.insert(vch.end(), hash.data(), hash.data() + 4);
143143
return EncodeBase58(vch);
144144
}
145145

src/script/miniscript.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
17981798
// Get threshold
17991799
int next_comma = FindNextChar(in, ',');
18001800
if (next_comma < 1) return false;
1801-
const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.begin(), next_comma))};
1801+
const auto k_to_integral{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
18021802
if (!k_to_integral.has_value()) return false;
18031803
const int64_t k{k_to_integral.value()};
18041804
in = in.subspan(next_comma + 1);
@@ -1954,15 +1954,15 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
19541954
} else if (Const("after(", in)) {
19551955
int arg_size = FindNextChar(in, ')');
19561956
if (arg_size < 1) return {};
1957-
const auto num{ToIntegral<int64_t>(std::string_view(in.begin(), arg_size))};
1957+
const auto num{ToIntegral<int64_t>(std::string_view(in.data(), arg_size))};
19581958
if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
19591959
constructed.push_back(MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::AFTER, *num));
19601960
in = in.subspan(arg_size + 1);
19611961
script_size += 1 + (*num > 16) + (*num > 0x7f) + (*num > 0x7fff) + (*num > 0x7fffff);
19621962
} else if (Const("older(", in)) {
19631963
int arg_size = FindNextChar(in, ')');
19641964
if (arg_size < 1) return {};
1965-
const auto num{ToIntegral<int64_t>(std::string_view(in.begin(), arg_size))};
1965+
const auto num{ToIntegral<int64_t>(std::string_view(in.data(), arg_size))};
19661966
if (!num.has_value() || *num < 1 || *num >= 0x80000000L) return {};
19671967
constructed.push_back(MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext(), Fragment::OLDER, *num));
19681968
in = in.subspan(arg_size + 1);
@@ -1974,7 +1974,7 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
19741974
} else if (Const("thresh(", in)) {
19751975
int next_comma = FindNextChar(in, ',');
19761976
if (next_comma < 1) return {};
1977-
const auto k{ToIntegral<int64_t>(std::string_view(in.begin(), next_comma))};
1977+
const auto k{ToIntegral<int64_t>(std::string_view(in.data(), next_comma))};
19781978
if (!k.has_value() || *k < 1) return {};
19791979
in = in.subspan(next_comma + 1);
19801980
// n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH

src/script/solver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
#include <attributes.h>
1212
#include <script/script.h>
13+
#include <span.h>
1314

1415
#include <string>
1516
#include <optional>
1617
#include <utility>
1718
#include <vector>
1819

1920
class CPubKey;
20-
template <typename C> class Span;
2121

2222
enum class TxoutType {
2323
NONSTANDARD,

src/serialize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri
264264
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
265265
template <typename Stream, BasicByte B, int N> void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); }
266266
template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
267+
template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, std::span<B, N> span) { s.write(std::as_bytes(span)); }
267268
template <typename Stream, BasicByte B> void Serialize(Stream& s, Span<B> span) { s.write(AsBytes(span)); }
268269

269270
template <typename Stream, CharNotInt8 V> void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t
@@ -278,6 +279,7 @@ template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a =
278279
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
279280
template <typename Stream, BasicByte B, int N> void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
280281
template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
282+
template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::span<B, N> span) { s.read(std::as_writable_bytes(span)); }
281283
template <typename Stream, BasicByte B> void Unserialize(Stream& s, Span<B> span) { s.read(AsWritableBytes(span)); }
282284

283285
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }

src/span.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ template <typename T>
248248
T& SpanPopBack(Span<T>& span)
249249
{
250250
size_t size = span.size();
251-
ASSERT_IF_DEBUG(size > 0);
252-
T& back = span[size - 1];
253-
span = Span<T>(span.data(), size - 1);
251+
T& back = span.back();
252+
span = span.first(size - 1);
254253
return back;
255254
}
256255

src/streams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class VectorWriter
7979
memcpy(vchData.data() + nPos, src.data(), nOverwrite);
8080
}
8181
if (nOverwrite < src.size()) {
82-
vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.end()));
82+
vchData.insert(vchData.end(), UCharCast(src.data()) + nOverwrite, UCharCast(src.data() + src.size()));
8383
}
8484
nPos += src.size();
8585
}

src/test/script_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,9 +1700,8 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
17001700
BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str());
17011701

17021702
// To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash.
1703-
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << Span{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
1703+
BOOST_CHECK_EQUAL(HexStr((HashWriter{HASHER_TAPSIGHASH} << std::span<const uint8_t>{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
17041704
}
1705-
17061705
}
17071706
}
17081707

src/test/serialize_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class Base
304304
if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
305305
s << m_base_data;
306306
} else {
307-
s << Span{HexStr(Span{&m_base_data, 1})};
307+
s << std::span<const char>{HexStr(Span{&m_base_data, 1})};
308308
}
309309
}
310310

src/test/util/net.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <netaddress.h>
1313
#include <node/connection_types.h>
1414
#include <node/eviction.h>
15+
#include <span.h>
1516
#include <sync.h>
1617
#include <util/sock.h>
1718

@@ -28,9 +29,6 @@
2829

2930
class FastRandomContext;
3031

31-
template <typename C>
32-
class Span;
33-
3432
struct ConnmanTestMsg : public CConnman {
3533
using CConnman::CConnman;
3634

src/util/hasher.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
#include <crypto/common.h>
99
#include <crypto/siphash.h>
1010
#include <primitives/transaction.h>
11+
#include <span.h>
1112
#include <uint256.h>
1213

1314
#include <cstdint>
1415
#include <cstring>
1516

16-
template <typename C> class Span;
17-
1817
class SaltedTxidHasher
1918
{
2019
private:

0 commit comments

Comments
 (0)