Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaLanfranchi committed Aug 19, 2021
1 parent 8b20394 commit fec5613
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 13 deletions.
72 changes: 71 additions & 1 deletion libcrypto/hash_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include <stdint.h>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <sstream>

#include "endianess.hpp"

Expand Down Expand Up @@ -67,6 +67,7 @@ struct le

struct be
{
static uint32_t uint32(uint32_t x) noexcept { return bswap32(x); }
static uint64_t uint64(uint64_t x) noexcept { return bswap64(x); }
};

Expand Down Expand Up @@ -133,6 +134,75 @@ inline std::string to_hex(const hash256& value)
return s.str();
}

inline void shiftLeft(hash256& hash, uint32_t bitsToShift)
{
// use hash_256.word64s
static const size_t size_of_element = sizeof(hash.word64s[0]);
static const size_t bits_of_element = size_of_element * 8;
static const size_t num_of_elements = sizeof(hash) / size_of_element;


if (!bitsToShift)
{
return;
}

hash256 tmp;
for (size_t i{0}; i < num_of_elements; i++)
{
tmp.word64s[i] = be::uint64(hash.word64s[i]);
hash.word64s[i] = 0;
}

int elements_to_jump_over = bitsToShift / bits_of_element;
bitsToShift = bitsToShift % bits_of_element;

for (int i = num_of_elements - 1; i - elements_to_jump_over >= 0; i--)
{
if (i - elements_to_jump_over - 1 >= 0)
{
hash.word64s[i - elements_to_jump_over - 1] = be::uint64(tmp.word64s[i] >> (bits_of_element - bitsToShift));
}
if (i - elements_to_jump_over >= 0)
{
hash.word64s[i - elements_to_jump_over] |= be::uint64(tmp.word64s[i] << bitsToShift);
}
}
}

inline hash256 from_compact(const uint32_t nbits, bool* pfNegative = nullptr, bool* pfOverflow = nullptr)
{
// see https://doxygen.bitcoincore.org/classarith__uint256.html
// https://doxygen.bitcoincore.org/classarith__uint256.html#a06c0f1937edece69b8d33f88e8d35bc8
// https://github.com/RavenCommunity/kawpowminer/blob/e5deed9afe8300748b97f1d79cce64a77f3e7713/libpoolprotocols/stratum/arith_uint256.h#L266-L285
// https://github.com/RavenCommunity/kawpowminer/blob/e5deed9afe8300748b97f1d79cce64a77f3e7713/libpoolprotocols/stratum/arith_uint256.cpp#L207-L225

hash256 res{};
uint32_t nSize = nbits >> 24;
uint32_t nWord = nbits & 0x007fffff;
if (nSize <= 3)
{
nWord >>= 8 * (3 - nSize);
res.word32s[7] = be::uint32(nWord);
}
else
{
res.word32s[7] = be::uint32(nWord);
shiftLeft(res, 8 * (nSize - 3));
}

if (pfNegative)
{
*pfNegative = nWord != 0 && (nbits & 0x00800000) != 0;
}
if (pfOverflow)
{
*pfOverflow = nWord != 0 && ((nSize > 34) || (nWord > 0xff && nSize > 33) || (nWord > 0xffff && nSize > 32));
}

return res;
}

} // namespace ethash

