aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2019-04-15 09:18:10 +0200
committerRiccardo Spagni <ric@spagni.net>2019-04-15 09:18:10 +0200
commit9bd0983d5eee1f422bad4d920ab78c70c8e6faeb (patch)
tree2e599a575c50a17043c279942a88468cce6098ab
parentMerge pull request #5398 (diff)
parentrpc: make wide_difficulty hexadecimal (diff)
downloadmonero-9bd0983d5eee1f422bad4d920ab78c70c8e6faeb.tar.xz
Merge pull request #5402
cbf32241 rpc: make wide_difficulty hexadecimal (moneromooo-monero)
-rw-r--r--src/cryptonote_basic/difficulty.cpp15
-rw-r--r--src/cryptonote_basic/difficulty.h3
-rw-r--r--src/rpc/core_rpc_server.cpp2
-rwxr-xr-xtests/functional_tests/blockchain.py14
4 files changed, 26 insertions, 8 deletions
diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp
index 5162e53e6..859173aa5 100644
--- a/src/cryptonote_basic/difficulty.cpp
+++ b/src/cryptonote_basic/difficulty.cpp
@@ -239,4 +239,19 @@ namespace cryptonote {
return res.convert_to<difficulty_type>();
}
+ std::string hex(difficulty_type v)
+ {
+ static const char chars[] = "0123456789abcdef";
+ std::string s;
+ while (v > 0)
+ {
+ s.push_back(chars[(v & 0xf).convert_to<unsigned>()]);
+ v >>= 4;
+ }
+ if (s.empty())
+ s += "0";
+ std::reverse(s.begin(), s.end());
+ return "0x" + s;
+ }
+
}
diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h
index f7a9376fb..02ed89e5a 100644
--- a/src/cryptonote_basic/difficulty.h
+++ b/src/cryptonote_basic/difficulty.h
@@ -32,6 +32,7 @@
#include <cstdint>
#include <vector>
+#include <string>
#include <boost/multiprecision/cpp_int.hpp>
#include "crypto/hash.h"
@@ -58,4 +59,6 @@ namespace cryptonote
bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty);
bool check_hash(const crypto::hash &hash, difficulty_type difficulty);
difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds);
+
+ std::string hex(difficulty_type v);
}
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 71bfcc950..bd1455c43 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -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);
stop64 = (difficulty >> 64).convert_to<uint64_t>();
}
}
diff --git a/tests/functional_tests/blockchain.py b/tests/functional_tests/blockchain.py
index 3957200d8..56164600d 100755
--- a/tests/functional_tests/blockchain.py
+++ b/tests/functional_tests/blockchain.py
@@ -61,8 +61,8 @@ class BlockchainTest():
prev_block = res_info.top_block_hash
res_height = daemon.get_height()
assert res_height.height == height
- assert int(res_info.wide_cumulative_difficulty) == (res_info.cumulative_difficulty_top64 << 64) + res_info.cumulative_difficulty
- cumulative_difficulty = int(res_info.wide_cumulative_difficulty)
+ assert int(res_info.wide_cumulative_difficulty, 16) == (res_info.cumulative_difficulty_top64 << 64) + res_info.cumulative_difficulty
+ cumulative_difficulty = int(res_info.wide_cumulative_difficulty, 16)
# we should not see a block at height
ok = False
@@ -91,11 +91,11 @@ class BlockchainTest():
assert block_header.orphan_status == False
assert block_header.depth == blocks - n - 1
assert block_header.prev_hash == prev_block, prev_block
- assert int(block_header.wide_difficulty) == (block_header.difficulty_top64 << 64) + block_header.difficulty
- assert int(block_header.wide_cumulative_difficulty) == (block_header.cumulative_difficulty_top64 << 64) + block_header.cumulative_difficulty
+ assert int(block_header.wide_difficulty, 16) == (block_header.difficulty_top64 << 64) + block_header.difficulty
+ assert int(block_header.wide_cumulative_difficulty, 16) == (block_header.cumulative_difficulty_top64 << 64) + block_header.cumulative_difficulty
assert block_header.reward >= 600000000000 # tail emission
- cumulative_difficulty += int(block_header.wide_difficulty)
- assert cumulative_difficulty == int(block_header.wide_cumulative_difficulty)
+ cumulative_difficulty += int(block_header.wide_difficulty, 16)
+ assert cumulative_difficulty == int(block_header.wide_cumulative_difficulty, 16)
assert block_header.block_size > 0
assert block_header.block_weight >= block_header.block_size
assert block_header.long_term_weight > 0
@@ -123,7 +123,7 @@ class BlockchainTest():
assert res_getblocktemplate.expected_reward >= 600000000000
assert len(res_getblocktemplate.blocktemplate_blob) > 0
assert len(res_getblocktemplate.blockhashing_blob) > 0
- assert int(res_getblocktemplate.wide_difficulty) == (res_getblocktemplate.difficulty_top64 << 64) + res_getblocktemplate.difficulty
+ assert int(res_getblocktemplate.wide_difficulty, 16) == (res_getblocktemplate.difficulty_top64 << 64) + res_getblocktemplate.difficulty
# diff etc should be the same
assert res_getblocktemplate.prev_hash == res_info.top_block_hash