Browse Source

Compile fixes.

cl-refactor
Gav Wood 10 years ago
parent
commit
15f74352e3
  1. 29
      libethcore/Common.h
  2. 40
      libethcore/Ethash.cpp
  3. 12
      libethcore/Ethash.h
  4. 36
      libethcore/EthashAux.cpp
  5. 6
      libethcore/EthashAux.h
  6. 16
      libethcore/Miner.h
  7. 2
      libethereum/Client.h
  8. 2
      libethereum/Farm.h
  9. 5
      libethereum/Interface.h
  10. 2
      libethereum/TransactionQueue.h
  11. 10
      mix/MixClient.cpp
  12. 2
      mix/MixClient.h

29
libethcore/Common.h

@ -96,42 +96,35 @@ enum class ImportResult
BadChain
};
class Signal;
class Handler
/// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
class Signal
{
public:
Handler() = default;
Handler(Handler const&) = delete;
~Handler() { reset(); }
Handler& operator=(Handler const& _h) = delete;
class HandlerAux
{
friend class Signal;
void reset();
public:
~HandlerAux() { if (m_s) m_s->m_fire.erase(m_i); m_s = nullptr; }
private:
Handler(unsigned _i, Signal* _s): m_i(_i), m_s(_s) {}
HandlerAux(unsigned _i, Signal* _s): m_i(_i), m_s(_s) {}
unsigned m_i = 0;
Signal* m_s = nullptr;
};
/// Super-duper signal mechanism. TODO: replace with somthing a bit heavier weight.
class Signal
{
public:
using Callback = std::function<void()>;
using Callbacks = std::vector<ReadyCallback>;
Handler add(Callback const& _h) { auto n = m_onReady.size() ? m_onReady.rbegin()->first + 1 : 0; m_onReady[n] = _h; return Handler(n, this); }
std::shared_ptr<HandlerAux> add(Callback const& _h) { auto n = m_fire.empty() ? 0 : (m_fire.rbegin()->first + 1); m_fire[n] = _h; return std::shared_ptr<HandlerAux>(new HandlerAux(n, this)); }
void operator()() { for (auto const& f: m_fire) f.second(); }
private:
std::map<unsigned, Callbacks> m_fire;
std::map<unsigned, Callback> m_fire;
};
inline void Handler::reset() { if (m_s) m_s->m_fire->erase(m_i); m_s = nullptr; }
using Handler = std::shared_ptr<Signal::HandlerAux>;
}
}

40
libethcore/Ethash.cpp