#endif // !CRYPTO_HASH_TYPES_HPP_
1 change: 1 addition & 0 deletions libethash-cpu/CPUMiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ void CPUMiner::search(const dev::eth::WorkPackage& w)
if (ethash::is_less_or_equal(result.final_hash, boundary))
{
h256 mix{reinterpret_cast<::byte*>(result.mix_hash.bytes), h256::ConstructFromPointer};
h256 fin{reinterpret_cast<::byte*>(result.final_hash.bytes), h256::ConstructFromPointer};
Solution sol{nonce, mix, w, std::chrono::steady_clock::now(), m_index};
cpulog << EthWhite << "Job: " << w.header.abridged() << " Sol: " << toHex(sol.nonce, HexPrefix::Add)
<< EthReset;
Expand Down
17 changes: 7 additions & 10 deletions libethcore/Miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ struct TelemetryType
int hoursSize = (hours.count() > 9 ? (hours.count() > 99 ? 3 : 2) : 1);
duration -= hours;
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
_ret << EthGreen << std::setw(hoursSize) << hours.count() << ":" << std::setfill('0')
<< std::setw(2) << minutes.count() << EthReset << EthWhiteBold << " "
<< farm.solutions.str() << EthReset << " ";
_ret << EthGreen << std::setw(hoursSize) << hours.count() << ":" << std::setfill('0') << std::setw(2)
<< minutes.count() << EthReset << EthWhiteBold << " " << farm.solutions.str() << EthReset << " ";

/*
Github : @AndreaLanfranchi
Expand All @@ -282,8 +281,8 @@ struct TelemetryType
magnitude++;
}

_ret << EthTealBold << std::fixed << std::setprecision(2) << hr << " "
<< suffixes[magnitude] << EthReset << " - ";
_ret << EthTealBold << std::fixed << std::setprecision(2) << hr << " " << suffixes[magnitude] << EthReset
<< " - ";

int i = -1; // Current miner index
int m = miners.size() - 1; // Max miner index
Expand All @@ -294,8 +293,8 @@ struct TelemetryType
if (hr > 0.0f)
hr /= pow(1000.0f, magnitude);

_ret << (miner.paused ? EthRed : "") << miner.prefix << i << " " << EthTeal
<< std::fixed << std::setprecision(2) << hr << EthReset;
_ret << (miner.paused ? EthRed : "") << miner.prefix << i << " " << EthTeal << std::fixed
<< std::setprecision(2) << hr << EthReset;

if (hwmon)
_ret << " " << EthTeal << miner.sensors.str() << EthReset;
Expand Down Expand Up @@ -392,9 +391,7 @@ class FarmFace
class Miner : public Worker
{
public:
Miner(std::string const& _name, unsigned _index)
: Worker(_name + std::to_string(_index)), m_index(_index)
{}
Miner(std::string const& _name, unsigned _index) : Worker(_name + std::to_string(_index)), m_index(_index) {}

~Miner() override = default;

Expand Down
4 changes: 2 additions & 2 deletions libpoolprotocols/PoolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void PoolManager::setClientHandlers()
else
{
newEpoch = (m_currentWp.epoch.value() != wp.epoch.value());
newDiff = (m_currentWp.boundary != wp.boundary);
newDiff = (m_currentWp.get_boundary() != wp.get_boundary());
}

// Save package
Expand Down Expand Up @@ -456,7 +456,7 @@ void PoolManager::showMiningAt()
return;
}

double d = dev::getHashesToTarget(m_currentWp.boundary.hex(HexPrefix::Add));
double d = dev::getHashesToTarget(m_currentWp.get_boundary().hex(HexPrefix::Add));
cnote << "Epoch : " EthWhite << m_currentWp.epoch.value() << EthReset << " Difficulty : " EthWhite
<< dev::getFormattedHashes(d) << EthReset;
}
Expand Down
6 changes: 6 additions & 0 deletions libpoolprotocols/getwork/EthGetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ void EthGetworkClient::processResponse(Json::Value& JRes)
newWp.epoch = strtoul(JPrm["pprpcepoch"].asString().c_str(), nullptr, 0);
auto seed = ethash::calculate_seed_from_epoch(newWp.epoch.value());
newWp.seed = h256(seed.bytes, dev::h256::ConstructFromPointer);

// Compute block boundary from bits
uint32_t bits = std::strtoul(JPrm["bits"].asString().c_str(), nullptr, 16);
auto block_target = ethash::from_compact(bits);
newWp.block_boundary = h256(block_target.bytes, dev::h256::ConstructFromPointer);

newWp.boundary = h256(JPrm["target"].asString());
newWp.block = strtoul(JPrm["height"].asString().c_str(), nullptr, 0);
newWp.job = newWp.header.hex();
Expand Down

0 comments on commit fec5613

Please sign in to comment.