Parse full 128-bit difficulty from monerod

This commit is contained in:
SChernykh 2021-09-03 22:28:54 +02:00
parent 661d596107
commit 08d2fbdbd1
3 changed files with 34 additions and 6 deletions

View file

@ -258,9 +258,9 @@ struct MinerData
struct ChainMain struct ChainMain
{ {
FORCEINLINE ChainMain() : difficulty(0), height(0), timestamp(0), reward(0), id() {} FORCEINLINE ChainMain() : difficulty(), height(0), timestamp(0), reward(0), id() {}
uint64_t difficulty; difficulty_type difficulty;
uint64_t height; uint64_t height;
uint64_t timestamp; uint64_t timestamp;
uint64_t reward; uint64_t reward;

View file

@ -180,7 +180,7 @@ void p2pool::handle_miner_data(MinerData& data)
{ {
WriteLock lock(m_mainchainLock); WriteLock lock(m_mainchainLock);
m_mainchainByHeight[data.height].difficulty = data.difficulty.lo; m_mainchainByHeight[data.height].difficulty = data.difficulty;
ChainMain& c = m_mainchainByHeight[data.height - 1]; ChainMain& c = m_mainchainByHeight[data.height - 1];
c.height = data.height - 1; c.height = data.height - 1;
@ -725,7 +725,13 @@ bool p2pool::parse_block_header(const char* data, size_t size, ChainMain& c)
} }
const auto& v = it2->value; const auto& v = it2->value;
if (!PARSE(v, c, difficulty) || !PARSE(v, c, height) || !PARSE(v, c, timestamp) || !PARSE(v, c, reward) || !parseValue(v, "hash", c.id)) {
if (!parseValue(v, "difficulty", c.difficulty.lo) || !parseValue(v, "difficulty_top64", c.difficulty.hi)) {
LOGERR(1, "parse_block_header: invalid JSON response from daemon: failed to parse difficulty");
return false;
}
if (!PARSE(v, c, height) || !PARSE(v, c, timestamp) || !PARSE(v, c, reward) || !parseValue(v, "hash", c.id)) {
LOGERR(1, "parse_block_header: invalid JSON response from daemon: failed to parse 'block_header'"); LOGERR(1, "parse_block_header: invalid JSON response from daemon: failed to parse 'block_header'");
return false; return false;
} }
@ -773,7 +779,12 @@ uint32_t p2pool::parse_block_headers_range(const char* data, size_t size)
} }
ChainMain c; ChainMain c;
if (PARSE(*i, c, difficulty) && PARSE(*i, c, height) && PARSE(*i, c, timestamp) && PARSE(*i, c, reward) && parseValue(*i, "hash", c.id)) {
if (!parseValue(*i, "difficulty", c.difficulty.lo) || !parseValue(*i, "difficulty_top64", c.difficulty.hi)) {
continue;
}
if (PARSE(*i, c, height) && PARSE(*i, c, timestamp) && PARSE(*i, c, reward) && parseValue(*i, "hash", c.id)) {
min_height = std::min(min_height, c.height); min_height = std::min(min_height, c.height);
max_height = std::max(max_height, c.height); max_height = std::max(max_height, c.height);
m_mainchainByHeight[c.height] = c; m_mainchainByHeight[c.height] = c;
@ -860,7 +871,7 @@ void p2pool::api_update_block_found(const ChainMain* data)
if (data) { if (data) {
{ {
ReadLock lock(m_mainchainLock); ReadLock lock(m_mainchainLock);
diff.lo = m_mainchainByHeight[data->height].difficulty; diff = m_mainchainByHeight[data->height].difficulty;
} }
std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app); std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app);

View file

@ -22,6 +22,17 @@
namespace p2pool { namespace p2pool {
TEST(difficulty_type, constructors)
{
difficulty_type diff;
ASSERT_EQ(diff.lo, 0);
ASSERT_EQ(diff.hi, 0);
difficulty_type diff2(123, 456);
ASSERT_EQ(diff2.lo, 123);
ASSERT_EQ(diff2.hi, 456);
}
TEST(difficulty_type, target) TEST(difficulty_type, target)
{ {
// diff = 0 // diff = 0
@ -42,6 +53,12 @@ TEST(difficulty_type, target)
ASSERT_EQ(d.target(), 1); ASSERT_EQ(d.target(), 1);
} }
// diff = max
{
difficulty_type d(std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max());
ASSERT_EQ(d.target(), 1);
}
// diff = 2^32 // diff = 2^32
{ {
difficulty_type d(1ull << 32, 0); difficulty_type d(1ull << 32, 0);