@ -62,7 +62,7 @@ unsigned Ethash::revision()
void Ethash::prep(BlockInfo const& _header)
{
if (_header.number % ETHASH_EPOCH_LENGTH == 1)
EthashAux::full(bi);
EthashAux::full(_header);
}
bool Ethash::preVerify(BlockInfo const& _header)
@ -88,7 +88,7 @@ bool Ethash::verify(BlockInfo const& _header)
#endif
h256 boundary = u256((bigint(1) << 256) / _header.difficulty);
auto result = eval(_header);
auto result = EthashAux::eval(_header);
bool slow = result.value <= boundary && result.mixHash == _header.mixHash;
#if ETH_DEBUG || !ETH_TRUE
@ -111,29 +111,27 @@ bool Ethash::verify(BlockInfo const& _header)
void Ethash::CPUMiner::workLoop()
{
auto tid = std::this_thread::get_id();
static std::mt19937_64 s_eng((time(0) + *reinterpret_cast<unsigned*>(m_last.data()) + std::hash<decltype(tid)>()(tid)));
static std::mt19937_64 s_eng((time(0) + std::hash<decltype(tid)>()(tid)));
uint64_t tryNonce = Nonce::random(s_eng);
uint64_t tryNonce = (uint64_t)(u64)Nonce::random(s_eng);
ethash_return_value ethashReturn;
auto p = Ethash::params(m_work.seedHash);
void const* dagPointer = Ethash::full(m_work.headerHash).data();
auto p = EthashAux::params(m_work.seedHash);
void const* dagPointer = EthashAux::full(m_work.headerHash).data();
uint8_t const* headerHashPointer = m_work.headerHash.data();
h256 boundary = m_work.boundary();
h256 boundary = m_work.boundary;
unsigned hashCount = 0;
for (; !shouldStop(); tryNonce++, hashCount++)
{
ethash_compute_full(&ethashReturn, dagPointer, &p, headerHashPointer, tryNonce);
h256 value = h256(ethashReturn.result, h256::ConstructFromPointer);
if (value <= boundary && submitProof(Solution{value, h256(ethashReturn.mix_hash, h256::ConstructFromPointer)}))
if (value <= boundary && submitProof(Solution{(Nonce)(u64)tryNonce, h256(ethashReturn.mix_hash, h256::ConstructFromPointer)}))
break;
}
}
#if ETH_ETHASHCL || !ETH_TRUE
namespace dev { namespace eth {
class EthashCLHook: public ethash_cl_miner::search_hook
{
public:
@ -148,8 +146,8 @@ public:
m_abort = true;
for (unsigned timeout = 0; timeout < 100 && !m_aborted; ++timeout)
std::this_thread::sleep_for(chrono::milliseconds(30));
if (!m_aborted)
cwarn << "Couldn't abort. Abandoning OpenCL process.";
// if (!m_aborted)
// cwarn << "Couldn't abort. Abandoning OpenCL process.";
m_aborted = m_abort = false;
}
@ -161,7 +159,7 @@ protected:
// cdebug << "Found nonces: " << vector<uint64_t>(_nonces, _nonces + _count);
for (uint32_t i = 0; i < _count; ++i)
{
if (m_owner->found(_nonces[i]))
if (m_owner->report(_nonces[i]))
{
m_aborted = true;
return true;
@ -193,19 +191,17 @@ private:
Ethash::GPUMiner* m_owner = nullptr;
};
} }
Ethash::GPUMiner::GPUMiner(ConstructionInfo const& _ci):
Miner(_ci),
m_hook(new EthashCLHook(this))
{
}
void Ethash::GPUMiner::report(uint64_t _nonce)
bool Ethash::GPUMiner::report(uint64_t _nonce)
{
Nonce n = (Nonce)(u64)_nonce;
Result r = Ethash::eval(m_work.seedHash, m_work.headerHash, n);
if (r.value < m_work.boundary)
Result r = EthashAux::eval(m_lastWork.seedHash, m_lastWork.headerHash, n);
if (r.value < m_lastWork.boundary)
return submitProof(Solution{n, r.mixHash});
return false;
}
@ -217,17 +213,17 @@ void Ethash::GPUMiner::kickOff(WorkPackage const& _work)
if (m_miner)
m_hook->abort();
m_miner.reset(new ethash_cl_miner);
auto p = Ethash::params(_work.seedHash);
auto cb = [&](void* d) { EthashAux::readFull(_work.seedHash, bytesRef((byte*)d, p.full_size)); };
auto p = EthashAux::params(_work.seedHash);
auto cb = [&](void* d) { EthashAux::full(_work.seedHash, bytesRef((byte*)d, p.full_size)); };
m_miner->init(p, cb, 32);
}
if (m_lastWork.headerHash != _work.headerHash)
{
m_hook->abort();
uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)_work.boundary >> 192);
m_miner->search(_work.headerHash, upper64OfBoundary, *m_hook);
m_miner->search(_work.headerHash.data(), upper64OfBoundary, *m_hook);
}
m_work = _work;
m_lastWork = _work;
}
void Ethash::GPUMiner::pause()

12
libethcore/Ethash.h

