From 1efd3c26876cb05efcdce073b967088e01b9818b Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 10 Apr 2015 08:50:24 +0200 Subject: [PATCH 1/7] Squashed 'libjsqrc/ethereumjs/' changes from 73b9ed2..4def095 4def095 updated README.md 15b4dbd updated bower && package.json files 0ccc05a web3 in global namespace ccc59d1 version 0.2.6 9c2c946 Merge branch 'web3' into develop 6763f34 tests for creating new contract with nondefault constructor, added missing files 2ef5efc fixed #70, creating contract with nondefault constructor dbe4015 Merge branch 'master' into develop 48b351a add gasPrice test and fixed failing uncle tests 7f75f3e fixed gasPrice output ddec629 fixed getUncle parameter count 1e89ef0 changed to eth_protocolVersion 6add9bd Merge pull request #151 from ethereum/develop git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: 4def0958d35cb5b8d92d277423af4d435dd89f16 --- abi.formatConstructorParams.js | 106 +++++++++++++++++++++++++++++++++ abi.inputParser.js | 11 ++-- utils.filters.js | 2 +- utils.isAddress.js | 2 +- utils.isStrictAddress.js | 23 +++++++ web3.eth.contract.js | 49 ++++++++++++--- web3.eth.gasPrice.js | 39 ++++++++++++ web3.eth.getUncle.js | 6 +- 8 files changed, 220 insertions(+), 18 deletions(-) create mode 100644 abi.formatConstructorParams.js create mode 100644 utils.isStrictAddress.js create mode 100644 web3.eth.gasPrice.js diff --git a/abi.formatConstructorParams.js b/abi.formatConstructorParams.js new file mode 100644 index 000000000..9113f02c6 --- /dev/null +++ b/abi.formatConstructorParams.js @@ -0,0 +1,106 @@ +var chai = require('chai'); +var assert = require('assert'); +var abi = require('../lib/solidity/abi'); + +describe('lib/solidity/abi', function () { + describe('formatConstructorParams', function () { + it('should format uint256 properly', function () { + // given + var description = [{ + "name": "test", + "type": "constructor", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ] + }]; + + // when + var bytes = abi.formatConstructorParams(description, [2]); + + // then + assert.equal(bytes, '0000000000000000000000000000000000000000000000000000000000000002'); + }); + + it('should not find matching constructor', function () { + // given + var description = [{ + "name": "test", + "type": "constructor", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ] + }]; + + // when + var bytes = abi.formatConstructorParams(description, []); + + // then + assert.equal(bytes, ''); + }); + + it('should not find matching constructor2', function () { + // given + var description = [{ + "name": "test", + "type": "constructor", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ] + }]; + + // when + var bytes = abi.formatConstructorParams(description, [1,2]); + + // then + assert.equal(bytes, ''); + }); + + it('should not find matching constructor3', function () { + // given + var description = [{ + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ] + }]; + + // when + var bytes = abi.formatConstructorParams(description, [2]); + + // then + assert.equal(bytes, ''); + }); + + it('should find matching constructor with multiple args', function () { + // given + var description = [{ + "name": "test", + "type": "constructor", + "inputs": [{ + "name": "a", + "type": "uint256" + }, { + "name": "b", + "type": "uint256" + }] + }]; + + // when + var bytes = abi.formatConstructorParams(description, ['1', '5']); + + // then + assert.equal(bytes, '00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000005'); + }); + }); +}); + + diff --git a/abi.inputParser.js b/abi.inputParser.js index a488bb2e5..7dc50b537 100644 --- a/abi.inputParser.js +++ b/abi.inputParser.js @@ -1,6 +1,7 @@ -var assert = require('assert'); +var chai = require('chai'); +var assert = chai.assert; var BigNumber = require('bignumber.js'); -var abi = require('../lib/solidity/abi.js'); +var abi = require('../lib/solidity/abi'); var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; var description = [{ @@ -19,9 +20,9 @@ var description = [{ ] }]; -describe('lib/solidity/abi', function() { - describe('inputParser', function() { - it('should parse input uint', function() { +describe('lib/solidity/abi', function () { + describe('inputParser', function () { + it('should parse input uint', function () { // given var d = clone(description); diff --git a/utils.filters.js b/utils.filters.js index 5e6870efa..5f37b6f1a 100644 --- a/utils.filters.js +++ b/utils.filters.js @@ -1,5 +1,5 @@ var assert = require('assert'); -var utils = require('../lib/utils/utils.js'); +var utils = require('../lib/solidity/utils'); describe('lib/utils/utils', function() { it('should filter functions and events from input array properly', function () { diff --git a/utils.isAddress.js b/utils.isAddress.js index 3ebbce837..a0658e303 100644 --- a/utils.isAddress.js +++ b/utils.isAddress.js @@ -8,7 +8,7 @@ var tests = [ { value: 'function', is: false}, { value: {}, is: false}, { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', is: true }, - { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: false } + { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: true } ]; describe('lib/utils/utils', function () { diff --git a/utils.isStrictAddress.js b/utils.isStrictAddress.js new file mode 100644 index 000000000..e23e3deec --- /dev/null +++ b/utils.isStrictAddress.js @@ -0,0 +1,23 @@ +var chai = require('chai'); +var utils = require('../lib/utils/utils.js'); +var assert = chai.assert; + +var tests = [ + { value: function () {}, is: false}, + { value: new Function(), is: false}, + { value: 'function', is: false}, + { value: {}, is: false}, + { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', is: true }, + { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: false } +]; + +describe('lib/utils/utils', function () { + describe('isStrictAddress', function () { + tests.forEach(function (test) { + it('shoud test if value ' + test.value + ' is address: ' + test.is, function () { + assert.equal(utils.isStrictAddress(test.value), test.is); + }); + }); + }); +}); + diff --git a/web3.eth.contract.js b/web3.eth.contract.js index 0f2fd8640..a657545db 100644 --- a/web3.eth.contract.js +++ b/web3.eth.contract.js @@ -1,5 +1,7 @@ var assert = require('assert'); var contract = require('../lib/web3/contract.js'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var web3 = require('../index'); describe('web3.eth.contract', function() { it('should create simple contract with one method from abi with explicit type name', function () { @@ -20,10 +22,11 @@ describe('web3.eth.contract', function() { } ] }]; + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('function', typeof myCon.test); @@ -48,10 +51,11 @@ describe('web3.eth.contract', function() { } ] }]; + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('function', typeof myCon.test); @@ -90,10 +94,11 @@ describe('web3.eth.contract', function() { } ] }]; + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('function', typeof myCon.test); @@ -134,10 +139,11 @@ describe('web3.eth.contract', function() { } ] }]; + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('function', typeof myCon.test); @@ -162,11 +168,11 @@ describe('web3.eth.contract', function() { } ] }]; - + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('undefined', typeof myCon.test); @@ -191,11 +197,11 @@ describe('web3.eth.contract', function() { } ] }]; - + var address = '0x1234567890123456789012345678901234567890'; // when var Con = contract(description); - var myCon = new Con(null); + var myCon = new Con(address); // then assert.equal('function', typeof myCon.test); @@ -203,5 +209,32 @@ describe('web3.eth.contract', function() { }); + it('should create contract with nondefault constructor', function (done) { + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + web3.reset(); // reset different polls + var address = '0x1234567890123456789012345678901234567890'; + var code = '0x31241231231123123123123121cf121212i123123123123123512312412512111111'; + var description = [{ + "name": "test", + "type": "constructor", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ] + }]; + + provider.injectResult(address); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, 'eth_sendTransaction'); + assert.equal(payload.params[0].data, code + '0000000000000000000000000000000000000000000000000000000000000002'); + done(); + }); + + var Con = contract(description); + var myCon = new Con(code, 2); + }); }); diff --git a/web3.eth.gasPrice.js b/web3.eth.gasPrice.js new file mode 100644 index 000000000..eb7272b4d --- /dev/null +++ b/web3.eth.gasPrice.js @@ -0,0 +1,39 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var BigNumber = require('bignumber.js'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +var method = 'gasPrice'; + +var tests = [{ + result: '0x15f90', + formattedResult: new BigNumber(90000), + call: 'eth_'+ method +}]; + +describe('web3.eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('property test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, []); + }); + + // when + var result = web3.eth[method]; + + // then + assert.deepEqual(test.formattedResult, result); + }); + }); + }); +}); + diff --git a/web3.eth.getUncle.js b/web3.eth.getUncle.js index 111865355..c5d723dc9 100644 --- a/web3.eth.getUncle.js +++ b/web3.eth.getUncle.js @@ -118,19 +118,19 @@ var formattedBlockResultWithTx = { var tests = [{ args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 2], - formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x2', false], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x2'], result: blockResult, formattedResult: formattedBlockResult, call: 'eth_getUncleByBlockHashAndIndex' },{ args: [436, 1], - formattedArgs: ['0x1b4', '0x1', false], + formattedArgs: ['0x1b4', '0x1'], result: blockResult, formattedResult: formattedBlockResult, call: 'eth_getUncleByBlockNumberAndIndex' },{ args: [436, 1, true], - formattedArgs: ['0x1b4', '0x1', true], + formattedArgs: ['0x1b4', '0x1'], result: blockResultWithTx, formattedResult: formattedBlockResultWithTx, call: 'eth_getUncleByBlockNumberAndIndex' From fbb5adc9f9be5b9749c88592060eb07d71e60ab1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 11 Apr 2015 15:52:36 +0200 Subject: [PATCH 2/7] Some early refactoring to support async miners better. --- blockchain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain.cpp b/blockchain.cpp index 15cda8037..6d7bc97ef 100644 --- a/blockchain.cpp +++ b/blockchain.cpp @@ -575,7 +575,7 @@ void overwriteBlockHeader(BlockInfo& _currentBlockHeader, mObject& _blObj) _currentBlockHeader = tmp; ProofOfWork pow; - std::pair ret; + std::pair ret; while (!ProofOfWork::verify(_currentBlockHeader)) { ret = pow.mine(_currentBlockHeader, 1000, true); @@ -621,7 +621,7 @@ BlockInfo constructBlock(mObject& _o) void updatePoW(BlockInfo& _bi) { ProofOfWork pow; - std::pair ret; + std::pair ret; while (!ProofOfWork::verify(_bi)) { ret = pow.mine(_bi, 10000, true); From 8f659f39813d4418d1c1e016f4d84309a8044946 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 13 Apr 2015 18:12:05 +0200 Subject: [PATCH 3/7] Various fixes for mining. --- TestHelper.cpp | 31 +++++++++++++ TestHelper.h | 5 ++- blockchain.cpp | 111 +++++++++++++++++----------------------------- dagger.cpp | 12 ++--- stateOriginal.cpp | 11 +++-- 5 files changed, 85 insertions(+), 85 deletions(-) diff --git a/TestHelper.cpp b/TestHelper.cpp index e86b84aad..8b528b3ee 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 6d7bc97ef..258117695 100644 --- a/blockchain.cpp +++ b/blockchain.cpp @@ -190,11 +190,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) { @@ -519,76 +515,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); } } @@ -620,13 +595,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..367c422ad 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(bytesConstRef((byte const*)EthashAux::get()->light(header), cacheSize)), 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/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; From b6ee801879a583c5ca199262ab2df5babaea10c4 Mon Sep 17 00:00:00 2001 From: subtly Date: Tue, 14 Apr 2015 02:44:01 -0400 Subject: [PATCH 4/7] proper neighbours packet size w/test --- net.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/net.cpp b/net.cpp index ec1efb360..c6aeec8f1 100644 --- a/net.cpp +++ b/net.cpp @@ -190,6 +190,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(); From d0efe9333fc88bae5ab82ef0435d99d9be4e21d4 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 11 Apr 2015 15:52:36 +0200 Subject: [PATCH 5/7] Some early refactoring to support async miners better. --- blockchain.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain.cpp b/blockchain.cpp index 97171e3f2..8f5605898 100644 --- a/blockchain.cpp +++ b/blockchain.cpp @@ -587,7 +587,7 @@ void overwriteBlockHeader(BlockInfo& _currentBlockHeader, mObject& _blObj) _currentBlockHeader = tmp; ProofOfWork pow; - std::pair ret; + std::pair ret; while (!ProofOfWork::verify(_currentBlockHeader)) { ret = pow.mine(_currentBlockHeader, 1000, true); @@ -632,7 +632,7 @@ BlockInfo constructBlock(mObject& _o) void updatePoW(BlockInfo& _bi) { ProofOfWork pow; - std::pair ret; + std::pair ret; while (!ProofOfWork::verify(_bi)) { ret = pow.mine(_bi, 10000, true); From efdd3790a0405654933f456751230254cef95151 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 13 Apr 2015 18:12:05 +0200 Subject: [PATCH 6/7] Various fixes for mining. --- TestHelper.cpp | 31 +++++++++++++ TestHelper.h | 5 ++- blockchain.cpp | 111 +++++++++++++++++----------------------------- dagger.cpp | 12 ++--- stateOriginal.cpp | 11 +++-- 5 files changed, 85 insertions(+), 85 deletions(-) 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 8f5605898..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..367c422ad 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(bytesConstRef((byte const*)EthashAux::get()->light(header), cacheSize)), 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/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; From 5469743891679267ea1fdad0cd68099101e43b19 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Wed, 15 Apr 2015 17:24:57 +0200 Subject: [PATCH 7/7] Attempt a fix of dagger tests to fix the build --- dagger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dagger.cpp b/dagger.cpp index 367c422ad..cb8908d32 100644 --- a/dagger.cpp +++ b/dagger.cpp @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(basic_test) unsigned cacheSize(o["cache_size"].get_int()); h256 cacheHash(o["cache_hash"].get_str()); BOOST_REQUIRE_EQUAL(EthashAux::get()->params(header).cache_size, cacheSize); - BOOST_REQUIRE_EQUAL(sha3(bytesConstRef((byte const*)EthashAux::get()->light(header), cacheSize)), cacheHash); + BOOST_REQUIRE_EQUAL(sha3(EthashAux::get()->light(header)->data()), cacheHash); #if TEST_FULL unsigned fullSize(o["full_size"].get_int());