Skip to content

Commit 8b1de78

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23413: Replace MakeSpan helper with Span deduction guide
11daf6c More Span simplifications (Pieter Wuille) 568dd2f Replace MakeSpan helper with Span deduction guide (Pieter Wuille) Pull request description: C++17 supports [user-defined deduction guides](https://en.cppreference.com/w/cpp/language/class_template_argument_deduction), allowing class constructors to be invoked without specifying class template arguments. Instead, the code can contain rules to infer the template arguments from the constructor argument types. This alleviates the need for the `MakeSpan` helper. Convert the existing MakeSpan rules into deduction rules for `Span` itself, and replace all invocations of `MakeSpan` with just `Span` ones. ACKs for top commit: MarcoFalke: re-ACK 11daf6c Only change is removing a hunk in the tests 🌕 Tree-SHA512: 10f3e82e4338f39d9b7b407cd11aac7ebe1e9191b58e3d7f4e5e338a4636c0e126b4a1d912127c7446f57ba356c8d6544482e47f97901efea6a54fffbfd7895f
2 parents fd1c9e2 + 11daf6c commit 8b1de78

25 files changed

+125
-124
lines changed

src/base58.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
149149
return false;
150150
}
151151
// re-calculate the checksum, ensure it matches the included 4-byte checksum
152-
uint256 hash = Hash(MakeSpan(vchRet).first(vchRet.size() - 4));
152+
uint256 hash = Hash(Span{vchRet}.first(vchRet.size() - 4));
153153
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
154154
vchRet.clear();
155155
return false;

src/compressor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ struct ScriptCompression
6565
void Ser(Stream &s, const CScript& script) {
6666
CompressedScript compr;
6767
if (CompressScript(script, compr)) {
68-
s << MakeSpan(compr);
68+
s << Span{compr};
6969
return;
7070
}
7171
unsigned int nSize = script.size() + nSpecialScripts;
7272
s << VARINT(nSize);
73-
s << MakeSpan(script);
73+
s << Span{script};
7474
}
7575

7676
template<typename Stream>
@@ -79,7 +79,7 @@ struct ScriptCompression
7979
s >> VARINT(nSize);
8080
if (nSize < nSpecialScripts) {
8181
CompressedScript vch(GetSpecialScriptSize(nSize), 0x00);
82-
s >> MakeSpan(vch);
82+
s >> Span{vch};
8383
DecompressScript(script, nSize, vch);
8484
return;
8585
}
@@ -90,7 +90,7 @@ struct ScriptCompression
9090
s.ignore(nSize);
9191
} else {
9292
script.resize(nSize);
93-
s >> MakeSpan(script);
93+
s >> Span{script};
9494
}
9595
}
9696
};

src/net.cpp

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

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
18151815
if (!ReadRawBlockFromDisk(block_data, pindex, m_chainparams.MessageStart())) {
18161816
assert(!"cannot load block from disk");
18171817
}
1818-
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, MakeSpan(block_data)));
1818+
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, Span{block_data}));
18191819
// Don't set pblock as we've sent the block
18201820
} else {
18211821
// Send block from disk

src/netaddress.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKS
196196

197197
SHA3_256 hasher;
198198

199-
hasher.Write(MakeSpan(prefix).first(prefix_len));
199+
hasher.Write(Span{prefix}.first(prefix_len));
200200
hasher.Write(addr_pubkey);
201201
hasher.Write(VERSION);
202202

@@ -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

@@ -693,13 +693,13 @@ uint32_t CNetAddr::GetLinkedIPv4() const
693693
return ReadBE32(m_addr.data());
694694
} else if (IsRFC6052() || IsRFC6145()) {
695695
// mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
696-
return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
696+
return ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
697697
} else if (IsRFC3964()) {
698698
// 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
699-
return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
699+
return ReadBE32(Span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
700700
} else if (IsRFC4380()) {
701701
// Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
702-
return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
702+
return ~ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
703703
}
704704
assert(false);
705705
}

src/netaddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class CNetAddr
433433

434434
if (SetNetFromBIP155Network(bip155_net, address_size)) {
435435
m_addr.resize(address_size);
436-
s >> MakeSpan(m_addr);
436+
s >> Span{m_addr};
437437

438438
if (m_net != NET_IPV6) {
439439
return;

src/policy/policy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
251251
// - No annexes
252252
if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) {
253253
// Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341)
254-
auto stack = MakeSpan(tx.vin[i].scriptWitness.stack);
254+
Span stack{tx.vin[i].scriptWitness.stack};
255255
if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
256256
// Annexes are nonstandard as long as no semantics are defined for them.
257257
return false;

src/psbt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct PSBTInput
8181
if (final_script_sig.empty() && final_script_witness.IsNull()) {
8282
// Write any partial signatures
8383
for (auto sig_pair : partial_sigs) {
84-
SerializeToVector(s, PSBT_IN_PARTIAL_SIG, MakeSpan(sig_pair.second.first));
84+
SerializeToVector(s, PSBT_IN_PARTIAL_SIG, Span{sig_pair.second.first});
8585
s << sig_pair.second.second;
8686
}
8787

src/pubkey.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ class CPubKey
157157
//! Get the KeyID of this public key (hash of its serialization)
158158
CKeyID GetID() const
159159
{
160-
return CKeyID(Hash160(MakeSpan(vch).first(size())));
160+
return CKeyID(Hash160(Span{vch}.first(size())));
161161
}
162162

163163
//! Get the 256-bit hash of this public key.
164164
uint256 GetHash() const
165165
{
166-
return Hash(MakeSpan(vch).first(size()));
166+
return Hash(Span{vch}.first(size()));
167167
}
168168

169169
/*
@@ -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
};

0 commit comments

Comments
 (0)