@ -26,6 +26,7 @@
#include <chrono>
#include <thread>
#include <cstdint>
#include <libdevcore/CommonIO.h>
#include "Common.h"
#include "BlockInfo.h"
#include "Miner.h"
@ -37,9 +38,13 @@ namespace dev
namespace eth
{
class EthashCLHook;
class Ethash
{
public:
using Miner = GenericMiner<Ethash>;
struct Solution
{
Nonce nonce;
@ -73,7 +78,7 @@ public:
public:
CPUMiner(ConstructionInfo const& _ci): Miner(_ci), Worker("miner" + toString(index())) {}
static unsigned instances() { return thread::hardware_concurrency(); }
static unsigned instances() { return std::thread::hardware_concurrency(); }
protected:
void kickOff(WorkPackage const& _work) override
@ -93,10 +98,9 @@ public:
};
#if ETH_ETHASHCL || !ETH_TRUE
class EthashCLHook;
class GPUMiner: public Miner
{
friend class EthashCLHook;
friend class dev::eth::EthashCLHook;
public:
GPUMiner(ConstructionInfo const& _ci);
@ -108,7 +112,7 @@ public:
void pause() override;
private:
void report(uint64_t _nonce);
bool report(uint64_t _nonce);
std::unique_ptr<EthashCLHook> m_hook;
std::unique_ptr<ethash_cl_miner> m_miner;

36
libethcore/EthashAux.cpp

@ -102,13 +102,13 @@ void const* EthashAux::light(BlockInfo const& _header)
void const* EthashAux::light(h256 const& _seedHash)
{
RecursiveGuard l(x_this);
if (!m_lights.count(_header.seedHash()))
RecursiveGuard l(get()->x_this);
if (!get()->m_lights.count(_seedHash))
{
ethash_params p = params(_seedHash);
m_lights[_seedHash] = ethash_new_light(&p, _seedHash.data());
get()->m_lights[_seedHash] = ethash_new_light(&p, _seedHash.data());
}
return m_lights[_seedHash];
return get()->m_lights[_seedHash];
}
bytesConstRef EthashAux::full(BlockInfo const& _header, bytesRef _dest)
@ -118,14 +118,14 @@ bytesConstRef EthashAux::full(BlockInfo const& _header, bytesRef _dest)
bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
{
RecursiveGuard l(x_this);
if (m_fulls.count(_seedHash) && _dest)
RecursiveGuard l(get()->x_this);
if (get()->m_fulls.count(_seedHash) && _dest)
{
assert(m_fulls.size() <= _dest.size());
m_fulls.at(_seedHash).copyTo(_dest);
return;
assert(get()->m_fulls.size() <= _dest.size());
get()->m_fulls.at(_seedHash).copyTo(_dest);
return _dest;
}
if (!m_fulls.count(_seedHash))
if (!get()->m_fulls.count(_seedHash))
{
// @memoryleak @bug place it on a pile for deletion - perhaps use shared_ptr.
/* if (!m_fulls.empty())
@ -138,7 +138,7 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
boost::filesystem::create_directories(getDataDir("ethash"));
} catch (...) {}
auto info = rlpList(revision(), _seedHash);
auto info = rlpList(Ethash::revision(), _seedHash);
std::string oldMemoFile = getDataDir("ethash") + "/full";
std::string memoFile = getDataDir("ethash") + "/full-R" + toString(ETHASH_REVISION) + "-" + toHex(_seedHash.ref().cropped(0, 8));
if (boost::filesystem::exists(oldMemoFile) && contents(oldMemoFile + ".info") == info)
@ -147,8 +147,8 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
boost::filesystem::rename(oldMemoFile, memoFile);
}
IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile));
IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile + ".info"));
ETH_IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile));
ETH_IGNORE_EXCEPTIONS(boost::filesystem::remove(oldMemoFile + ".info"));
ethash_params p = params(_seedHash);
assert(!_dest || _dest.size() >= p.full_size); // must be big enough.
@ -162,14 +162,14 @@ bytesConstRef EthashAux::full(h256 const& _seedHash, bytesRef _dest)
r = _dest;
else
r = bytesRef(new byte[p.full_size], p.full_size);
ethash_prep_full(r, &p, light(_seedHash));
ethash_prep_full(r.data(), &p, light(_seedHash));
writeFile(memoFile, r);
}
if (_dest)
return _dest;
m_fulls[_seedHash] = r;
get()->m_fulls[_seedHash] = r;
}
return m_fulls[_seedHash];
return get()->m_fulls[_seedHash];
}
Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce)
@ -179,12 +179,12 @@ Ethash::Result EthashAux::eval(BlockInfo const& _header, Nonce const& _nonce)
Ethash::Result EthashAux::eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce)
{
auto p = EthashAux::params(_header);
auto p = EthashAux::params(_seedHash);
ethash_return_value r;
if (EthashAux::get()->m_fulls.count(_seedHash))
ethash_compute_full(&r, EthashAux::get()->full(_seedHash).data(), &p, _headerHash.data(), (uint64_t)(u64)_nonce);
else
ethash_compute_light(&r, EthashAux::get()->light(_seedHash), &p, _headerHash.data(), (uint64_t)(u64)_nonce);
// cdebug << "EthashAux::eval sha3(cache):" << sha3(EthashAux::get()->cache(_header)) << "hh:" << _header.headerHash(WithoutNonce) << "nonce:" << _nonce << " => " << h256(r.result, h256::ConstructFromPointer);
return Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)};
return Ethash::Result{h256(r.result, h256::ConstructFromPointer), h256(r.mix_hash, h256::ConstructFromPointer)};
}

6
libethcore/EthashAux.h

@ -28,7 +28,7 @@ namespace eth{
class EthashAux
{
EthashAux() {}
public:
~EthashAux();
static EthashAux* get() { if (!s_this) s_this = new EthashAux(); return s_this; }
@ -49,9 +49,11 @@ class EthashAux
static Ethash::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce);
private:
EthashAux() {}
void killCache(h256 const& _s);
static Ethash* s_this;
static EthashAux* s_this;
RecursiveMutex x_this;
std::map<h256, LightType> m_lights;

16
libethcore/Miner.h

@ -58,19 +58,19 @@ struct MiningProgress
unsigned ms = 0; ///< Total number of milliseconds of mining thus far.
};
template <class PoW> class Miner;
template <class PoW> class GenericMiner;
/**
* @brief Class for hosting one or more Miners.
* @warning Must be implemented in a threadsafe manner since it will be called from multiple
* miner threads.
*/
template <class PoW> class FarmFace
template <class PoW> class GenericFarmFace
{
public:
using WorkPackage = typename PoW::WorkPackage;
using Solution = typename PoW::Solution;
using Miner = Miner<PoW>;
using Miner = GenericMiner<PoW>;
/**
* @brief Called from a Miner to note a WorkPackage has a solution.
@ -85,15 +85,15 @@ public:
/**
* @brief A miner - a member and adoptee of the Farm.
*/
template <class PoW> class Miner
template <class PoW> class GenericMiner
{
public:
using ConstructionInfo = std::pair<FarmFace<PoW>*, unsigned>;
using WorkPackage = typename PoW::WorkPackage;
using Solution = typename PoW::Solution;
using FarmFace = FarmFace<PoW>;
using FarmFace = GenericFarmFace<PoW>;
using ConstructionInfo = std::pair<FarmFace*, unsigned>;
Miner(ConstructionInfo const& _ci):
GenericMiner(ConstructionInfo const& _ci):
m_farm(_ci.first),
m_index(_ci.second)
{}
@ -144,6 +144,8 @@ protected:
return true;
}
WorkPackage const& work() const { return m_work; }
private:
FarmFace* m_farm = nullptr;
unsigned m_index;

2
libethereum/Client.h

@ -290,7 +290,7 @@ private:
std::weak_ptr<EthereumHost> m_host; ///< Our Ethereum Host. Don't do anything if we can't lock.
Farm<ProofOfWork> m_farm; ///< Our mining farm.
GenericFarm<ProofOfWork> m_farm; ///< Our mining farm.
mutable Mutex x_remoteMiner; ///< The remote miner lock.
RemoteMiner m_remoteMiner; ///< The remote miner.

2
libethereum/Farm.h

@ -42,7 +42,7 @@ namespace eth
* @threadsafe
*/
template <class PoW>
class Farm: public FarmFace<PoW>
class GenericFarm: public GenericFarmFace<PoW>
{
public:
/**

5
libethereum/Interface.h

@ -171,11 +171,6 @@ public:
/// Get the coinbase address.
virtual Address address() const = 0;
/// Stops mining and sets the number of mining threads (0 for automatic).
virtual void setMiningThreads(unsigned _threads = 0) = 0;
/// Get the effective number of mining threads.
virtual unsigned miningThreads() const = 0;
/// Start mining.
/// NOT thread-safe - call it & stopMining only from a single thread
virtual void startMining() = 0;

2
libethereum/TransactionQueue.h

@ -48,7 +48,7 @@ class TransactionQueue
public:
using ImportCallback = std::function<void(ImportResult)>;
ImportResult import(bytes const& _tx, ImportCallback const& _cb = ImportCallback()) { return import(&_tx); }
ImportResult import(bytes const& _tx, ImportCallback const& _cb = ImportCallback()) { return import(&_tx, _cb); }
ImportResult import(bytesConstRef _tx, ImportCallback const& _cb = ImportCallback());
void drop(h256 _txHash);

10
mix/MixClient.cpp

@ -372,16 +372,6 @@ void MixClient::setAddress(Address _us)
m_state.setAddress(_us);
}
void MixClient::setMiningThreads(unsigned _threads)
{
m_miningThreads = _threads;
}
unsigned MixClient::miningThreads() const
{
return m_miningThreads;
}
void MixClient::startMining()
{
//no-op

2
mix/MixClient.h

@ -63,8 +63,6 @@ public:
dev::eth::ExecutionResult call(Secret _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, eth::BlockNumber _blockNumber, bool _gasAuto, eth::FudgeFactor _ff = eth::FudgeFactor::Strict);
void setAddress(Address _us) override;
void setMiningThreads(unsigned _threads) override;
unsigned miningThreads() const override;
void startMining() override;
void stopMining() override;
bool isMining() const override;

Loading…
Cancel
Save