Fabcoin Core  0.16.2
P2P Digital Currency
EthashAux.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 <condition_variable>
25 #include <libethash/ethash.h>
26 #include <libdevcore/Log.h>
27 #include <libdevcore/Worker.h>
28 #include "EthashProofOfWork.h"
29 #include "Ethash.h"
30 
31 namespace dev
32 {
33 namespace eth
34 {
35 
36 struct DAGChannel: public LogChannel { static const char* name(); static const int verbosity = 1; };
37 
38 class BlockHeader;
39 
40 class EthashAux
41 {
42 public:
43  ~EthashAux();
44 
45  static EthashAux* get();
46 
48  {
49  LightAllocation(h256 const& _seedHash);
50  ~LightAllocation();
51  bytesConstRef data() const;
52  EthashProofOfWork::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
54  uint64_t size;
55  };
56 
58  {
60  ~FullAllocation();
61  EthashProofOfWork::Result compute(h256 const& _headerHash, Nonce const& _nonce) const;
62  bytesConstRef data() const;
63  uint64_t size() const { return ethash_full_dag_size(full); }
65  };
66 
67  using LightType = std::shared_ptr<LightAllocation>;
68  using FullType = std::shared_ptr<FullAllocation>;
69 
70  static h256 seedHash(unsigned _number);
71  static uint64_t number(h256 const& _seedHash);
72  static uint64_t cacheSize(BlockHeader const& _header);
73  static uint64_t dataSize(uint64_t _blockNumber);
74 
75  static LightType light(h256 const& _seedHash);
76 
77  static const uint64_t NotGenerating = (uint64_t)-1;
79  static unsigned computeFull(h256 const& _seedHash, bool _createIfMissing = true);
81  static std::pair<uint64_t, unsigned> fullGeneratingProgress() { return std::make_pair(get()->m_generatingFullNumber, get()->m_fullProgress); }
83  static FullType full(h256 const& _seedHash, bool _createIfMissing = false, std::function<int(unsigned)> const& _f = std::function<int(unsigned)>());
84 
85  static EthashProofOfWork::Result eval(h256 const& _seedHash, h256 const& _headerHash, Nonce const& _nonce);
86 
87 private:
88  EthashAux() {}
89 
91 
92  void killCache(h256 const& _s);
93 
94  static EthashAux* s_this;
95 
97  std::unordered_map<h256, std::shared_ptr<LightAllocation>> m_lights;
98 
100  std::condition_variable m_fullsChanged;
101  std::unordered_map<h256, std::weak_ptr<FullAllocation>> m_fulls;
103  std::unique_ptr<std::thread> m_fullGenerator;
104  uint64_t m_generatingFullNumber = NotGenerating;
105  unsigned m_fullProgress;
106 
108  std::unordered_map<h256, unsigned> m_epochs;
110 };
111 
112 }
113 }
static const int verbosity
Definition: EthashAux.h:36
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define function(a, b, c, d, k, s)
std::unordered_map< h256, std::shared_ptr< LightAllocation > > m_lights
Definition: EthashAux.h:97
Encapsulation of a block header.
Definition: BlockHeader.h:95
std::shared_ptr< FullAllocation > FullType
Definition: EthashAux.h:68
static EthashAux * s_this
Definition: EthashAux.h:94
std::unordered_map< h256, std::weak_ptr< FullAllocation > > m_fulls
Definition: EthashAux.h:101
static std::pair< uint64_t, unsigned > fullGeneratingProgress()
Information on the generation progress.
Definition: EthashAux.h:81
std::unordered_map< h256, unsigned > m_epochs
Definition: EthashAux.h:108
std::shared_ptr< LightAllocation > LightType
Definition: EthashAux.h:67
std::condition_variable m_fullsChanged
Definition: EthashAux.h:100
unsigned m_fullProgress
Definition: EthashAux.h:105
SharedMutex x_lights
Definition: EthashAux.h:96
FullType m_lastUsedFull
Definition: EthashAux.h:102
uint64_t ethash_full_dag_size(ethash_full_t full)
Get the size of the DAG data.
Definition: internal.c:600
std::unique_ptr< std::thread > m_fullGenerator
Definition: EthashAux.h:103
boost::shared_mutex SharedMutex
Definition: Guards.h:39
int(* ethash_callback_t)(unsigned)
Definition: ethash.h:62
The default logging channels.
Definition: Log.h:130
static const char * name()
Definition: EthashAux.cpp:43
std::mutex Mutex
Definition: Guards.h:37
std::vector< h256 > h256s
Definition: FixedHash.h:345
uint8_t const * data
Definition: sha3.h:19