diff --git a/TestHelper.cpp b/TestHelper.cpp index 45fe55b07..93c564e62 100644 --- a/TestHelper.cpp +++ b/TestHelper.cpp @@ -62,6 +62,37 @@ void connectClients(Client& c1, Client& c2) c2.connect("127.0.0.1", c1Port); #endif } + +void mine(State& s, BlockChain const& _bc) +{ + s.commitToMine(_bc); + GenericFarm f; + bool completed = false; + f.onSolutionFound([&](ProofOfWork::Solution sol) + { + return completed = s.completeMine(sol); + }); + f.setWork(s.info()); + f.startCPU(); + while (!completed) + this_thread::sleep_for(chrono::milliseconds(20)); +} + +void mine(BlockInfo& _bi) +{ + GenericFarm 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 diff --git a/TestHelper.h b/TestHelper.h index 04ca95be4..92745bc36 100644 --- a/TestHelper.h +++ b/TestHelper.h @@ -36,9 +36,12 @@ namespace eth { class Client; +class State; void mine(Client& c, int numBlocks); void connectClients(Client& c1, Client& c2); +void mine(State& _s, BlockChain const& _bc); +void mine(BlockInfo& _bi); } @@ -225,7 +228,5 @@ public: }; }; - - } } diff --git a/blockchain.cpp b/blockchain.cpp index 97171e3f2..4aa70c63a 100644 --- a/blockchain.cpp +++ b/blockchain.cpp @@ -191,11 +191,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin) { state.sync(bc); state.sync(bc, txs, gp); - state.commitToMine(bc); - MineInfo info; - ProofOfWork pow; - for (info.completed = false; !info.completed; info = state.mine(&pow)) {} - state.completeMine(); + mine(state, bc); } catch (Exception const& _e) { @@ -531,76 +527,55 @@ bytes createBlockRLPFromFields(mObject& _tObj) 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 = _currentBlockHeader; - - if (_blObj["blockHeader"].get_obj().count("parentHash")) - tmp.parentHash = h256(_blObj["blockHeader"].get_obj()["parentHash"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("uncleHash")) - tmp.sha3Uncles = h256(_blObj["blockHeader"].get_obj()["uncleHash"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("coinbase")) - tmp.coinbaseAddress = Address(_blObj["blockHeader"].get_obj()["coinbase"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("stateRoot")) - tmp.stateRoot = h256(_blObj["blockHeader"].get_obj()["stateRoot"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("transactionsTrie")) - tmp.transactionsRoot = h256(_blObj["blockHeader"].get_obj()["transactionsTrie"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("receiptTrie")) - tmp.receiptsRoot = h256(_blObj["blockHeader"].get_obj()["receiptTrie"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("bloom")) - tmp.logBloom = LogBloom(_blObj["blockHeader"].get_obj()["bloom"].get_str()); - - if (_blObj["blockHeader"].get_obj().count("difficulty")) - tmp.difficulty = toInt(_blObj["blockHeader"].get_obj()["difficulty"]); - - if (_blObj["blockHeader"].get_obj().count("number")) - tmp.number = toInt(_blObj["blockHeader"].get_obj()["number"]); - - 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()); + BlockInfo tmp = _header; + if (ho.count("parentHash")) + tmp.parentHash = h256(ho["parentHash"].get_str()); + if (ho.count("uncleHash")) + tmp.sha3Uncles = h256(ho["uncleHash"].get_str()); + if (ho.count("coinbase")) + tmp.coinbaseAddress = Address(ho["coinbase"].get_str()); + if (ho.count("stateRoot")) + tmp.stateRoot = h256(ho["stateRoot"].get_str()); + if (ho.count("transactionsTrie")) + tmp.transactionsRoot = h256(ho["transactionsTrie"].get_str()); + if (ho.count("receiptTrie")) + tmp.receiptsRoot = h256(ho["receiptTrie"].get_str()); + if (ho.count("bloom")) + tmp.logBloom = LogBloom(ho["bloom"].get_str()); + if (ho.count("difficulty")) + tmp.difficulty = toInt(ho["difficulty"]); + if (ho.count("number")) + tmp.number = toInt(ho["number"]); + if (ho.count("gasLimit")) + tmp.gasLimit = toInt(ho["gasLimit"]); + if (ho.count("gasUsed")) + tmp.gasUsed = toInt(ho["gasUsed"]); + if (ho.count("timestamp")) + tmp.timestamp = toInt(ho["timestamp"]); + if (ho.count("extraData")) + tmp.extraData = importByteArray(ho["extraData"].get_str()); + if (ho.count("mixHash")) + tmp.mixHash = h256(ho["mixHash"].get_str()); + tmp.noteDirty(); // find new valid nonce - - if (tmp != _currentBlockHeader) + if (tmp != _header) { - _currentBlockHeader = tmp; - - ProofOfWork pow; - std::pair ret; - while (!ProofOfWork::verify(_currentBlockHeader)) - { - ret = pow.mine(_currentBlockHeader, 1000, true); - Ethash::assignResult(ret.second, _currentBlockHeader); - } + mine(tmp); + _header = tmp; } } else { // 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); - _currentBlockHeader.populateFromHeader(c_bRLP, IgnoreNonce); + _header.populateFromHeader(c_bRLP, IgnoreNonce); } } @@ -631,13 +606,7 @@ BlockInfo constructBlock(mObject& _o) void updatePoW(BlockInfo& _bi) { - ProofOfWork pow; - std::pair ret; - while (!ProofOfWork::verify(_bi)) - { - ret = pow.mine(_bi, 10000, true); - Ethash::assignResult(ret.second, _bi); - } + mine(_bi); _bi.noteDirty(); } diff --git a/dagger.cpp b/dagger.cpp index 4abba5090..cb8908d32 100644 --- a/dagger.cpp +++ b/dagger.cpp @@ -25,7 +25,7 @@ #include "JsonSpiritHeaders.h" #include #include -#include +#include #include #include "TestHelper.h" @@ -63,18 +63,18 @@ BOOST_AUTO_TEST_CASE(basic_test) unsigned cacheSize(o["cache_size"].get_int()); h256 cacheHash(o["cache_hash"].get_str()); - BOOST_REQUIRE_EQUAL(Ethasher::get()->params(header).cache_size, cacheSize); - BOOST_REQUIRE_EQUAL(sha3(bytesConstRef((byte const*)Ethasher::get()->light(header), cacheSize)), cacheHash); + BOOST_REQUIRE_EQUAL(EthashAux::get()->params(header).cache_size, cacheSize); + BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header)->data()), cacheHash); #if TEST_FULL unsigned fullSize(o["full_size"].get_int()); h256 fullHash(o["full_hash"].get_str()); - BOOST_REQUIRE_EQUAL(Ethasher::get()->full(header).size(), fullSize); - BOOST_REQUIRE_EQUAL(sha3(Ethasher::get()->full(header)), fullHash); + BOOST_REQUIRE_EQUAL(EthashAux::get()->full(header).size(), fullSize); + BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->full(header)), fullHash); #endif 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.mixHash, header.mixHash); } diff --git a/net.cpp b/net.cpp index ef7227cb8..28b4f4286 100644 --- a/net.cpp +++ b/net.cpp @@ -229,6 +229,33 @@ BOOST_AUTO_TEST_CASE(v2PingNodePacket) BOOST_REQUIRE(p.version == 2); } +BOOST_AUTO_TEST_CASE(neighboursPacketLength) +{ + KeyPair k = KeyPair::create(); + std::vector> testNodes(TestNodeTable::createTestNodes(16)); + bi::udp::endpoint to(boost::asio::ip::address::from_string("127.0.0.1"), 30000); + + // hash(32), signature(65), overhead: packet(2), type(1), nodeList(2), ts(9), + static unsigned const nlimit = (1280 - 111) / 87; + for (unsigned offset = 0; offset < testNodes.size(); offset += nlimit) + { + Neighbours out(to); + + auto limit = nlimit ? std::min(testNodes.size(), (size_t)(offset + nlimit)) : testNodes.size(); + for (auto i = offset; i < limit; i++) + { + Neighbours::Node node; + node.ipAddress = boost::asio::ip::address::from_string("200.200.200.200").to_string(); + node.port = testNodes[i].second; + node.node = testNodes[i].first.pub(); + out.nodes.push_back(node); + } + + out.sign(k.sec()); + BOOST_REQUIRE_LE(out.data.size(), 1280); + } +} + BOOST_AUTO_TEST_CASE(test_neighbours_packet) { KeyPair k = KeyPair::create(); diff --git a/stateOriginal.cpp b/stateOriginal.cpp index 40f759434..e1a3c7c4a 100644 --- a/stateOriginal.cpp +++ b/stateOriginal.cpp @@ -25,7 +25,9 @@ #include #include #include +#include #include +#include "TestHelper.h" using namespace std; using namespace dev; using namespace dev::eth; @@ -67,10 +69,8 @@ BOOST_AUTO_TEST_CASE(Complex) cout << s; // Mine to get some ether! - s.commitToMine(bc); - ProofOfWork pow; - while (!s.mine(&pow).completed) {} - s.completeMine(); + mine(s, bc); + bc.attemptImport(s.blockData(), stateDB); cout << bc; @@ -89,8 +89,7 @@ BOOST_AUTO_TEST_CASE(Complex) // Mine to get some ether and set in stone. s.commitToMine(bc); s.commitToMine(bc); - while (!s.mine(&pow).completed) {} - s.completeMine(); + mine(s, bc); bc.attemptImport(s.blockData(), stateDB); cout << bc;