Ethereum  PoC-8
The C++ Implementation of Ethereum
GenericMiner.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 <libdevcore/Common.h>
25 #include <libdevcore/Log.h>
26 #include <libdevcore/Worker.h>
27 #include <libethcore/Common.h>
28 
29 namespace dev
30 {
31 
32 namespace eth
33 {
34 
35 inline std::ostream& operator<<(std::ostream& _out, WorkingProgress _p)
36 {
37  _out << _p.rate() << " H/s = " << _p.hashes << " hashes / " << (double(_p.ms) / 1000) << " s";
38  return _out;
39 }
40 
41 template <class PoW> class GenericMiner;
42 
48 template <class PoW> class GenericFarmFace
49 {
50 public:
51  using WorkPackage = typename PoW::WorkPackage;
52  using Solution = typename PoW::Solution;
54 
55  virtual ~GenericFarmFace() {}
56 
64  virtual bool submitProof(Solution const& _p, Miner* _finder) = 0;
65 };
66 
71 template <class PoW> class GenericMiner
72 {
73 public:
74  using WorkPackage = typename PoW::WorkPackage;
75  using Solution = typename PoW::Solution;
77  using ConstructionInfo = std::pair<FarmFace*, unsigned>;
78 
80  m_farm(_ci.first),
81  m_index(_ci.second)
82  {}
83  virtual ~GenericMiner() {}
84 
85  // API FOR THE FARM TO CALL IN WITH
86 
87  void setWork(WorkPackage const& _work = WorkPackage())
88  {
89  bool const old_exists = !!m_work;
90  {
91  Guard l(x_work);
92  m_work = _work;
93  }
94  if (!!_work)
95  {
96  DEV_TIMED_ABOVE("pause", 250)
97  pause();
98  DEV_TIMED_ABOVE("kickOff", 250)
99  kickOff();
100  }
101  else if (!_work && old_exists)
102  pause();
103  Guard l(x_hashCount);
104  m_hashCount = 0;
105  }
106 
107  uint64_t hashCount() const { Guard l(x_hashCount); return m_hashCount; }
108 
109  void resetHashCount() { Guard l(x_hashCount); m_hashCount = 0; }
110 
111  unsigned index() const { return m_index; }
112 
113 protected:
114 
115  // REQUIRED TO BE REIMPLEMENTED BY A SUBCLASS:
116 
121  virtual void kickOff() = 0;
122 
126  virtual void pause() = 0;
127 
128  // AVAILABLE FOR A SUBCLASS TO CALL:
129 
135  bool submitProof(Solution const& _s)
136  {
137  if (!m_farm)
138  return true;
139  if (m_farm->submitProof(_s, this))
140  {
141  Guard l(x_work);
142  m_work.reset();
143  return true;
144  }
145  return false;
146  }
147 
148  WorkPackage const& work() const { Guard l(x_work); return m_work; }
149 
150  void accumulateHashes(unsigned _n) { Guard l(x_hashCount); m_hashCount += _n; }
151 
152 private:
153  FarmFace* m_farm = nullptr;
154  unsigned m_index;
155 
156  uint64_t m_hashCount = 0;
157  mutable Mutex x_hashCount;
158 
159  WorkPackage m_work;
160  mutable Mutex x_work;
161 };
162 
163 }
164 }
dev::eth::GenericFarmFace::WorkPackage
typename PoW::WorkPackage WorkPackage
Definition: GenericMiner.h:51
dev::eth::GenericFarmFace
Class for hosting one or more Miners.
Definition: GenericMiner.h:49
dev::eth::WorkingProgress::rate
u256 rate() const
Definition: Common.h:210
dev::eth::GenericMiner
A miner - a member and adoptee of the Farm.
Definition: GenericMiner.h:41
dev::Guard
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
dev::eth::GenericMiner::work
WorkPackage const & work() const
Definition: GenericMiner.h:148
dev::eth::WorkingProgress
Describes the progress of a mining operation.
Definition: Common.h:206
dev::eth::GenericFarmFace::~GenericFarmFace
virtual ~GenericFarmFace()
Definition: GenericMiner.h:55
dev::eth::WorkingProgress::hashes
uint64_t hashes
Total number of hashes computed.
Definition: Common.h:208
Worker.h
DEV_TIMED_ABOVE
#define DEV_TIMED_ABOVE(S, MS)
Definition: Common.h:276
Common.h
dev::eth::GenericMiner::submitProof
bool submitProof(Solution const &_s)
Notes that the Miner found a solution.
Definition: GenericMiner.h:135
dev::eth::GenericFarmFace::Solution
typename PoW::Solution Solution
Definition: GenericMiner.h:52
dev::eth::GenericMiner::Solution
typename PoW::Solution Solution
Definition: GenericMiner.h:75
Common.h
dev::eth::GenericMiner::hashCount
uint64_t hashCount() const
Definition: GenericMiner.h:107
dev::eth::GenericMiner::~GenericMiner
virtual ~GenericMiner()
Definition: GenericMiner.h:83
dev::eth::GenericMiner::kickOff
virtual void kickOff()=0
Begin working on a given work package, discarding any previous work.
dev::eth::GenericMiner::WorkPackage
typename PoW::WorkPackage WorkPackage
Definition: GenericMiner.h:74
dev::eth::GenericMiner::accumulateHashes
void accumulateHashes(unsigned _n)
Definition: GenericMiner.h:150
dev::eth::GenericFarmFace::submitProof
virtual bool submitProof(Solution const &_p, Miner *_finder)=0
Called from a Miner to note a WorkPackage has a solution.
dev::eth::GenericMiner::setWork
void setWork(WorkPackage const &_work=WorkPackage())
Definition: GenericMiner.h:87
dev::Mutex
std::mutex Mutex
Definition: Guards.h:37
dev::eth::GenericMiner::ConstructionInfo
std::pair< FarmFace *, unsigned > ConstructionInfo
Definition: GenericMiner.h:77
dev::eth::GenericMiner::GenericMiner
GenericMiner(ConstructionInfo const &_ci)
Definition: GenericMiner.h:79
dev::eth::WorkingProgress::ms
uint64_t ms
Total number of milliseconds of mining thus far.
Definition: Common.h:209
dev::eth::operator<<
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:217
dev::eth::GenericMiner::resetHashCount
void resetHashCount()
Definition: GenericMiner.h:109
dev::eth::GenericMiner::FarmFace
GenericFarmFace< PoW > FarmFace
Definition: GenericMiner.h:76
dev
Definition: Address.cpp:21
dev::eth::GenericMiner::pause
virtual void pause()=0
No work left to be done. Pause until told to kickOff().
Log.h
dev::eth::GenericMiner::index
unsigned index() const
Definition: GenericMiner.h:111