-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: Make HexStr take a span #19660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
55e2632
to
c0bf0d7
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c0bf0d7
src/uint256.cpp
Outdated
@@ -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++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Perhaps use the pre-increment (++i
) operator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
@@ -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]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I'm wrong, but isn't this duplicating memory usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really: is is a local variable, all this does is create a small (32 bytes for uint256) temporary buffer on the stack.
(I've opted to do this instead of creating a vector from the iterators, which would be a heap allocation)
Concept ACK. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c0bf0d7, tested on Linux Mint 20 (x86_64).
src/util/strencodings.h
Outdated
* Convert a span of bytes to a lower-case hexadecimal string. | ||
*/ | ||
std::string HexStr(const Span<const uint8_t> s); | ||
static inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does static
is really needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is
src/util/strencodings.cpp
Outdated
std::string HexStr(const Span<const uint8_t> s) | ||
{ | ||
std::string rv; | ||
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. Does this make a difference for an array?
Edit: well it works so I don't think there any hurt in changing it…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK c0bf0d7.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK c0c3262
Concept ACK |
Make HexStr take a span of bytes, instead of an awkward pair of templated iterators.
f0dd45e
to
0a8aa62
Compare
Squashed the fixme commits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK 0a8aa62
@@ -260,7 +260,7 @@ class DescribeAddressVisitor : public boost::static_visitor<UniValue> | |||
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))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we want to give WitnessUnknown
.data()
and .size()
fields to avoid having to do this explicitly. Don't know. Probably not in thie PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-ACK 0a8aa62
Nice! |
0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
356988e util: make EncodeBase58Check consume Spans (Sebastian Falbesoner) f0fce06 util: make EncodeBase58 consume Spans (Sebastian Falbesoner) Pull request description: This PR improves the interfaces for the functions `EncodeBase58{Check}` by using Spans, in a similar fashion to e.g. PRs #19660, #19687. Note that on the master branch there are currently two versions of `EncodeBase58`: one that takes two pointers (marking begin and end) and another one that takes a `std::vector<unsigned char>` const-ref. The PR branch only leaves one generic Span-interface, both simplifying the interface and allowing more generic containers to be passed. The same is done for `EncodeBase58Check`, where only one interface existed but it's more generic now (e.g. a std::array can be directly passed, as done in the benchmarks). ACKs for top commit: laanwj: Code review ACK 356988e Tree-SHA512: 47cfccdd7f3a2d4694bb8785e6e5fd756daee04ce1652ee59a7822e7e833b4a441ae9362b9bd67ea020d2b5b7d927629c9addb6abaa9881d8564fd3b1257f512
356988e util: make EncodeBase58Check consume Spans (Sebastian Falbesoner) f0fce06 util: make EncodeBase58 consume Spans (Sebastian Falbesoner) Pull request description: This PR improves the interfaces for the functions `EncodeBase58{Check}` by using Spans, in a similar fashion to e.g. PRs bitcoin#19660, bitcoin#19687. Note that on the master branch there are currently two versions of `EncodeBase58`: one that takes two pointers (marking begin and end) and another one that takes a `std::vector<unsigned char>` const-ref. The PR branch only leaves one generic Span-interface, both simplifying the interface and allowing more generic containers to be passed. The same is done for `EncodeBase58Check`, where only one interface existed but it's more generic now (e.g. a std::array can be directly passed, as done in the benchmarks). ACKs for top commit: laanwj: Code review ACK 356988e Tree-SHA512: 47cfccdd7f3a2d4694bb8785e6e5fd756daee04ce1652ee59a7822e7e833b4a441ae9362b9bd67ea020d2b5b7d927629c9addb6abaa9881d8564fd3b1257f512
Summary: ``` Make HexStr take a span of bytes, instead of an awkward pair of templated iterators. ``` Backport of core [[bitcoin/bitcoin#19660 | PR19660]]. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Differential Revision: https://reviews.bitcoinabc.org/D9183
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
Merge bitcoin#19660, bitcoin#19373, bitcoin#19841, bitcoin#13862, bitcoin#13866, bitcoin#17280, bitcoin#17682 and partial bitcoin#19326, bitcoin#14978: Auxiliary Backports
d4408cd Make HexStr() take a Span (furszy) 4c755ec dead code: Remove dead option in HexStr conversion. (Lenny Maiorani) Pull request description: Follow up to #2470. Only the last two commits are from this work. Make `HexStr` take a span of bytes, instead of an awkward pair of templated iterators. Simplifying most of the uses. Adaptation of bitcoin#19660 and bitcoin#15573. ACKs for top commit: random-zebra: utACK d4408cd Tree-SHA512: 541f80e1af9abea2d662e5fc31fc4d5b4057ff93eefeba5632c6ddca391f3b148c8d819a56802720c3932c3c2afb69ddd1efb4890a59ecf1bf5afd6686cd5a67
Modified some code not backported yet 0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
Modified some code not backported yet 0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
Modified some code not backported yet 0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
Partial because it modified some code not backported yet 0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
Partial because it modified some code not backported yet 0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
0a8aa62 refactor: Make HexStr take a span (Wladimir J. van der Laan) Pull request description: Make `HexSt`r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses. ACKs for top commit: elichai: Code review ACK 0a8aa62 hebasto: re-ACK 0a8aa62 jonatack: re-ACK 0a8aa62 Tree-SHA512: 6e178ece5cbac62119c857a10299b1e85422938084c3f03063e17119a5129e0c28016e05a6fabaa4c271a7e0a37c7cd89fa47c435ee19b38a5acfe80d00de992
Make
HexSt
r take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses.