Various fixes for mining.

This commit is contained in:
Gav Wood 2015-04-13 18:12:05 +02:00
parent fbb5adc9f9
commit 8f659f3981
5 changed files with 85 additions and 85 deletions

View File

@ -62,6 +62,37 @@ void connectClients(Client& c1, Client& c2)
c2.connect("127.0.0.1", c1Port); c2.connect("127.0.0.1", c1Port);
#endif #endif
} }
void mine(State& s, BlockChain const& _bc)
{
s.commitToMine(_bc);
GenericFarm<ProofOfWork> f;
bool completed = false;
f.onSolutionFound([&](ProofOfWork::Solution sol)
{
return completed = s.completeMine<ProofOfWork>(sol);
});
f.setWork(s.info());
f.startCPU();
while (!completed)
this_thread::sleep_for(chrono::milliseconds(20));
}
void mine(BlockInfo& _bi)
{
GenericFarm<ProofOfWork> f;
bool completed = false;
f.onSolutionFound([&](ProofOfWork::Solution sol)
{
ProofOfWork::assignResult(sol, _bi);
return completed = true;
});
f.setWork(_bi);
f.startCPU();
while (!completed)
this_thread::sleep_for(chrono::milliseconds(20));
}
} }
namespace test namespace test

View File

@ -36,9 +36,12 @@ namespace eth
{ {
class Client; class Client;
class State;
void mine(Client& c, int numBlocks); void mine(Client& c, int numBlocks);
void connectClients(Client& c1, Client& c2); void connectClients(Client& c1, Client& c2);
void mine(State& _s, BlockChain const& _bc);
void mine(BlockInfo& _bi);
} }
@ -225,7 +228,5 @@ public:
}; };
}; };
} }
} }

View File

