Skip to content
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

rpc: make wide_difficulty hexadecimal #5402

Merged
merged 1 commit into from
Apr 15, 2019

Conversation

moneromooo-monero
Copy link
Collaborator

This should be friendlier for clients which don't have bignum support

This should be friendlier for clients which don't have bignum support
std::string hex(difficulty_type v)
{
static const char chars[] = "0123456789abcdef";
std::string s;
Copy link
Contributor

Choose a reason for hiding this comment

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

A call to reserve will constrain the number of allocations.

if (s.empty())
s += "0";
std::reverse(s.begin(), s.end());
return "0x" + s;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would do this before the while loop, to prevent another possible allocation to handle the front insertion. Then do a if (s.size() == 2) s += "0"; std::reverse(s.begin() + 2, s.end());.

@@ -74,7 +74,7 @@ namespace
void store_difficulty(cryptonote::difficulty_type difficulty, uint64_t &sdiff, std::string &swdiff, uint64_t &stop64)
{
sdiff = (difficulty << 64 >> 64).convert_to<uint64_t>();
swdiff = difficulty.convert_to<std::string>();
swdiff = cryptonote::hex(difficulty);
Copy link
Contributor

Choose a reason for hiding this comment

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

This type has a built-in function difficulty.str(0, std::ios_base::hex); which looks to have the same output, except the characters are upper case. The leading "0x" is also output. It does not use a std::stringstream internally, and the implementation looks to be similar in performance.

Copy link
Contributor

@fluffypony fluffypony left a comment

Choose a reason for hiding this comment

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

Reviewed

@fluffypony fluffypony merged commit cbf3224 into monero-project:master Apr 15, 2019
fluffypony added a commit that referenced this pull request Apr 15, 2019
cbf3224 rpc: make wide_difficulty hexadecimal (moneromooo-monero)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants