Skip to content

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented Aug 4, 2020

Make HexStr take a span of bytes, instead of an awkward pair of templated iterators. This simplifies most of the uses.

@laanwj laanwj force-pushed the 2020_06_hexstr_span branch 2 times, most recently from 55e2632 to c0bf0d7 Compare August 4, 2020 17:25
@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 4, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, 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.

Copy link
Member

@jonatack jonatack left a 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++) {
Copy link
Member

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.

Copy link
Member Author

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);
Copy link
Contributor

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];
Copy link
Contributor

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?

Copy link
Member Author

@laanwj laanwj Aug 5, 2020

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)

@hebasto
Copy link
Member

hebasto commented Aug 6, 2020

Concept ACK.

Copy link
Member

@hebasto hebasto left a 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).

* 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)); }
Copy link
Member

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?

Copy link
Member Author

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

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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
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',

Copy link
Member Author

@laanwj laanwj Aug 6, 2020

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…

Copy link
Contributor

@promag promag left a 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.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK c0c3262

@theStack
Copy link
Contributor

theStack commented Aug 6, 2020

Concept ACK

Make HexStr take a span of bytes, instead of an awkward pair of
templated iterators.
@laanwj laanwj force-pushed the 2020_06_hexstr_span branch from f0dd45e to 0a8aa62 Compare August 6, 2020 17:42
@laanwj
Copy link
Member Author

laanwj commented Aug 6, 2020

Squashed the fixme commits

Copy link
Member

@hebasto hebasto left a 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)));
Copy link
Member Author

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.

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 0a8aa62

@elichai
Copy link
Contributor

elichai commented Aug 8, 2020

Nice!
Code review ACK 0a8aa62

@laanwj laanwj merged commit b75f2ad into bitcoin:master Aug 9, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 9, 2020
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
laanwj added a commit that referenced this pull request Aug 19, 2020
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
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 19, 2020
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
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 9, 2021
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
kwvg added a commit to kwvg/dash that referenced this pull request May 18, 2021
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
kwvg added a commit to kwvg/dash that referenced this pull request May 20, 2021
Comment from 6f7b52a: "The fix for CPubKey is a part of `bitcoin#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Aug 16, 2021
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
christiancfifi pushed a commit to christiancfifi/dash that referenced this pull request Aug 25, 2021
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
christiancfifi pushed a commit to christiancfifi/dash that referenced this pull request Aug 25, 2021
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
christiancfifi pushed a commit to christiancfifi/dash that referenced this pull request Aug 25, 2021
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
christiancfifi pushed a commit to christiancfifi/dash that referenced this pull request Aug 25, 2021
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
christiancfifi pushed a commit to christiancfifi/dash that referenced this pull request Aug 25, 2021
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
vijaydasmp pushed a commit to vijaydasmp/dash that referenced this pull request Sep 28, 2021
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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants