Skip to content

Commit

Permalink
Merge pull request #38 from luminousmining/fix/double_to_hash
Browse files Browse the repository at this point in the history
Fix set_difficulty ETHASH
  • Loading branch information
luminousmining authored Mar 30, 2024
2 parents 984f295 + cea4122 commit 777fc60
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 24 deletions.
36 changes: 17 additions & 19 deletions miner/sources/algo/hash_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,56 +64,54 @@ algo::hash256 algo::toHash256(


algo::hash256 algo::toHash256(
double const hashOrigin)
double const valueOriginal)
{
using namespace boost::multiprecision;
using BigInteger = boost::multiprecision::cpp_int;

double value{ hashOrigin };
static BigInteger const base { "0xffff0000000000000000000000000000000000000000000000000000" };

static BigInteger base("0x00000000ffff0000000000000000000000000000000000000000000000000000");
BigInteger product;
BigInteger product{};

if (0.0 == fabs(value))
if (0.0 == fabs(valueOriginal))
{
product = BigInteger("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
}
else
{
value = 1 / value;
double const value{ 1.0 / valueOriginal };

BigInteger idiff(value);
BigInteger const idiff{ value };
product = base * idiff;

std::string sdiff = boost::lexical_cast<std::string>(value);
size_t const ldiff = sdiff.length();
size_t const offset = sdiff.find('.');
std::string const stringDifficulty{ boost::lexical_cast<std::string>(value) };
size_t const lengthDifficulty { stringDifficulty.length() };
size_t const offset { stringDifficulty.find('.') };

if (offset != std::string::npos)
{
// Number of decimal places
size_t precision = (ldiff - 1) - offset;
size_t const precision { (lengthDifficulty - 1u) - offset };

// Effective sequence of decimal places
std::string decimals = sdiff.substr(offset + 1);
std::string decimals { stringDifficulty.substr(offset + 1u) };

// Strip leading zeroes. If a string begins with
// 0 or 0x boost parser considers it hex
decimals = decimals.erase(0, decimals.find_first_not_of('0'));

// Build up the divisor as string - just in case
// parser does some implicit conversion with 10^precision
std::string decimalDivisor = "1";
decimalDivisor.resize(precision + 1, '0');
std::string decimalDivisor { "1" };
decimalDivisor.resize(precision + 1u, '0');

// This is the multiplier for the decimal part
BigInteger multiplier(decimals);
// This is the multiplier for decimalsthe decimal part
BigInteger const multiplier { decimals };

// This is the divisor for the decimal part
BigInteger divisor(decimalDivisor);
BigInteger const divisor { decimalDivisor };

BigInteger decimalproduct;
decimalproduct = base * multiplier;
BigInteger decimalproduct{ base * multiplier };
decimalproduct /= divisor;

// Add the computed decimal part
Expand Down
1 change: 1 addition & 0 deletions miner/sources/algo/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(TEST_ALGO_HEADERS

set(TEST_ALGO_SOURCES
algo_type.cpp
hash.cpp
math.cpp
rol.cpp
)
Expand Down
128 changes: 128 additions & 0 deletions miner/sources/algo/tests/hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <gtest/gtest.h>

#include <algo/hash_utils.hpp>


struct HashTest : public testing::Test
{
HashTest() = default;
~HashTest() = default;
};


TEST_F(HashTest, doubleToHash256)
{
algo::hash256 const hash256 { algo::toHash256(1.999969) };

// "80000206012bb4b28a376b73d0f6f86aad62b60d19d23efa40b507f1"
EXPECT_EQ(0x00, hash256.ubytes[0]);
EXPECT_EQ(0x00, hash256.ubytes[1]);
EXPECT_EQ(0x00, hash256.ubytes[2]);
EXPECT_EQ(0x00, hash256.ubytes[3]);
EXPECT_EQ(0x80, hash256.ubytes[4]);
EXPECT_EQ(0x00, hash256.ubytes[5]);
EXPECT_EQ(0x02, hash256.ubytes[6]);
EXPECT_EQ(0x06, hash256.ubytes[7]);
EXPECT_EQ(0x01, hash256.ubytes[8]);
EXPECT_EQ(0x2b, hash256.ubytes[9]);
EXPECT_EQ(0xb4, hash256.ubytes[10]);
EXPECT_EQ(0xb2, hash256.ubytes[11]);
EXPECT_EQ(0x8a, hash256.ubytes[12]);
EXPECT_EQ(0x37, hash256.ubytes[13]);
EXPECT_EQ(0x6b, hash256.ubytes[14]);
EXPECT_EQ(0x73, hash256.ubytes[15]);
EXPECT_EQ(0xd0, hash256.ubytes[16]);
EXPECT_EQ(0xf6, hash256.ubytes[17]);
EXPECT_EQ(0xf8, hash256.ubytes[18]);
EXPECT_EQ(0x6a, hash256.ubytes[19]);
EXPECT_EQ(0xad, hash256.ubytes[20]);
EXPECT_EQ(0x62, hash256.ubytes[21]);
EXPECT_EQ(0xb6, hash256.ubytes[22]);
EXPECT_EQ(0x0d, hash256.ubytes[23]);
EXPECT_EQ(0x19, hash256.ubytes[24]);
EXPECT_EQ(0xd2, hash256.ubytes[25]);
EXPECT_EQ(0x3e, hash256.ubytes[26]);
EXPECT_EQ(0xfa, hash256.ubytes[27]);
EXPECT_EQ(0x40, hash256.ubytes[28]);
EXPECT_EQ(0xb5, hash256.ubytes[29]);
EXPECT_EQ(0x07, hash256.ubytes[30]);
EXPECT_EQ(0xf1, hash256.ubytes[31]);
}


TEST_F(HashTest, doubleToHash256MinimunValue)
{
algo::hash256 const hash256 { algo::toHash256(0.0001) };

EXPECT_EQ(0x00, hash256.ubytes[0]);
EXPECT_EQ(0x00, hash256.ubytes[1]);
EXPECT_EQ(0x27, hash256.ubytes[2]);
EXPECT_EQ(0x0f, hash256.ubytes[3]);
EXPECT_EQ(0xd8, hash256.ubytes[4]);
EXPECT_EQ(0xf0, hash256.ubytes[5]);
EXPECT_EQ(0x00, hash256.ubytes[6]);
EXPECT_EQ(0x00, hash256.ubytes[7]);
EXPECT_EQ(0x00, hash256.ubytes[8]);
EXPECT_EQ(0x00, hash256.ubytes[9]);
EXPECT_EQ(0x00, hash256.ubytes[10]);
EXPECT_EQ(0x00, hash256.ubytes[11]);
EXPECT_EQ(0x00, hash256.ubytes[12]);
EXPECT_EQ(0x00, hash256.ubytes[13]);
EXPECT_EQ(0x00, hash256.ubytes[14]);
EXPECT_EQ(0x00, hash256.ubytes[15]);
EXPECT_EQ(0x00, hash256.ubytes[16]);
EXPECT_EQ(0x00, hash256.ubytes[17]);
EXPECT_EQ(0x00, hash256.ubytes[18]);
EXPECT_EQ(0x00, hash256.ubytes[19]);
EXPECT_EQ(0x00, hash256.ubytes[20]);
EXPECT_EQ(0x00, hash256.ubytes[21]);
EXPECT_EQ(0x00, hash256.ubytes[22]);
EXPECT_EQ(0x00, hash256.ubytes[23]);
EXPECT_EQ(0x00, hash256.ubytes[24]);
EXPECT_EQ(0x00, hash256.ubytes[25]);
EXPECT_EQ(0x00, hash256.ubytes[26]);
EXPECT_EQ(0x00, hash256.ubytes[27]);
EXPECT_EQ(0x00, hash256.ubytes[28]);
EXPECT_EQ(0x00, hash256.ubytes[29]);
EXPECT_EQ(0x00, hash256.ubytes[30]);
EXPECT_EQ(0x00, hash256.ubytes[31]);
}


TEST_F(HashTest, doubleToHash256MaxValue)
{
algo::hash256 const hash256 { algo::toHash256(2.0) };

EXPECT_EQ(0x00, hash256.ubytes[0]);
EXPECT_EQ(0x00, hash256.ubytes[1]);
EXPECT_EQ(0x00, hash256.ubytes[2]);
EXPECT_EQ(0x00, hash256.ubytes[3]);
EXPECT_EQ(0x7f, hash256.ubytes[4]);
EXPECT_EQ(0xff, hash256.ubytes[5]);
EXPECT_EQ(0x80, hash256.ubytes[6]);
EXPECT_EQ(0x00, hash256.ubytes[7]);
EXPECT_EQ(0x00, hash256.ubytes[8]);
EXPECT_EQ(0x00, hash256.ubytes[9]);
EXPECT_EQ(0x00, hash256.ubytes[10]);
EXPECT_EQ(0x00, hash256.ubytes[11]);
EXPECT_EQ(0x00, hash256.ubytes[12]);
EXPECT_EQ(0x00, hash256.ubytes[13]);
EXPECT_EQ(0x00, hash256.ubytes[14]);
EXPECT_EQ(0x00, hash256.ubytes[15]);
EXPECT_EQ(0x00, hash256.ubytes[16]);
EXPECT_EQ(0x00, hash256.ubytes[17]);
EXPECT_EQ(0x00, hash256.ubytes[18]);
EXPECT_EQ(0x00, hash256.ubytes[19]);
EXPECT_EQ(0x00, hash256.ubytes[20]);
EXPECT_EQ(0x00, hash256.ubytes[21]);
EXPECT_EQ(0x00, hash256.ubytes[22]);
EXPECT_EQ(0x00, hash256.ubytes[23]);
EXPECT_EQ(0x00, hash256.ubytes[24]);
EXPECT_EQ(0x00, hash256.ubytes[25]);
EXPECT_EQ(0x00, hash256.ubytes[26]);
EXPECT_EQ(0x00, hash256.ubytes[27]);
EXPECT_EQ(0x00, hash256.ubytes[28]);
EXPECT_EQ(0x00, hash256.ubytes[29]);
EXPECT_EQ(0x00, hash256.ubytes[30]);
EXPECT_EQ(0x00, hash256.ubytes[31]);
}
3 changes: 3 additions & 0 deletions miner/sources/common/custom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#define MAX_LIMIT(value, max)\
(value <= max ? value : max)

#define MIN_LIMIT(value, minimun)\
(value >= minimun ? value : minimun)

namespace common
{
template<typename T>
Expand Down
21 changes: 17 additions & 4 deletions miner/sources/stratum/ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ void stratum::StratumEthash::onMiningSetDifficulty(
boost::json::object const& root)
{
auto const params{ root.at("params").as_array() };
double const difficulty{ common::boostJsonGetNumber<double>(params.at(0)) };
double const currentDifficulty { common::boostJsonGetNumber<double>(params.at(0)) };

double difficulty
{
currentDifficulty <= 2.0
? currentDifficulty
: ((currentDifficulty * 2.0) / 8589934592.0)
};

difficulty = MIN_LIMIT(difficulty, 0.0001);
difficulty = MAX_LIMIT(difficulty, 2.0);

jobInfo.boundary = algo::toHash256(difficulty);
jobInfo.boundaryU64 = algo::toUINT64(jobInfo.boundary);
Expand All @@ -109,16 +119,19 @@ void stratum::StratumEthash::onMiningSetDifficulty(
jobInfo.boundaryU64 = jobInfo.targetBits;
}

logInfo() << "Target: " << std::hex << jobInfo.boundaryU64;
logInfo()
<< "Target: "
<< std::hex << jobInfo.boundaryU64
<< std::dec << " (" << difficulty << ")";
}


void stratum::StratumEthash::onMiningSetTarget(
boost::json::object const& root)
{
auto const params{ root.at("params").as_array() };
auto boundary{ params.at(0).as_string().c_str() };
std::string const boundary{ params.at(0).as_string().c_str() };

jobInfo.boundary = algo::toHash256(boundary);
jobInfo.boundaryU64 = algo::toUINT64(jobInfo.boundary);
if (jobInfo.boundaryU64 < jobInfo.targetBits)
Expand Down
5 changes: 4 additions & 1 deletion miner/sources/stratum/progpow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ void stratum::StratumProgPOW::onMiningSetDifficulty(
jobInfo.boundaryU64 = jobInfo.targetBits;
}

logInfo() << "Target: " << std::hex << jobInfo.boundaryU64;
logInfo()
<< "Target: "
<< std::hex << jobInfo.boundaryU64
<< std::dec << " (" << difficulty << ")";
}


Expand Down

0 comments on commit 777fc60

Please sign in to comment.