Skip to content

Commit 50bc017

Browse files
committed
refactor: Hand-replace some ParseHex -> ""_hex
The following scripted-diff commit will replace ParseHex("...") with "..."_hex_u8, but this replacement will not work in cases where vectors are needed instead of arrays, and is not ideal in cases where std::byte is accepted. For example, it is currently necessary to use _hex_v_u8 when calling CScript operator<< because that operator does not currently support std::array or std::byte. Conversely, it is incorrect to use _hex_v instead of _hex in net_processing.cpp for the MakeAndPushMessage argument, because if the argument is a std::vector it is considered variable-length and serialized with a size prefix, but if the argument is a std::array or Span is it considered fixed length and serialized without a prefix. By the same logic, it is also safe to change the NUMS_H constant in pubkey.cpp from a std::vector to std::array because it is never serialized.
1 parent 5b74a84 commit 50bc017

20 files changed

+166
-153
lines changed

src/bench/bech32.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
#include <vector>
1010

11+
using namespace util::hex_literals;
1112

1213
static void Bech32Encode(benchmark::Bench& bench)
1314
{
14-
std::vector<uint8_t> v = ParseHex("c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db");
15+
constexpr std::array<uint8_t, 32> v{"c97f5a67ec381b760aeaf67573bc164845ff39a3bb26a1cee401ac67243b48db"_hex_u8};
1516
std::vector<unsigned char> tmp = {0};
16-
tmp.reserve(1 + 32 * 8 / 5);
17+
tmp.reserve(1 + v.size() * 8 / 5);
1718
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, v.begin(), v.end());
1819
bench.batch(v.size()).unit("byte").run([&] {
1920
bech32::Encode(bech32::Encoding::BECH32, "bc", tmp);

src/kernel/chainparams.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <cstring>
2727
#include <type_traits>
2828

29+
using namespace util::hex_literals;
30+
2931
// Workaround MSVC bug triggering C7595 when calling consteval constructors in
3032
// initializer lists.
3133
// A fix may be on the way:
@@ -71,7 +73,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
7173
static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
7274
{
7375
const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
74-
const CScript genesisOutputScript = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
76+
const CScript genesisOutputScript = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex_v_u8 << OP_CHECKSIG;
7577
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
7678
}
7779

@@ -351,7 +353,7 @@ class CTestNet4Params : public CChainParams {
351353
m_assumed_chain_state_size = 0;
352354

353355
const char* testnet4_genesis_msg = "03/May/2024 000000000000000000001ebd58c244970b3aa9d783bb001011fbe8ea8e98e00e";
354-
const CScript testnet4_genesis_script = CScript() << ParseHex("000000000000000000000000000000000000000000000000000000000000000000") << OP_CHECKSIG;
356+
const CScript testnet4_genesis_script = CScript() << "000000000000000000000000000000000000000000000000000000000000000000"_hex_v_u8 << OP_CHECKSIG;
355357
genesis = CreateGenesisBlock(testnet4_genesis_msg,
356358
testnet4_genesis_script,
357359
1714777860,
@@ -412,7 +414,7 @@ class SigNetParams : public CChainParams {
412414
vSeeds.clear();
413415

414416
if (!options.challenge) {
415-
bin = ParseHex("512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae");
417+
bin = "512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae"_hex_v_u8;
416418
vSeeds.emplace_back("seed.signet.bitcoin.sprovoost.nl.");
417419
vSeeds.emplace_back("seed.signet.achownodes.xyz."); // Ava Chow, only supports x1, x5, x9, x49, x809, x849, xd, x400, x404, x408, x448, xc08, xc48, x40c
418420

src/net_processing.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include <typeinfo>
5555
#include <utility>
5656

57+
using namespace util::hex_literals;
58+
5759
/** Headers download timeout.
5860
* Timeout = base + per_header * (expected number of headers) */
5961
static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
@@ -3931,8 +3933,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
39313933

39323934
// If the peer is old enough to have the old alert system, send it the final alert.
39333935
if (greatest_common_version <= 70012) {
3934-
const auto finalAlert{ParseHex("60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50")};
3935-
MakeAndPushMessage(pfrom, "alert", Span{finalAlert});
3936+
constexpr auto finalAlert{"60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"_hex};
3937+
MakeAndPushMessage(pfrom, "alert", finalAlert);
39363938
}
39373939

39383940
// Feeler connections exist only to verify if address is online.

src/pubkey.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <algorithm>
1919
#include <cassert>
2020

21+
using namespace util::hex_literals;
22+
2123
namespace {
2224

2325
struct Secp256k1SelfTester
@@ -190,8 +192,7 @@ int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned
190192
* For an example script for calculating H, refer to the unit tests in
191193
* ./test/functional/test_framework/crypto/secp256k1.py
192194
*/
193-
static const std::vector<unsigned char> NUMS_H_DATA{ParseHex("50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")};
194-
const XOnlyPubKey XOnlyPubKey::NUMS_H{NUMS_H_DATA};
195+
constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8};
195196

196197
std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
197198
{

src/test/base58_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <string>
1818

1919
using namespace std::literals;
20+
using namespace util::hex_literals;
2021

2122
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
2223

@@ -72,7 +73,7 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
7273
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
7374
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
7475
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
75-
std::vector<unsigned char> expected = ParseHex("971a55");
76+
constexpr auto expected{"971a55"_hex_u8};
7677
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
7778

7879
BOOST_CHECK(DecodeBase58Check("3vQB7B6MrGQZaxCuFg4oh"s, result, 100));

0 commit comments

Comments
 (0)