@ -190,11 +190,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin)
{ {
state.sync(bc); state.sync(bc);
state.sync(bc, txs, gp); state.sync(bc, txs, gp);
state.commitToMine(bc); mine(state, bc);
MineInfo info;
ProofOfWork pow;
for (info.completed = false; !info.completed; info = state.mine(&pow)) {}
state.completeMine();
} }
catch (Exception const& _e) catch (Exception const& _e)
{ {
@ -519,76 +515,55 @@ bytes createBlockRLPFromFields(mObject& _tObj)
return rlpStream.out(); return rlpStream.out();
} }
void overwriteBlockHeader(BlockInfo& _currentBlockHeader, mObject& _blObj) void overwriteBlockHeader(BlockInfo& _header, mObject& _blObj)
{ {
if (_blObj["blockHeader"].get_obj().size() != 14) auto ho = _blObj["blockHeader"].get_obj();
if (ho.size() != 14)
{ {
BlockInfo tmp = _header;
BlockInfo tmp = _currentBlockHeader; if (ho.count("parentHash"))
tmp.parentHash = h256(ho["parentHash"].get_str());
if (_blObj["blockHeader"].get_obj().count("parentHash")) if (ho.count("uncleHash"))
tmp.parentHash = h256(_blObj["blockHeader"].get_obj()["parentHash"].get_str()); tmp.sha3Uncles = h256(ho["uncleHash"].get_str());
if (ho.count("coinbase"))
if (_blObj["blockHeader"].get_obj().count("uncleHash")) tmp.coinbaseAddress = Address(ho["coinbase"].get_str());
tmp.sha3Uncles = h256(_blObj["blockHeader"].get_obj()["uncleHash"].get_str()); if (ho.count("stateRoot"))
tmp.stateRoot = h256(ho["stateRoot"].get_str());
if (_blObj["blockHeader"].get_obj().count("coinbase")) if (ho.count("transactionsTrie"))
tmp.coinbaseAddress = Address(_blObj["blockHeader"].get_obj()["coinbase"].get_str()); tmp.transactionsRoot = h256(ho["transactionsTrie"].get_str());
if (ho.count("receiptTrie"))
if (_blObj["blockHeader"].get_obj().count("stateRoot")) tmp.receiptsRoot = h256(ho["receiptTrie"].get_str());
tmp.stateRoot = h256(_blObj["blockHeader"].get_obj()["stateRoot"].get_str()); if (ho.count("bloom"))
tmp.logBloom = LogBloom(ho["bloom"].get_str());
if (_blObj["blockHeader"].get_obj().count("transactionsTrie")) if (ho.count("difficulty"))
tmp.transactionsRoot = h256(_blObj["blockHeader"].get_obj()["transactionsTrie"].get_str()); tmp.difficulty = toInt(ho["difficulty"]);
if (ho.count("number"))
if (_blObj["blockHeader"].get_obj().count("receiptTrie")) tmp.number = toInt(ho["number"]);
tmp.receiptsRoot = h256(_blObj["blockHeader"].get_obj()["receiptTrie"].get_str()); if (ho.count("gasLimit"))
tmp.gasLimit = toInt(ho["gasLimit"]);
if (_blObj["blockHeader"].get_obj().count("bloom")) if (ho.count("gasUsed"))
tmp.logBloom = LogBloom(_blObj["blockHeader"].get_obj()["bloom"].get_str()); tmp.gasUsed = toInt(ho["gasUsed"]);
if (ho.count("timestamp"))
if (_blObj["blockHeader"].get_obj().count("difficulty")) tmp.timestamp = toInt(ho["timestamp"]);
tmp.difficulty = toInt(_blObj["blockHeader"].get_obj()["difficulty"]); if (ho.count("extraData"))
tmp.extraData = importByteArray(ho["extraData"].get_str());
if (_blObj["blockHeader"].get_obj().count("number")) if (ho.count("mixHash"))
tmp.number = toInt(_blObj["blockHeader"].get_obj()["number"]); tmp.mixHash = h256(ho["mixHash"].get_str());
tmp.noteDirty();
if (_blObj["blockHeader"].get_obj().count("gasLimit"))
tmp.gasLimit = toInt(_blObj["blockHeader"].get_obj()["gasLimit"]);
if (_blObj["blockHeader"].get_obj().count("gasUsed"))
tmp.gasUsed = toInt(_blObj["blockHeader"].get_obj()["gasUsed"]);
if (_blObj["blockHeader"].get_obj().count("timestamp"))
tmp.timestamp = toInt(_blObj["blockHeader"].get_obj()["timestamp"]);
if (_blObj["blockHeader"].get_obj().count("extraData"))
tmp.extraData = importByteArray(_blObj["blockHeader"].get_obj()["extraData"].get_str());
if (_blObj["blockHeader"].get_obj().count("mixHash"))
tmp.mixHash = h256(_blObj["blockHeader"].get_obj()["mixHash"].get_str());
// find new valid nonce // find new valid nonce
if (tmp != _header)
if (tmp != _currentBlockHeader)
{ {
_currentBlockHeader = tmp; mine(tmp);
_header = tmp;
ProofOfWork pow;
std::pair<MineInfo, Ethash::Solution> ret;
while (!ProofOfWork::verify(_currentBlockHeader))
{
ret = pow.mine(_currentBlockHeader, 1000, true);
Ethash::assignResult(ret.second, _currentBlockHeader);
}
} }
} }
else else
{ {
// take the blockheader as is // take the blockheader as is
const bytes c_blockRLP = createBlockRLPFromFields(_blObj["blockHeader"].get_obj()); const bytes c_blockRLP = createBlockRLPFromFields(ho);
const RLP c_bRLP(c_blockRLP); const RLP c_bRLP(c_blockRLP);
_currentBlockHeader.populateFromHeader(c_bRLP, IgnoreNonce); _header.populateFromHeader(c_bRLP, IgnoreNonce);
} }
} }
@ -620,13 +595,7 @@ BlockInfo constructBlock(mObject& _o)
void updatePoW(BlockInfo& _bi) void updatePoW(BlockInfo& _bi)
{ {
ProofOfWork pow; mine(_bi);
std::pair<MineInfo, Ethash::Solution> ret;
while (!ProofOfWork::verify(_bi))
{
ret = pow.mine(_bi, 10000, true);
Ethash::assignResult(ret.second, _bi);
}
_bi.noteDirty(); _bi.noteDirty();
} }

