Skip to content

Commit

Permalink
fix wide difficulty conversion with some versions of boost /monero#5510
Browse files Browse the repository at this point in the history
  • Loading branch information
moneromooo-monero authored and stoffu committed Jun 17, 2019
1 parent 8d0d838 commit 8ef20a5
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,8 @@ void BlockchainLMDB::add_block(const block& blk, const size_t& block_size, uint6
bi.bi_timestamp = blk.timestamp;
bi.bi_coins = coins_generated;
bi.bi_size = block_size;
bi.bi_diff_hi = (cumulative_difficulty >> 64).convert_to<uint64_t>();
bi.bi_diff_lo = (cumulative_difficulty << 64 >> 64).convert_to<uint64_t>();
bi.bi_diff_hi = ((cumulative_difficulty >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
bi.bi_diff_lo = (cumulative_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();
bi.bi_hash = blk_hash;
bi.bi_cum_rct = num_rct_outs;
if (blk.major_version >= 4)
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_basic/cryptonote_boost_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ namespace boost
else
{
// store high part
cryptonote::difficulty_type x_ = x >> 64;
cryptonote::difficulty_type x_ = (x >> 64) & 0xffffffffffffffff;
uint64_t v = x_.convert_to<uint64_t>();
a & v;
// store low part
x_ = x << 64 >> 64;
x_ = x & 0xffffffffffffffff;
v = x_.convert_to<uint64_t>();
a & v;
}
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2275,8 +2275,8 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
if (result)
{
cryptonote::difficulty_type wide_cumulative_difficulty = m_db->get_block_cumulative_difficulty(resp.total_height - 1);
resp.cumulative_difficulty = (wide_cumulative_difficulty << 64 >> 64).convert_to<uint64_t>();
resp.cumulative_difficulty_top64 = (wide_cumulative_difficulty >> 64).convert_to<uint64_t>();
resp.cumulative_difficulty = (wide_cumulative_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();
resp.cumulative_difficulty_top64 = ((wide_cumulative_difficulty >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_protocol/cryptonote_protocol_handler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ namespace cryptonote
m_core.get_blockchain_top(hshd.current_height, hshd.top_id);
hshd.top_version = m_core.get_ideal_hard_fork_version(hshd.current_height);
difficulty_type wide_cumulative_difficulty = m_core.get_block_cumulative_difficulty(hshd.current_height);
hshd.cumulative_difficulty = (wide_cumulative_difficulty << 64 >> 64).convert_to<uint64_t>();
hshd.cumulative_difficulty_top64 = (wide_cumulative_difficulty >> 64).convert_to<uint64_t>();
hshd.cumulative_difficulty = (wide_cumulative_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();
hshd.cumulative_difficulty_top64 = ((wide_cumulative_difficulty >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
hshd.current_height +=1;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/core_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ 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>();
sdiff = (difficulty & 0xffffffffffffffff).convert_to<uint64_t>();
swdiff = difficulty.convert_to<std::string>();
stop64 = (difficulty >> 64).convert_to<uint64_t>();
stop64 = ((difficulty >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/rpc/daemon_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ namespace rpc
auto& chain = m_core.get_blockchain_storage();

res.info.wide_difficulty = chain.get_difficulty_for_next_block();
res.info.difficulty = (res.info.wide_difficulty << 64 >> 64).convert_to<uint64_t>();
res.info.difficulty = (res.info.wide_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();

res.info.target = chain.get_difficulty_target();

Expand All @@ -473,7 +473,7 @@ namespace rpc
res.info.testnet = m_core.get_nettype() == TESTNET;
res.info.stagenet = m_core.get_nettype() == STAGENET;
res.info.wide_cumulative_difficulty = m_core.get_blockchain_storage().get_db().get_block_cumulative_difficulty(res.info.height - 1);
res.info.cumulative_difficulty = (res.info.wide_cumulative_difficulty << 64 >> 64).convert_to<uint64_t>();
res.info.cumulative_difficulty = (res.info.wide_cumulative_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();
res.info.block_size_limit = m_core.get_blockchain_storage().get_current_cumulative_blocksize_limit();
res.info.start_time = (uint64_t)m_core.get_start_time();

Expand Down Expand Up @@ -799,7 +799,7 @@ namespace rpc
}

header.wide_difficulty = m_core.get_blockchain_storage().block_difficulty(header.height);
header.difficulty = (header.wide_difficulty << 64 >> 64).convert_to<uint64_t>();
header.difficulty = (header.wide_difficulty & 0xffffffffffffffff).convert_to<uint64_t>();

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/serialization/difficulty_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ inline bool do_serialize(Archive<true>& ar, cryptonote::difficulty_type &diff)
{
if (!ar.stream().good())
return false;
const uint64_t hi = (diff >> 64).convert_to<uint64_t>();
const uint64_t lo = (diff << 64 >> 64).convert_to<uint64_t>();
const uint64_t hi = ((diff >> 64) & 0xffffffffffffffff).convert_to<uint64_t>();
const uint64_t lo = (diff & 0xffffffffffffffff).convert_to<uint64_t>();
ar.serialize_varint(hi);
ar.serialize_varint(lo);
if (!ar.stream().good())
Expand Down
2 changes: 1 addition & 1 deletion tests/difficulty/difficulty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) {
cryptonote::difficulty_type wide_res = cryptonote::next_difficulty(
std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
std::vector<cryptonote::difficulty_type>(wide_cumulative_difficulties.begin() + begin, wide_cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET, n + 1, 850, 100);
if (wide_res.convert_to<uint64_t>() != res) {
if ((wide_res & 0xffffffffffffffff).convert_to<uint64_t>() != res) {
cerr << "Wrong wide difficulty for block " << n << endl
<< "Expected: " << res << endl
<< "Found: " << wide_res << endl;
Expand Down
2 changes: 1 addition & 1 deletion tests/hash-target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) {
for (int i = 31; i >= 0; i--) {
val = val * 256 + 255;
((char *) &h)[i] = static_cast<char>(static_cast<uint64_t>(val / diff));
val %= diff.convert_to<uint64_t>();
val %= (diff & 0xffffffffffffffff).convert_to<uint64_t>();
}
if (check_hash(h, diff) != true) {
return 3;
Expand Down
8 changes: 4 additions & 4 deletions tests/performance_tests/check_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class test_check_hash
difficulty = difficulty_high;
difficulty = (difficulty << 64) | difficulty_low;
boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target;
((uint64_t*)&hash)[0] = (hash_value << 64 >> 64).convert_to<uint64_t>();
((uint64_t*)&hash)[0] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&hash)[1] = (hash_value << 64 >> 64).convert_to<uint64_t>();
((uint64_t*)&hash)[1] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&hash)[2] = (hash_value << 64 >> 64).convert_to<uint64_t>();
((uint64_t*)&hash)[2] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&hash)[3] = (hash_value << 64 >> 64).convert_to<uint64_t>();
((uint64_t*)&hash)[3] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/difficulty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ static crypto::hash MKHASH(uint64_t high, uint64_t low)
hash_target = (hash_target << 64) | low;
boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target;
crypto::hash h;
((uint64_t*)&h)[0] = hash_value.convert_to<uint64_t>();
((uint64_t*)&h)[0] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&h)[1] = hash_value.convert_to<uint64_t>();
((uint64_t*)&h)[1] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&h)[2] = hash_value.convert_to<uint64_t>();
((uint64_t*)&h)[2] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
hash_value >>= 64;
((uint64_t*)&h)[3] = hash_value.convert_to<uint64_t>();
((uint64_t*)&h)[3] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
return h;
}

Expand Down

0 comments on commit 8ef20a5

Please sign in to comment.