You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
4.9 KiB
212 lines
4.9 KiB
10 years ago
|
/*
|
||
|
This file is part of cpp-ethereum.
|
||
|
|
||
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
cpp-ethereum is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
10 years ago
|
/** @file Farm.h
|
||
10 years ago
|
* @author Gav Wood <i@gavwood.com>
|
||
10 years ago
|
* @date 2015
|
||
10 years ago
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <thread>
|
||
|
#include <list>
|
||
|
#include <atomic>
|
||
|
#include <libdevcore/Common.h>
|
||
|
#include <libdevcore/Worker.h>
|
||
|
#include <libethcore/Common.h>
|
||
10 years ago
|
#include <libethcore/Miner.h>
|
||
|
#include <libethcore/BlockInfo.h>
|
||
10 years ago
|
#include <libethcore/ProofOfWork.h>
|
||
10 years ago
|
|
||
|
namespace dev
|
||
|
{
|
||
|
|
||
|
namespace eth
|
||
|
{
|
||
|
|
||
|
/**
|
||
|
* @brief A collective of Miners.
|
||
|
* Miners ask for work, then submit proofs
|
||
|
* @threadsafe
|
||
|
*/
|
||
10 years ago
|
template <class PoW>
|
||
10 years ago
|
class GenericFarm: public GenericFarmFace<PoW>
|
||
10 years ago
|
{
|
||
|
public:
|
||
10 years ago
|
using WorkPackage = typename PoW::WorkPackage;
|
||
|
using Solution = typename PoW::Solution;
|
||
|
using Miner = GenericMiner<PoW>;
|
||
|
|
||
10 years ago
|
~GenericFarm()
|
||
|
{
|
||
|
stop();
|
||
|
}
|
||
|
|
||
10 years ago
|
/**
|
||
|
* @brief Sets the current mining mission.
|
||
|
* @param _bi The block (header) we wish to be mining.
|
||
|
*/
|
||
10 years ago
|
void setWork(BlockInfo const& _bi) { setWork(PoW::package(_bi)); }
|
||
|
|
||
|
/**
|
||
|
* @brief Sets the current mining mission.
|
||
|
* @param _wp The work package we wish to be mining.
|
||
|
*/
|
||
|
void setWork(WorkPackage const& _wp)
|
||
10 years ago
|
{
|
||
10 years ago
|
WriteGuard l(x_minerWork);
|
||
10 years ago
|
if (_wp.headerHash == m_work.headerHash)
|
||
10 years ago
|
return;
|
||
10 years ago
|
m_work = _wp;
|
||
10 years ago
|
for (auto const& m: m_miners)
|
||
10 years ago
|
m->setWork(m_work);
|
||
10 years ago
|
resetTimer();
|
||
10 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief (Re)start miners for CPU only.
|
||
|
* @returns true if started properly.
|
||
|
*/
|
||
10 years ago
|
bool startCPU() { return start<typename PoW::CPUMiner>(); }
|
||
10 years ago
|
|
||
|
/**
|
||
|
* @brief (Re)start miners for GPU only.
|
||
|
* @returns true if started properly.
|
||
|
*/
|
||
10 years ago
|
bool startGPU() { return start<typename PoW::GPUMiner>(); }
|
||
10 years ago
|
|
||
|
/**
|
||
|
* @brief Stop all mining activities.
|
||
|
*/
|
||
|
void stop()
|
||
|
{
|
||
10 years ago
|
WriteGuard l(x_minerWork);
|
||
10 years ago
|
m_miners.clear();
|
||
10 years ago
|
m_work.reset();
|
||
10 years ago
|
m_isMining = false;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
bool isMining() const
|
||
|
{
|
||
10 years ago
|
return m_isMining;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/**
|
||
|
* @brief Get information on the progress of mining this work package.
|
||
|
* @return The progress with mining so far.
|
||
|
*/
|
||
10 years ago
|
MiningProgress const& miningProgress() const
|
||
|
{
|
||
|
MiningProgress p;
|
||
|
p.ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_lastStart).count();
|
||
|
{
|
||
10 years ago
|
ReadGuard l2(x_minerWork);
|
||
10 years ago
|
for (auto const& i: m_miners)
|
||
|
p.hashes += i->hashCount();
|
||
|
}
|
||
|
ReadGuard l(x_progress);
|
||
|
m_progress = p;
|
||
|
return m_progress;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
|
* @brief Reset the mining progess counter.
|
||
|
*/
|
||
|
void resetMiningProgress()
|
||
|
{
|
||
10 years ago
|
DEV_READ_GUARDED(x_minerWork)
|
||
10 years ago
|
for (auto const& i: m_miners)
|
||
|
i->resetHashCount();
|
||
|
resetTimer();
|
||
|
}
|
||
|
|
||
10 years ago
|
using SolutionFound = std::function<bool(Solution const&)>;
|
||
|
|
||
10 years ago
|
/**
|
||
|
* @brief Provides a valid header based upon that received previously with setWork().
|
||
|
* @param _bi The now-valid header.
|
||
|
* @return true if the header was good and that the Farm should pause until more work is submitted.
|
||
|
*/
|
||
10 years ago
|
void onSolutionFound(SolutionFound const& _handler) { m_onSolutionFound = _handler; }
|
||
10 years ago
|
|
||
10 years ago
|
WorkPackage work() const { ReadGuard l(x_minerWork); return m_work; }
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
|
/**
|
||
|
* @brief Called from a Miner to note a WorkPackage has a solution.
|
||
|
* @param _p The solution.
|
||
|
* @param _wp The WorkPackage that the Solution is for.
|
||
|
* @return true iff the solution was good (implying that mining should be .
|
||
|
*/
|
||
10 years ago
|
bool submitProof(Solution const& _s, Miner* _m) override
|
||
10 years ago
|
{
|
||
10 years ago
|
if (m_onSolutionFound && m_onSolutionFound(_s))
|
||
10 years ago
|
{
|
||
10 years ago
|
if (x_minerWork.try_lock())
|
||
|
{
|
||
|
for (std::shared_ptr<Miner> const& m: m_miners)
|
||
|
if (m.get() != _m)
|
||
|
m->setWork();
|
||
|
m_work.reset();
|
||
|
x_minerWork.unlock();
|
||
|
return true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief Start a number of miners.
|
||
|
*/
|
||
|
template <class MinerType>
|
||
|
bool start()
|
||
|
{
|
||
10 years ago
|
WriteGuard l(x_minerWork);
|
||
10 years ago
|
if (!m_miners.empty() && !!std::dynamic_pointer_cast<MinerType>(m_miners[0]))
|
||
|
return true;
|
||
|
m_miners.clear();
|
||
|
m_miners.reserve(MinerType::instances());
|
||
|
for (unsigned i = 0; i < MinerType::instances(); ++i)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_miners.push_back(std::shared_ptr<Miner>(new MinerType(std::make_pair(this, i))));
|
||
10 years ago
|
m_miners.back()->setWork(m_work);
|
||
|
}
|
||
10 years ago
|
m_isMining = true;
|
||
10 years ago
|
resetTimer();
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
void resetTimer()
|
||
|
{
|
||
|
m_lastStart = std::chrono::steady_clock::now();
|
||
|
}
|
||
|
|
||
10 years ago
|
mutable SharedMutex x_minerWork;
|
||
10 years ago
|
std::vector<std::shared_ptr<Miner>> m_miners;
|
||
10 years ago
|
WorkPackage m_work;
|
||
10 years ago
|
|
||
10 years ago
|
std::atomic<bool> m_isMining = {false};
|
||
|
|
||
10 years ago
|
mutable SharedMutex x_progress;
|
||
10 years ago
|
mutable MiningProgress m_progress;
|
||
|
std::chrono::steady_clock::time_point m_lastStart;
|
||
10 years ago
|
|
||
10 years ago
|
SolutionFound m_onSolutionFound;
|
||
10 years ago
|
};
|
||
|
|
||
|
}
|
||
|
}
|