From 11daf6ceb1d9ea1f8d638b123eecfe39d162a7c3 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 2 Nov 2021 10:07:46 -0400 Subject: [PATCH] More Span simplifications Based on suggestions by MarcoFalke --- src/net.cpp | 5 +++-- src/netaddress.cpp | 2 +- src/pubkey.h | 2 +- src/rpc/util.cpp | 2 +- src/script/descriptor.cpp | 2 +- src/script/interpreter.cpp | 6 +++--- src/signet.cpp | 2 +- src/span.h | 4 ++-- src/test/fuzz/asmap.cpp | 2 +- src/test/fuzz/utxo_snapshot.cpp | 2 +- src/test/key_io_tests.cpp | 2 +- src/test/util_tests.cpp | 6 ++---- src/wallet/rpcdump.cpp | 2 +- src/wallet/rpcwallet.cpp | 2 +- 14 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index db496c21856..f8b73cdc6ba 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -760,7 +760,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const std::chrono::microseconds if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) { LogPrint(BCLog::NET, "Header error: Wrong checksum (%s, %u bytes), expected %s was %s, peer=%d\n", SanitizeString(msg.m_command), msg.m_message_size, - HexStr(Span(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)), + HexStr(Span{hash}.first(CMessageHeader::CHECKSUM_SIZE)), HexStr(hdr.pchChecksum), m_node_id); reject_message = true; @@ -1582,8 +1582,9 @@ void CConnman::SocketHandlerConnected(const std::vector& nodes, if (nBytes > 0) { bool notify = false; - if (!pnode->ReceiveMsgBytes(Span(pchBuf, nBytes), notify)) + if (!pnode->ReceiveMsgBytes({pchBuf, (size_t)nBytes}, notify)) { pnode->CloseSocketDisconnect(); + } RecordBytesRecv(nBytes); if (notify) { size_t nSizeAdded = 0; diff --git a/src/netaddress.cpp b/src/netaddress.cpp index eb67445baf0..f6bc7fbb944 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -303,7 +303,7 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr) CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope) { - SetLegacyIPv6(Span(reinterpret_cast(&ipv6Addr), sizeof(ipv6Addr))); + SetLegacyIPv6({reinterpret_cast(&ipv6Addr), sizeof(ipv6Addr)}); m_scope_id = scope; } diff --git a/src/pubkey.h b/src/pubkey.h index cba104a5e54..2453c92d92d 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -240,7 +240,7 @@ public: explicit XOnlyPubKey(Span bytes); /** Construct an x-only pubkey from a normal pubkey. */ - explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span(pubkey.begin() + 1, pubkey.begin() + 33)) {} + explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span{pubkey}.subspan(1, 32)) {} /** Verify a Schnorr signature against this public key. * diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 2d7f5f2894f..683cf88af80 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -317,7 +317,7 @@ public: UniValue obj(UniValue::VOBJ); obj.pushKV("iswitness", true); obj.pushKV("witness_version", (int)id.version); - obj.pushKV("witness_program", HexStr(Span(id.program, id.length))); + obj.pushKV("witness_program", HexStr({id.program, id.length})); return obj; } }; diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 1c99df147b0..30f929664f1 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1252,7 +1252,7 @@ std::unique_ptr InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseS 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.data() + 1, script.data() + 33}}; + XOnlyPubKey key{Span{script}.subspan(1, 32)}; return std::make_unique(InferXOnlyPubkey(key, ctx, provider)); } diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index d83ec7192b4..6433ba1b58f 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1858,7 +1858,7 @@ uint256 ComputeTaprootMerkleRoot(Span control, const uint25 uint256 k = tapleaf_hash; for (int i = 0; i < path_len; ++i) { CHashWriter ss_branch{HASHER_TAPBRANCH}; - Span node(control.data() + TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE); + Span node{Span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)}; if (std::lexicographical_compare(k.begin(), k.end(), node.begin(), node.end())) { ss_branch << k << node; } else { @@ -1874,7 +1874,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.data() + 1, control.data() + TAPROOT_CONTROL_BASE_SIZE}}; + const XOnlyPubKey p{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. @@ -1886,7 +1886,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}; + Span stack{witness.stack}; ScriptExecutionData execdata; if (witversion == 0) { diff --git a/src/signet.cpp b/src/signet.cpp index aafd1999eeb..40d6ae2f3c3 100644 --- a/src/signet.cpp +++ b/src/signet.cpp @@ -38,7 +38,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() > (size_t) header.size() && Span(pushdata.data(), header.size()) == header) { + if (!found_header && pushdata.size() > (size_t)header.size() && 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 ce5b7ca3160..6703e889c5d 100644 --- a/src/span.h +++ b/src/span.h @@ -258,12 +258,12 @@ Span AsWritableBytes(Span s) noexcept template Span MakeByteSpan(V&& v) noexcept { - return AsBytes(MakeSpan(std::forward(v))); + return AsBytes(Span{std::forward(v)}); } template Span MakeWritableByteSpan(V&& v) noexcept { - return AsWritableBytes(MakeSpan(std::forward(v))); + return AsWritableBytes(Span{std::forward(v)}); } // Helper functions to safely cast to unsigned char pointers. diff --git a/src/test/fuzz/asmap.cpp b/src/test/fuzz/asmap.cpp index d402f8632ca..c5e9c560498 100644 --- a/src/test/fuzz/asmap.cpp +++ b/src/test/fuzz/asmap.cpp @@ -49,7 +49,7 @@ FUZZ_TARGET(asmap) CNetAddr net_addr; if (ipv6) { assert(addr_size == ADDR_IPV6_SIZE); - net_addr.SetLegacyIPv6(Span(addr_data, addr_size)); + net_addr.SetLegacyIPv6({addr_data, addr_size}); } else { assert(addr_size == ADDR_IPV4_SIZE); in_addr ipv4; diff --git a/src/test/fuzz/utxo_snapshot.cpp b/src/test/fuzz/utxo_snapshot.cpp index 8d2a06f11ae..d625403fa0a 100644 --- a/src/test/fuzz/utxo_snapshot.cpp +++ b/src/test/fuzz/utxo_snapshot.cpp @@ -38,7 +38,7 @@ FUZZ_TARGET_INIT(utxo_snapshot, initialize_chain) { CAutoFile outfile{fsbridge::fopen(snapshot_path, "wb"), SER_DISK, CLIENT_VERSION}; const auto file_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)}; - outfile << Span{file_data}; + outfile << Span{file_data}; } const auto ActivateFuzzedSnapshot{[&] { diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 0361618c824..02268dbcf5c 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse) privkey = DecodeSecret(exp_base58string); BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest); - BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest); + BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest); // Private key must be invalid public key destination = DecodeDestination(exp_base58string); diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 76a690fd286..9540cead24b 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -142,13 +142,11 @@ BOOST_AUTO_TEST_CASE(util_HexStr) "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); BOOST_CHECK_EQUAL( - HexStr(Span( - ParseHex_expected + sizeof(ParseHex_expected), - ParseHex_expected + sizeof(ParseHex_expected))), + HexStr(Span{ParseHex_expected}.last(0)), ""); BOOST_CHECK_EQUAL( - HexStr(Span(ParseHex_expected, ParseHex_expected)), + HexStr(Span{ParseHex_expected}.first(0)), ""); { diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index ece75dc43f9..65459557cbf 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -35,7 +35,7 @@ std::string static EncodeDumpString(const std::string &str) { std::stringstream ret; for (const unsigned char c : str) { if (c <= 32 || c >= 128 || c == '%') { - ret << '%' << HexStr(Span(&c, 1)); + ret << '%' << HexStr({&c, 1}); } else { ret << c; } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 64433327867..171ec8ae80b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4937,5 +4937,5 @@ static const CRPCCommand commands[] = { "wallet", &walletprocesspsbt, }, }; // clang-format on - return Span{commands}; + return commands; }