Browse Source

Further fixes in Ethash integration and testing

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
6b28fbe4a6
  1. 3
      libethcore/Ethash.cpp
  2. 26
      libethcore/EthashAux.cpp
  3. 8
      libethcore/EthashAux.h
  4. 8
      test/libethcore/dagger.cpp

3
libethcore/Ethash.cpp

@ -311,7 +311,8 @@ void Ethash::GPUMiner::workLoop()
unsigned device = instances() > 1 ? index() : s_deviceId; unsigned device = instances() > 1 ? index() : s_deviceId;
EthashAux::FullType dag = EthashAux::full(w.blockNumber); EthashAux::FullType dag = EthashAux::full(w.blockNumber);
m_miner->init((uint8_t const*)dag->data(), dag->size(), 32, s_platformId, device); bytesConstRef dagData = dag->data();
m_miner->init(dagData.data(), dagData.size(), 32, s_platformId, device);
} }
uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)w.boundary >> 192); uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)w.boundary >> 192);

26
libethcore/EthashAux.cpp

@ -33,6 +33,7 @@
#include <libdevcrypto/CryptoPP.h> #include <libdevcrypto/CryptoPP.h>
#include <libdevcrypto/SHA3.h> #include <libdevcrypto/SHA3.h>
#include <libdevcrypto/FileSystem.h> #include <libdevcrypto/FileSystem.h>
#include <libethash/internal.h>
#include "BlockInfo.h" #include "BlockInfo.h"
#include "Exceptions.h" #include "Exceptions.h"
using namespace std; using namespace std;
@ -54,6 +55,11 @@ ethash_h256_t EthashAux::bytesToEthash256T(uint8_t const* _bytes)
return ret; return ret;
} }
uint64_t EthashAux::cacheSize(BlockInfo const& _header)
{
return ethash_get_cachesize((uint64_t)_header.number);
}
h256 EthashAux::seedHash(unsigned _number) h256 EthashAux::seedHash(unsigned _number)
{ {
unsigned epoch = _number / ETHASH_EPOCH_LENGTH; unsigned epoch = _number / ETHASH_EPOCH_LENGTH;
@ -91,8 +97,8 @@ EthashAux::LightType EthashAux::light(BlockInfo const& _header)
EthashAux::LightType EthashAux::light(uint64_t _blockNumber) EthashAux::LightType EthashAux::light(uint64_t _blockNumber)
{ {
h256 seedHash = EthashAux::seedHash(_blockNumber);
RecursiveGuard l(get()->x_this); RecursiveGuard l(get()->x_this);
h256 seedHash = EthashAux::seedHash(_blockNumber);
LightType ret = get()->m_lights[seedHash]; LightType ret = get()->m_lights[seedHash];
return ret ? ret : (get()->m_lights[seedHash] = make_shared<LightAllocation>(_blockNumber)); return ret ? ret : (get()->m_lights[seedHash] = make_shared<LightAllocation>(_blockNumber));
} }
@ -100,6 +106,7 @@ EthashAux::LightType EthashAux::light(uint64_t _blockNumber)
EthashAux::LightAllocation::LightAllocation(uint64_t _blockNumber) EthashAux::LightAllocation::LightAllocation(uint64_t _blockNumber)
{ {
light = ethash_light_new(_blockNumber); light = ethash_light_new(_blockNumber);
size = ethash_get_cachesize(_blockNumber);
} }
EthashAux::LightAllocation::~LightAllocation() EthashAux::LightAllocation::~LightAllocation()
@ -107,6 +114,11 @@ EthashAux::LightAllocation::~LightAllocation()
ethash_light_delete(light); ethash_light_delete(light);
} }
bytesConstRef EthashAux::LightAllocation::data() const
{
return bytesConstRef((byte const*)light->cache, size);
}
EthashAux::FullAllocation::FullAllocation(ethash_light_t _light, ethash_callback_t _cb) EthashAux::FullAllocation::FullAllocation(ethash_light_t _light, ethash_callback_t _cb)
{ {
full = ethash_full_new(_light, _cb); full = ethash_full_new(_light, _cb);
@ -117,6 +129,11 @@ EthashAux::FullAllocation::~FullAllocation()
ethash_full_delete(full); ethash_full_delete(full);
} }
bytesConstRef EthashAux::FullAllocation::data() const
{
return bytesConstRef((byte const*)ethash_full_dag(full), size());
}
EthashAux::FullType EthashAux::full(BlockInfo const& _header) EthashAux::FullType EthashAux::full(BlockInfo const& _header)
{ {
return full((uint64_t) _header.number); return full((uint64_t) _header.number);
@ -124,16 +141,15 @@ EthashAux::FullType EthashAux::full(BlockInfo const& _header)
EthashAux::FullType EthashAux::full(uint64_t _blockNumber) EthashAux::FullType EthashAux::full(uint64_t _blockNumber)
{ {
h256 seedHash = EthashAux::seedHash(_blockNumber);
RecursiveGuard l(get()->x_this); RecursiveGuard l(get()->x_this);
h256 seedHash = EthashAux::seedHash(_blockNumber);
FullType ret = get()->m_fulls[seedHash].lock(); FullType ret = get()->m_fulls[seedHash].lock();
if (ret) { if (ret) {
get()->m_lastUsedFull = ret; get()->m_lastUsedFull = ret;
return ret; return ret;
} }
get()->m_fulls[seedHash] = make_shared<FullAllocation>(light(_blockNumber)->light, nullptr); ret = get()->m_lastUsedFull = make_shared<FullAllocation>(light(_blockNumber)->light, nullptr);
ret = get()->m_fulls[seedHash].lock(); get()->m_fulls[seedHash] = ret;
get()->m_lastUsedFull = ret;
return ret; return ret;
} }

8
libethcore/EthashAux.h

@ -38,7 +38,7 @@ public:
{ {
LightAllocation(uint64_t _blockNumber); LightAllocation(uint64_t _blockNumber);
~LightAllocation(); ~LightAllocation();
bytesConstRef data() const { return bytesConstRef((byte const*)light, size); } bytesConstRef data() const;
Ethash::Result compute(h256 const& _headerHash, Nonce const& _nonce) const; Ethash::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
ethash_light_t light; ethash_light_t light;
uint64_t size; uint64_t size;
@ -49,16 +49,18 @@ public:
FullAllocation(ethash_light_t _light, ethash_callback_t _cb); FullAllocation(ethash_light_t _light, ethash_callback_t _cb);
~FullAllocation(); ~FullAllocation();
Ethash::Result compute(h256 const& _headerHash, Nonce const& _nonce) const; Ethash::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
void const* data() const { return ethash_full_dag(full); } bytesConstRef data() const;
uint64_t size() const { return ethash_full_dag_size(full); } uint64_t size() const { return ethash_full_dag_size(full); }
ethash_full_t full; ethash_full_t full;
}; };
using LightType = std::shared_ptr<LightAllocation>; using LightType = std::shared_ptr<LightAllocation>;
using FullType = std::shared_ptr<FullAllocation>; using FullType = std::shared_ptr<FullAllocation>;
static h256 seedHash(unsigned _number); static h256 seedHash(unsigned _number);
static ethash_h256_t bytesToEthash256T(uint8_t const* _bytes); static ethash_h256_t bytesToEthash256T(uint8_t const* _bytes);
static uint64_t cacheSize(BlockInfo const& _header);
static LightType light(BlockInfo const& _header); static LightType light(BlockInfo const& _header);
static LightType light(uint64_t _blockNumber); static LightType light(uint64_t _blockNumber);
static FullType full(BlockInfo const& _header); static FullType full(BlockInfo const& _header);

8
test/libethcore/dagger.cpp

@ -41,7 +41,6 @@ BOOST_AUTO_TEST_SUITE(DashimotoTests)
BOOST_AUTO_TEST_CASE(basic_test) BOOST_AUTO_TEST_CASE(basic_test)
{ {
#if 0 // LTODO: Uncomment me and make me work !!!
string testPath = test::getTestPath(); string testPath = test::getTestPath();
testPath += "/PoWTests"; testPath += "/PoWTests";
@ -64,14 +63,14 @@ BOOST_AUTO_TEST_CASE(basic_test)
unsigned cacheSize(o["cache_size"].get_int()); unsigned cacheSize(o["cache_size"].get_int());
h256 cacheHash(o["cache_hash"].get_str()); h256 cacheHash(o["cache_hash"].get_str());
BOOST_REQUIRE_EQUAL(EthashAux::get()->params(header).cache_size, cacheSize); BOOST_REQUIRE_EQUAL(EthashAux::get()->light(header)->size, cacheSize);
BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header)->data()), cacheHash); BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header)->data()), cacheHash);
#if TEST_FULL #if TEST_FULL
unsigned fullSize(o["full_size"].get_int()); unsigned fullSize(o["full_size"].get_int());
h256 fullHash(o["full_hash"].get_str()); h256 fullHash(o["full_hash"].get_str());
BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header).size(), fullSize); BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header)->size(), fullSize);
BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header)), fullHash); BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header)->data()), fullHash);
#endif #endif
h256 result(o["result"].get_str()); h256 result(o["result"].get_str());
@ -79,7 +78,6 @@ BOOST_AUTO_TEST_CASE(basic_test)
BOOST_REQUIRE_EQUAL(r.value, result); BOOST_REQUIRE_EQUAL(r.value, result);
BOOST_REQUIRE_EQUAL(r.mixHash, header.mixHash); BOOST_REQUIRE_EQUAL(r.mixHash, header.mixHash);
} }
#endif
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save