aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@protonmail.com>2020-06-24 17:26:47 +0200
committerWladimir J. van der Laan <laanwj@protonmail.com>2020-08-06 19:41:43 +0200
commit0a8aa626dd69a357e1b798b07b64cf4177a464a3 (patch)
tree44736ce734e8b6b2259becff3962ee3290f7969d
parent34eb2362581d4d8f0bfd3baa12ba750afaf85c62 (diff)
downloadbitcoin-0a8aa626dd69a357e1b798b07b64cf4177a464a3.tar.xz
refactor: Make HexStr take a span
Make HexStr take a span of bytes, instead of an awkward pair of templated iterators.
-rw-r--r--src/core_write.cpp7
-rw-r--r--src/net.cpp4
-rw-r--r--src/rpc/rawtransaction.cpp2
-rw-r--r--src/rpc/request.cpp2
-rw-r--r--src/rpc/util.cpp2
-rw-r--r--src/script/descriptor.cpp2
-rw-r--r--src/test/crypto_tests.cpp2
-rw-r--r--src/test/settings_tests.cpp2
-rw-r--r--src/test/util_tests.cpp41
-rw-r--r--src/uint256.cpp6
-rw-r--r--src/util/strencodings.cpp13
-rw-r--r--src/util/strencodings.h27
-rw-r--r--src/validation.cpp4
-rw-r--r--src/wallet/bdb.cpp2
-rw-r--r--src/wallet/rpcdump.cpp2
-rw-r--r--src/wallet/rpcwallet.cpp2
16 files changed, 50 insertions, 70 deletions
diff --git a/src/core_write.cpp b/src/core_write.cpp
index 34cfeecc6f..f9d918cb6d 100644
--- a/src/core_write.cpp
+++ b/src/core_write.cpp
@@ -48,13 +48,14 @@ std::string FormatScript(const CScript& script)
}
}
if (vch.size() > 0) {
- ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
+ ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
+ HexStr(std::vector<uint8_t>(it - vch.size(), it)));
} else {
- ret += strprintf("0x%x ", HexStr(it2, it));
+ ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
}
continue;
}
- ret += strprintf("0x%x ", HexStr(it2, script.end()));
+ ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
break;
}
return ret.substr(0, ret.size() - 1);
diff --git a/src/net.cpp b/src/net.cpp
index 9c72c62df9..25fa4709b0 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -722,8 +722,8 @@ CNetMessage V1TransportDeserializer::GetMessage(const CMessageHeader::MessageSta
if (!msg.m_valid_checksum) {
LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s\n",
SanitizeString(msg.m_command), msg.m_message_size,
- HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
- HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
+ HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
+ HexStr(hdr.pchChecksum));
}
// store receive time
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index cf856af6e9..9b6ef15785 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -1348,7 +1348,7 @@ UniValue finalizepsbt(const JSONRPCRequest& request)
if (complete && extract) {
ssTx << mtx;
- result_str = HexStr(ssTx.str());
+ result_str = HexStr(ssTx);
result.pushKV("hex", result_str);
} else {
ssTx << psbtx;
diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp
index 7fef45f50e..d9ad70fa37 100644
--- a/src/rpc/request.cpp
+++ b/src/rpc/request.cpp
@@ -78,7 +78,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
const size_t COOKIE_SIZE = 32;
unsigned char rand_pwd[COOKIE_SIZE];
GetRandBytes(rand_pwd, COOKIE_SIZE);
- std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
+ std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
/** the umask determines what permissions are used to create this file -
* these are set to 077 in init.cpp unless overridden with -sysperms.
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 9f4c7bee9c..073a7688a9 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -260,7 +260,7 @@ public:
UniValue obj(UniValue::VOBJ);
obj.pushKV("iswitness", true);
obj.pushKV("witness_version", (int)id.version);
- obj.pushKV("witness_program", HexStr(id.program, id.program + id.length));
+ obj.pushKV("witness_program", HexStr(Span<const unsigned char>(id.program, id.length)));
return obj;
}
};
diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp
index 9978d084d5..6c0a98cca2 100644
--- a/src/script/descriptor.cpp
+++ b/src/script/descriptor.cpp
@@ -190,7 +190,7 @@ class OriginPubkeyProvider final : public PubkeyProvider
std::string OriginString() const
{
- return HexStr(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint)) + FormatHDKeypath(m_origin.path);
+ return HexStr(m_origin.fingerprint) + FormatHDKeypath(m_origin.path);
}
public:
diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp
index bf5c587774..b3cc8cefd9 100644
--- a/src/test/crypto_tests.cpp
+++ b/src/test/crypto_tests.cpp
@@ -183,7 +183,7 @@ static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &sa
CHKDF_HMAC_SHA256_L32 hkdf32(initial_key_material.data(), initial_key_material.size(), salt_stringified);
unsigned char out[32];
hkdf32.Expand32(info_stringified, out);
- BOOST_CHECK(HexStr(out, out + 32) == okm_check_hex);
+ BOOST_CHECK(HexStr(out) == okm_check_hex);
}
static std::string LongTestString()
diff --git a/src/test/settings_tests.cpp b/src/test/settings_tests.cpp
index 91e039416c..548fd020a6 100644
--- a/src/test/settings_tests.cpp
+++ b/src/test/settings_tests.cpp
@@ -241,7 +241,7 @@ BOOST_FIXTURE_TEST_CASE(Merge, MergeTestingSetup)
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
out_sha.Finalize(out_sha_bytes);
- std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
+ std::string out_sha_hex = HexStr(out_sha_bytes);
// If check below fails, should manually dump the results with:
//
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 15a2c1e300..b49370c967 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -105,47 +105,24 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
BOOST_AUTO_TEST_CASE(util_HexStr)
{
BOOST_CHECK_EQUAL(
- HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
+ HexStr(ParseHex_expected),
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
BOOST_CHECK_EQUAL(
- HexStr(ParseHex_expected + sizeof(ParseHex_expected),
- ParseHex_expected + sizeof(ParseHex_expected)),
+ HexStr(Span<const unsigned char>(
+ ParseHex_expected + sizeof(ParseHex_expected),
+ ParseHex_expected + sizeof(ParseHex_expected))),
"");
BOOST_CHECK_EQUAL(
- HexStr(ParseHex_expected, ParseHex_expected),
+ HexStr(Span<const unsigned char>(ParseHex_expected, ParseHex_expected)),
"");
std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
BOOST_CHECK_EQUAL(
- HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend()),
- "b0fd8a6704"
- );
-
- BOOST_CHECK_EQUAL(
- HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
- std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
- ""
- );
-
- BOOST_CHECK_EQUAL(
- HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
- std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
- "04"
- );
-
- BOOST_CHECK_EQUAL(
- HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
- std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
- "b0fd8a6704"
- );
-
- BOOST_CHECK_EQUAL(
- HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 65),
- std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
- "5f1df16b2b704c8a578d0bbaf74d385cde12c11ee50455f3c438ef4c3fbcf649b6de611feae06279a60939e028a8d65c10b73071a6f16719274855feb0fd8a6704"
+ HexStr(ParseHex_vec),
+ "04678afdb0"
);
}
@@ -1022,7 +999,7 @@ BOOST_FIXTURE_TEST_CASE(util_ArgsMerge, ArgsMergeTestingSetup)
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
out_sha.Finalize(out_sha_bytes);
- std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
+ std::string out_sha_hex = HexStr(out_sha_bytes);
// If check below fails, should manually dump the results with:
//
@@ -1125,7 +1102,7 @@ BOOST_FIXTURE_TEST_CASE(util_ChainMerge, ChainMergeTestingSetup)
unsigned char out_sha_bytes[CSHA256::OUTPUT_SIZE];
out_sha.Finalize(out_sha_bytes);
- std::string out_sha_hex = HexStr(std::begin(out_sha_bytes), std::end(out_sha_bytes));
+ std::string out_sha_hex = HexStr(out_sha_bytes);
// If check below fails, should manually dump the results with:
//
diff --git a/src/uint256.cpp b/src/uint256.cpp
index a5dfba41e2..ee1b34eadd 100644
--- a/src/uint256.cpp
+++ b/src/uint256.cpp
@@ -19,7 +19,11 @@ base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
template <unsigned int BITS>
std::string base_blob<BITS>::GetHex() const
{
- return HexStr(std::reverse_iterator<const uint8_t*>(m_data + sizeof(m_data)), std::reverse_iterator<const uint8_t*>(m_data));
+ uint8_t m_data_rev[WIDTH];
+ for (int i = 0; i < WIDTH; ++i) {
+ m_data_rev[i] = m_data[WIDTH - 1 - i];
+ }
+ return HexStr(m_data_rev);
}
template <unsigned int BITS>
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index 3a903b6897..d10f92ffe6 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -569,3 +569,16 @@ std::string Capitalize(std::string str)
str[0] = ToUpper(str.front());
return str;
}
+
+std::string HexStr(const Span<const uint8_t> s)
+{
+ std::string rv;
+ static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+ rv.reserve(s.size() * 2);
+ for (uint8_t v: s) {
+ rv.push_back(hexmap[v >> 4]);
+ rv.push_back(hexmap[v & 15]);
+ }
+ return rv;
+}
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
index bd988f1410..eaa0fa9992 100644
--- a/src/util/strencodings.h
+++ b/src/util/strencodings.h
@@ -10,6 +10,7 @@
#define BITCOIN_UTIL_STRENCODINGS_H
#include <attributes.h>
+#include <span.h>
#include <cstdint>
#include <iterator>
@@ -119,27 +120,11 @@ NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
*/
NODISCARD bool ParseDouble(const std::string& str, double *out);
-template<typename T>
-std::string HexStr(const T itbegin, const T itend)
-{
- std::string rv;
- static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- rv.reserve(std::distance(itbegin, itend) * 2);
- for(T it = itbegin; it < itend; ++it)
- {
- unsigned char val = (unsigned char)(*it);
- rv.push_back(hexmap[val>>4]);
- rv.push_back(hexmap[val&15]);
- }
- return rv;
-}
-
-template<typename T>
-inline std::string HexStr(const T& vch)
-{
- return HexStr(vch.begin(), vch.end());
-}
+/**
+ * Convert a span of bytes to a lower-case hexadecimal string.
+ */
+std::string HexStr(const Span<const uint8_t> s);
+inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
/**
* Format a paragraph of text to a fixed width, adding spaces for
diff --git a/src/validation.cpp b/src/validation.cpp
index 81af972777..82032611ce 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -1198,8 +1198,8 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
if (memcmp(blk_start, message_start, CMessageHeader::MESSAGE_START_SIZE)) {
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
- HexStr(blk_start, blk_start + CMessageHeader::MESSAGE_START_SIZE),
- HexStr(message_start, message_start + CMessageHeader::MESSAGE_START_SIZE));
+ HexStr(blk_start),
+ HexStr(message_start));
}
if (blk_size > MAX_SIZE) {
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index a04311fdf5..24eb2ee34c 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -38,7 +38,7 @@ void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filena
for (const auto& item : env.m_fileids) {
if (fileid == item.second && &fileid != &item.second) {
throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (duplicates fileid %s from %s)", filename,
- HexStr(std::begin(item.second.value), std::end(item.second.value)), item.first));
+ HexStr(item.second.value), item.first));
}
}
}
diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp
index 3b752ca936..e0c3a1287a 100644
--- a/src/wallet/rpcdump.cpp
+++ b/src/wallet/rpcdump.cpp
@@ -34,7 +34,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(&c, &c + 1);
+ ret << '%' << HexStr(Span<const unsigned char>(&c, 1));
} else {
ret << c;
}
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index 9d334063c4..558e64df12 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -3688,7 +3688,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
if (meta->has_key_origin) {
ret.pushKV("hdkeypath", WriteHDKeypath(meta->key_origin.path));
ret.pushKV("hdseedid", meta->hd_seed_id.GetHex());
- ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint, meta->key_origin.fingerprint + 4));
+ ret.pushKV("hdmasterfingerprint", HexStr(meta->key_origin.fingerprint));
}
}
}