From 5b385908b32b0ba2ee88ec9a535e67c852467005 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 8 Jan 2015 19:35:32 +0100 Subject: [PATCH 01/27] mocha test init --- methods.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 methods.js diff --git a/methods.js b/methods.js new file mode 100644 index 000000000..54d96b486 --- /dev/null +++ b/methods.js @@ -0,0 +1,49 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +var methodExists = function (object, method) { + assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented'); +}; + +var propertyExists = function (object, property) { + assert.equal('object', typeof object[property], 'property ' + property + ' is not implemented'); +}; + +describe('web3', function() { + describe('eth', function() { + it('should have all methods implemented', function() { + methodExists(web3.eth, 'balanceAt'); + methodExists(web3.eth, 'stateAt'); + methodExists(web3.eth, 'storageAt'); + methodExists(web3.eth, 'countAt'); + methodExists(web3.eth, 'codeAt'); + methodExists(web3.eth, 'transact'); + methodExists(web3.eth, 'call'); + methodExists(web3.eth, 'block'); + methodExists(web3.eth, 'transaction'); + methodExists(web3.eth, 'uncle'); + methodExists(web3.eth, 'compilers'); + methodExists(web3.eth, 'lll'); + methodExists(web3.eth, 'solidity'); + methodExists(web3.eth, 'serpent'); + methodExists(web3.eth, 'logs'); + }); + + it('should have all properties implemented', function () { + propertyExists(web3.eth, 'coinbase'); + propertyExists(web3.eth, 'listening'); + propertyExists(web3.eth, 'mining'); + propertyExists(web3.eth, 'gasPrice'); + propertyExists(web3.eth, 'account'); + propertyExists(web3.eth, 'accounts'); + propertyExists(web3.eth, 'peerCount'); + propertyExists(web3.eth, 'defaultBlock'); + propertyExists(web3.eth, 'number'); + }); + }); +}) + + From e0ad98388a3b619a076158b5e235edffc8c4206e Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 8 Jan 2015 19:59:03 +0100 Subject: [PATCH 02/27] methods existance tests in progress --- db.methods.js | 18 ++++++++++++++++++ eth.methods.js | 42 ++++++++++++++++++++++++++++++++++++++++++ methods.js | 49 ------------------------------------------------- shh.methods.js | 19 +++++++++++++++++++ utils.js | 15 +++++++++++++++ web3.methods.js | 18 ++++++++++++++++++ 6 files changed, 112 insertions(+), 49 deletions(-) create mode 100644 db.methods.js create mode 100644 eth.methods.js delete mode 100644 methods.js create mode 100644 shh.methods.js create mode 100644 utils.js create mode 100644 web3.methods.js diff --git a/db.methods.js b/db.methods.js new file mode 100644 index 000000000..b4abfc4d7 --- /dev/null +++ b/db.methods.js @@ -0,0 +1,18 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('db', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.db, 'put'); + u.methodExists(web3.db, 'get'); + u.methodExists(web3.db, 'putString'); + u.methodExists(web3.db, 'getString'); + }); + }); +}); + diff --git a/eth.methods.js b/eth.methods.js new file mode 100644 index 000000000..7190b27d2 --- /dev/null +++ b/eth.methods.js @@ -0,0 +1,42 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('eth', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.eth, 'balanceAt'); + u.methodExists(web3.eth, 'stateAt'); + u.methodExists(web3.eth, 'storageAt'); + u.methodExists(web3.eth, 'countAt'); + u.methodExists(web3.eth, 'codeAt'); + u.methodExists(web3.eth, 'transact'); + u.methodExists(web3.eth, 'call'); + u.methodExists(web3.eth, 'block'); + u.methodExists(web3.eth, 'transaction'); + u.methodExists(web3.eth, 'uncle'); + u.methodExists(web3.eth, 'compilers'); + u.methodExists(web3.eth, 'lll'); + u.methodExists(web3.eth, 'solidity'); + u.methodExists(web3.eth, 'serpent'); + u.methodExists(web3.eth, 'logs'); + }); + + it('should have all properties implemented', function () { + u.propertyExists(web3.eth, 'coinbase'); + u.propertyExists(web3.eth, 'listening'); + u.propertyExists(web3.eth, 'mining'); + u.propertyExists(web3.eth, 'gasPrice'); + u.propertyExists(web3.eth, 'account'); + u.propertyExists(web3.eth, 'accounts'); + u.propertyExists(web3.eth, 'peerCount'); + u.propertyExists(web3.eth, 'defaultBlock'); + u.propertyExists(web3.eth, 'number'); + }); + }); +}); + + diff --git a/methods.js b/methods.js deleted file mode 100644 index 54d96b486..000000000 --- a/methods.js +++ /dev/null @@ -1,49 +0,0 @@ -require('es6-promise').polyfill(); - -var assert = require('assert'); -var web3 = require('../index.js'); -web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider - -var methodExists = function (object, method) { - assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented'); -}; - -var propertyExists = function (object, property) { - assert.equal('object', typeof object[property], 'property ' + property + ' is not implemented'); -}; - -describe('web3', function() { - describe('eth', function() { - it('should have all methods implemented', function() { - methodExists(web3.eth, 'balanceAt'); - methodExists(web3.eth, 'stateAt'); - methodExists(web3.eth, 'storageAt'); - methodExists(web3.eth, 'countAt'); - methodExists(web3.eth, 'codeAt'); - methodExists(web3.eth, 'transact'); - methodExists(web3.eth, 'call'); - methodExists(web3.eth, 'block'); - methodExists(web3.eth, 'transaction'); - methodExists(web3.eth, 'uncle'); - methodExists(web3.eth, 'compilers'); - methodExists(web3.eth, 'lll'); - methodExists(web3.eth, 'solidity'); - methodExists(web3.eth, 'serpent'); - methodExists(web3.eth, 'logs'); - }); - - it('should have all properties implemented', function () { - propertyExists(web3.eth, 'coinbase'); - propertyExists(web3.eth, 'listening'); - propertyExists(web3.eth, 'mining'); - propertyExists(web3.eth, 'gasPrice'); - propertyExists(web3.eth, 'account'); - propertyExists(web3.eth, 'accounts'); - propertyExists(web3.eth, 'peerCount'); - propertyExists(web3.eth, 'defaultBlock'); - propertyExists(web3.eth, 'number'); - }); - }); -}) - - diff --git a/shh.methods.js b/shh.methods.js new file mode 100644 index 000000000..08f573a3c --- /dev/null +++ b/shh.methods.js @@ -0,0 +1,19 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('shh', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.shh, 'post'); + u.methodExists(web3.shh, 'newIdentity'); + u.methodExists(web3.shh, 'haveIdentity'); + u.methodExists(web3.shh, 'newGroup'); + u.methodExists(web3.shh, 'addToGroup'); + }); + }); +}); + diff --git a/utils.js b/utils.js new file mode 100644 index 000000000..4c508da67 --- /dev/null +++ b/utils.js @@ -0,0 +1,15 @@ +var assert = require('assert'); + +var methodExists = function (object, method) { + assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented'); +}; + +var propertyExists = function (object, property) { + assert.equal('object', typeof object[property], 'property ' + property + ' is not implemented'); +}; + +module.exports = { + methodExists: methodExists, + propertyExists: propertyExists +}; + diff --git a/web3.methods.js b/web3.methods.js new file mode 100644 index 000000000..a7e020978 --- /dev/null +++ b/web3.methods.js @@ -0,0 +1,18 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + it('should have all methods implemented', function() { + u.methodExists(web3, 'sha3'); + u.methodExists(web3, 'toAscii'); + u.methodExists(web3, 'fromAscii'); + u.methodExists(web3, 'toFixed'); + u.methodExists(web3, 'fromFixed'); + u.methodExists(web3, 'offset'); + }); +}); + From 2ee9229ca65a2f76e35aa7bcd72fa346ab55a45d Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 8 Jan 2015 20:24:30 +0100 Subject: [PATCH 03/27] mocha opts file && init of parser tests --- abi.parsers.js | 37 +++++++++++++++++++++++++++++++++++++ mocha.opts | 2 ++ 2 files changed, 39 insertions(+) create mode 100644 abi.parsers.js create mode 100644 mocha.opts diff --git a/abi.parsers.js b/abi.parsers.js new file mode 100644 index 000000000..06a77fb86 --- /dev/null +++ b/abi.parsers.js @@ -0,0 +1,37 @@ +var assert = require('assert'); +var abi = require('../lib/abi.js'); + +describe('abi', function() { + describe('inputParser', function() { + it('should parse ...', function() { + + var desc = [{ + "name": "multiply", + "inputs": [ + { + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + var iParser = abi.inputParser(desc); + assert.equal(iParser.multiply(1), "0x000000000000000000000000000000000000000000000000000000000000000001"); + + }); + }); + + + describe('outputParser', function() { + it('parse ...', function() { + + }); + }); +}); + diff --git a/mocha.opts b/mocha.opts new file mode 100644 index 000000000..b83917bb2 --- /dev/null +++ b/mocha.opts @@ -0,0 +1,2 @@ +--reporter Nyan + From 22ddeea10270f787a120ec8f79af50c3305a9224 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 9 Jan 2015 00:58:32 +0100 Subject: [PATCH 04/27] Padding for ABI types. --- SolidityEndToEndTest.cpp | 135 +++++++++++++++++------------------ solidityExecutionFramework.h | 43 +++++++---- 2 files changed, 95 insertions(+), 83 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index bc184dfca..7ed7ad594 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -331,10 +331,23 @@ BOOST_AUTO_TEST_CASE(packing_unpacking_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0")) + BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", true, fromHex("0f0f0f0f"), fromHex("f0f0f0f0f0f0f0f0")) == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f")); } +BOOST_AUTO_TEST_CASE(packing_signed_types) +{ + char const* sourceCode = "contract test {\n" + " function run() returns(int8 y) {\n" + " uint8 x = 0xfa;\n" + " return int8(x);\n" + " }\n" + "}\n"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("run()") + == fromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa")); +} + BOOST_AUTO_TEST_CASE(multiple_return_values) { char const* sourceCode = "contract test {\n" @@ -343,8 +356,7 @@ BOOST_AUTO_TEST_CASE(multiple_return_values) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(bool,uint256)", bytes(1, 1) + toBigEndian(u256(0xcd))) - == toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0))); + BOOST_CHECK(callContractFunction("run(bool,uint256)", true, 0xcd) == encodeArgs(0xcd, true, 0)); } BOOST_AUTO_TEST_CASE(short_circuiting) @@ -450,20 +462,8 @@ BOOST_AUTO_TEST_CASE(strings) " }\n" "}\n"; compileAndRun(sourceCode); - bytes expectation(32, 0); - expectation[0] = byte('a'); - expectation[1] = byte('b'); - expectation[2] = byte('c'); - expectation[3] = byte(0); - expectation[4] = byte(0xff); - expectation[5] = byte('_'); - expectation[6] = byte('_'); - BOOST_CHECK(callContractFunction("fixed()", bytes()) == expectation); - expectation = bytes(17, 0); - expectation[0] = 0; - expectation[1] = 2; - expectation[16] = 1; - BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", bytes({0x00, 0x02, 0x01})) == expectation); + BOOST_CHECK(callContractFunction("fixed()") == encodeArgs(string("abc\0\xff__", 7))); + BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true)); } BOOST_AUTO_TEST_CASE(empty_string_on_stack) @@ -477,7 +477,7 @@ BOOST_AUTO_TEST_CASE(empty_string_on_stack) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(string0,uint8)", bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00})); + BOOST_CHECK(callContractFunction("run(string0,uint8)", string(), byte(0x02)) == encodeArgs(0x2, string(""), string("abc\0"))); } BOOST_AUTO_TEST_CASE(state_smoke_test) @@ -495,14 +495,14 @@ BOOST_AUTO_TEST_CASE(state_smoke_test) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes()); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes()); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x1234))); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0x8765))); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes()); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x3))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0)); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x1234) == encodeArgs()); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x01), 0x8765) == encodeArgs()); + BOOST_CHECK(callContractFunction("get(uint8)", byte( 0x00)) == encodeArgs(0x1234)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0x8765)); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x3) == encodeArgs()); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0x3)); } BOOST_AUTO_TEST_CASE(compound_assign) @@ -553,22 +553,21 @@ BOOST_AUTO_TEST_CASE(simple_mapping) "}"; compileAndRun(sourceCode); - // msvc seems to have problems with initializer-list, when there is only 1 param in the list - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x01, 0xa1})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x00, 0xef})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x01, 0x05})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x05)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x01), byte(0xa1)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x00), byte(0xef)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x01), byte(0x05)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x05))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); } BOOST_AUTO_TEST_CASE(mapping_state) @@ -736,9 +735,9 @@ BOOST_AUTO_TEST_CASE(structs) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(false)); BOOST_CHECK(callContractFunction("set()") == bytes()); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(true)); } BOOST_AUTO_TEST_CASE(struct_reference) @@ -764,9 +763,9 @@ BOOST_AUTO_TEST_CASE(struct_reference) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(false)); BOOST_CHECK(callContractFunction("set()") == bytes()); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(true)); } BOOST_AUTO_TEST_CASE(constructor) @@ -799,7 +798,7 @@ BOOST_AUTO_TEST_CASE(balance) " }\n" "}\n"; compileAndRun(sourceCode, 23); - BOOST_CHECK(callContractFunction("getBalance()") == toBigEndian(u256(23))); + BOOST_CHECK(callContractFunction("getBalance()") == encodeArgs(23)); } BOOST_AUTO_TEST_CASE(blockchain) @@ -812,7 +811,7 @@ BOOST_AUTO_TEST_CASE(blockchain) " }\n" "}\n"; compileAndRun(sourceCode, 27); - BOOST_CHECK(callContractFunction("someInfo()", bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1))); + BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0, 1)); } BOOST_AUTO_TEST_CASE(function_types) @@ -831,8 +830,8 @@ BOOST_AUTO_TEST_CASE(function_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("a(bool)", bytes{0}) == toBigEndian(u256(11))); - BOOST_CHECK(callContractFunction("a(bool)", bytes{1}) == toBigEndian(u256(12))); + BOOST_CHECK(callContractFunction("a(bool)", false) == encodeArgs(11)); + BOOST_CHECK(callContractFunction("a(bool)", true) == encodeArgs(12)); } BOOST_AUTO_TEST_CASE(send_ether) @@ -846,7 +845,7 @@ BOOST_AUTO_TEST_CASE(send_ether) u256 amount(130); compileAndRun(sourceCode, amount + 1); u160 address(23); - BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == toBigEndian(u256(1))); + BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == encodeArgs(1)); BOOST_CHECK_EQUAL(m_state.balance(address), amount); } @@ -1031,7 +1030,7 @@ BOOST_AUTO_TEST_CASE(ecrecover) u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f"); u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549"); u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"); - BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == toBigEndian(addr)); + BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == encodeArgs(addr)); } BOOST_AUTO_TEST_CASE(inter_contract_calls) @@ -1058,10 +1057,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) @@ -1088,11 +1087,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == toBigEndian(a * 3)); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == toBigEndian(b * 3)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == encodeArgs(b * 3)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) @@ -1119,8 +1118,8 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); - BOOST_REQUIRE(callContractFunction("callHelper()") == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(helperAddress)); } BOOST_AUTO_TEST_CASE(calls_to_this) @@ -1150,10 +1149,10 @@ BOOST_AUTO_TEST_CASE(calls_to_this) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 10)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) @@ -1185,10 +1184,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 9)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9)); } BOOST_AUTO_TEST_CASE(strings_in_calls) @@ -1215,8 +1214,8 @@ BOOST_AUTO_TEST_CASE(strings_in_calls) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); - BOOST_CHECK(callContractFunction("callHelper(string2,bool)", bytes({0, 'a', 1})) == bytes({0, 'a', 0, 0, 0})); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); } BOOST_AUTO_TEST_CASE(constructor_arguments) @@ -1241,8 +1240,8 @@ BOOST_AUTO_TEST_CASE(constructor_arguments) function getName() returns (string3 ret) { return h.getName(); } })"; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("getFlag()") == bytes({byte(0x01)})); - BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); } BOOST_AUTO_TEST_CASE(functions_called_by_constructor) @@ -1259,7 +1258,7 @@ BOOST_AUTO_TEST_CASE(functions_called_by_constructor) function setName(string3 _name) { name = _name; } })"; compileAndRun(sourceCode); - BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/solidityExecutionFramework.h b/solidityExecutionFramework.h index 5ef23fdb1..8d3c7e77c 100644 --- a/solidityExecutionFramework.h +++ b/solidityExecutionFramework.h @@ -32,9 +32,6 @@ namespace dev { -/// Provides additional overloads for toBigEndian to encode arguments and return values. -inline bytes toBigEndian(byte _value) { return bytes({_value}); } -inline bytes toBigEndian(bool _value) { return bytes({byte(_value)}); } namespace solidity { @@ -56,18 +53,20 @@ public: return m_output; } - bytes const& callContractFunction(std::string _sig, bytes const& _data = bytes(), - u256 const& _value = 0) + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, + Args const&... _arguments) { + FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + _data, false, _value); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); return m_output; } template bytes const& callContractFunction(std::string _sig, Args const&... _arguments) { - return callContractFunction(_sig, argsToBigEndian(_arguments...)); + return callContractFunctionWithValue(_sig, 0, _arguments...); } template @@ -89,19 +88,33 @@ public: bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match." "\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult) + - "\nArgument: " + toHex(toBigEndian(argument))); + "\nArgument: " + toHex(encode(argument))); } } -private: - template - bytes argsToBigEndian(FirstArg const& _firstArg, Args const&... _followingArgs) const + static bytes encode(bool _value) { return encode(byte(_value)); } + static bytes encode(int _value) { return encode(u256(_value)); } + static bytes encode(char const* _value) { return encode(std::string(_value)); } + static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } + static bytes encode(u256 const& _value) { return toBigEndian(_value); } + static bytes encode(bytes const& _value, bool _padLeft = true) { - return toBigEndian(_firstArg) + argsToBigEndian(_followingArgs...); + bytes padding = bytes((32 - _value.size() % 32) % 32, 0); + return _padLeft ? padding + _value : _value + padding; + } + static bytes encode(std::string const& _value) { return encode(asBytes(_value), false); } + + template + static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) + { + return encode(_firstArg) + encodeArgs(_followingArgs...); + } + static bytes encodeArgs() + { + return bytes(); } - bytes argsToBigEndian() const { return bytes(); } - +private: template auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) -> typename std::enable_if::value, bytes>::type @@ -113,7 +126,7 @@ private: auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) -> typename std::enable_if::value, bytes>::type { - return toBigEndian(_cppFunction(_arguments...)); + return encode(_cppFunction(_arguments...)); } void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) From 3d896c74e605dd160994f0f1c84ec72d29484fac Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 7 Jan 2015 23:45:26 +0100 Subject: [PATCH 05/27] Correct type conversions. --- SolidityEndToEndTest.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index bc184dfca..baad4b42d 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -835,6 +835,21 @@ BOOST_AUTO_TEST_CASE(function_types) BOOST_CHECK(callContractFunction("a(bool)", bytes{1}) == toBigEndian(u256(12))); } +BOOST_AUTO_TEST_CASE(type_conversions_cleanup) +{ + // 22-byte integer converted to a contract (i.e. address, 20 bytes), converted to a 32 byte + // integer should drop the first two bytes + char const* sourceCode = R"( + contract Test { + function test() returns (uint ret) { return uint(address(Test(address(0x11223344556677889900112233445566778899001122)))); } + })"; + compileAndRun(sourceCode); + BOOST_REQUIRE(callContractFunction(0) == bytes({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, + 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22})); +} + + BOOST_AUTO_TEST_CASE(send_ether) { char const* sourceCode = "contract test {\n" From 4b11eea653edf2361fad58a97e2010eeffca9832 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 7 Jan 2015 22:54:56 +0100 Subject: [PATCH 06/27] Contracts are Addresses. --- SolidityEndToEndTest.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index baad4b42d..9f2e0d429 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -844,9 +844,9 @@ BOOST_AUTO_TEST_CASE(type_conversions_cleanup) function test() returns (uint ret) { return uint(address(Test(address(0x11223344556677889900112233445566778899001122)))); } })"; compileAndRun(sourceCode); - BOOST_REQUIRE(callContractFunction(0) == bytes({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, - 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22})); + BOOST_REQUIRE(callContractFunction("test()") == bytes({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, + 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22})); } @@ -1277,6 +1277,24 @@ BOOST_AUTO_TEST_CASE(functions_called_by_constructor) BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); } +BOOST_AUTO_TEST_CASE(contracts_as_addresses) +{ + char const* sourceCode = R"( + contract helper { + } + contract test { + helper h; + function test() { h = new helper(); h.send(5); } + function getBalance() returns (uint256 myBalance, uint256 helperBalance) { + myBalance = this.balance; + helperBalance = h.balance; + } + } + )"; + compileAndRun(sourceCode, 20); + BOOST_REQUIRE(callContractFunction("getBalance()") == toBigEndian(u256(20 - 5)) + toBigEndian(u256(5))); +} + BOOST_AUTO_TEST_SUITE_END() } From 5489ad821441447b0536639164dad8f9c88ea6c6 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 9 Jan 2015 21:36:25 +0100 Subject: [PATCH 07/27] Coding style: Prefix for const variables. --- SolidityEndToEndTest.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 7ed7ad594..0ba3c137c 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -1054,10 +1054,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b)); @@ -1084,10 +1084,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3)); @@ -1115,11 +1115,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); - BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); + BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(c_helperAddress)); } BOOST_AUTO_TEST_CASE(calls_to_this) @@ -1146,10 +1146,10 @@ BOOST_AUTO_TEST_CASE(calls_to_this) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10)); @@ -1181,10 +1181,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9)); @@ -1211,10 +1211,10 @@ BOOST_AUTO_TEST_CASE(strings_in_calls) } })"; compileAndRun(sourceCode, 0, "Helper"); - u160 const helperAddress = m_contractAddress; + u160 const c_helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress)); BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); } From b83542c7ee30066ff9d40f0bda9a5618fe661c92 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 11 Jan 2015 17:04:30 +0100 Subject: [PATCH 08/27] spec reporter instead of nyan --- mocha.opts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mocha.opts b/mocha.opts index b83917bb2..b2db8d5a7 100644 --- a/mocha.opts +++ b/mocha.opts @@ -1,2 +1,2 @@ ---reporter Nyan +--reporter Spec From dcf362c0d5ca53dd8f41afafcd8ce2a45f38c594 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 11 Jan 2015 18:16:50 +0100 Subject: [PATCH 09/27] Squashed 'libjsqrc/ethereumjs/' changes from 47687cf..ab745d3 ab745d3 updated readme b915c7a updated readme 6c26255 Merge branch 'tests' into develop e2c4525 Merge branch 'master' into develop e778e12 spec reporter instead of nyan a4e6f0b fixed travis.yml && added nmp test script 048e777 Merge branch 'master' into tests 15c2293 updated package json 24814a4 Merge branch 'master' of https://github.com/ethereum/ethereum.js 6188e64 Update README.md c34b0c6 Update README.md 29c60c0 Merge branch 'develop' into tests b3eda29 Merge branch 'master' into tests c397e35 mocha opts file && init of parser tests b6058a8 methods existance tests in progress 5518022 mocha test init git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: ab745d3b0eb89d67db1ed953020c665be3d072ed --- abi.parsers.js | 37 +++++++++++++++++++++++++++++++++++++ db.methods.js | 18 ++++++++++++++++++ eth.methods.js | 42 ++++++++++++++++++++++++++++++++++++++++++ mocha.opts | 2 ++ shh.methods.js | 19 +++++++++++++++++++ utils.js | 15 +++++++++++++++ web3.methods.js | 18 ++++++++++++++++++ 7 files changed, 151 insertions(+) create mode 100644 abi.parsers.js create mode 100644 db.methods.js create mode 100644 eth.methods.js create mode 100644 mocha.opts create mode 100644 shh.methods.js create mode 100644 utils.js create mode 100644 web3.methods.js diff --git a/abi.parsers.js b/abi.parsers.js new file mode 100644 index 000000000..06a77fb86 --- /dev/null +++ b/abi.parsers.js @@ -0,0 +1,37 @@ +var assert = require('assert'); +var abi = require('../lib/abi.js'); + +describe('abi', function() { + describe('inputParser', function() { + it('should parse ...', function() { + + var desc = [{ + "name": "multiply", + "inputs": [ + { + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + var iParser = abi.inputParser(desc); + assert.equal(iParser.multiply(1), "0x000000000000000000000000000000000000000000000000000000000000000001"); + + }); + }); + + + describe('outputParser', function() { + it('parse ...', function() { + + }); + }); +}); + diff --git a/db.methods.js b/db.methods.js new file mode 100644 index 000000000..b4abfc4d7 --- /dev/null +++ b/db.methods.js @@ -0,0 +1,18 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('db', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.db, 'put'); + u.methodExists(web3.db, 'get'); + u.methodExists(web3.db, 'putString'); + u.methodExists(web3.db, 'getString'); + }); + }); +}); + diff --git a/eth.methods.js b/eth.methods.js new file mode 100644 index 000000000..7190b27d2 --- /dev/null +++ b/eth.methods.js @@ -0,0 +1,42 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('eth', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.eth, 'balanceAt'); + u.methodExists(web3.eth, 'stateAt'); + u.methodExists(web3.eth, 'storageAt'); + u.methodExists(web3.eth, 'countAt'); + u.methodExists(web3.eth, 'codeAt'); + u.methodExists(web3.eth, 'transact'); + u.methodExists(web3.eth, 'call'); + u.methodExists(web3.eth, 'block'); + u.methodExists(web3.eth, 'transaction'); + u.methodExists(web3.eth, 'uncle'); + u.methodExists(web3.eth, 'compilers'); + u.methodExists(web3.eth, 'lll'); + u.methodExists(web3.eth, 'solidity'); + u.methodExists(web3.eth, 'serpent'); + u.methodExists(web3.eth, 'logs'); + }); + + it('should have all properties implemented', function () { + u.propertyExists(web3.eth, 'coinbase'); + u.propertyExists(web3.eth, 'listening'); + u.propertyExists(web3.eth, 'mining'); + u.propertyExists(web3.eth, 'gasPrice'); + u.propertyExists(web3.eth, 'account'); + u.propertyExists(web3.eth, 'accounts'); + u.propertyExists(web3.eth, 'peerCount'); + u.propertyExists(web3.eth, 'defaultBlock'); + u.propertyExists(web3.eth, 'number'); + }); + }); +}); + + diff --git a/mocha.opts b/mocha.opts new file mode 100644 index 000000000..b2db8d5a7 --- /dev/null +++ b/mocha.opts @@ -0,0 +1,2 @@ +--reporter Spec + diff --git a/shh.methods.js b/shh.methods.js new file mode 100644 index 000000000..08f573a3c --- /dev/null +++ b/shh.methods.js @@ -0,0 +1,19 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + describe('shh', function() { + it('should have all methods implemented', function() { + u.methodExists(web3.shh, 'post'); + u.methodExists(web3.shh, 'newIdentity'); + u.methodExists(web3.shh, 'haveIdentity'); + u.methodExists(web3.shh, 'newGroup'); + u.methodExists(web3.shh, 'addToGroup'); + }); + }); +}); + diff --git a/utils.js b/utils.js new file mode 100644 index 000000000..4c508da67 --- /dev/null +++ b/utils.js @@ -0,0 +1,15 @@ +var assert = require('assert'); + +var methodExists = function (object, method) { + assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented'); +}; + +var propertyExists = function (object, property) { + assert.equal('object', typeof object[property], 'property ' + property + ' is not implemented'); +}; + +module.exports = { + methodExists: methodExists, + propertyExists: propertyExists +}; + diff --git a/web3.methods.js b/web3.methods.js new file mode 100644 index 000000000..a7e020978 --- /dev/null +++ b/web3.methods.js @@ -0,0 +1,18 @@ +require('es6-promise').polyfill(); + +var assert = require('assert'); +var web3 = require('../index.js'); +var u = require('./utils.js'); +web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider + +describe('web3', function() { + it('should have all methods implemented', function() { + u.methodExists(web3, 'sha3'); + u.methodExists(web3, 'toAscii'); + u.methodExists(web3, 'fromAscii'); + u.methodExists(web3, 'toFixed'); + u.methodExists(web3, 'fromFixed'); + u.methodExists(web3, 'offset'); + }); +}); + From b2ae9dffac726b710ab49a16ee40f45b9aa92ccd Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 12 Jan 2015 11:35:54 +0100 Subject: [PATCH 10/27] read in vm test over standard in --- vm.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/vm.cpp b/vm.cpp index 6ae95f256..8187378e5 100644 --- a/vm.cpp +++ b/vm.cpp @@ -560,6 +560,40 @@ BOOST_AUTO_TEST_CASE(vmRandom) } } +BOOST_AUTO_TEST_CASE(checkRandomTest) +{ + for (int i = 1; i < boost::unit_test::framework::master_test_suite().argc; ++i) + { + string arg = boost::unit_test::framework::master_test_suite().argv[i]; + if (arg == "--randomTest") + { + try + { + cout << "RANDOM::::::RANDOM" << endl; + json_spirit::mValue v; + string s;// = boost::unit_test::framework::master_test_suite().argv[i + 1]; + string line; + while ( getline(cin, line) && !line.empty() ) + s += line; + cout << "my test: AAAHHHAAA: \n" << s << endl; + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content of argument is empty"); + json_spirit::read_string(s, v); + doVMTests(v, false); + cout << "RANDOM::::::RANDOM--done" << endl; + } + catch (Exception const& _e) + { + BOOST_ERROR("Failed test with Exception: " << diagnostic_information(_e)); + } + catch (std::exception const& _e) + { + BOOST_ERROR("Failed test with Exception: " << _e.what()); + } + break; + } + } +} + BOOST_AUTO_TEST_CASE(userDefinedFileVM) { dev::test::userDefinedTest("--vmtest", dev::test::doVMTests); From d4f1ab9145132d98e8ebe09e8a067f1f7f013574 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 12 Jan 2015 12:46:40 +0100 Subject: [PATCH 11/27] start with block info opcodes --- createRandomTest.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index 1af12f64d..40ed1aa9c 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -54,18 +54,26 @@ int main(int argc, char *argv[]) gen.seed(static_cast(timeSinceEpoch)); boost::random::uniform_int_distribution<> lengthOfCodeDist(2, 16); boost::random::uniform_int_distribution<> opcodeDist(0, 255); + boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x41, 0x45); boost::random::variate_generator > randGen(gen, opcodeDist); + boost::random::variate_generator > randGenBlockInfoOpcode(gen, BlockInfoOpcodeDist); int lengthOfCode = lengthOfCodeDist(gen); string randomCode; for (int i = 0; i < lengthOfCode; ++i) { - uint8_t opcode = randGen(); + if (i < 8 && (randGen() < 192)) + { + randomCode += toHex(toCompactBigEndian((uint8_t)randGenBlockInfoOpcode())); + continue; + } - // disregard all invalid commands, except of one (0x10) - if (dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || opcode == 0x10) + uint8_t opcode = randGen(); + // disregard all invalid commands, except of one (0x0c) + if (dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || opcode == 0x0a) randomCode += toHex(toCompactBigEndian(opcode)); else i--; @@ -77,9 +85,9 @@ int main(int argc, char *argv[]) \"env\" : {\n\ \"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\ \"currentNumber\" : \"0\",\n\ - \"currentGasLimit\" : \"1000000\",\n\ - \"currentDifficulty\" : \"256\",\n\ - \"currentTimestamp\" : 1,\n\ + \"currentGasLimit\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\ + \"currentDifficulty\" : \"1\",\n\ + \"currentTimestamp\" : 2,\n\ \"currentCoinbase\" : \"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba\"\n\ },\n\ \"pre\" : {\n\ @@ -106,7 +114,7 @@ int main(int argc, char *argv[]) read_string(s, v); // insert new random code - v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode; + v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + "55"; // execute code in vm doMyTests(v); From f729e862e9490752aeccf5ef218b3d381c1b5738 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 12 Jan 2015 15:29:44 +0100 Subject: [PATCH 12/27] Fixing bug where empty tagless docstring in Natspec would result in infinite loop --- SolidityNatspecJSON.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/SolidityNatspecJSON.cpp b/SolidityNatspecJSON.cpp index f6a33247a..7edb97a7b 100644 --- a/SolidityNatspecJSON.cpp +++ b/SolidityNatspecJSON.cpp @@ -510,6 +510,19 @@ BOOST_AUTO_TEST_CASE(dev_title_at_function_error) BOOST_CHECK_THROW(checkNatspec(sourceCode, natspec, false), DocstringParsingError); } +// test for bug where having no tags in docstring would cause infinite loop +BOOST_AUTO_TEST_CASE(natspec_no_tags) +{ + char const* sourceCode = "contract test {\n" + " /// I do something awesome\n" + " function mul(uint a, uint second) returns(uint d) { return a * 7 + second; }\n" + "}\n"; + + char const* natspec = "{\"methods\": {}}"; + + checkNatspec(sourceCode, natspec, false); +} + BOOST_AUTO_TEST_SUITE_END() } From a7641cdae710c5f46624af21653ad90d172f0097 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 12 Jan 2015 15:42:28 +0100 Subject: [PATCH 13/27] rests current gas limit in vm test --- createRandomTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index 40ed1aa9c..d9638cebe 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -85,8 +85,8 @@ int main(int argc, char *argv[]) \"env\" : {\n\ \"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\ \"currentNumber\" : \"0\",\n\ - \"currentGasLimit\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\ - \"currentDifficulty\" : \"1\",\n\ + \"currentGasLimit\" : \"1000000\",\n\ + \"currentDifficulty\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\ \"currentTimestamp\" : 2,\n\ \"currentCoinbase\" : \"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba\"\n\ },\n\ From 3e172da03f0d24dd594fdf91dba2b1a8689da6d5 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 12 Jan 2015 16:28:56 +0100 Subject: [PATCH 14/27] addmod mulmod div by zero tests --- vmArithmeticTestFiller.json | 168 ++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) diff --git a/vmArithmeticTestFiller.json b/vmArithmeticTestFiller.json index 12574f51c..9e8b3f61c 100644 --- a/vmArithmeticTestFiller.json +++ b/vmArithmeticTestFiller.json @@ -1346,6 +1346,90 @@ } }, + "addmodDivByZero": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (ADDMOD 4 1 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + + "addmodDivByZero1": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (ADDMOD 0 1 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + + "addmodDivByZero1": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (ADDMOD 1 0 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + "mulmod0": { "env" : { @@ -1543,6 +1627,90 @@ } }, + "mulmoddivByZero": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (MULMOD 5 1 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + + "mulmoddivByZero1": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (MULMOD 0 1 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + + "mulmoddivByZero2": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (MULMOD 1 0 0) } ", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f2947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + "exp0": { "env" : { From c028ad2949b5a8a53696ba4393e54528d42d00f8 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Mon, 12 Jan 2015 17:17:05 +0100 Subject: [PATCH 15/27] call to precomppiled contract test --- vmSystemOperationsTestFiller.json | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/vmSystemOperationsTestFiller.json b/vmSystemOperationsTestFiller.json index 1df2697e0..a5cd79030 100644 --- a/vmSystemOperationsTestFiller.json +++ b/vmSystemOperationsTestFiller.json @@ -147,6 +147,36 @@ } }, + "CallToPrecompiledContract" : { + "env" : { + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", + "currentDifficulty" : "115792089237316195423570985008687907853269984665640564039457584007913129639935", + "currentGasLimit" : "1000000", + "currentNumber" : "0", + "currentTimestamp" : "2", + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6" + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "code" : "0x4243434242434243f14555", + "data" : "0x", + "gas" : "10000", + "gasPrice" : "100000000000000", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "code" : "0x4243434242434243f14555", + "nonce" : "0", + "storage" : { + } + } + } + }, + "CallToReturn1": { "env" : { "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", From b1ddaed08a31fc413e492c9af968b9944c7160f4 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 09:53:46 +0100 Subject: [PATCH 16/27] reinclude invalid opcode in random tests --- createRandomTest.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index d9638cebe..d2522cf47 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) gen.seed(static_cast(timeSinceEpoch)); boost::random::uniform_int_distribution<> lengthOfCodeDist(2, 16); boost::random::uniform_int_distribution<> opcodeDist(0, 255); - boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x41, 0x45); + boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x40, 0x45); boost::random::variate_generator > randGen(gen, opcodeDist); boost::random::variate_generator Date: Tue, 13 Jan 2015 11:18:08 +0100 Subject: [PATCH 17/27] fixed warnings for msvc build --- SolidityEndToEndTest.cpp | 4 ++++ boostTest.cpp | 2 +- vm.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 2afe875f2..9543497a7 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -28,6 +28,10 @@ #include #include +#ifdef _MSC_VER +#pragma warning(disable: 4307) //integral constant overflow for high_bits_cleaning +#endif + using namespace std; namespace dev diff --git a/boostTest.cpp b/boostTest.cpp index cef3cc0a7..1523a7a11 100644 --- a/boostTest.cpp +++ b/boostTest.cpp @@ -23,6 +23,6 @@ #define BOOST_TEST_MODULE EthereumTests #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" +#define BOOST_DISABLE_WIN32 //disables SEH warning #include -#pragma warning(pop) #pragma GCC diagnostic pop diff --git a/vm.cpp b/vm.cpp index 6ae95f256..8b8c75a04 100644 --- a/vm.cpp +++ b/vm.cpp @@ -345,7 +345,7 @@ void doVMTests(json_spirit::mValue& v, bool _fillin) output = vm->go(fev, fev.simpleTrace()).toBytes(); gas = vm->gas(); } - catch (VMException const& _e) + catch (VMException const&) { cnote << "Safe VM Exception"; vmExceptionOccured = true; From eaf6e8fcfe213ae9f3e1d42810019e8f24a39bdd Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 14:22:12 +0100 Subject: [PATCH 18/27] check test with JIT for random test simulations --- CMakeLists.txt | 6 + checkRandomTest.cpp | 279 +++++++++++++++++++++++++++++++++++++++++++ createRandomTest.cpp | 2 + vm.cpp | 34 ------ 4 files changed, 287 insertions(+), 34 deletions(-) create mode 100644 checkRandomTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cedc117b..764bf928e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_policy(SET CMP0015 NEW) aux_source_directory(. SRC_LIST) list(REMOVE_ITEM SRC_LIST "./createRandomTest.cpp") +list(REMOVE_ITEM SRC_LIST "./checkRandomTest.cpp") include_directories(${Boost_INCLUDE_DIRS}) include_directories(${CRYPTOPP_INCLUDE_DIRS}) @@ -12,6 +13,7 @@ include_directories(..) file(GLOB HEADERS "*.h") add_executable(testeth ${SRC_LIST} ${HEADERS}) add_executable(createRandomTest createRandomTest.cpp vm.cpp TestHelper.cpp) +add_executable(checkRandomTest checkRandomTest.cpp vm.cpp TestHelper.cpp) target_link_libraries(testeth ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES}) target_link_libraries(testeth ${CURL_LIBRARIES}) @@ -29,3 +31,7 @@ endif() target_link_libraries(createRandomTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES}) target_link_libraries(createRandomTest ethereum) target_link_libraries(createRandomTest ethcore) +target_link_libraries(checkRandomTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES}) +target_link_libraries(checkRandomTest ethereum) +target_link_libraries(checkRandomTest ethcore) + diff --git a/checkRandomTest.cpp b/checkRandomTest.cpp new file mode 100644 index 000000000..4299cdac8 --- /dev/null +++ b/checkRandomTest.cpp @@ -0,0 +1,279 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** @file checkRandomTest.cpp + * @author Christoph Jentzsch + * @date 2015 + * Check a random test and return 0/1 for success or failure. To be used for efficiency in the random test simulation. + */ + +#include +#include +#include +#include +#include "vm.h" +#pragma GCC diagnostic ignored "-Wunused-parameter" + +using namespace std; +using namespace json_spirit; +using namespace dev::test; +using namespace dev; + +bool doVMTest(mValue& v); + +int main(int argc, char *argv[]) +{ + g_logVerbosity = 0; + bool ret = false; + + try + { + mValue v; + string s; + for (int i = 1; i < argc; ++i) + s += argv[i]; + if (asserts(s.length() > 0)) + { + cout << "Content of argument is empty\n"; + return 1; + } + read_string(s, v); + ret = doVMTest(v); + } + catch (Exception const& _e) + { + cout << "Failed test with Exception: " << diagnostic_information(_e) << endl; + ret = false; + } + catch (std::exception const& _e) + { + cout << "Failed test with Exception: " << _e.what() << endl; + ret = false; + } + return ret; +} + +bool doVMTest(mValue& v) +{ + eth::VMFactory::setKind(eth::VMKind::JIT); + + for (auto& i: v.get_obj()) + { + cnote << i.first; + mObject& o = i.second.get_obj(); + + assert(o.count("env") > 0); + assert(o.count("pre") > 0); + assert(o.count("exec") > 0); + + FakeExtVM fev; + fev.importEnv(o["env"].get_obj()); + fev.importState(o["pre"].get_obj()); + + fev.importExec(o["exec"].get_obj()); + if (fev.code.empty()) + { + fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); + fev.code = fev.thisTxCode; + } + + bytes output; + u256 gas; + bool vmExceptionOccured = false; + try + { + auto vm = eth::VMFactory::create(fev.gas); + output = vm->go(fev, fev.simpleTrace()).toBytes(); + gas = vm->gas(); + } + catch (eth::VMException const& _e) + { + cnote << "Safe VM Exception"; + vmExceptionOccured = true; + } + catch (Exception const& _e) + { + cnote << "VM did throw an exception: " << diagnostic_information(_e); + cnote << "Failed VM Test with Exception: " << _e.what(); + return 1; + } + catch (std::exception const& _e) + { + cnote << "VM did throw an exception: " << _e.what(); + cnote << "Failed VM Test with Exception: " << _e.what(); + return 1; + } + + // delete null entries in storage for the sake of comparison + for (auto &a: fev.addresses) + { + vector keystoDelete; + for (auto &s: get<2>(a.second)) + { + if (s.second == 0) + keystoDelete.push_back(s.first); + } + for (auto const key: keystoDelete ) + { + get<2>(a.second).erase(key); + } + } + + if (o.count("post") > 0) // No exceptions expected + { + if (asserts(!vmExceptionOccured) || asserts(o.count("post") > 0) || asserts(o.count("callcreates") > 0) || asserts(o.count("out") > 0) || asserts(o.count("gas") > 0) || asserts(o.count("logs") > 0)) + return 1; + + dev::test::FakeExtVM test; + test.importState(o["post"].get_obj()); + test.importCallCreates(o["callcreates"].get_array()); + test.sub.logs = importLog(o["logs"].get_array()); + + //checkOutput(output, o); + int j = 0; + if (o["out"].type() == array_type) + for (auto const& d: o["out"].get_array()) + { + if (asserts(output[j] == toInt(d))) + { + cout << "Output byte [" << j << "] different!"; + return 1; + } + ++j; + } + else if (o["out"].get_str().find("0x") == 0) + { + if (asserts(output == fromHex(o["out"].get_str().substr(2)))) + return 1; + } + else + { + if (asserts(output == fromHex(o["out"].get_str()))) + return 1; + } + + if (asserts(toInt(o["gas"]) == gas)) + return 1; + + auto& expectedAddrs = test.addresses; + auto& resultAddrs = fev.addresses; + for (auto&& expectedPair : expectedAddrs) + { + auto& expectedAddr = expectedPair.first; + auto resultAddrIt = resultAddrs.find(expectedAddr); + if (resultAddrIt == resultAddrs.end()) + { + cout << "Missing expected address " << expectedAddr; + return 1; + } + else + { + auto& expectedState = expectedPair.second; + auto& resultState = resultAddrIt->second; + if (asserts(std::get<0>(expectedState) == std::get<0>(resultState))) + { + cout << expectedAddr << ": incorrect balance " << std::get<0>(resultState) << ", expected " << std::get<0>(expectedState); + return 1; + } + if (asserts(std::get<1>(expectedState) == std::get<1>(resultState))) + { + cout << expectedAddr << ": incorrect txCount " << std::get<1>(resultState) << ", expected " << std::get<1>(expectedState); + return 1; + } + if (asserts(std::get<3>(expectedState) == std::get<3>(resultState))) + { + cout << expectedAddr << ": incorrect code"; + return 1; + } + + //checkStorage(std::get<2>(expectedState), std::get<2>(resultState), expectedAddr); + for (auto&& expectedStorePair : std::get<2>(expectedState)) + { + auto& expectedStoreKey = expectedStorePair.first; + auto resultStoreIt = std::get<2>(resultState).find(expectedStoreKey); + if (resultStoreIt == std::get<2>(resultState).end()) + { + cout << expectedAddr << ": missing store key " << expectedStoreKey << endl; + return 1; + } + else + { + auto& expectedStoreValue = expectedStorePair.second; + auto& resultStoreValue = resultStoreIt->second; + if (asserts(expectedStoreValue == resultStoreValue)) + { + cout << expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue << endl; + return 1; + } + } + } + if (assertsEqual(std::get<2>(resultState).size(), std::get<2>(expectedState).size())) + return 1; + for (auto&& resultStorePair: std::get<2>(resultState)) + { + if (!std::get<2>(expectedState).count(resultStorePair.first)) + { + cout << expectedAddr << ": unexpected store key " << resultStorePair.first << endl; + return 1; + } + } + } + } + + //checkAddresses, bytes> > >(test.addresses, fev.addresses); + for (auto& resultPair : fev.addresses) + { + auto& resultAddr = resultPair.first; + auto expectedAddrIt = test.addresses.find(resultAddr); + if (expectedAddrIt == test.addresses.end()) + { + cout << "Missing result address " << resultAddr << endl; + return 1; + } + } + if (asserts(test.addresses == fev.addresses)) + return 1; + + if (asserts(test.callcreates == fev.callcreates)) + return 1; + + //checkLog(fev.sub.logs, test.sub.logs); + { + if (assertsEqual(fev.sub.logs.size(), test.sub.logs.size())) + return 1; + + for (size_t i = 0; i < fev.sub.logs.size(); ++i) + { + if (assertsEqual(fev.sub.logs[i].address, test.sub.logs[i].address)) + return 1; + if (assertsEqual(fev.sub.logs[i].topics, test.sub.logs[i].topics)) + return 1; + if (asserts(fev.sub.logs[i].data == test.sub.logs[i].data)) + return 1; + } + } + + } + else // Exception expected + { + if (asserts(vmExceptionOccured)) + return 1; + } + } + // test passed + return 0; +} + diff --git a/createRandomTest.cpp b/createRandomTest.cpp index 1af12f64d..b3700fd0f 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -119,6 +119,8 @@ int main(int argc, char *argv[]) void doMyTests(json_spirit::mValue& v) { + eth::VMFactory::setKind(eth::VMKind::Interpreter); + for (auto& i: v.get_obj()) { cnote << i.first; diff --git a/vm.cpp b/vm.cpp index 8187378e5..6ae95f256 100644 --- a/vm.cpp +++ b/vm.cpp @@ -560,40 +560,6 @@ BOOST_AUTO_TEST_CASE(vmRandom) } } -BOOST_AUTO_TEST_CASE(checkRandomTest) -{ - for (int i = 1; i < boost::unit_test::framework::master_test_suite().argc; ++i) - { - string arg = boost::unit_test::framework::master_test_suite().argv[i]; - if (arg == "--randomTest") - { - try - { - cout << "RANDOM::::::RANDOM" << endl; - json_spirit::mValue v; - string s;// = boost::unit_test::framework::master_test_suite().argv[i + 1]; - string line; - while ( getline(cin, line) && !line.empty() ) - s += line; - cout << "my test: AAAHHHAAA: \n" << s << endl; - BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content of argument is empty"); - json_spirit::read_string(s, v); - doVMTests(v, false); - cout << "RANDOM::::::RANDOM--done" << endl; - } - catch (Exception const& _e) - { - BOOST_ERROR("Failed test with Exception: " << diagnostic_information(_e)); - } - catch (std::exception const& _e) - { - BOOST_ERROR("Failed test with Exception: " << _e.what()); - } - break; - } - } -} - BOOST_AUTO_TEST_CASE(userDefinedFileVM) { dev::test::userDefinedTest("--vmtest", dev::test::doVMTests); From f9f40035ae6765552a201870d30f7ce0b01213e7 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 14:38:36 +0100 Subject: [PATCH 19/27] random test optimization --- createRandomTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index d2522cf47..b3a704e9d 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) \"currentNumber\" : \"0\",\n\ \"currentGasLimit\" : \"1000000\",\n\ \"currentDifficulty\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\ - \"currentTimestamp\" : 5,\n\ + \"currentTimestamp\" : 2,\n\ \"currentCoinbase\" : \"2adc25665018aa1fe0e6bc666dac8fc2697ff9ba\"\n\ },\n\ \"pre\" : {\n\ From 108388c4401ce16609bfc23c4dcb6b3e09638e02 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 15:47:36 +0100 Subject: [PATCH 20/27] fix callcreates imoport --- TestHelper.cpp | 13 +++++++++++++ TestHelper.h | 2 ++ vm.cpp | 7 ++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/TestHelper.cpp b/TestHelper.cpp index ff330d60e..355a5080d 100644 --- a/TestHelper.cpp +++ b/TestHelper.cpp @@ -350,6 +350,19 @@ void checkLog(LogEntries _resultLogs, LogEntries _expectedLogs) } } +void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates) +{ + BOOST_REQUIRE_EQUAL(_resultCallCreates.size(), _expectedCallCreates.size()); + + for (size_t i = 0; i < _resultCallCreates.size(); ++i) + { + BOOST_CHECK(_resultCallCreates[i].data() == _expectedCallCreates[i].data()); + BOOST_CHECK(_resultCallCreates[i].receiveAddress() == _expectedCallCreates[i].receiveAddress()); + BOOST_CHECK(_resultCallCreates[i].gas() == _expectedCallCreates[i].gas()); + BOOST_CHECK(_resultCallCreates[i].value() == _expectedCallCreates[i].value()); + } +} + std::string getTestPath() { string testPath; diff --git a/TestHelper.h b/TestHelper.h index 85017c842..2ef5c6d53 100644 --- a/TestHelper.h +++ b/TestHelper.h @@ -73,6 +73,8 @@ json_spirit::mArray exportLog(eth::LogEntries _logs); void checkOutput(bytes const& _output, json_spirit::mObject& _o); void checkStorage(std::map _expectedStore, std::map _resultStore, Address _expectedAddr); void checkLog(eth::LogEntries _resultLogs, eth::LogEntries _expectedLogs); +void checkCallCreates(eth::Transactions _resultCallCreates, eth::Transactions _expectedCallCreates); + void executeTests(const std::string& _name, const std::string& _testPathAppendix, std::function doTests); std::string getTestPath(); void userDefinedTest(std::string testTypeFlag, std::function doTests); diff --git a/vm.cpp b/vm.cpp index 8b8c75a04..8af752caa 100644 --- a/vm.cpp +++ b/vm.cpp @@ -237,8 +237,8 @@ void FakeExtVM::importCallCreates(mArray& _callcreates) BOOST_REQUIRE(tx.count("destination") > 0); BOOST_REQUIRE(tx.count("gasLimit") > 0); Transaction t = tx["destination"].get_str().empty() ? - Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), data.toBytes()) : - Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), Address(tx["destination"].get_str()), data.toBytes()); + Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), fromHex(tx["data"].get_str())) : + Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), Address(tx["destination"].get_str()), fromHex(tx["data"].get_str())); callcreates.push_back(t); } } @@ -448,7 +448,8 @@ void doVMTests(json_spirit::mValue& v, bool _fillin) } checkAddresses, bytes> > >(test.addresses, fev.addresses); - BOOST_CHECK(test.callcreates == fev.callcreates); + + checkCallCreates(fev.callcreates, test.callcreates); checkLog(fev.sub.logs, test.sub.logs); } From cf6641c345074c7e3e7cc582726891f774e2996a Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 15:59:07 +0100 Subject: [PATCH 21/27] fix spaces->tab, detailed callcreate check --- checkRandomTest.cpp | 474 +++++++++++++++++++++++--------------------- 1 file changed, 246 insertions(+), 228 deletions(-) diff --git a/checkRandomTest.cpp b/checkRandomTest.cpp index 4299cdac8..e3442d438 100644 --- a/checkRandomTest.cpp +++ b/checkRandomTest.cpp @@ -1,18 +1,18 @@ /* - This file is part of cpp-ethereum. + This file is part of cpp-ethereum. - cpp-ethereum is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - cpp-ethereum is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with cpp-ethereum. If not, see . + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . */ /** @file checkRandomTest.cpp * @author Christoph Jentzsch @@ -36,244 +36,262 @@ bool doVMTest(mValue& v); int main(int argc, char *argv[]) { - g_logVerbosity = 0; - bool ret = false; + g_logVerbosity = 0; + bool ret = false; - try - { - mValue v; - string s; - for (int i = 1; i < argc; ++i) - s += argv[i]; - if (asserts(s.length() > 0)) - { - cout << "Content of argument is empty\n"; - return 1; - } - read_string(s, v); - ret = doVMTest(v); - } - catch (Exception const& _e) - { - cout << "Failed test with Exception: " << diagnostic_information(_e) << endl; - ret = false; - } - catch (std::exception const& _e) - { - cout << "Failed test with Exception: " << _e.what() << endl; - ret = false; - } - return ret; + try + { + mValue v; + string s; + for (int i = 1; i < argc; ++i) + s += argv[i]; + if (asserts(s.length() > 0)) + { + cout << "Content of argument is empty\n"; + return 1; + } + read_string(s, v); + ret = doVMTest(v); + } + catch (Exception const& _e) + { + cout << "Failed test with Exception: " << diagnostic_information(_e) << endl; + ret = false; + } + catch (std::exception const& _e) + { + cout << "Failed test with Exception: " << _e.what() << endl; + ret = false; + } + return ret; } bool doVMTest(mValue& v) { - eth::VMFactory::setKind(eth::VMKind::JIT); + eth::VMFactory::setKind(eth::VMKind::JIT); - for (auto& i: v.get_obj()) - { - cnote << i.first; - mObject& o = i.second.get_obj(); + for (auto& i: v.get_obj()) + { + cnote << i.first; + mObject& o = i.second.get_obj(); - assert(o.count("env") > 0); - assert(o.count("pre") > 0); - assert(o.count("exec") > 0); + assert(o.count("env") > 0); + assert(o.count("pre") > 0); + assert(o.count("exec") > 0); - FakeExtVM fev; - fev.importEnv(o["env"].get_obj()); - fev.importState(o["pre"].get_obj()); + FakeExtVM fev; + fev.importEnv(o["env"].get_obj()); + fev.importState(o["pre"].get_obj()); - fev.importExec(o["exec"].get_obj()); - if (fev.code.empty()) - { - fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); - fev.code = fev.thisTxCode; - } + fev.importExec(o["exec"].get_obj()); + if (fev.code.empty()) + { + fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); + fev.code = fev.thisTxCode; + } - bytes output; - u256 gas; - bool vmExceptionOccured = false; - try - { - auto vm = eth::VMFactory::create(fev.gas); - output = vm->go(fev, fev.simpleTrace()).toBytes(); - gas = vm->gas(); - } - catch (eth::VMException const& _e) - { - cnote << "Safe VM Exception"; - vmExceptionOccured = true; - } - catch (Exception const& _e) - { - cnote << "VM did throw an exception: " << diagnostic_information(_e); - cnote << "Failed VM Test with Exception: " << _e.what(); - return 1; - } - catch (std::exception const& _e) - { - cnote << "VM did throw an exception: " << _e.what(); - cnote << "Failed VM Test with Exception: " << _e.what(); - return 1; - } + bytes output; + u256 gas; + bool vmExceptionOccured = false; + try + { + auto vm = eth::VMFactory::create(fev.gas); + output = vm->go(fev, fev.simpleTrace()).toBytes(); + gas = vm->gas(); + } + catch (eth::VMException) + { + cnote << "Safe VM Exception"; + vmExceptionOccured = true; + } + catch (Exception const& _e) + { + cnote << "VM did throw an exception: " << diagnostic_information(_e); + cnote << "Failed VM Test with Exception: " << _e.what(); + return 1; + } + catch (std::exception const& _e) + { + cnote << "VM did throw an exception: " << _e.what(); + cnote << "Failed VM Test with Exception: " << _e.what(); + return 1; + } - // delete null entries in storage for the sake of comparison - for (auto &a: fev.addresses) - { - vector keystoDelete; - for (auto &s: get<2>(a.second)) - { - if (s.second == 0) - keystoDelete.push_back(s.first); - } - for (auto const key: keystoDelete ) - { - get<2>(a.second).erase(key); - } - } + // delete null entries in storage for the sake of comparison + for (auto &a: fev.addresses) + { + vector keystoDelete; + for (auto &s: get<2>(a.second)) + { + if (s.second == 0) + keystoDelete.push_back(s.first); + } + for (auto const key: keystoDelete ) + { + get<2>(a.second).erase(key); + } + } - if (o.count("post") > 0) // No exceptions expected - { - if (asserts(!vmExceptionOccured) || asserts(o.count("post") > 0) || asserts(o.count("callcreates") > 0) || asserts(o.count("out") > 0) || asserts(o.count("gas") > 0) || asserts(o.count("logs") > 0)) - return 1; + if (o.count("post") > 0) // No exceptions expected + { + if (asserts(!vmExceptionOccured) || asserts(o.count("post") > 0) || asserts(o.count("callcreates") > 0) || asserts(o.count("out") > 0) || asserts(o.count("gas") > 0) || asserts(o.count("logs") > 0)) + return 1; - dev::test::FakeExtVM test; - test.importState(o["post"].get_obj()); - test.importCallCreates(o["callcreates"].get_array()); - test.sub.logs = importLog(o["logs"].get_array()); + dev::test::FakeExtVM test; + test.importState(o["post"].get_obj()); + test.importCallCreates(o["callcreates"].get_array()); + test.sub.logs = importLog(o["logs"].get_array()); - //checkOutput(output, o); - int j = 0; - if (o["out"].type() == array_type) - for (auto const& d: o["out"].get_array()) - { - if (asserts(output[j] == toInt(d))) - { - cout << "Output byte [" << j << "] different!"; - return 1; - } - ++j; - } - else if (o["out"].get_str().find("0x") == 0) - { - if (asserts(output == fromHex(o["out"].get_str().substr(2)))) - return 1; - } - else - { - if (asserts(output == fromHex(o["out"].get_str()))) - return 1; - } + //checkOutput(output, o); + int j = 0; + if (o["out"].type() == array_type) + for (auto const& d: o["out"].get_array()) + { + if (asserts(output[j] == toInt(d))) + { + cout << "Output byte [" << j << "] different!"; + return 1; + } + ++j; + } + else if (o["out"].get_str().find("0x") == 0) + { + if (asserts(output == fromHex(o["out"].get_str().substr(2)))) + return 1; + } + else + { + if (asserts(output == fromHex(o["out"].get_str()))) + return 1; + } - if (asserts(toInt(o["gas"]) == gas)) - return 1; + if (asserts(toInt(o["gas"]) == gas)) + return 1; - auto& expectedAddrs = test.addresses; - auto& resultAddrs = fev.addresses; - for (auto&& expectedPair : expectedAddrs) - { - auto& expectedAddr = expectedPair.first; - auto resultAddrIt = resultAddrs.find(expectedAddr); - if (resultAddrIt == resultAddrs.end()) - { - cout << "Missing expected address " << expectedAddr; - return 1; - } - else - { - auto& expectedState = expectedPair.second; - auto& resultState = resultAddrIt->second; - if (asserts(std::get<0>(expectedState) == std::get<0>(resultState))) - { - cout << expectedAddr << ": incorrect balance " << std::get<0>(resultState) << ", expected " << std::get<0>(expectedState); - return 1; - } - if (asserts(std::get<1>(expectedState) == std::get<1>(resultState))) - { - cout << expectedAddr << ": incorrect txCount " << std::get<1>(resultState) << ", expected " << std::get<1>(expectedState); - return 1; - } - if (asserts(std::get<3>(expectedState) == std::get<3>(resultState))) - { - cout << expectedAddr << ": incorrect code"; - return 1; - } + auto& expectedAddrs = test.addresses; + auto& resultAddrs = fev.addresses; + for (auto&& expectedPair : expectedAddrs) + { + auto& expectedAddr = expectedPair.first; + auto resultAddrIt = resultAddrs.find(expectedAddr); + if (resultAddrIt == resultAddrs.end()) + { + cout << "Missing expected address " << expectedAddr; + return 1; + } + else + { + auto& expectedState = expectedPair.second; + auto& resultState = resultAddrIt->second; + if (asserts(std::get<0>(expectedState) == std::get<0>(resultState))) + { + cout << expectedAddr << ": incorrect balance " << std::get<0>(resultState) << ", expected " << std::get<0>(expectedState); + return 1; + } + if (asserts(std::get<1>(expectedState) == std::get<1>(resultState))) + { + cout << expectedAddr << ": incorrect txCount " << std::get<1>(resultState) << ", expected " << std::get<1>(expectedState); + return 1; + } + if (asserts(std::get<3>(expectedState) == std::get<3>(resultState))) + { + cout << expectedAddr << ": incorrect code"; + return 1; + } - //checkStorage(std::get<2>(expectedState), std::get<2>(resultState), expectedAddr); - for (auto&& expectedStorePair : std::get<2>(expectedState)) - { - auto& expectedStoreKey = expectedStorePair.first; - auto resultStoreIt = std::get<2>(resultState).find(expectedStoreKey); - if (resultStoreIt == std::get<2>(resultState).end()) - { - cout << expectedAddr << ": missing store key " << expectedStoreKey << endl; - return 1; - } - else - { - auto& expectedStoreValue = expectedStorePair.second; - auto& resultStoreValue = resultStoreIt->second; - if (asserts(expectedStoreValue == resultStoreValue)) - { - cout << expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue << endl; - return 1; - } - } - } - if (assertsEqual(std::get<2>(resultState).size(), std::get<2>(expectedState).size())) - return 1; - for (auto&& resultStorePair: std::get<2>(resultState)) - { - if (!std::get<2>(expectedState).count(resultStorePair.first)) - { - cout << expectedAddr << ": unexpected store key " << resultStorePair.first << endl; - return 1; - } - } - } - } + //checkStorage(std::get<2>(expectedState), std::get<2>(resultState), expectedAddr); + for (auto&& expectedStorePair : std::get<2>(expectedState)) + { + auto& expectedStoreKey = expectedStorePair.first; + auto resultStoreIt = std::get<2>(resultState).find(expectedStoreKey); + if (resultStoreIt == std::get<2>(resultState).end()) + { + cout << expectedAddr << ": missing store key " << expectedStoreKey << endl; + return 1; + } + else + { + auto& expectedStoreValue = expectedStorePair.second; + auto& resultStoreValue = resultStoreIt->second; + if (asserts(expectedStoreValue == resultStoreValue)) + { + cout << expectedAddr << ": store[" << expectedStoreKey << "] = " << resultStoreValue << ", expected " << expectedStoreValue << endl; + return 1; + } + } + } + if (assertsEqual(std::get<2>(resultState).size(), std::get<2>(expectedState).size())) + return 1; + for (auto&& resultStorePair: std::get<2>(resultState)) + { + if (!std::get<2>(expectedState).count(resultStorePair.first)) + { + cout << expectedAddr << ": unexpected store key " << resultStorePair.first << endl; + return 1; + } + } + } + } - //checkAddresses, bytes> > >(test.addresses, fev.addresses); - for (auto& resultPair : fev.addresses) - { - auto& resultAddr = resultPair.first; - auto expectedAddrIt = test.addresses.find(resultAddr); - if (expectedAddrIt == test.addresses.end()) - { - cout << "Missing result address " << resultAddr << endl; - return 1; - } - } - if (asserts(test.addresses == fev.addresses)) - return 1; + //checkAddresses, bytes> > >(test.addresses, fev.addresses); + for (auto& resultPair : fev.addresses) + { + auto& resultAddr = resultPair.first; + auto expectedAddrIt = test.addresses.find(resultAddr); + if (expectedAddrIt == test.addresses.end()) + { + cout << "Missing result address " << resultAddr << endl; + return 1; + } + } + if (asserts(test.addresses == fev.addresses)) + return 1; - if (asserts(test.callcreates == fev.callcreates)) - return 1; + if (asserts(test.callcreates == fev.callcreates)) + return 1; - //checkLog(fev.sub.logs, test.sub.logs); - { - if (assertsEqual(fev.sub.logs.size(), test.sub.logs.size())) - return 1; + //checkCallCreates(fev.callcreates, test.callcreates); + { + if (assertsEqual(test.callcreates.size(), fev.callcreates.size())) + return 1; - for (size_t i = 0; i < fev.sub.logs.size(); ++i) - { - if (assertsEqual(fev.sub.logs[i].address, test.sub.logs[i].address)) - return 1; - if (assertsEqual(fev.sub.logs[i].topics, test.sub.logs[i].topics)) - return 1; - if (asserts(fev.sub.logs[i].data == test.sub.logs[i].data)) - return 1; - } - } + for (size_t i = 0; i < test.callcreates.size(); ++i) + { + if (asserts(test.callcreates[i].data() == fev.callcreates[i].data())) + return 1; + if (asserts(test.callcreates[i].receiveAddress() == fev.callcreates[i].receiveAddress())) + return 1; + if (asserts(test.callcreates[i].gas() == fev.callcreates[i].gas())) + return 1; + if (asserts(test.callcreates[i].value() == fev.callcreates[i].value())) + return 1; + } + } - } - else // Exception expected - { - if (asserts(vmExceptionOccured)) - return 1; - } - } - // test passed - return 0; + //checkLog(fev.sub.logs, test.sub.logs); + { + if (assertsEqual(fev.sub.logs.size(), test.sub.logs.size())) + return 1; + + for (size_t i = 0; i < fev.sub.logs.size(); ++i) + { + if (assertsEqual(fev.sub.logs[i].address, test.sub.logs[i].address)) + return 1; + if (assertsEqual(fev.sub.logs[i].topics, test.sub.logs[i].topics)) + return 1; + if (asserts(fev.sub.logs[i].data == test.sub.logs[i].data)) + return 1; + } + } + + } + else // Exception expected + { + if (asserts(vmExceptionOccured)) + return 1; + } + } + // test passed + return 0; } From ebe54acb77bf495cd9c61b3b0c43d9906b8e1fef Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 17:58:14 +0100 Subject: [PATCH 22/27] more randomness in the optimizations --- createRandomTest.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index b3a704e9d..37d61fac9 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) uint8_t opcode = randGen(); // disregard all invalid commands, except of one (0x0c) - if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (opcode == 0x0c))) + if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 128))) randomCode += toHex(toCompactBigEndian(opcode)); else i--; @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) \"randomVMtest\": {\n\ \"env\" : {\n\ \"previousHash\" : \"5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6\",\n\ - \"currentNumber\" : \"0\",\n\ + \"currentNumber\" : \"300\",\n\ \"currentGasLimit\" : \"1000000\",\n\ \"currentDifficulty\" : \"115792089237316195423570985008687907853269984665640564039457584007913129639935\",\n\ \"currentTimestamp\" : 2,\n\ @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) read_string(s, v); // insert new random code - v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + "55"; + v.get_obj().find("randomVMtest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + (randGen() > 128 ? "55" : ""); // execute code in vm doMyTests(v); From a00d05e2bb93a21e589360b7672f62c9bc086c53 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 13 Jan 2015 18:00:00 +0100 Subject: [PATCH 23/27] replace BOOST_REQUIRE with assert, because it is used outside the boost test framework --- vm.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm.cpp b/vm.cpp index 8b8c75a04..42113e7b6 100644 --- a/vm.cpp +++ b/vm.cpp @@ -232,10 +232,10 @@ void FakeExtVM::importCallCreates(mArray& _callcreates) for (mValue& v: _callcreates) { auto tx = v.get_obj(); - BOOST_REQUIRE(tx.count("data") > 0); - BOOST_REQUIRE(tx.count("value") > 0); - BOOST_REQUIRE(tx.count("destination") > 0); - BOOST_REQUIRE(tx.count("gasLimit") > 0); + assert(tx.count("data") > 0); + assert(tx.count("value") > 0); + assert(tx.count("destination") > 0); + assert(tx.count("gasLimit") > 0); Transaction t = tx["destination"].get_str().empty() ? Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), data.toBytes()) : Transaction(toInt(tx["value"]), 0, toInt(tx["gasLimit"]), Address(tx["destination"].get_str()), data.toBytes()); From 522a7709e8fa65f57a4213d4dda875e04af0c64e Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 12 Jan 2015 12:47:37 +0100 Subject: [PATCH 24/27] Modify gas and value for external function call. --- SolidityEndToEndTest.cpp | 80 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 9543497a7..774df3d9f 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -1295,7 +1295,85 @@ BOOST_AUTO_TEST_CASE(contracts_as_addresses) } )"; compileAndRun(sourceCode, 20); - BOOST_REQUIRE(callContractFunction("getBalance()") == toBigEndian(u256(20 - 5)) + toBigEndian(u256(5))); + BOOST_REQUIRE(callContractFunction("getBalance()") == encodeArgs(u256(20 - 5), u256(5))); +} + +BOOST_AUTO_TEST_CASE(gas_and_value_basic) +{ + char const* sourceCode = R"( + contract helper { + bool flag; + function getBalance() returns (uint256 myBalance) { + return this.balance; + } + function setFlag() { flag = true; } + function getFlag() returns (bool fl) { return flag; } + } + contract test { + helper h; + function test() { h = new helper(); } + function sendAmount(uint amount) returns (uint256 bal) { + return h.getBalance.value(amount)(); + } + function outOfGas() returns (bool flagBefore, bool flagAfter, uint myBal) { + flagBefore = h.getFlag(); + h.setFlag.gas(2)(); // should fail due to OOG, return value can be garbage + flagAfter = h.getFlag(); + myBal = this.balance; + } + } + )"; + compileAndRun(sourceCode, 20); + BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(5)); + // call to helper should not succeed but amount should be transferred anyway + BOOST_REQUIRE(callContractFunction("outOfGas()", 5) == encodeArgs(false, false, 20 - 5)); +} + +BOOST_AUTO_TEST_CASE(value_complex) +{ + char const* sourceCode = R"( + contract helper { + function getBalance() returns (uint256 myBalance) { + return this.balance; + } + } + contract test { + helper h; + function test() { h = new helper(); } + function sendAmount(uint amount) returns (uint256 bal) { + var x1 = h.getBalance.value(amount); + uint someStackElement = 20; + var x2 = x1.gas(1000); + return x2.value(amount + 3)();// overwrite value + } + } + )"; + compileAndRun(sourceCode, 20); + BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(8)); +} + +BOOST_AUTO_TEST_CASE(value_insane) +{ + char const* sourceCode = R"( + contract helper { + function getBalance() returns (uint256 myBalance) { + return this.balance; + } + } + contract test { + helper h; + function test() { h = new helper(); } + function sendAmount(uint amount) returns (uint256 bal) { + var x1 = h.getBalance.value; + uint someStackElement = 20; + var x2 = x1(amount).gas; + var x3 = x2(1000).value; + return x3(amount + 3)();// overwrite value + } + } + )"; + compileAndRun(sourceCode, 20); + BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(8)); } BOOST_AUTO_TEST_SUITE_END() From e1559df82e16737e694613e8ffd2065a0bc21fa1 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 13 Jan 2015 18:12:19 +0100 Subject: [PATCH 25/27] Specify value for contract creation. --- SolidityEndToEndTest.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 774df3d9f..16787c8e7 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -1376,6 +1376,34 @@ BOOST_AUTO_TEST_CASE(value_insane) BOOST_REQUIRE(callContractFunction("sendAmount(uint256)", 5) == encodeArgs(8)); } +BOOST_AUTO_TEST_CASE(value_for_constructor) +{ + char const* sourceCode = R"( + contract Helper { + string3 name; + bool flag; + function Helper(string3 x, bool f) { + name = x; + flag = f; + } + function getName() returns (string3 ret) { return name; } + function getFlag() returns (bool ret) { return flag; } + } + contract Main { + Helper h; + function Main() { + h = new Helper.value(10)("abc", true); + } + function getFlag() returns (bool ret) { return h.getFlag(); } + function getName() returns (string3 ret) { return h.getName(); } + function getBalances() returns (uint me, uint them) { me = this.balance; them = h.balance;} + })"; + compileAndRun(sourceCode, 22, "Main"); + BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); + BOOST_REQUIRE(callContractFunction("getBalances()") == encodeArgs(12, 10)); +} + BOOST_AUTO_TEST_SUITE_END() } From 69bd72c6670a2fd3b5268e4ae9ca426b823b738e Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Wed, 14 Jan 2015 09:39:21 +0100 Subject: [PATCH 26/27] less invalid opcode tests --- createRandomTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/createRandomTest.cpp b/createRandomTest.cpp index 37d61fac9..a913d697f 100644 --- a/createRandomTest.cpp +++ b/createRandomTest.cpp @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) uint8_t opcode = randGen(); // disregard all invalid commands, except of one (0x0c) - if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 128))) + if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 250))) randomCode += toHex(toCompactBigEndian(opcode)); else i--; From 212a0c52516950af1a5b14cfc0855ad9ec897e5a Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 14 Jan 2015 10:16:58 +0100 Subject: [PATCH 27/27] Check for hash collisions already before compiling. --- SolidityNameAndTypeResolution.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SolidityNameAndTypeResolution.cpp b/SolidityNameAndTypeResolution.cpp index 94271b1f7..e2b4f160d 100644 --- a/SolidityNameAndTypeResolution.cpp +++ b/SolidityNameAndTypeResolution.cpp @@ -357,6 +357,18 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases) } } + +BOOST_AUTO_TEST_CASE(hash_collision_in_interface) +{ + char const* text = "contract test {\n" + " function gsf() {\n" + " }\n" + " function tgeo() {\n" + " }\n" + "}\n"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + BOOST_AUTO_TEST_SUITE_END() }