Fabcoin Core  0.16.2
P2P Digital Currency
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 struct MineInfo: public WorkingProgress {};
36 
37 inline std::ostream& operator<<(std::ostream& _out, WorkingProgress _p)
38 {
39  _out << _p.rate() << " H/s = " << _p.hashes << " hashes / " << (double(_p.ms) / 1000) << " s";
40  return _out;
41 }
42 
43 template <class PoW> class GenericMiner;
44 
50 template <class PoW> class GenericFarmFace
51 {
52 public:
53  using WorkPackage = typename PoW::WorkPackage;
54  using Solution = typename PoW::Solution;
56 
57  virtual ~GenericFarmFace() {}
58 
66  virtual bool submitProof(Solution const& _p, Miner* _finder) = 0;
67 };
68 
73 template <class PoW> class GenericMiner
74 {
75 public:
76  using WorkPackage = typename PoW::WorkPackage;
77  using Solution = typename PoW::Solution;
79  using ConstructionInfo = std::pair<FarmFace*, unsigned>;
80 
82  m_farm(_ci.first),
83  m_index(_ci.second)
84  {}
85  virtual ~GenericMiner() {}
86 
87  // API FOR THE FARM TO CALL IN WITH
88 
89  void setWork(WorkPackage const& _work = WorkPackage())
90  {
91  auto old = m_work;
92  {
93  Guard l(x_work);
94  m_work = _work;
95  }
96  if (!!_work)
97  {
98  DEV_TIMED_ABOVE("pause", 250)
99  pause();
100  DEV_TIMED_ABOVE("kickOff", 250)
101  kickOff();
102  }
103  else if (!_work && !!old)
104  pause();
105  m_hashCount = 0;
106  }
107 
108  uint64_t hashCount() const { return m_hashCount; }
109 
110  void resetHashCount() { m_hashCount = 0; }
111 
112  unsigned index() const { return m_index; }
113 
114 protected:
115 
116  // REQUIRED TO BE REIMPLEMENTED BY A SUBCLASS:
117 
122  virtual void kickOff() = 0;
123 
127  virtual void pause() = 0;
128 
129  // AVAILABLE FOR A SUBCLASS TO CALL:
130 
136  bool submitProof(Solution const& _s)
137  {
138  if (!m_farm)
139  return true;
140  if (m_farm->submitProof(_s, this))
141  {
142  Guard l(x_work);
143  m_work.reset();
144  return true;
145  }
146  return false;
147  }
148 
149  WorkPackage const& work() const { Guard l(x_work); return m_work; }
150 
151  void accumulateHashes(unsigned _n) { m_hashCount += _n; }
152 
153 private:
154  FarmFace* m_farm = nullptr;
155  unsigned m_index;
156 
157  uint64_t m_hashCount = 0;
158 
160  mutable Mutex x_work;
161 };
162 
163 }
164 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bool submitProof(Solution const &_s)
Notes that the Miner found a solution.
Definition: GenericMiner.h:136
WorkPackage const & work() const
Definition: GenericMiner.h:149
GenericMiner(ConstructionInfo const &_ci)
Definition: GenericMiner.h:81
unsigned index() const
Definition: GenericMiner.h:112
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:194
typename dev::eth::EthashProofOfWork::WorkPackage WorkPackage
Definition: GenericMiner.h:53
u256 rate() const
Definition: Common.h:210
uint64_t hashCount() const
Definition: GenericMiner.h:108
void accumulateHashes(unsigned _n)
Definition: GenericMiner.h:151
void setWork(WorkPackage const &_work=WorkPackage())
Definition: GenericMiner.h:89
#define DEV_TIMED_ABOVE(S, MS)
Definition: Common.h:295
std::pair< FarmFace *, unsigned > ConstructionInfo
Definition: GenericMiner.h:79
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
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
typename dev::eth::EthashProofOfWork::Solution Solution
Definition: GenericMiner.h:54
uint64_t hashes
Total number of hashes computed.
Definition: Common.h:208
typename EthashProofOfWork::Solution Solution
Definition: GenericMiner.h:77
Describes the progress of a mining operation.
Definition: Common.h:205
A miner - a member and adoptee of the Farm.
Definition: GenericMiner.h:43
std::mutex Mutex
Definition: Guards.h:37
typename EthashProofOfWork::WorkPackage WorkPackage
Definition: GenericMiner.h:76