From 8967476a8d5371e81e224ec284fb9100816242c7 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Fri, 15 May 2015 12:23:13 +0200 Subject: [PATCH 01/14] added error jump instead of STOP instraction in case of exception --- libsolidity/SolidityCompiler.cpp | 192 --------------------------- libsolidity/SolidityEndToEndTest.cpp | 1 + 2 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 libsolidity/SolidityCompiler.cpp diff --git a/libsolidity/SolidityCompiler.cpp b/libsolidity/SolidityCompiler.cpp deleted file mode 100644 index dda7847ed..000000000 --- a/libsolidity/SolidityCompiler.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/* - 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 . -*/ -/** - * @author Christian - * @date 2014 - * Unit tests for the solidity compiler. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace std; -using namespace dev::eth; - -namespace dev -{ -namespace solidity -{ -namespace test -{ - -namespace -{ - -bytes compileContract(const string& _sourceCode) -{ - Parser parser; - ASTPointer sourceUnit; - BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared(CharStream(_sourceCode)))); - NameAndTypeResolver resolver({}); - resolver.registerDeclarations(*sourceUnit); - for (ASTPointer const& node: sourceUnit->getNodes()) - if (ContractDefinition* contract = dynamic_cast(node.get())) - { - BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract)); - } - for (ASTPointer const& node: sourceUnit->getNodes()) - if (ContractDefinition* contract = dynamic_cast(node.get())) - { - BOOST_REQUIRE_NO_THROW(resolver.checkTypeRequirements(*contract)); - } - for (ASTPointer const& node: sourceUnit->getNodes()) - if (ContractDefinition* contract = dynamic_cast(node.get())) - { - Compiler compiler; - compiler.compileContract(*contract, map{}); - - // debug - //compiler.streamAssembly(cout); - return compiler.getAssembledBytecode(); - } - BOOST_FAIL("No contract found in source."); - return bytes(); -} - -/// Checks that @a _compiledCode is present starting from offset @a _offset in @a _expectation. -/// This is necessary since the compiler will add boilerplate add the beginning that is not -/// tested here. -void checkCodePresentAt(bytes const& _compiledCode, bytes const& _expectation, unsigned _offset) -{ - BOOST_REQUIRE(_compiledCode.size() >= _offset + _expectation.size()); - auto checkStart = _compiledCode.begin() + _offset; - BOOST_CHECK_EQUAL_COLLECTIONS(checkStart, checkStart + _expectation.size(), - _expectation.begin(), _expectation.end()); -} - -} // end anonymous namespace - -BOOST_AUTO_TEST_SUITE(SolidityCompiler) - -BOOST_AUTO_TEST_CASE(smoke_test) -{ - char const* sourceCode = "contract test {\n" - " function f() { var x = 2; }\n" - "}\n"; - bytes code = compileContract(sourceCode); - - unsigned boilerplateSize = 73; - bytes expectation({byte(Instruction::JUMPDEST), - byte(Instruction::PUSH1), 0x0, // initialize local variable x - byte(Instruction::PUSH1), 0x2, - byte(Instruction::SWAP1), - byte(Instruction::POP), - byte(Instruction::JUMPDEST), - byte(Instruction::POP), - byte(Instruction::JUMP)}); - checkCodePresentAt(code, expectation, boilerplateSize); -} - -BOOST_AUTO_TEST_CASE(ifStatement) -{ - char const* sourceCode = "contract test {\n" - " function f() { bool x; if (x) 77; else if (!x) 78; else 79; }" - "}\n"; - bytes code = compileContract(sourceCode); - unsigned shift = 60; - unsigned boilerplateSize = 73; - bytes expectation({ - byte(Instruction::JUMPDEST), - byte(Instruction::PUSH1), 0x0, - byte(Instruction::DUP1), - byte(Instruction::ISZERO), - byte(Instruction::PUSH1), byte(0x0f + shift), // "false" target - byte(Instruction::JUMPI), - // "if" body - byte(Instruction::PUSH1), 0x4d, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x21 + shift), - byte(Instruction::JUMP), - // new check "else if" condition - byte(Instruction::JUMPDEST), - byte(Instruction::DUP1), - byte(Instruction::ISZERO), - byte(Instruction::ISZERO), - byte(Instruction::PUSH1), byte(0x1c + shift), - byte(Instruction::JUMPI), - // "else if" body - byte(Instruction::PUSH1), 0x4e, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x20 + shift), - byte(Instruction::JUMP), - // "else" body - byte(Instruction::JUMPDEST), - byte(Instruction::PUSH1), 0x4f, - byte(Instruction::POP), - }); - checkCodePresentAt(code, expectation, boilerplateSize); -} - -BOOST_AUTO_TEST_CASE(loops) -{ - char const* sourceCode = "contract test {\n" - " function f() { while(true){1;break;2;continue;3;return;4;} }" - "}\n"; - bytes code = compileContract(sourceCode); - unsigned shift = 60; - unsigned boilerplateSize = 73; - bytes expectation({byte(Instruction::JUMPDEST), - byte(Instruction::JUMPDEST), - byte(Instruction::PUSH1), 0x1, - byte(Instruction::ISZERO), - byte(Instruction::PUSH1), byte(0x21 + shift), - byte(Instruction::JUMPI), - byte(Instruction::PUSH1), 0x1, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x21 + shift), - byte(Instruction::JUMP), // break - byte(Instruction::PUSH1), 0x2, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x2 + shift), - byte(Instruction::JUMP), // continue - byte(Instruction::PUSH1), 0x3, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x22 + shift), - byte(Instruction::JUMP), // return - byte(Instruction::PUSH1), 0x4, - byte(Instruction::POP), - byte(Instruction::PUSH1), byte(0x2 + shift), - byte(Instruction::JUMP), - byte(Instruction::JUMPDEST), - byte(Instruction::JUMPDEST), - byte(Instruction::JUMP)}); - - checkCodePresentAt(code, expectation, boilerplateSize); -} - -BOOST_AUTO_TEST_SUITE_END() - -} -} -} // end namespaces diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 503615a5a..f8d20d70f 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -1784,6 +1784,7 @@ BOOST_AUTO_TEST_CASE(contracts_as_addresses) } )"; compileAndRun(sourceCode, 20); + auto res = callContractFunction("getBalance()"); BOOST_REQUIRE(callContractFunction("getBalance()") == encodeArgs(u256(20 - 5), u256(5))); } From 042491e0ccae9600b6d513c9e9023d35c9184feb Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Tue, 19 May 2015 14:55:12 +0200 Subject: [PATCH 02/14] added test to check evm exception --- libsolidity/SolidityEndToEndTest.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index f8d20d70f..faa5dad4d 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4111,6 +4111,30 @@ BOOST_AUTO_TEST_CASE(struct_delete_struct_in_mapping) BOOST_CHECK(callContractFunction("deleteIt()") == encodeArgs(0)); } +BOOST_AUTO_TEST_CASE(evm_exceptions) +{ + char const* sourceCode = R"( + contract A { + uint[3] arr; + bool public test = false; + function getElement(uint i) returns (uint) + { + return arr[i]; + } + function testIt() returns (bool) + { + uint i = this.getElement(5); + test = true; + return true; + } + } + )"; + compileAndRun(sourceCode, 0, "A"); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(false)); + BOOST_CHECK(callContractFunction("testIt()") == encodeArgs()); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(false)); +} + BOOST_AUTO_TEST_SUITE_END() } From 4a2f18b41a9977045a7bd5e859f96715e7f27e84 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 21 May 2015 12:10:26 +0200 Subject: [PATCH 03/14] one more test to test the call of non-existed function Conflicts: test/libsolidity/SolidityEndToEndTest.cpp --- libsolidity/SolidityEndToEndTest.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index faa5dad4d..839ad7928 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4111,7 +4111,7 @@ BOOST_AUTO_TEST_CASE(struct_delete_struct_in_mapping) BOOST_CHECK(callContractFunction("deleteIt()") == encodeArgs(0)); } -BOOST_AUTO_TEST_CASE(evm_exceptions) +BOOST_AUTO_TEST_CASE(evm_exceptions_out_of_band_access) { char const* sourceCode = R"( contract A { @@ -4135,6 +4135,32 @@ BOOST_AUTO_TEST_CASE(evm_exceptions) BOOST_CHECK(callContractFunction("test()") == encodeArgs(false)); } +BOOST_AUTO_TEST_CASE(evm_exceptions_when_calling_non_existing_function) +{ + char const* sourceCode = R"( + contract A { + uint public test = 0; + function badFunction() returns (uint) + { + this.call("123"); + test = 1; + return 2; + } + function testIt() returns (bool) + { + this.badFunction(); + test = 2; + return true; + } + } + )"; + compileAndRun(sourceCode, 0, "A"); + + BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); + BOOST_CHECK(callContractFunction("testIt()") == encodeArgs()); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); +} + BOOST_AUTO_TEST_SUITE_END() } From 82dc10d492d74fc1a9e07549f39d5ef2230822cc Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 21 May 2015 13:02:24 +0200 Subject: [PATCH 04/14] test for exception in constructor --- libsolidity/SolidityEndToEndTest.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 839ad7928..76952c676 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4161,6 +4161,23 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_when_calling_non_existing_function) BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); } +BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor) +{ + char const* sourceCode = R"( + contract A { + uint public test = 0; + function A() + { + this.call("123"); + test = 1; + } + } + )"; + compileAndRun(sourceCode, 0, "A"); + + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); +} + BOOST_AUTO_TEST_SUITE_END() } From f1c042df8c66f13ba102bdfae03337c45173a248 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 21 May 2015 13:45:32 +0200 Subject: [PATCH 05/14] test for constructor (out of band exception) --- libsolidity/SolidityEndToEndTest.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 76952c676..c150a488c 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4161,7 +4161,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_when_calling_non_existing_function) BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); } -BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor) +BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) { char const* sourceCode = R"( contract A { @@ -4169,7 +4169,25 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor) function A() { this.call("123"); - test = 1; + ++test; + } + } + )"; + compileAndRun(sourceCode, 0, "A"); + + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); +} + +BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_band) +{ + char const* sourceCode = R"( + contract A { + uint public test = 0; + uint[3] arr; + function A() + { + test = arr[5]; + ++test; } } )"; From fde4ed6bf60180d6212ec81f9ecbf630377087b3 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Fri, 22 May 2015 14:07:05 +0200 Subject: [PATCH 06/14] remove line for debugging --- libsolidity/SolidityEndToEndTest.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index c150a488c..e4c5b47f1 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -1784,7 +1784,6 @@ BOOST_AUTO_TEST_CASE(contracts_as_addresses) } )"; compileAndRun(sourceCode, 20); - auto res = callContractFunction("getBalance()"); BOOST_REQUIRE(callContractFunction("getBalance()") == encodeArgs(u256(20 - 5), u256(5))); } From 19fb836bf6440db32c8a392618bb76d44ec90953 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Fri, 22 May 2015 17:48:38 +0200 Subject: [PATCH 07/14] modified the test --- libsolidity/SolidityEndToEndTest.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index e4c5b47f1..538174ab5 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4164,24 +4164,32 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) { char const* sourceCode = R"( contract A { - uint public test = 0; function A() { this.call("123"); + + } + } + contract B { + uint public test = 1; + function testIt() + { + A a; ++test; } } )"; - compileAndRun(sourceCode, 0, "A"); + compileAndRun(sourceCode, 0, "B"); - BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); + BOOST_CHECK(callContractFunction("testIt()") == encodeArgs()); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(2)); } BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_band) { char const* sourceCode = R"( contract A { - uint public test = 0; + uint public test = 1; uint[3] arr; function A() { From 3ddae3209ed3ff8b7a4eae35a8984a7bb8d0ed58 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Tue, 26 May 2015 15:05:58 +0200 Subject: [PATCH 08/14] special handle of send --- libsolidity/SolidityEndToEndTest.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 538174ab5..efebbb2f0 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4167,7 +4167,6 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) function A() { this.call("123"); - } } contract B { From ca8104397dd20195e34e1b6933daef7f84e1d3ba Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 28 May 2015 14:48:37 +0200 Subject: [PATCH 09/14] removed exception when function is not found --- libsolidity/SolidityEndToEndTest.cpp | 32 ++-------------------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index efebbb2f0..d2faaae08 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4080,7 +4080,6 @@ BOOST_AUTO_TEST_CASE(struct_delete_member) } )"; compileAndRun(sourceCode, 0, "test"); - auto res = callContractFunction("deleteMember()"); BOOST_CHECK(callContractFunction("deleteMember()") == encodeArgs(0)); } @@ -4106,7 +4105,6 @@ BOOST_AUTO_TEST_CASE(struct_delete_struct_in_mapping) } )"; compileAndRun(sourceCode, 0, "test"); - auto res = callContractFunction("deleteIt()"); BOOST_CHECK(callContractFunction("deleteIt()") == encodeArgs(0)); } @@ -4134,32 +4132,6 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_out_of_band_access) BOOST_CHECK(callContractFunction("test()") == encodeArgs(false)); } -BOOST_AUTO_TEST_CASE(evm_exceptions_when_calling_non_existing_function) -{ - char const* sourceCode = R"( - contract A { - uint public test = 0; - function badFunction() returns (uint) - { - this.call("123"); - test = 1; - return 2; - } - function testIt() returns (bool) - { - this.badFunction(); - test = 2; - return true; - } - } - )"; - compileAndRun(sourceCode, 0, "A"); - - BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); - BOOST_CHECK(callContractFunction("testIt()") == encodeArgs()); - BOOST_CHECK(callContractFunction("test()") == encodeArgs(0)); -} - BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) { char const* sourceCode = R"( @@ -4184,7 +4156,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) BOOST_CHECK(callContractFunction("test()") == encodeArgs(2)); } -BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_band) +BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) { char const* sourceCode = R"( contract A { @@ -4199,7 +4171,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_band) )"; compileAndRun(sourceCode, 0, "A"); - BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); + //BOOST_CHECK(m_output.empty()); todo } BOOST_AUTO_TEST_SUITE_END() From 47f0e847e549d6c4bb13cafcb90b49832531cedd Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Fri, 29 May 2015 15:39:42 +0200 Subject: [PATCH 10/14] style fixes in test/libsolidity/solidityExecutionFramework.h fixed the test --- libsolidity/SolidityEndToEndTest.cpp | 4 +- libsolidity/solidityExecutionFramework.h | 263 ++++++++++++----------- 2 files changed, 141 insertions(+), 126 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index d2faaae08..ef7b5c2a4 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4169,9 +4169,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) } } )"; - compileAndRun(sourceCode, 0, "A"); - - //BOOST_CHECK(m_output.empty()); todo + BOOST_CHECK(execute(sourceCode, 0, "A").empty()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index fa25fb12c..2c70415eb 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -40,142 +40,159 @@ namespace test class ExecutionFramework { public: - ExecutionFramework() { g_logVerbosity = 0; } + ExecutionFramework() { g_logVerbosity = 0; } - bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") - { - m_compiler.reset(false, m_addStandardSources); - m_compiler.addSource("", _sourceCode); - ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); + bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + { + m_compiler.reset(false, m_addStandardSources); + m_compiler.addSource("", _sourceCode); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); - bytes code = m_compiler.getBytecode(_contractName); - sendMessage(code, true, _value); - BOOST_REQUIRE(!m_output.empty()); - return m_output; - } + bytes code = m_compiler.getBytecode(_contractName); + sendMessage(code, true, _value); + return m_output; + } - template - bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, - Args const&... _arguments) - { - FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); - return m_output; - } + bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + { + execute(_sourceCode, _value, _contractName); + BOOST_REQUIRE(!m_output.empty()); + return m_output; + } - template - bytes const& callContractFunction(std::string _sig, Args const&... _arguments) - { - return callContractFunctionWithValue(_sig, 0, _arguments...); - } + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) + { + FixedHash<4> hash(dev::sha3(_sig)); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); + return m_output; + } - template - void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) - { - bytes solidityResult = callContractFunction(_sig, _arguments...); - bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); - BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match." - "\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult)); - } + template + bytes const& callContractFunction(std::string _sig, Args const&... _arguments) + { + return callContractFunctionWithValue(_sig, 0, _arguments...); + } - template - void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, - u256 const& _rangeStart, u256 const& _rangeEnd) - { - for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) - { - bytes solidityResult = callContractFunction(_sig, argument); - bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); - BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match." - "\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult) + - "\nArgument: " + toHex(encode(argument))); - } - } + template + void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) + { + bytes solidityResult = callContractFunction(_sig, _arguments...); + bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, "Computed values do not match.\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult)); + } - 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(h256 const& _value) { return _value.asBytes(); } - static bytes encode(bytes const& _value, bool _padLeft = true) - { - 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 + void testSolidityAgainstCppOnRange( + std::string _sig, + CppFunction const& _cppFunction, + u256 const& _rangeStart, + u256 const& _rangeEnd + ) + { + for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) + { + bytes solidityResult = callContractFunction(_sig, argument); + bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult) + + "\nArgument: " + + toHex(encode(argument)) + ); + } + } - template - static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) - { - return encode(_firstArg) + encodeArgs(_followingArgs...); - } - static bytes encodeArgs() - { - return bytes(); - } + 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(h256 const& _value) { return _value.asBytes(); } + static bytes encode(bytes const& _value, bool _padLeft = true) + { + 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(); + } private: - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - _cppFunction(_arguments...); - return bytes(); - } - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - return encode(_cppFunction(_arguments...)); - } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + _cppFunction(_arguments...); + return bytes(); + } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + return encode(_cppFunction(_arguments...)); + } protected: - void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) - { - m_state.addBalance(m_sender, _value); // just in case - eth::Executive executive(m_state, eth::LastHashes(), 0); - eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) - : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); - bytes transactionRLP = t.rlp(); - try - { - // this will throw since the transaction is invalid, but it should nevertheless store the transaction - executive.initialize(&transactionRLP); - executive.execute(); - } - catch (...) {} - if (_isCreation) - { - BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); - m_contractAddress = executive.newAddress(); - BOOST_REQUIRE(m_contractAddress); - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - } - else - { - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); - } - BOOST_REQUIRE(executive.go()); - m_state.noteSending(m_sender); - executive.finalize(); - m_gasUsed = executive.gasUsed(); - m_output = executive.out().toVector(); - m_logs = executive.logs(); - } + void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) + { + m_state.addBalance(m_sender, _value); // just in case + eth::Executive executive(m_state, eth::LastHashes(), 0); + eth::Transaction t = _isCreation ? eth::Transaction( + _value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec() + ) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); - bool m_optimize = false; - bool m_addStandardSources = false; - dev::solidity::CompilerStack m_compiler; - Address m_sender; - Address m_contractAddress; - eth::State m_state; - u256 const m_gasPrice = 100 * eth::szabo; - u256 const m_gas = 100000000; - bytes m_output; - eth::LogEntries m_logs; - u256 m_gasUsed; + bytes transactionRLP = t.rlp(); + try + { + // this will throw since the transaction is invalid, but it should nevertheless store the transaction + executive.initialize(&transactionRLP); + executive.execute(); + } + catch (...) {} + if (_isCreation) + { + BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); + m_contractAddress = executive.newAddress(); + BOOST_REQUIRE(m_contractAddress); + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + } + else + { + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); + } + BOOST_REQUIRE(executive.go()); + m_state.noteSending(m_sender); + executive.finalize(); + m_gasUsed = executive.gasUsed(); + m_output = executive.out().toVector(); + m_logs = executive.logs(); + } + + bool m_optimize = false; + bool m_addStandardSources = false; + dev::solidity::CompilerStack m_compiler; + Address m_sender; + Address m_contractAddress; + eth::State m_state; + u256 const m_gasPrice = 100 * eth::szabo; + u256 const m_gas = 100000000; + bytes m_output; + eth::LogEntries m_logs; + u256 m_gasUsed; }; } From 945418cc108f7abef4e03c6196a4e23245b1fec6 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Fri, 29 May 2015 18:38:57 +0200 Subject: [PATCH 11/14] style fixes --- libsolidity/SolidityEndToEndTest.cpp | 2 +- libsolidity/solidityExecutionFramework.h | 284 +++++++++++------------ 2 files changed, 142 insertions(+), 144 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index ef7b5c2a4..db115b104 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4145,7 +4145,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_call_fail) uint public test = 1; function testIt() { - A a; + A a = new A(); ++test; } } diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 2c70415eb..879667fde 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -40,159 +40,157 @@ namespace test class ExecutionFramework { public: - ExecutionFramework() { g_logVerbosity = 0; } + ExecutionFramework() { g_logVerbosity = 0; } - bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") - { - m_compiler.reset(false, m_addStandardSources); - m_compiler.addSource("", _sourceCode); - ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); - - bytes code = m_compiler.getBytecode(_contractName); - sendMessage(code, true, _value); - return m_output; - } - - bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") - { - execute(_sourceCode, _value, _contractName); - BOOST_REQUIRE(!m_output.empty()); - return m_output; - } - - template - bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) - { - FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); - return m_output; - } - - template - bytes const& callContractFunction(std::string _sig, Args const&... _arguments) - { - return callContractFunctionWithValue(_sig, 0, _arguments...); - } - - template - void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) - { - bytes solidityResult = callContractFunction(_sig, _arguments...); - bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, "Computed values do not match.\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult)); - } - - template - void testSolidityAgainstCppOnRange( - std::string _sig, - CppFunction const& _cppFunction, - u256 const& _rangeStart, - u256 const& _rangeEnd - ) - { - for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) + bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { - bytes solidityResult = callContractFunction(_sig, argument); - bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, - "Computed values do not match.\nSolidity: " + - toHex(solidityResult) + - "\nC++: " + - toHex(cppResult) + - "\nArgument: " + - toHex(encode(argument)) - ); + m_compiler.reset(false, m_addStandardSources); + m_compiler.addSource("", _sourceCode); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); + bytes code = m_compiler.getBytecode(_contractName); + sendMessage(code, true, _value); + return m_output; } - } - 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(h256 const& _value) { return _value.asBytes(); } - static bytes encode(bytes const& _value, bool _padLeft = true) - { - 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); } + bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + { + execute(_sourceCode, _value, _contractName); + BOOST_REQUIRE(!m_output.empty()); + return m_output; + } - template - static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) - { - return encode(_firstArg) + encodeArgs(_followingArgs...); - } - static bytes encodeArgs() - { - return bytes(); - } + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) + { + FixedHash<4> hash(dev::sha3(_sig)); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); + return m_output; + } + + template + bytes const& callContractFunction(std::string _sig, Args const&... _arguments) + { + return callContractFunctionWithValue(_sig, 0, _arguments...); + } + + template + void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) + { + bytes solidityResult = callContractFunction(_sig, _arguments...); + bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult)); + } + + template + void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) + { + for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) + { + bytes solidityResult = callContractFunction(_sig, argument); + bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult) + + "\nArgument: " + + toHex(encode(argument)) + ); + } + } + + 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(h256 const& _value) { return _value.asBytes(); } + static bytes encode(bytes const& _value, bool _padLeft = true) + { + 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(); + } private: - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - _cppFunction(_arguments...); - return bytes(); - } - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - return encode(_cppFunction(_arguments...)); - } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + _cppFunction(_arguments...); + return bytes(); + } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + return encode(_cppFunction(_arguments...)); + } protected: - void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) - { - m_state.addBalance(m_sender, _value); // just in case - eth::Executive executive(m_state, eth::LastHashes(), 0); - eth::Transaction t = _isCreation ? eth::Transaction( - _value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec() - ) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); + void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) + { + m_state.addBalance(m_sender, _value); // just in case + eth::Executive executive(m_state, eth::LastHashes(), 0); + eth::Transaction t = + _isCreation ? + eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : + eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); + bytes transactionRLP = t.rlp(); + try + { + // this will throw since the transaction is invalid, but it should nevertheless store the transaction + executive.initialize(&transactionRLP); + executive.execute(); + } + catch (...) {} + if (_isCreation) + { + BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); + m_contractAddress = executive.newAddress(); + BOOST_REQUIRE(m_contractAddress); + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + } + else + { + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); + } + BOOST_REQUIRE(executive.go()); + m_state.noteSending(m_sender); + executive.finalize(); + m_gasUsed = executive.gasUsed(); + m_output = executive.out().toVector(); + m_logs = executive.logs(); + } - bytes transactionRLP = t.rlp(); - try - { - // this will throw since the transaction is invalid, but it should nevertheless store the transaction - executive.initialize(&transactionRLP); - executive.execute(); - } - catch (...) {} - if (_isCreation) - { - BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); - m_contractAddress = executive.newAddress(); - BOOST_REQUIRE(m_contractAddress); - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - } - else - { - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); - } - BOOST_REQUIRE(executive.go()); - m_state.noteSending(m_sender); - executive.finalize(); - m_gasUsed = executive.gasUsed(); - m_output = executive.out().toVector(); - m_logs = executive.logs(); - } - - bool m_optimize = false; - bool m_addStandardSources = false; - dev::solidity::CompilerStack m_compiler; - Address m_sender; - Address m_contractAddress; - eth::State m_state; - u256 const m_gasPrice = 100 * eth::szabo; - u256 const m_gas = 100000000; - bytes m_output; - eth::LogEntries m_logs; - u256 m_gasUsed; + bool m_optimize = false; + bool m_addStandardSources = false; + dev::solidity::CompilerStack m_compiler; + Address m_sender; + Address m_contractAddress; + eth::State m_state; + u256 const m_gasPrice = 100 * eth::szabo; + u256 const m_gas = 100000000; + bytes m_output; + eth::LogEntries m_logs; + u256 m_gasUsed; }; } From d863c8d088bd681762cbb4e3deb204e594108f81 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 1 Jun 2015 13:03:29 +0200 Subject: [PATCH 12/14] corrected intends in solidityExecutionFramwork.h --- libsolidity/solidityExecutionFramework.h | 276 +++++++++++------------ 1 file changed, 138 insertions(+), 138 deletions(-) diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 879667fde..29f3c4710 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -40,157 +40,157 @@ namespace test class ExecutionFramework { public: - ExecutionFramework() { g_logVerbosity = 0; } +ExecutionFramework() { g_logVerbosity = 0; } - bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") - { - m_compiler.reset(false, m_addStandardSources); - m_compiler.addSource("", _sourceCode); - ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); - bytes code = m_compiler.getBytecode(_contractName); - sendMessage(code, true, _value); - return m_output; - } + bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + { + m_compiler.reset(false, m_addStandardSources); + m_compiler.addSource("", _sourceCode); + ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize), "Compiling contract failed"); + bytes code = m_compiler.getBytecode(_contractName); + sendMessage(code, true, _value); + return m_output; + } - bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") - { - execute(_sourceCode, _value, _contractName); - BOOST_REQUIRE(!m_output.empty()); - return m_output; - } + bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + { + execute(_sourceCode, _value, _contractName); + BOOST_REQUIRE(!m_output.empty()); + return m_output; + } - template - bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) - { - FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); - return m_output; - } + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, Args const&... _arguments) + { + FixedHash<4> hash(dev::sha3(_sig)); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); + return m_output; + } - template - bytes const& callContractFunction(std::string _sig, Args const&... _arguments) - { - return callContractFunctionWithValue(_sig, 0, _arguments...); - } + template + bytes const& callContractFunction(std::string _sig, Args const&... _arguments) + { + return callContractFunctionWithValue(_sig, 0, _arguments...); + } - template - void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) - { - bytes solidityResult = callContractFunction(_sig, _arguments...); - bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, - "Computed values do not match.\nSolidity: " + - toHex(solidityResult) + - "\nC++: " + - toHex(cppResult)); - } + template + void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) + { + bytes solidityResult = callContractFunction(_sig, _arguments...); + bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult)); + } - template - void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) - { - for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) - { - bytes solidityResult = callContractFunction(_sig, argument); - bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); - BOOST_CHECK_MESSAGE( - solidityResult == cppResult, - "Computed values do not match.\nSolidity: " + - toHex(solidityResult) + - "\nC++: " + - toHex(cppResult) + - "\nArgument: " + - toHex(encode(argument)) - ); - } - } + template + void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) + { + for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) + { + bytes solidityResult = callContractFunction(_sig, argument); + bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); + BOOST_CHECK_MESSAGE( + solidityResult == cppResult, + "Computed values do not match.\nSolidity: " + + toHex(solidityResult) + + "\nC++: " + + toHex(cppResult) + + "\nArgument: " + + toHex(encode(argument)) + ); + } + } - 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(h256 const& _value) { return _value.asBytes(); } - static bytes encode(bytes const& _value, bool _padLeft = true) - { - 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); } + 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(h256 const& _value) { return _value.asBytes(); } + static bytes encode(bytes const& _value, bool _padLeft = true) + { + 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(); - } + template + static bytes encodeArgs(FirstArg const& _firstArg, Args const&... _followingArgs) + { + return encode(_firstArg) + encodeArgs(_followingArgs...); + } + static bytes encodeArgs() + { + return bytes(); + } private: - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - _cppFunction(_arguments...); - return bytes(); - } - template - auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) - -> typename std::enable_if::value, bytes>::type - { - return encode(_cppFunction(_arguments...)); - } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + _cppFunction(_arguments...); + return bytes(); + } + template + auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) + -> typename std::enable_if::value, bytes>::type + { + return encode(_cppFunction(_arguments...)); + } protected: - void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) - { - m_state.addBalance(m_sender, _value); // just in case - eth::Executive executive(m_state, eth::LastHashes(), 0); - eth::Transaction t = - _isCreation ? - eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : - eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); - bytes transactionRLP = t.rlp(); - try - { - // this will throw since the transaction is invalid, but it should nevertheless store the transaction - executive.initialize(&transactionRLP); - executive.execute(); - } - catch (...) {} - if (_isCreation) - { - BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); - m_contractAddress = executive.newAddress(); - BOOST_REQUIRE(m_contractAddress); - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - } - else - { - BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); - BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); - } - BOOST_REQUIRE(executive.go()); - m_state.noteSending(m_sender); - executive.finalize(); - m_gasUsed = executive.gasUsed(); - m_output = executive.out().toVector(); - m_logs = executive.logs(); - } + void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0) + { + m_state.addBalance(m_sender, _value); // just in case + eth::Executive executive(m_state, eth::LastHashes(), 0); + eth::Transaction t = + _isCreation ? + eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : + eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); + bytes transactionRLP = t.rlp(); + try + { + // this will throw since the transaction is invalid, but it should nevertheless store the transaction + executive.initialize(&transactionRLP); + executive.execute(); + } + catch (...) {} + if (_isCreation) + { + BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender)); + m_contractAddress = executive.newAddress(); + BOOST_REQUIRE(m_contractAddress); + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + } + else + { + BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); + BOOST_REQUIRE(!executive.call(m_contractAddress, m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender)); + } + BOOST_REQUIRE(executive.go()); + m_state.noteSending(m_sender); + executive.finalize(); + m_gasUsed = executive.gasUsed(); + m_output = executive.out().toVector(); + m_logs = executive.logs(); + } - bool m_optimize = false; - bool m_addStandardSources = false; - dev::solidity::CompilerStack m_compiler; - Address m_sender; - Address m_contractAddress; - eth::State m_state; - u256 const m_gasPrice = 100 * eth::szabo; - u256 const m_gas = 100000000; - bytes m_output; - eth::LogEntries m_logs; - u256 m_gasUsed; + bool m_optimize = false; + bool m_addStandardSources = false; + dev::solidity::CompilerStack m_compiler; + Address m_sender; + Address m_contractAddress; + eth::State m_state; + u256 const m_gasPrice = 100 * eth::szabo; + u256 const m_gas = 100000000; + bytes m_output; + eth::LogEntries m_logs; + u256 m_gasUsed; }; } From c6a8938ed74b54b0351f30d8d4a3601c8d1eb871 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 1 Jun 2015 16:39:09 +0200 Subject: [PATCH 13/14] fixed CALL case. added exception --- libsolidity/solidityExecutionFramework.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index 29f3c4710..ddbbb0161 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -40,7 +40,7 @@ namespace test class ExecutionFramework { public: -ExecutionFramework() { g_logVerbosity = 0; } + ExecutionFramework() { g_logVerbosity = 0; } bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { From b810df04f0ebcc65ed7a57fa650d5bb090c8b31d Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Mon, 1 Jun 2015 16:48:13 +0200 Subject: [PATCH 14/14] renamed the test framwork function. --- libsolidity/SolidityEndToEndTest.cpp | 2 +- libsolidity/solidityExecutionFramework.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index db115b104..9f6f27d7d 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4169,7 +4169,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) } } )"; - BOOST_CHECK(execute(sourceCode, 0, "A").empty()); + BOOST_CHECK(compileAndRunWthoutCheck(sourceCode, 0, "A").empty()); } BOOST_AUTO_TEST_SUITE_END() diff --git a/libsolidity/solidityExecutionFramework.h b/libsolidity/solidityExecutionFramework.h index ddbbb0161..c29257dd9 100644 --- a/libsolidity/solidityExecutionFramework.h +++ b/libsolidity/solidityExecutionFramework.h @@ -42,7 +42,7 @@ class ExecutionFramework public: ExecutionFramework() { g_logVerbosity = 0; } - bytes const& execute(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") + bytes const& compileAndRunWthoutCheck(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", _sourceCode); @@ -54,7 +54,7 @@ public: bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { - execute(_sourceCode, _value, _contractName); + compileAndRunWthoutCheck(_sourceCode, _value, _contractName); BOOST_REQUIRE(!m_output.empty()); return m_output; }