Fabcoin Core  0.16.2
P2P Digital Currency
GenericFarm.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #pragma once
23 
24 #include <thread>
25 #include <list>
26 #include <atomic>
27 #include <libdevcore/Common.h>
28 #include <libdevcore/Worker.h>
29 #include <libethcore/Common.h>
31 #include <libethcore/BlockHeader.h>
32 
33 namespace dev
34 {
35 
36 namespace eth
37 {
38 
44 template <class PoW>
45 class GenericFarm: public GenericFarmFace<PoW>
46 {
47 public:
48  using WorkPackage = typename PoW::WorkPackage;
49  using Solution = typename PoW::Solution;
51 
53  {
54  std::function<unsigned()> instances;
55  std::function<Miner*(typename Miner::ConstructionInfo ci)> create;
56  };
57 
59  {
60  stop();
61  }
62 
67  void setWork(WorkPackage const& _wp)
68  {
70  if (_wp.headerHash == m_work.headerHash)
71  return;
72  m_work = _wp;
73  for (auto const& m: m_miners)
74  m->setWork(m_work);
75  resetTimer();
76  }
77 
78  void setSealers(std::map<std::string, SealerDescriptor> const& _sealers) { m_sealers = _sealers; }
79 
83  bool start(std::string const& _sealer)
84  {
86  if (!m_miners.empty() && m_lastSealer == _sealer)
87  return true;
88  if (!m_sealers.count(_sealer))
89  return false;
90 
91  m_miners.clear();
92  auto ins = m_sealers[_sealer].instances();
93  m_miners.reserve(ins);
94  for (unsigned i = 0; i < ins; ++i)
95  {
96  m_miners.push_back(std::shared_ptr<Miner>(m_sealers[_sealer].create(std::make_pair(this, i))));
97  m_miners.back()->setWork(m_work);
98  }
99  m_isMining = true;
100  m_lastSealer = _sealer;
101  resetTimer();
102  return true;
103  }
107  void stop()
108  {
110  m_miners.clear();
111  m_work.reset();
112  m_isMining = false;
113  }
114 
115  bool isMining() const
116  {
117  return m_isMining;
118  }
119 
125  {
126  WorkingProgress p;
127  p.ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_lastStart).count();
128  {
130  for (auto const& i: m_miners)
131  p.hashes += i->hashCount();
132  }
134  m_progress = p;
135  return m_progress;
136  }
137 
142  {
144  for (auto const& i: m_miners)
145  i->resetHashCount();
146  resetTimer();
147  }
148 
149  using SolutionFound = std::function<bool(Solution const&)>;
150 
156  void onSolutionFound(SolutionFound const& _handler) { m_onSolutionFound = _handler; }
157 
158  WorkPackage work() const { ReadGuard l(x_minerWork); return m_work; }
159 
166  bool submitProof(Solution const& _s, Miner* _m) override
167  {
169  if (x_minerWork.try_lock())
170  {
171  for (std::shared_ptr<Miner> const& m: m_miners)
172  if (m.get() != _m)
173  m->setWork();
174  m_work.reset();
175  x_minerWork.unlock();
176  return true;
177  }
178  return false;
179  }
180 
181 private:
182  void resetTimer()
183  {
184  m_lastStart = std::chrono::steady_clock::now();
185  }
186 
188  std::vector<std::shared_ptr<Miner>> m_miners;
190 
191  std::atomic<bool> m_isMining = {false};
192 
196 
198 
199  std::map<std::string, SealerDescriptor> m_sealers;
200  std::string m_lastSealer;
201 };
202 
203 }
204 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::function< unsigned()> instances
Definition: GenericFarm.h:54
void setSealers(std::map< std::string, SealerDescriptor > const &_sealers)
Definition: GenericFarm.h:78
SolutionFound m_onSolutionFound
Definition: GenericFarm.h:197
WorkingProgress const & miningProgress() const
Get information on the progress of mining this work package.
Definition: GenericFarm.h:124
size_t count
Definition: ExecStats.cpp:37
SharedMutex x_progress
Definition: GenericFarm.h:193
void stop()
Stop all mining activities.
Definition: GenericFarm.h:107
A collective of Miners.
Definition: GenericFarm.h:45
std::function< bool(Solution const &)> SolutionFound
Definition: GenericFarm.h:149
bool isMining() const
Definition: GenericFarm.h:115
std::vector< std::shared_ptr< Miner > > m_miners
Definition: GenericFarm.h:188
SharedMutex x_minerWork
Definition: GenericFarm.h:187
std::function< Miner *(typename Miner::ConstructionInfo ci)> create
Definition: GenericFarm.h:55
void resetMiningProgress()
Reset the mining progess counter.
Definition: GenericFarm.h:141
std::atomic< bool > m_isMining
Definition: GenericFarm.h:191
void onSolutionFound(SolutionFound const &_handler)
Provides a valid header based upon that received previously with setWork().
Definition: GenericFarm.h:156
typename dev::eth::EthashProofOfWork::WorkPackage WorkPackage
Definition: GenericFarm.h:48
std::map< std::string, SealerDescriptor > m_sealers
Definition: GenericFarm.h:199
#define DEV_READ_GUARDED(MUTEX)
Definition: Guards.h:146
Class for hosting one or more Miners.
Definition: GenericMiner.h:50
uint64_t ms
Total number of milliseconds of mining thus far.
Definition: Common.h:209
bool start(std::string const &_sealer)
Start a number of miners.
Definition: GenericFarm.h:83
boost::shared_lock< boost::shared_mutex > ReadGuard
Definition: Guards.h:44
typename dev::eth::EthashProofOfWork::Solution Solution
Definition: GenericFarm.h:49
boost::unique_lock< boost::shared_mutex > WriteGuard
Definition: Guards.h:47
std::chrono::steady_clock::time_point m_lastStart
Definition: GenericFarm.h:195
uint64_t hashes
Total number of hashes computed.
Definition: Common.h:208
Describes the progress of a mining operation.
Definition: Common.h:205
void setWork(WorkPackage const &_wp)
Sets the current mining mission.
Definition: GenericFarm.h:67
bool submitProof(Solution const &_s, Miner *_m) override
Called from a Miner to note a WorkPackage has a solution.
Definition: GenericFarm.h:166
boost::shared_mutex SharedMutex
Definition: Guards.h:39
WorkPackage work() const
Definition: GenericFarm.h:158
clock::time_point time_point
Definition: bench.h:49
A miner - a member and adoptee of the Farm.
Definition: GenericMiner.h:43
std::string m_lastSealer
Definition: GenericFarm.h:200
WorkingProgress m_progress
Definition: GenericFarm.h:194