Skip to content

Commit 11daf6c

Browse files
committed
More Span simplifications
Based on suggestions by MarcoFalke <[email protected]>
1 parent 568dd2f commit 11daf6c

File tree

14 files changed

+20
-21
lines changed

14 files changed

+20
-21
lines changed

src/net.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds
760760
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) {
761761
LogPrint(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n",
762762
SanitizeString(msg.m_command), msg.m_message_size,
763-
HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
763+
HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)),
764764
HexStr(hdr.pchChecksum),
765765
m_node_id);
766766
reject_message = true;
@@ -1582,8 +1582,9 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
15821582
if (nBytes > 0)
15831583
{
15841584
bool notify = false;
1585-
if (!pnode->ReceiveMsgBytes(Span<const uint8_t>(pchBuf, nBytes), notify))
1585+
if (!pnode->ReceiveMsgBytes({pchBuf, (size_t)nBytes}, notify)) {
15861586
pnode->CloseSocketDisconnect();
1587+
}
15871588
RecordBytesRecv(nBytes);
15881589
if (notify) {
15891590
size_t nSizeAdded = 0;

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
303303

304304
CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
305305
{
306-
SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
306+
SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
307307
m_scope_id = scope;
308308
}
309309

src/pubkey.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class XOnlyPubKey
240240
explicit XOnlyPubKey(Span<const unsigned char> bytes);
241241

242242
/** Construct an x-only pubkey from a normal pubkey. */
243-
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span<const unsigned char>(pubkey.begin() + 1, pubkey.begin() + 33)) {}
243+
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span{pubkey}.subspan(1, 32)) {}
244244

245245
/** Verify a Schnorr signature against this public key.
246246
*

src/rpc/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class DescribeAddressVisitor
317317
UniValue obj(UniValue::VOBJ);
318318
obj.pushKV("iswitness", true);
319319
obj.pushKV("witness_version", (int)id.version);
320-
obj.pushKV("witness_program", HexStr(Span<const unsigned char>(id.program, id.length)));
320+
obj.pushKV("witness_program", HexStr({id.program, id.length}));
321321
return obj;
322322
}
323323
};

src/script/descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ std::unique_ptr<PubkeyProvider> InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseS
12521252
std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
12531253
{
12541254
if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) {
1255-
XOnlyPubKey key{Span<const unsigned char>{script.data() + 1, script.data() + 33}};
1255+
XOnlyPubKey key{Span{script}.subspan(1, 32)};
12561256
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider));
12571257
}
12581258

src/script/interpreter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ uint256 ComputeTaprootMerkleRoot(Span<const unsigned char> control, const uint25
18581858
uint256 k = tapleaf_hash;
18591859
for (int i = 0; i < path_len; ++i) {
18601860
CHashWriter ss_branch{HASHER_TAPBRANCH};
1861-
Span<const unsigned char> node(control.data() + TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE);
1861+
Span node{Span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)};
18621862
if (std::lexicographical_compare(k.begin(), k.end(), node.begin(), node.end())) {
18631863
ss_branch << k << node;
18641864
} else {
@@ -1874,7 +1874,7 @@ static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, c
18741874
assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE);
18751875
assert(program.size() >= uint256::size());
18761876
//! The internal pubkey (x-only, so no Y coordinate parity).
1877-
const XOnlyPubKey p{Span<const unsigned char>{control.data() + 1, control.data() + TAPROOT_CONTROL_BASE_SIZE}};
1877+
const XOnlyPubKey p{Span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)};
18781878
//! The output pubkey (taken from the scriptPubKey).
18791879
const XOnlyPubKey q{program};
18801880
// Compute the Merkle root from the leaf and the provided path.
@@ -1886,7 +1886,7 @@ static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, c
18861886
static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion, const std::vector<unsigned char>& program, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror, bool is_p2sh)
18871887
{
18881888
CScript exec_script; //!< Actually executed script (last stack item in P2WSH; implied P2PKH script in P2WPKH; leaf script in P2TR)
1889-
Span<const valtype> stack{witness.stack};
1889+
Span stack{witness.stack};
18901890
ScriptExecutionData execdata;
18911891

18921892
if (witversion == 0) {

src/signet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static bool FetchAndClearCommitmentSection(const Span<const uint8_t> header, CSc
3838
std::vector<uint8_t> pushdata;
3939
while (witness_commitment.GetOp(pc, opcode, pushdata)) {
4040
if (pushdata.size() > 0) {
41-
if (!found_header && pushdata.size() > (size_t) header.size() && Span<const uint8_t>(pushdata.data(), header.size()) == header) {
41+
if (!found_header && pushdata.size() > (size_t)header.size() && Span{pushdata}.first(header.size()) == header) {
4242
// pushdata only counts if it has the header _and_ some data
4343
result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
4444
pushdata.erase(pushdata.begin() + header.size(), pushdata.end());

src/span.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ Span<std::byte> AsWritableBytes(Span<T> s) noexcept
258258
template <typename V>
259259
Span<const std::byte> MakeByteSpan(V&& v) noexcept
260260
{
261-
return AsBytes(MakeSpan(std::forward<V>(v)));
261+
return AsBytes(Span{std::forward<V>(v)});
262262
}
263263
template <typename V>
264264
Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
265265
{
266-
return AsWritableBytes(MakeSpan(std::forward<V>(v)));
266+
return AsWritableBytes(Span{std::forward<V>(v)});
267267
}
268268

269269
// Helper functions to safely cast to unsigned char pointers.

src/test/fuzz/asmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ FUZZ_TARGET(asmap)
4949
CNetAddr net_addr;
5050
if (ipv6) {
5151
assert(addr_size == ADDR_IPV6_SIZE);
52-
net_addr.SetLegacyIPv6(Span<const uint8_t>(addr_data, addr_size));
52+
net_addr.SetLegacyIPv6({addr_data, addr_size});
5353
} else {
5454
assert(addr_size == ADDR_IPV4_SIZE);
5555
in_addr ipv4;

src/test/fuzz/utxo_snapshot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain)
3838
{
3939
CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION};
4040
const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
41-
outfile << Span<const uint8_t>{file_data};
41+
outfile << Span{file_data};
4242
}
4343

4444
const auto ActivateFuzzedSnapshot{[&] {

0 commit comments

Comments
 (0)