View File

@ -25,7 +25,7 @@
#include "JsonSpiritHeaders.h" #include "JsonSpiritHeaders.h"
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libethcore/ProofOfWork.h> #include <libethcore/ProofOfWork.h>
#include <libethcore/Ethasher.h> #include <libethcore/EthashAux.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include "TestHelper.h" #include "TestHelper.h"
@ -63,18 +63,18 @@ BOOST_AUTO_TEST_CASE(basic_test)
unsigned cacheSize(o["cache_size"].get_int()); unsigned cacheSize(o["cache_size"].get_int());
h256 cacheHash(o["cache_hash"].get_str()); h256 cacheHash(o["cache_hash"].get_str());
BOOST_REQUIRE_EQUAL(Ethasher::get()->params(header).cache_size, cacheSize); BOOST_REQUIRE_EQUAL(EthashAux::get()->params(header).cache_size, cacheSize);
BOOST_REQUIRE_EQUAL(sha3(bytesConstRef((byte const*)Ethasher::get()->light(header), cacheSize)), cacheHash); BOOST_REQUIRE_EQUAL(sha3(bytesConstRef((byte const*)EthashAux::get()->light(header), cacheSize)), cacheHash);
#if TEST_FULL #if TEST_FULL
unsigned fullSize(o["full_size"].get_int()); unsigned fullSize(o["full_size"].get_int());
h256 fullHash(o["full_hash"].get_str()); h256 fullHash(o["full_hash"].get_str());
BOOST_REQUIRE_EQUAL(Ethasher::get()->full(header).size(), fullSize); BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header).size(), fullSize);
BOOST_REQUIRE_EQUAL(sha3(Ethasher::get()->full(header)), fullHash); BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header)), fullHash);
#endif #endif
h256 result(o["result"].get_str()); h256 result(o["result"].get_str());
Ethasher::Result r = Ethasher::eval(header); Ethash::Result r = EthashAux::eval(header);
BOOST_REQUIRE_EQUAL(r.value, result); BOOST_REQUIRE_EQUAL(r.value, result);
BOOST_REQUIRE_EQUAL(r.mixHash, header.mixHash); BOOST_REQUIRE_EQUAL(r.mixHash, header.mixHash);
} }

View File

@ -25,7 +25,9 @@
#include <secp256k1/secp256k1.h> #include <secp256k1/secp256k1.h>
#include <libethereum/CanonBlockChain.h> #include <libethereum/CanonBlockChain.h>
#include <libethereum/State.h> #include <libethereum/State.h>
#include <libethereum/Farm.h>
#include <libethereum/Defaults.h> #include <libethereum/Defaults.h>
#include "TestHelper.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
@ -67,10 +69,8 @@ BOOST_AUTO_TEST_CASE(Complex)
cout << s; cout << s;
// Mine to get some ether! // Mine to get some ether!
s.commitToMine(bc); mine(s, bc);
ProofOfWork pow;
while (!s.mine(&pow).completed) {}
s.completeMine();
bc.attemptImport(s.blockData(), stateDB); bc.attemptImport(s.blockData(), stateDB);
cout << bc; cout << bc;
@ -89,8 +89,7 @@ BOOST_AUTO_TEST_CASE(Complex)
// Mine to get some ether and set in stone. // Mine to get some ether and set in stone.
s.commitToMine(bc); s.commitToMine(bc);
s.commitToMine(bc); s.commitToMine(bc);
while (!s.mine(&pow).completed) {} mine(s, bc);
s.completeMine();
bc.attemptImport(s.blockData(), stateDB); bc.attemptImport(s.blockData(), stateDB);
cout << bc; cout << bc;