Fabcoin Core  0.16.2
P2P Digital Currency
EthashClient.cpp
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 #include "EthashClient.h"
23 #include "Ethash.h"
24 using namespace std;
25 using namespace dev;
26 using namespace dev::eth;
27 using namespace p2p;
28 
30 {
31  if (dynamic_cast<Ethash*>(_c.sealEngine()))
32  return dynamic_cast<EthashClient&>(_c);
33  throw InvalidSealEngine();
34 }
35 
37 {
38  if (dynamic_cast<Ethash*>(_c->sealEngine()))
39  return &dynamic_cast<EthashClient&>(*_c);
40  throw InvalidSealEngine();
41 }
42 
43 DEV_SIMPLE_EXCEPTION(ChainParamsNotEthash);
44 
45 EthashClient::EthashClient(
46  ChainParams const& _params,
47  int _networkID,
48  p2p::Host* _host,
49  std::shared_ptr<GasPricer> _gpForAdoption,
50  std::string const& _dbPath,
51  WithExisting _forceAction,
52  TransactionQueue::Limits const& _limits
53 ):
54  Client(_params, _networkID, _host, _gpForAdoption, _dbPath, _forceAction, _limits)
55 {
56  // will throw if we're not an Ethash seal engine.
57  asEthashClient(*this);
58 }
59 
61 {
62  return dynamic_cast<Ethash*>(Client::sealEngine());
63 }
64 
66 {
67  return ethash()->farm().isMining();
68 }
69 
71 {
72  if (isMining())
73  return ethash()->farm().miningProgress();
74  return WorkingProgress();
75 }
76 
78 {
79  u256 r = externalHashrate();
80  if (isMining())
81  r += miningProgress().rate();
82  return r;
83 }
84 
85 std::tuple<h256, h256, h256> EthashClient::getEthashWork()
86 {
87  // lock the work so a later submission isn't invalidated by processing a transaction elsewhere.
88  // this will be reset as soon as a new block arrives, allowing more transactions to be processed.
89  bool oldShould = shouldServeWork();
90  m_lastGetWork = chrono::system_clock::now();
91 
92  if (!sealEngine()->shouldSeal(this))
93  return std::tuple<h256, h256, h256>();
94 
95  // if this request has made us bother to serve work, prep it now.
96  if (!oldShould && shouldServeWork())
98  else
99  // otherwise, set this to true so that it gets prepped next time.
100  m_remoteWorking = true;
103 }
104 
105 bool EthashClient::submitEthashWork(h256 const& _mixHash, h64 const& _nonce)
106 {
107  ethash()->manuallySubmitWork(_mixHash, _nonce);
108  return true;
109 }
110 
112 {
113  bytes trueBytes {1};
114  bytes falseBytes {0};
115  sealEngine()->setOption("precomputeDAG", _precompute ? trueBytes: falseBytes);
116 }
117 
118 void EthashClient::submitExternalHashrate(u256 const& _rate, h256 const& _id)
119 {
121  m_externalRates[_id] = make_pair(_rate, chrono::steady_clock::now());
122 }
123 
125 {
126  u256 ret = 0;
128  for (auto i = m_externalRates.begin(); i != m_externalRates.end();)
129  if (chrono::steady_clock::now() - i->second.second > chrono::seconds(5))
130  i = m_externalRates.erase(i);
131  else
132  ret += i++->second.first;
133  return ret;
134 }
DEV_SIMPLE_EXCEPTION(ChainParamsNotEthash)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
BlockHeader m_sealingInfo
The header we&#39;re attempting to seal on (derived from m_postSeal).
Definition: Client.h:305
u256 hashrate() const
The hashrate...
void onPostStateChanged()
Called when the post state has changed (i.e.
Definition: Client.cpp:576
Ethash * ethash() const
h256 hash(IncludeSeal _i=WithSeal) const
Definition: BlockHeader.cpp:64
EthashClient & asEthashClient(Interface &_c)
SharedMutex x_externalRates
Definition: EthashClient.h:85
bool shouldServeWork() const
Definition: EthashClient.h:65
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
std::tuple< h256, h256, h256 > getEthashWork()
Update to the latest transactions and get hash of the current block to be mined minus the nonce (the ...
bool submitEthashWork(h256 const &_mixHash, h64 const &_nonce)
Submit the proof for the proof-of-work.
static h256 seedHash(BlockHeader const &_bi)
Definition: Ethash.cpp:66
std::hash for asio::adress
Definition: Common.h:323
u256 rate() const
Definition: Common.h:210
void submitExternalHashrate(u256 const &_rate, h256 const &_id)
void manuallySubmitWork(h256 const &_mixHash, Nonce _nonce)
Definition: Ethash.cpp:177
void setShouldPrecomputeDAG(bool _precompute)
Enable/disable precomputing of the DAG for next epoch.
void manuallySetWork(BlockHeader const &_work)
Definition: Ethash.h:71
WithExisting
Definition: Common.h:310
static h256 boundary(BlockHeader const &_bi)
Definition: Ethash.h:64
bool isMining() const
Are we mining now?
std::vector< byte > bytes
Definition: Common.h:75
Main API hub for interfacing with Ethereum.
Definition: Client.h:75
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
boost::unique_lock< boost::shared_mutex > WriteGuard
Definition: Guards.h:47
Describes the progress of a mining operation.
Definition: Common.h:205
std::chrono::system_clock::time_point m_lastGetWork
Is there an active and valid remote worker?
Definition: Client.h:309
std::unordered_map< h256, std::pair< u256, std::chrono::steady_clock::time_point > > m_externalRates
Definition: EthashClient.h:84
bool setOption(std::string const &_name, bytes const &_value)
Definition: SealEngine.h:64
virtual SealEngineFace * sealEngine() const
Get the seal engine.
Definition: Interface.h:241
u256 externalHashrate() const
WorkingProgress miningProgress() const
Check the progress of the mining.
SealEngineFace * sealEngine() const override
Get the seal engine.
Definition: Client.h:176
eth::GenericFarm< EthashProofOfWork > & farm()
Definition: Ethash.h:58
bool m_remoteWorking
Has the remote worker recently been reset?
Definition: Client.h:307