From b8b75c841b053a20e405cba0a50461f2ea836683 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Wed, 7 Jan 2015 16:39:21 +0100 Subject: [PATCH 01/10] Solidity getInterfaceFunctions is now a map of hash to Function - Also introduced dependency between libsolidity and libdevcrypto - Compler's appendFunctionSelector now has a first version of using function signature hash instead of index --- libsolidity/AST.cpp | 16 ++++++++-------- libsolidity/AST.h | 5 +++-- libsolidity/CMakeLists.txt | 1 + libsolidity/Compiler.cpp | 30 +++++++++++++----------------- libsolidity/InterfaceHandler.cpp | 25 ++++++++++++++----------- libsolidity/Types.cpp | 10 ++++++---- 6 files changed, 45 insertions(+), 42 deletions(-) diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index 0c56cb7a7..300303ac5 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -27,6 +27,8 @@ #include #include +#include + using namespace std; namespace dev @@ -50,18 +52,16 @@ void ContractDefinition::checkTypeRequirements() function->checkTypeRequirements(); } -vector ContractDefinition::getInterfaceFunctions() const +map, FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const { - vector exportedFunctions; + map, FunctionDefinition const*> exportedFunctions; for (ASTPointer const& f: m_definedFunctions) if (f->isPublic() && f->getName() != getName()) - exportedFunctions.push_back(f.get()); - auto compareNames = [](FunctionDefinition const* _a, FunctionDefinition const* _b) - { - return _a->getName().compare(_b->getName()) < 0; - }; + { + FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); + exportedFunctions[hash] = f.get(); + } - sort(exportedFunctions.begin(), exportedFunctions.end(), compareNames); return exportedFunctions; } diff --git a/libsolidity/AST.h b/libsolidity/AST.h index 8493d4323..95121d4cb 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -183,8 +183,9 @@ public: /// Can contain a nullptr in which case indicates absence of documentation ASTPointer const& getDocumentation() const { return m_documentation; } - /// Returns the functions that make up the calling interface in the intended order. - std::vector getInterfaceFunctions() const; + /// @returns a map of canonical function signatures to FunctionDefinitions + /// as intended for use by the ABI. + std::map, FunctionDefinition const*> getInterfaceFunctions() const; /// Returns the constructor or nullptr if no constructor was specified FunctionDefinition const* getConstructor() const; diff --git a/libsolidity/CMakeLists.txt b/libsolidity/CMakeLists.txt index 0a0b62bdd..9c0b50775 100644 --- a/libsolidity/CMakeLists.txt +++ b/libsolidity/CMakeLists.txt @@ -28,6 +28,7 @@ endif() target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES}) target_link_libraries(${EXECUTABLE} evmcore) target_link_libraries(${EXECUTABLE} devcore) +target_link_libraries(${EXECUTABLE} devcrypto) install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 394ae5f84..83fd69f52 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -118,34 +118,30 @@ set Compiler::getFunctionsNeededByConstructor(Functio void Compiler::appendFunctionSelector(ContractDefinition const& _contract) { - vector interfaceFunctions = _contract.getInterfaceFunctions(); + map, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions(); vector callDataUnpackerEntryPoints; - if (interfaceFunctions.size() > 255) - BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 255 public functions for contract.")); + if (interfaceFunctions.size() > 4294967295) // 2 ** 32 + BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract.")); - // retrieve the first byte of the call data, which determines the called function - // @todo This code had a jump table in a previous version which was more efficient but also - // error prone (due to the optimizer and variable length tag addresses) - m_context << u256(1) << u256(0) // some constants - << eth::dupInstruction(1) << eth::Instruction::CALLDATALOAD - << eth::dupInstruction(2) << eth::Instruction::BYTE - << eth::dupInstruction(2); + // retrieve the first function signature hash from the calldata + m_context << u256(1) << u256(0) << u256(2.6959947e+67) // some constants + << eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD + << eth::Instruction::DIV; - // stack here: 1 0 0, stack top will be counted up until it matches funid - for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid) + // stack now is: 1 0 2.6959947e+67 + for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it) { callDataUnpackerEntryPoints.push_back(m_context.newTag()); - m_context << eth::dupInstruction(2) << eth::dupInstruction(2) << eth::Instruction::EQ; + m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it->first)) << eth::Instruction::EQ; m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back()); - if (funid < interfaceFunctions.size() - 1) - m_context << eth::dupInstruction(4) << eth::Instruction::ADD; } m_context << eth::Instruction::STOP; // function not found - for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid) + unsigned funid = 0; + for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it, ++funid) { - FunctionDefinition const& function = *interfaceFunctions[funid]; + FunctionDefinition const& function = *it->second; m_context << callDataUnpackerEntryPoints[funid]; eth::AssemblyItem returnTag = m_context.pushNewTag(); appendCalldataUnpacker(function); diff --git a/libsolidity/InterfaceHandler.cpp b/libsolidity/InterfaceHandler.cpp index 224234cbd..0843c3637 100644 --- a/libsolidity/InterfaceHandler.cpp +++ b/libsolidity/InterfaceHandler.cpp @@ -35,8 +35,9 @@ std::unique_ptr InterfaceHandler::getDocumentation(ContractDefiniti std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinition const& _contractDef) { Json::Value methods(Json::arrayValue); + auto interfaceFunctions = _contractDef.getInterfaceFunctions(); - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) { Json::Value method; Json::Value inputs(Json::arrayValue); @@ -55,10 +56,10 @@ std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinitio return params; }; - method["name"] = f->getName(); - method["constant"] = f->isDeclaredConst(); - method["inputs"] = populateParameters(f->getParameters()); - method["outputs"] = populateParameters(f->getReturnParameters()); + method["name"] = it->second->getName(); + method["constant"] = it->second->isDeclaredConst(); + method["inputs"] = populateParameters(it->second->getParameters()); + method["outputs"] = populateParameters(it->second->getReturnParameters()); methods.append(method); } return std::unique_ptr(new std::string(m_writer.write(methods))); @@ -68,11 +69,12 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi { Json::Value doc; Json::Value methods(Json::objectValue); + auto interfaceFunctions = _contractDef.getInterfaceFunctions(); - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) { Json::Value user; - auto strPtr = f->getDocumentation(); + auto strPtr = it->second->getDocumentation(); if (strPtr) { resetUser(); @@ -80,7 +82,7 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi if (!m_notice.empty()) {// since @notice is the only user tag if missing function should not appear user["notice"] = Json::Value(m_notice); - methods[f->getName()] = user; + methods[it->second->getName()] = user; } } } @@ -110,10 +112,11 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin doc["title"] = m_title; } - for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) + auto interfaceFunctions = _contractDef.getInterfaceFunctions(); + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) { Json::Value method; - auto strPtr = f->getDocumentation(); + auto strPtr = it->second->getDocumentation(); if (strPtr) { resetDev(); @@ -136,7 +139,7 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin method["return"] = m_return; if (!method.empty()) // add the function, only if we have any documentation to add - methods[f->getName()] = method; + methods[it->second->getName()] = method; } } doc["methods"] = methods; diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index 7a4c45c6f..1a04fe616 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -301,9 +301,10 @@ MemberList const& ContractType::getMembers() const // We need to lazy-initialize it because of recursive references. if (!m_members) { + auto interfaceFunctions = m_contract.getInterfaceFunctions(); map> members; - for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) - members[function->getName()] = make_shared(*function, false); + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + members[it->second->getName()] = make_shared(*it->second, false); m_members.reset(new MemberList(members)); } return *m_members; @@ -325,9 +326,10 @@ shared_ptr const& ContractType::getConstructorType() const unsigned ContractType::getFunctionIndex(string const& _functionName) const { unsigned index = 0; - for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) + auto interfaceFunctions = m_contract.getInterfaceFunctions(); + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) { - if (function->getName() == _functionName) + if (it->second->getName() == _functionName) return index; ++index; } From b2362f41137a60c9386155d301bd7d5373d6cc10 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Wed, 7 Jan 2015 18:13:03 +0100 Subject: [PATCH 02/10] Adjusting SolidityCompiler tests - Adjusting SolidityCompiler tests to take into account the size of the new Function Selector --- test/SolidityCompiler.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/SolidityCompiler.cpp b/test/SolidityCompiler.cpp index 385a3e577..ea09678d9 100644 --- a/test/SolidityCompiler.cpp +++ b/test/SolidityCompiler.cpp @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(smoke_test) "}\n"; bytes code = compileContract(sourceCode); - unsigned boilerplateSize = 40; + unsigned boilerplateSize = 72; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, // initialize local variable x byte(Instruction::PUSH1), 0x2, @@ -115,9 +115,8 @@ BOOST_AUTO_TEST_CASE(different_argument_numbers) " function g() returns (uint e, uint h) { h = f(1, 2, 3); }\n" "}\n"; bytes code = compileContract(sourceCode); - - unsigned shift = 68; - unsigned boilerplateSize = 81; + unsigned shift = 102; + unsigned boilerplateSize = 115; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, // initialize return variable d byte(Instruction::DUP3), @@ -166,9 +165,8 @@ BOOST_AUTO_TEST_CASE(ifStatement) " function f() { bool x; if (x) 77; else if (!x) 78; else 79; }" "}\n"; bytes code = compileContract(sourceCode); - - unsigned shift = 27; - unsigned boilerplateSize = 40; + unsigned shift = 59; + unsigned boilerplateSize = 72; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, byte(Instruction::DUP1), @@ -208,9 +206,8 @@ BOOST_AUTO_TEST_CASE(loops) " function f() { while(true){1;break;2;continue;3;return;4;} }" "}\n"; bytes code = compileContract(sourceCode); - - unsigned shift = 27; - unsigned boilerplateSize = 40; + unsigned shift = 59; + unsigned boilerplateSize = 72; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x1, From 69a0dcfa15fe4789dc4bfb02eda3427f24780caa Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 8 Jan 2015 00:19:19 +0100 Subject: [PATCH 03/10] work in progress in testing the new function selector in the end to end tests --- libsolidity/Compiler.cpp | 6 +++--- test/solidityExecutionFramework.h | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 83fd69f52..514e1a27c 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -124,12 +124,12 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) if (interfaceFunctions.size() > 4294967295) // 2 ** 32 BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract.")); - // retrieve the first function signature hash from the calldata - m_context << u256(1) << u256(0) << u256(2.6959947e+67) // some constants + // retrieve the function signature hash from the calldata + m_context << u256(1) << u256(0) << u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296)// some constants << eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD << eth::Instruction::DIV; - // stack now is: 1 0 2.6959947e+67 + // stack now is: 1 0 for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it) { callDataUnpackerEntryPoints.push_back(m_context.newTag()); diff --git a/test/solidityExecutionFramework.h b/test/solidityExecutionFramework.h index 9f25b3725..9beabdc54 100644 --- a/test/solidityExecutionFramework.h +++ b/test/solidityExecutionFramework.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace dev { @@ -48,17 +49,32 @@ public: bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { - dev::solidity::CompilerStack compiler; - compiler.compile(_sourceCode, m_optimize); - bytes code = compiler.getBytecode(_contractName); + /* dev::solidity::CompilerStack compiler; */ + m_compiler.compile(_sourceCode, m_optimize); + bytes code = m_compiler.getBytecode(_contractName); + /* m_contractDefinition = compiler.getContractDefinition(_contractName); */ sendMessage(code, true, _value); BOOST_REQUIRE(!m_output.empty()); return m_output; } - bytes const& callContractFunction(byte _index, bytes const& _data = bytes(), u256 const& _value = 0) + bytes const& callContractFunction(byte _index, bytes const& _data = bytes(), + u256 const& _value = 0) { - sendMessage(bytes(1, _index) + _data, false, _value); + /* if (!_contractDef) */ + /* _contractDef = m_contractDefinition; */ + + unsigned index = 0; + auto interfaceFunctions = m_compiler.getContractDefinition("").getInterfaceFunctions(); + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++index) + if (index == _index) + { + sendMessage(it->first.asBytes() + _data, false, _value); + /* sendMessage(bytes(1, _index) + _data, false, _value); */ + return m_output; + } + + BOOST_FAIL("Function with index " << _index << "not found"); return m_output; } @@ -149,6 +165,9 @@ protected: bool m_optimize = false; Address m_sender; Address m_contractAddress; + /* ContractDefinition m_contractDefinition; */ + dev::solidity::CompilerStack m_compiler; + eth::State m_state; u256 const m_gasPrice = 100 * eth::szabo; u256 const m_gas = 1000000; From c201e85c01d22279de25552f5d1901724aa64e13 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 8 Jan 2015 12:20:14 +0100 Subject: [PATCH 04/10] mix uses new getInterfaceFunctions() --- mix/QContractDefinition.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mix/QContractDefinition.cpp b/mix/QContractDefinition.cpp index 72f96dbcc..bee9cfe49 100644 --- a/mix/QContractDefinition.cpp +++ b/mix/QContractDefinition.cpp @@ -33,11 +33,9 @@ using namespace dev::mix; QContractDefinition::QContractDefinition(dev::solidity::ContractDefinition const* _contract): QBasicNodeDefinition(_contract) { - std::vector functions = _contract->getInterfaceFunctions(); - for (unsigned i = 0; i < functions.size(); i++) - { - FunctionDefinition const* func = functions.at(i); - m_functions.append(new QFunctionDefinition(func, i)); - } + auto interfaceFunctions = _contract->getInterfaceFunctions(); + unsigned i = 0; + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++i) + m_functions.append(new QFunctionDefinition(it->second, i)); } From 9fff09af6b8c7e9b818780f16cc30ae26a057cec Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 8 Jan 2015 17:18:31 +0100 Subject: [PATCH 05/10] Compiler EVM generation now takes into account for the new function hash identifier - Changed tests to comply with the new function hash identifier - Changed the function index offset to 4, and made it a constant for easy adjustment in the future --- libsolidity/Compiler.cpp | 10 +- libsolidity/CompilerUtils.h | 3 + libsolidity/ExpressionCompiler.cpp | 17 +- libsolidity/Types.cpp | 9 +- libsolidity/Types.h | 2 +- test/SolidityEndToEndTest.cpp | 284 ++++++++++++++--------------- test/SolidityOptimizer.cpp | 26 +-- test/solidityExecutionFramework.h | 41 ++--- 8 files changed, 190 insertions(+), 202 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 514e1a27c..d49807bec 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor) { m_context << u256(argumentSize); m_context.appendProgramSize(); - m_context << u256(1); // copy it to byte one as expected for ABI calls + m_context << u256(g_functionIdentifierOffset); // copy it to byte four as expected for ABI calls m_context << eth::Instruction::CODECOPY; appendCalldataUnpacker(_constructor, true); } @@ -125,12 +125,12 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract.")); // retrieve the function signature hash from the calldata - m_context << u256(1) << u256(0) << u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296) * u256(4294967296)// some constants + m_context << u256(1) << u256(0) << (u256(1) << 224) // some constants << eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD << eth::Instruction::DIV; // stack now is: 1 0 - for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it) + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) { callDataUnpackerEntryPoints.push_back(m_context.newTag()); m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it->first)) << eth::Instruction::EQ; @@ -139,7 +139,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) m_context << eth::Instruction::STOP; // function not found unsigned funid = 0; - for (auto it = interfaceFunctions.begin(); it != interfaceFunctions.end(); ++it, ++funid) + for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++funid) { FunctionDefinition const& function = *it->second; m_context << callDataUnpackerEntryPoints[funid]; @@ -154,7 +154,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory) { // We do not check the calldata size, everything is zero-padded. - unsigned dataOffset = 1; + unsigned dataOffset = g_functionIdentifierOffset; // the 4 bytes of the function hash signature //@todo this can be done more efficiently, saving some CALLDATALOAD calls for (ASTPointer const& var: _function.getParameters()) { diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h index 928f0e2d3..bffd6f49d 100644 --- a/libsolidity/CompilerUtils.h +++ b/libsolidity/CompilerUtils.h @@ -30,6 +30,9 @@ namespace solidity { class Type; // forward +/// The size in bytes of the function (hash) identifier +static const unsigned int g_functionIdentifierOffset = 4; + class CompilerUtils { public: diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 6bf14f559..5fefd5282 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -335,7 +335,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) case Type::Category::CONTRACT: { ContractType const& type = dynamic_cast(*_memberAccess.getExpression().getType()); - m_context << type.getFunctionIndex(member); + m_context << type.getFunctionIdentifier(member); break; } case Type::Category::MAGIC: @@ -590,7 +590,11 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio { solAssert(_arguments.size() == _functionType.getParameterTypes().size(), ""); - unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index + _options.obtainAddress(); + if (!_options.bare) + CompilerUtils(m_context).storeInMemory(0, g_functionIdentifierOffset); + + unsigned dataOffset = _options.bare ? 0 : g_functionIdentifierOffset; // reserve 4 bytes for the function's hash identifier for (unsigned i = 0; i < _arguments.size(); ++i) { _arguments[i]->accept(*this); @@ -617,12 +621,13 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio _options.obtainValue(); else m_context << u256(0); - _options.obtainAddress(); - if (!_options.bare) - m_context << u256(0) << eth::Instruction::MSTORE8; + m_context << eth::dupInstruction(6); //copy contract address + m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB << eth::Instruction::CALL - << eth::Instruction::POP; // @todo do not ignore failure indicator + << eth::Instruction::POP // @todo do not ignore failure indicator + << eth::Instruction::POP; // pop contract address + if (retSize > 0) { bool const leftAligned = firstType->getCategory() == Type::Category::STRING; diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index 1a04fe616..e5bfa30f9 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -323,16 +323,13 @@ shared_ptr const& ContractType::getConstructorType() const return m_constructorType; } -unsigned ContractType::getFunctionIndex(string const& _functionName) const +u256 ContractType::getFunctionIdentifier(string const& _functionName) const { - unsigned index = 0; auto interfaceFunctions = m_contract.getInterfaceFunctions(); for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) - { if (it->second->getName() == _functionName) - return index; - ++index; - } + return FixedHash<4>::Arith(it->first); + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested.")); } diff --git a/libsolidity/Types.h b/libsolidity/Types.h index 0fe685b87..9d34e19a0 100644 --- a/libsolidity/Types.h +++ b/libsolidity/Types.h @@ -254,7 +254,7 @@ public: /// is not used, as this type cannot be the type of a variable or expression. std::shared_ptr const& getConstructorType() const; - unsigned getFunctionIndex(std::string const& _functionName) const; + u256 getFunctionIdentifier(std::string const& _functionName) const; private: ContractDefinition const& m_contract; diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index 9559e3702..e58d167f1 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(smoke_test) " function f(uint a) returns(uint d) { return a * 7; }\n" "}\n"; compileAndRun(sourceCode); - testSolidityAgainstCppOnRange(0, [](u256 const& a) -> u256 { return a * 7; }, 0, 100); + testSolidityAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return a * 7; }, 0, 100); } BOOST_AUTO_TEST_CASE(empty_contract) @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(empty_contract) char const* sourceCode = "contract test {\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes()).empty()); + BOOST_CHECK(callContractFunction("iam_not_there()", bytes()).empty()); } BOOST_AUTO_TEST_CASE(recursive_calls) @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(recursive_calls) return n * recursive_calls_cpp(n - 1); }; - testSolidityAgainstCppOnRange(0, recursive_calls_cpp, 0, 5); + testSolidityAgainstCppOnRange("f(uint256)", recursive_calls_cpp, 0, 5); } BOOST_AUTO_TEST_CASE(multiple_functions) @@ -84,11 +84,11 @@ BOOST_AUTO_TEST_CASE(multiple_functions) " function f() returns(uint n) { return 3; }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes()) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction(1, bytes()) == toBigEndian(u256(1))); - BOOST_CHECK(callContractFunction(2, bytes()) == toBigEndian(u256(2))); - BOOST_CHECK(callContractFunction(3, bytes()) == toBigEndian(u256(3))); - BOOST_CHECK(callContractFunction(4, bytes()) == bytes()); + BOOST_CHECK(callContractFunction("a()", bytes()) == toBigEndian(u256(0))); + BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(1))); + BOOST_CHECK(callContractFunction("c()", bytes()) == toBigEndian(u256(2))); + BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(3))); + BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()) == bytes()); } BOOST_AUTO_TEST_CASE(while_loop) @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE(while_loop) return nfac; }; - testSolidityAgainstCppOnRange(0, while_loop_cpp, 0, 5); + testSolidityAgainstCppOnRange("f(uint256)", while_loop_cpp, 0, 5); } BOOST_AUTO_TEST_CASE(break_outside_loop) @@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(break_outside_loop) " }\n" "}\n"; compileAndRun(sourceCode); - testSolidityAgainstCpp(0, [](u256 const&) -> u256 { return 2; }, u256(0)); + testSolidityAgainstCpp("f(uint256)", [](u256 const&) -> u256 { return 2; }, u256(0)); } BOOST_AUTO_TEST_CASE(nested_loops) @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(nested_loops) return n; }; - testSolidityAgainstCppOnRange(0, nested_loops_cpp, 0, 12); + testSolidityAgainstCppOnRange("f(uint256)", nested_loops_cpp, 0, 12); } BOOST_AUTO_TEST_CASE(for_loop) @@ -195,7 +195,7 @@ BOOST_AUTO_TEST_CASE(for_loop) return nfac; }; - testSolidityAgainstCppOnRange(0, for_loop_cpp, 0, 5); + testSolidityAgainstCppOnRange("f(uint256)", for_loop_cpp, 0, 5); } BOOST_AUTO_TEST_CASE(for_loop_empty) @@ -223,7 +223,7 @@ BOOST_AUTO_TEST_CASE(for_loop_empty) return ret; }; - testSolidityAgainstCpp(0, for_loop_empty_cpp); + testSolidityAgainstCpp("f()", for_loop_empty_cpp); } BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr) @@ -247,7 +247,7 @@ BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr) return nfac; }; - testSolidityAgainstCppOnRange(0, for_loop_simple_init_expr_cpp, 0, 5); + testSolidityAgainstCppOnRange("f(uint256)", for_loop_simple_init_expr_cpp, 0, 5); } BOOST_AUTO_TEST_CASE(calling_other_functions) @@ -292,11 +292,11 @@ BOOST_AUTO_TEST_CASE(calling_other_functions) return y; }; - testSolidityAgainstCpp(2, collatz_cpp, u256(0)); - testSolidityAgainstCpp(2, collatz_cpp, u256(1)); - testSolidityAgainstCpp(2, collatz_cpp, u256(2)); - testSolidityAgainstCpp(2, collatz_cpp, u256(8)); - testSolidityAgainstCpp(2, collatz_cpp, u256(127)); + testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(0)); + testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(1)); + testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(2)); + testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(8)); + testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(127)); } BOOST_AUTO_TEST_CASE(many_local_variables) @@ -317,7 +317,7 @@ BOOST_AUTO_TEST_CASE(many_local_variables) u256 y = a + b + c + x1 + x2 + x3; return y + b + x2; }; - testSolidityAgainstCpp(0, f, u256(0x1000), u256(0x10000), u256(0x100000)); + testSolidityAgainstCpp("run(uint256,uint256,uint256)", f, u256(0x1000), u256(0x10000), u256(0x100000)); } BOOST_AUTO_TEST_CASE(packing_unpacking_types) @@ -330,7 +330,7 @@ BOOST_AUTO_TEST_CASE(packing_unpacking_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0")) + BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0")) == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f")); } @@ -342,7 +342,7 @@ BOOST_AUTO_TEST_CASE(multiple_return_values) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes(1, 1) + toBigEndian(u256(0xcd))) + BOOST_CHECK(callContractFunction("run(bool,uint256)", bytes(1, 1) + toBigEndian(u256(0xcd))) == toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0))); } @@ -362,7 +362,7 @@ BOOST_AUTO_TEST_CASE(short_circuiting) return n; }; - testSolidityAgainstCppOnRange(0, short_circuiting_cpp, 0, 2); + testSolidityAgainstCppOnRange("run(uint256)", short_circuiting_cpp, 0, 2); } BOOST_AUTO_TEST_CASE(high_bits_cleaning) @@ -382,7 +382,7 @@ BOOST_AUTO_TEST_CASE(high_bits_cleaning) return 0; return x; }; - testSolidityAgainstCpp(0, high_bits_cleaning_cpp); + testSolidityAgainstCpp("run()", high_bits_cleaning_cpp); } BOOST_AUTO_TEST_CASE(sign_extension) @@ -402,7 +402,7 @@ BOOST_AUTO_TEST_CASE(sign_extension) return 0; return u256(x) * -1; }; - testSolidityAgainstCpp(0, sign_extension_cpp); + testSolidityAgainstCpp("run()", sign_extension_cpp); } BOOST_AUTO_TEST_CASE(small_unsigned_types) @@ -419,7 +419,7 @@ BOOST_AUTO_TEST_CASE(small_unsigned_types) uint32_t x = uint32_t(0xffffff) * 0xffffff; return x / 0x100; }; - testSolidityAgainstCpp(0, small_unsigned_types_cpp); + testSolidityAgainstCpp("run()", small_unsigned_types_cpp); } BOOST_AUTO_TEST_CASE(small_signed_types) @@ -434,7 +434,7 @@ BOOST_AUTO_TEST_CASE(small_signed_types) { return -int32_t(10) * -int64_t(20); }; - testSolidityAgainstCpp(0, small_signed_types_cpp); + testSolidityAgainstCpp("run()", small_signed_types_cpp); } BOOST_AUTO_TEST_CASE(strings) @@ -457,12 +457,12 @@ BOOST_AUTO_TEST_CASE(strings) expectation[4] = byte(0xff); expectation[5] = byte('_'); expectation[6] = byte('_'); - BOOST_CHECK(callContractFunction(0, bytes()) == expectation); + BOOST_CHECK(callContractFunction("fixed()", bytes()) == expectation); expectation = bytes(17, 0); expectation[0] = 0; expectation[1] = 2; expectation[16] = 1; - BOOST_CHECK(callContractFunction(1, bytes({0x00, 0x02, 0x01})) == expectation); + BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", bytes({0x00, 0x02, 0x01})) == expectation); } BOOST_AUTO_TEST_CASE(empty_string_on_stack) @@ -476,7 +476,7 @@ BOOST_AUTO_TEST_CASE(empty_string_on_stack) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00})); + BOOST_CHECK(callContractFunction("run(string0,uint8)", bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00})); } BOOST_AUTO_TEST_CASE(state_smoke_test) @@ -494,14 +494,14 @@ BOOST_AUTO_TEST_CASE(state_smoke_test) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes()); - BOOST_CHECK(callContractFunction(1, bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes()); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x1234))); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0x8765))); - BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes()); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x3))); + 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_AUTO_TEST_CASE(compound_assign) @@ -529,14 +529,14 @@ BOOST_AUTO_TEST_CASE(compound_assign) value2 *= value3 + value1; return value2 += 7; }; - testSolidityAgainstCpp(0, f, u256(0), u256(6)); - testSolidityAgainstCpp(0, f, u256(1), u256(3)); - testSolidityAgainstCpp(0, f, u256(2), u256(25)); - testSolidityAgainstCpp(0, f, u256(3), u256(69)); - testSolidityAgainstCpp(0, f, u256(4), u256(84)); - testSolidityAgainstCpp(0, f, u256(5), u256(2)); - testSolidityAgainstCpp(0, f, u256(6), u256(51)); - testSolidityAgainstCpp(0, f, u256(7), u256(48)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(0), u256(6)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(1), u256(3)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(2), u256(25)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(3), u256(69)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(4), u256(84)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(5), u256(2)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(6), u256(51)); + testSolidityAgainstCpp("f(uint256,uint256)", f, u256(7), u256(48)); } BOOST_AUTO_TEST_CASE(simple_mapping) @@ -553,21 +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(0, bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction(1, bytes({0x01, 0xa1})); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction(1, bytes({0x00, 0xef})); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction(1, bytes({0x01, 0x05})); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0x05)); - BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00)); + 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_AUTO_TEST_CASE(mapping_state) @@ -611,38 +611,38 @@ BOOST_AUTO_TEST_CASE(mapping_state) auto getVoteCount = bind(&Ballot::getVoteCount, &ballot, _1); auto grantVoteRight = bind(&Ballot::grantVoteRight, &ballot, _1); auto vote = bind(&Ballot::vote, &ballot, _1, _2); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); // voting without vote right shourd be rejected - testSolidityAgainstCpp(2, vote, u160(0), u160(2)); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); // grant vote rights - testSolidityAgainstCpp(1, grantVoteRight, u160(0)); - testSolidityAgainstCpp(1, grantVoteRight, u160(1)); + testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(0)); + testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(1)); // vote, should increase 2's vote count - testSolidityAgainstCpp(2, vote, u160(0), u160(2)); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); // vote again, should be rejected - testSolidityAgainstCpp(2, vote, u160(0), u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); // vote without right to vote - testSolidityAgainstCpp(2, vote, u160(2), u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); // grant vote right and now vote again - testSolidityAgainstCpp(1, grantVoteRight, u160(2)); - testSolidityAgainstCpp(2, vote, u160(2), u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(0)); - testSolidityAgainstCpp(0, getVoteCount, u160(1)); - testSolidityAgainstCpp(0, getVoteCount, u160(2)); + testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(2)); + testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1)); + testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2)); } BOOST_AUTO_TEST_CASE(mapping_state_inc_dec) @@ -673,7 +673,7 @@ BOOST_AUTO_TEST_CASE(mapping_state_inc_dec) table[value]++; return --table[value++]; }; - testSolidityAgainstCppOnRange(0, f, 0, 5); + testSolidityAgainstCppOnRange("f(uint256)", f, 0, 5); } BOOST_AUTO_TEST_CASE(multi_level_mapping) @@ -693,14 +693,14 @@ BOOST_AUTO_TEST_CASE(multi_level_mapping) if (_z == 0) return table[_x][_y]; else return table[_x][_y] = _z; }; - testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0)); - testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0)); - testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(9)); - testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0)); - testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0)); - testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(7)); - testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0)); - testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(9)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(7)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0)); + testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0)); } BOOST_AUTO_TEST_CASE(structs) @@ -735,9 +735,9 @@ BOOST_AUTO_TEST_CASE(structs) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction(1) == bytes()); - BOOST_CHECK(callContractFunction(0) == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("set()") == bytes()); + BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); } BOOST_AUTO_TEST_CASE(struct_reference) @@ -763,9 +763,9 @@ BOOST_AUTO_TEST_CASE(struct_reference) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction(1) == bytes()); - BOOST_CHECK(callContractFunction(0) == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("set()") == bytes()); + BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); } BOOST_AUTO_TEST_CASE(constructor) @@ -786,8 +786,8 @@ BOOST_AUTO_TEST_CASE(constructor) { return data[_x]; }; - testSolidityAgainstCpp(0, get, u256(6)); - testSolidityAgainstCpp(0, get, u256(7)); + testSolidityAgainstCpp("get(uint256)", get, u256(6)); + testSolidityAgainstCpp("get(uint256)", get, u256(7)); } BOOST_AUTO_TEST_CASE(balance) @@ -798,7 +798,7 @@ BOOST_AUTO_TEST_CASE(balance) " }\n" "}\n"; compileAndRun(sourceCode, 23); - BOOST_CHECK(callContractFunction(0) == toBigEndian(u256(23))); + BOOST_CHECK(callContractFunction("getBalance()") == toBigEndian(u256(23))); } BOOST_AUTO_TEST_CASE(blockchain) @@ -811,7 +811,7 @@ BOOST_AUTO_TEST_CASE(blockchain) " }\n" "}\n"; compileAndRun(sourceCode, 27); - BOOST_CHECK(callContractFunction(0, bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1))); + BOOST_CHECK(callContractFunction("someInfo()", bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1))); } BOOST_AUTO_TEST_CASE(function_types) @@ -830,8 +830,8 @@ BOOST_AUTO_TEST_CASE(function_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction(0, bytes{0}) == toBigEndian(u256(11))); - BOOST_CHECK(callContractFunction(0, bytes{1}) == toBigEndian(u256(12))); + BOOST_CHECK(callContractFunction("a(bool)", bytes{0}) == toBigEndian(u256(11))); + BOOST_CHECK(callContractFunction("a(bool)", bytes{1}) == toBigEndian(u256(12))); } BOOST_AUTO_TEST_CASE(send_ether) @@ -845,7 +845,7 @@ BOOST_AUTO_TEST_CASE(send_ether) u256 amount(130); compileAndRun(sourceCode, amount + 1); u160 address(23); - BOOST_CHECK(callContractFunction(0, address, amount) == toBigEndian(u256(1))); + BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == toBigEndian(u256(1))); BOOST_CHECK_EQUAL(m_state.balance(address), amount); } @@ -860,7 +860,7 @@ BOOST_AUTO_TEST_CASE(suicide) u256 amount(130); compileAndRun(sourceCode, amount); u160 address(23); - BOOST_CHECK(callContractFunction(0, address) == bytes()); + BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); BOOST_CHECK_EQUAL(m_state.balance(address), amount); } @@ -877,9 +877,9 @@ BOOST_AUTO_TEST_CASE(sha3) { return dev::sha3(toBigEndian(_x)); }; - testSolidityAgainstCpp(0, f, u256(4)); - testSolidityAgainstCpp(0, f, u256(5)); - testSolidityAgainstCpp(0, f, u256(-1)); + testSolidityAgainstCpp("a(hash256)", f, u256(4)); + testSolidityAgainstCpp("a(hash256)", f, u256(5)); + testSolidityAgainstCpp("a(hash256)", f, u256(-1)); } BOOST_AUTO_TEST_CASE(sha256) @@ -896,9 +896,9 @@ BOOST_AUTO_TEST_CASE(sha256) dev::sha256(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32)); return ret; }; - testSolidityAgainstCpp(0, f, u256(4)); - testSolidityAgainstCpp(0, f, u256(5)); - testSolidityAgainstCpp(0, f, u256(-1)); + testSolidityAgainstCpp("a(hash256)", f, u256(4)); + testSolidityAgainstCpp("a(hash256)", f, u256(5)); + testSolidityAgainstCpp("a(hash256)", f, u256(-1)); } BOOST_AUTO_TEST_CASE(ripemd) @@ -915,9 +915,9 @@ BOOST_AUTO_TEST_CASE(ripemd) dev::ripemd160(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32)); return u256(ret) >> (256 - 160); }; - testSolidityAgainstCpp(0, f, u256(4)); - testSolidityAgainstCpp(0, f, u256(5)); - testSolidityAgainstCpp(0, f, u256(-1)); + testSolidityAgainstCpp("a(hash256)", f, u256(4)); + testSolidityAgainstCpp("a(hash256)", f, u256(5)); + testSolidityAgainstCpp("a(hash256)", f, u256(-1)); } BOOST_AUTO_TEST_CASE(ecrecover) @@ -933,7 +933,7 @@ BOOST_AUTO_TEST_CASE(ecrecover) u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f"); u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549"); u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"); - BOOST_CHECK(callContractFunction(0, h, v, r, s) == toBigEndian(addr)); + BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == toBigEndian(addr)); } BOOST_AUTO_TEST_CASE(inter_contract_calls) @@ -959,11 +959,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) @@ -989,12 +989,12 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction(0, a, true, b) == toBigEndian(a * 3)); - BOOST_REQUIRE(callContractFunction(0, a, false, b) == toBigEndian(b * 3)); + 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_AUTO_TEST_CASE(inter_contract_calls_accessing_this) @@ -1020,9 +1020,9 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); - BOOST_REQUIRE(callContractFunction(0) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("callHelper()") == toBigEndian(helperAddress)); } BOOST_AUTO_TEST_CASE(calls_to_this) @@ -1051,11 +1051,11 @@ BOOST_AUTO_TEST_CASE(calls_to_this) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 10)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 10)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) @@ -1086,11 +1086,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 9)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 9)); } BOOST_AUTO_TEST_CASE(strings_in_calls) @@ -1116,9 +1116,9 @@ BOOST_AUTO_TEST_CASE(strings_in_calls) compileAndRun(sourceCode, 0, "Helper"); u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); - BOOST_CHECK(callContractFunction(0, bytes({0, 'a', 1})) == bytes({0, 'a', 0, 0, 0})); + 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_AUTO_TEST_CASE(constructor_arguments) @@ -1143,8 +1143,8 @@ BOOST_AUTO_TEST_CASE(constructor_arguments) function getName() returns (string3 ret) { return h.getName(); } })"; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction(0) == bytes({byte(0x01)})); - BOOST_REQUIRE(callContractFunction(1) == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getFlag()") == bytes({byte(0x01)})); + BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); } BOOST_AUTO_TEST_CASE(functions_called_by_constructor) @@ -1161,7 +1161,7 @@ BOOST_AUTO_TEST_CASE(functions_called_by_constructor) function setName(string3 _name) { name = _name; } })"; compileAndRun(sourceCode); - BOOST_REQUIRE(callContractFunction(0) == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/SolidityOptimizer.cpp b/test/SolidityOptimizer.cpp index ef5c6f9b5..f334eee18 100644 --- a/test/SolidityOptimizer.cpp +++ b/test/SolidityOptimizer.cpp @@ -55,12 +55,12 @@ public: } template - void compareVersions(byte _index, Args const&... _arguments) + void compareVersions(std::string _sig, Args const&... _arguments) { m_contractAddress = m_nonOptimizedContract; - bytes nonOptimizedOutput = callContractFunction(_index, _arguments...); + bytes nonOptimizedOutput = callContractFunction(_sig, _arguments...); m_contractAddress = m_optimizedContract; - bytes optimizedOutput = callContractFunction(_index, _arguments...); + bytes optimizedOutput = callContractFunction(_sig, _arguments...); BOOST_CHECK_MESSAGE(nonOptimizedOutput == optimizedOutput, "Computed values do not match." "\nNon-Optimized: " + toHex(nonOptimizedOutput) + "\nOptimized: " + toHex(optimizedOutput)); @@ -81,8 +81,8 @@ BOOST_AUTO_TEST_CASE(smoke_test) return a; } })"; - compileBothVersions(4, sourceCode); - compareVersions(0, u256(7)); + compileBothVersions(29, sourceCode); + compareVersions("f(uint256)", u256(7)); } BOOST_AUTO_TEST_CASE(large_integers) @@ -94,8 +94,8 @@ BOOST_AUTO_TEST_CASE(large_integers) b = 0x110000000000000000000000002; } })"; - compileBothVersions(28, sourceCode); - compareVersions(0); + compileBothVersions(53, sourceCode); + compareVersions("f()"); } BOOST_AUTO_TEST_CASE(invariants) @@ -106,8 +106,8 @@ BOOST_AUTO_TEST_CASE(invariants) return (((a + (1 - 1)) ^ 0) | 0) & (uint(0) - 1); } })"; - compileBothVersions(28, sourceCode); - compareVersions(0, u256(0x12334664)); + compileBothVersions(53, sourceCode); + compareVersions("f(uint256)", u256(0x12334664)); } BOOST_AUTO_TEST_CASE(unused_expressions) @@ -120,8 +120,8 @@ BOOST_AUTO_TEST_CASE(unused_expressions) data; } })"; - compileBothVersions(11, sourceCode); - compareVersions(0); + compileBothVersions(36, sourceCode); + compareVersions("f()"); } BOOST_AUTO_TEST_CASE(constant_folding_both_sides) @@ -135,8 +135,8 @@ BOOST_AUTO_TEST_CASE(constant_folding_both_sides) return 98 ^ (7 * ((1 | (x | 1000)) * 40) ^ 102); } })"; - compileBothVersions(31, sourceCode); - compareVersions(0); + compileBothVersions(56, sourceCode); + compareVersions("f(uint256)"); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/solidityExecutionFramework.h b/test/solidityExecutionFramework.h index 9beabdc54..07c804cf6 100644 --- a/test/solidityExecutionFramework.h +++ b/test/solidityExecutionFramework.h @@ -29,7 +29,6 @@ #include #include #include -#include namespace dev { @@ -49,57 +48,44 @@ public: bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") { - /* dev::solidity::CompilerStack compiler; */ - m_compiler.compile(_sourceCode, m_optimize); - bytes code = m_compiler.getBytecode(_contractName); - /* m_contractDefinition = compiler.getContractDefinition(_contractName); */ + dev::solidity::CompilerStack compiler; + compiler.compile(_sourceCode, m_optimize); + bytes code = compiler.getBytecode(_contractName); sendMessage(code, true, _value); BOOST_REQUIRE(!m_output.empty()); return m_output; } - bytes const& callContractFunction(byte _index, bytes const& _data = bytes(), + bytes const& callContractFunction(std::string _sig, bytes const& _data = bytes(), u256 const& _value = 0) { - /* if (!_contractDef) */ - /* _contractDef = m_contractDefinition; */ - - unsigned index = 0; - auto interfaceFunctions = m_compiler.getContractDefinition("").getInterfaceFunctions(); - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++index) - if (index == _index) - { - sendMessage(it->first.asBytes() + _data, false, _value); - /* sendMessage(bytes(1, _index) + _data, false, _value); */ - return m_output; - } - - BOOST_FAIL("Function with index " << _index << "not found"); + FixedHash<4> hash(dev::sha3(_sig)); + sendMessage(hash.asBytes() + _data, false, _value); return m_output; } template - bytes const& callContractFunction(byte _index, Args const&... _arguments) + bytes const& callContractFunction(std::string _sig, Args const&... _arguments) { - return callContractFunction(_index, argsToBigEndian(_arguments...)); + return callContractFunction(_sig, argsToBigEndian(_arguments...)); } template - void testSolidityAgainstCpp(byte _index, CppFunction const& _cppFunction, Args const&... _arguments) + void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments) { - bytes solidityResult = callContractFunction(_index, _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(byte _index, CppFunction const& _cppFunction, + void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction, u256 const& _rangeStart, u256 const& _rangeEnd) { for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument) { - bytes solidityResult = callContractFunction(_index, 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) + @@ -165,9 +151,6 @@ protected: bool m_optimize = false; Address m_sender; Address m_contractAddress; - /* ContractDefinition m_contractDefinition; */ - dev::solidity::CompilerStack m_compiler; - eth::State m_state; u256 const m_gasPrice = 100 * eth::szabo; u256 const m_gas = 1000000; From 4331bd9a87fc524d960509818acde20c70940a07 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Thu, 8 Jan 2015 17:51:31 +0100 Subject: [PATCH 06/10] adjusting byte difference in optimizer large integers test --- test/SolidityOptimizer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SolidityOptimizer.cpp b/test/SolidityOptimizer.cpp index 23b122b82..972332502 100644 --- a/test/SolidityOptimizer.cpp +++ b/test/SolidityOptimizer.cpp @@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE(large_integers) b = 0x10000000000000000000000002; } })"; - compileBothVersions(53, sourceCode); + compileBothVersions(58, sourceCode); compareVersions("f()"); } From 299305548f3ee0404b28f37fbdec6edadf07f0dd Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 9 Jan 2015 00:27:26 +0100 Subject: [PATCH 07/10] Adjustments to Solidity compiler code for Function Hash --- libsolidity/AST.cpp | 4 +++- libsolidity/Compiler.cpp | 30 +++++++++++++----------------- libsolidity/CompilerUtils.cpp | 2 ++ libsolidity/CompilerUtils.h | 7 ++++--- libsolidity/ExpressionCompiler.cpp | 4 ++-- libsolidity/InterfaceHandler.cpp | 25 +++++++++++-------------- libsolidity/Types.cpp | 5 ++--- test/SolidityEndToEndTest.cpp | 2 +- 8 files changed, 38 insertions(+), 41 deletions(-) diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index fb71e9003..fd4cc57c8 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -59,7 +59,9 @@ map, FunctionDefinition const*> ContractDefinition::getInterfaceFun if (f->isPublic() && f->getName() != getName()) { FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); - exportedFunctions[hash] = f.get(); + auto res = exportedFunctions.insert(std::make_pair(hash,f.get())); + if (!res.second) + solAssert(false, "Hash collision at Function Definition Hash calculation"); } return exportedFunctions; diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index d49807bec..4e5b7f558 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor) { m_context << u256(argumentSize); m_context.appendProgramSize(); - m_context << u256(g_functionIdentifierOffset); // copy it to byte four as expected for ABI calls + m_context << u256(CompilerUtils::dataStartOffset); // copy it to byte four as expected for ABI calls m_context << eth::Instruction::CODECOPY; appendCalldataUnpacker(_constructor, true); } @@ -119,30 +119,26 @@ set Compiler::getFunctionsNeededByConstructor(Functio void Compiler::appendFunctionSelector(ContractDefinition const& _contract) { map, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions(); - vector callDataUnpackerEntryPoints; - - if (interfaceFunctions.size() > 4294967295) // 2 ** 32 - BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 4294967295 public functions for contract.")); + map, const eth::AssemblyItem> callDataUnpackerEntryPoints; // retrieve the function signature hash from the calldata - m_context << u256(1) << u256(0) << (u256(1) << 224) // some constants - << eth::dupInstruction(2) << eth::Instruction::CALLDATALOAD - << eth::Instruction::DIV; + m_context << u256(1) << u256(0); + CompilerUtils(m_context).loadFromMemory(0, 4, false, true); // stack now is: 1 0 - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + // for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + for (auto const& it: interfaceFunctions) { - callDataUnpackerEntryPoints.push_back(m_context.newTag()); - m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it->first)) << eth::Instruction::EQ; - m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back()); + callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag())); + m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ; + m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first)); } m_context << eth::Instruction::STOP; // function not found - unsigned funid = 0; - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++funid) + for (auto const& it: interfaceFunctions) { - FunctionDefinition const& function = *it->second; - m_context << callDataUnpackerEntryPoints[funid]; + FunctionDefinition const& function = *it.second; + m_context << callDataUnpackerEntryPoints.at(it.first); eth::AssemblyItem returnTag = m_context.pushNewTag(); appendCalldataUnpacker(function); m_context.appendJumpTo(m_context.getFunctionEntryLabel(function)); @@ -154,7 +150,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract) unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory) { // We do not check the calldata size, everything is zero-padded. - unsigned dataOffset = g_functionIdentifierOffset; // the 4 bytes of the function hash signature + unsigned dataOffset = CompilerUtils::dataStartOffset; // the 4 bytes of the function hash signature //@todo this can be done more efficiently, saving some CALLDATALOAD calls for (ASTPointer const& var: _function.getParameters()) { diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp index 680e9190b..a5254b421 100644 --- a/libsolidity/CompilerUtils.cpp +++ b/libsolidity/CompilerUtils.cpp @@ -31,6 +31,8 @@ namespace dev namespace solidity { +const unsigned int CompilerUtils::dataStartOffset = 4; + void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata) { if (_bytes == 0) diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h index bffd6f49d..6bd8d3155 100644 --- a/libsolidity/CompilerUtils.h +++ b/libsolidity/CompilerUtils.h @@ -30,9 +30,6 @@ namespace solidity { class Type; // forward -/// The size in bytes of the function (hash) identifier -static const unsigned int g_functionIdentifierOffset = 4; - class CompilerUtils { public: @@ -61,10 +58,14 @@ public: static unsigned getSizeOnStack(std::vector const& _variables); static unsigned getSizeOnStack(std::vector> const& _variableTypes); + /// Bytes we need to the start of call data. + /// - The size in bytes of the function (hash) identifier. + static const unsigned int dataStartOffset; private: CompilerContext& m_context; }; + template unsigned CompilerUtils::getSizeOnStack(std::vector const& _variables) { diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 1b10c854a..44a16d15c 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -592,9 +592,9 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio _options.obtainAddress(); if (!_options.bare) - CompilerUtils(m_context).storeInMemory(0, g_functionIdentifierOffset); + CompilerUtils(m_context).storeInMemory(0, CompilerUtils::dataStartOffset); - unsigned dataOffset = _options.bare ? 0 : g_functionIdentifierOffset; // reserve 4 bytes for the function's hash identifier + unsigned dataOffset = _options.bare ? 0 : CompilerUtils::dataStartOffset; // reserve 4 bytes for the function's hash identifier for (unsigned i = 0; i < _arguments.size(); ++i) { _arguments[i]->accept(*this); diff --git a/libsolidity/InterfaceHandler.cpp b/libsolidity/InterfaceHandler.cpp index 0843c3637..453951271 100644 --- a/libsolidity/InterfaceHandler.cpp +++ b/libsolidity/InterfaceHandler.cpp @@ -35,9 +35,8 @@ std::unique_ptr InterfaceHandler::getDocumentation(ContractDefiniti std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinition const& _contractDef) { Json::Value methods(Json::arrayValue); - auto interfaceFunctions = _contractDef.getInterfaceFunctions(); - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value method; Json::Value inputs(Json::arrayValue); @@ -56,10 +55,10 @@ std::unique_ptr InterfaceHandler::getABIInterface(ContractDefinitio return params; }; - method["name"] = it->second->getName(); - method["constant"] = it->second->isDeclaredConst(); - method["inputs"] = populateParameters(it->second->getParameters()); - method["outputs"] = populateParameters(it->second->getReturnParameters()); + method["name"] = it.second->getName(); + method["constant"] = it.second->isDeclaredConst(); + method["inputs"] = populateParameters(it.second->getParameters()); + method["outputs"] = populateParameters(it.second->getReturnParameters()); methods.append(method); } return std::unique_ptr(new std::string(m_writer.write(methods))); @@ -69,12 +68,11 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi { Json::Value doc; Json::Value methods(Json::objectValue); - auto interfaceFunctions = _contractDef.getInterfaceFunctions(); - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value user; - auto strPtr = it->second->getDocumentation(); + auto strPtr = it.second->getDocumentation(); if (strPtr) { resetUser(); @@ -82,7 +80,7 @@ std::unique_ptr InterfaceHandler::getUserDocumentation(ContractDefi if (!m_notice.empty()) {// since @notice is the only user tag if missing function should not appear user["notice"] = Json::Value(m_notice); - methods[it->second->getName()] = user; + methods[it.second->getName()] = user; } } } @@ -112,11 +110,10 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin doc["title"] = m_title; } - auto interfaceFunctions = _contractDef.getInterfaceFunctions(); - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) + for (auto const& it: _contractDef.getInterfaceFunctions()) { Json::Value method; - auto strPtr = it->second->getDocumentation(); + auto strPtr = it.second->getDocumentation(); if (strPtr) { resetDev(); @@ -139,7 +136,7 @@ std::unique_ptr InterfaceHandler::getDevDocumentation(ContractDefin method["return"] = m_return; if (!method.empty()) // add the function, only if we have any documentation to add - methods[it->second->getName()] = method; + methods[it.second->getName()] = method; } } doc["methods"] = methods; diff --git a/libsolidity/Types.cpp b/libsolidity/Types.cpp index 494bbd264..01fa77341 100644 --- a/libsolidity/Types.cpp +++ b/libsolidity/Types.cpp @@ -333,10 +333,9 @@ MemberList const& ContractType::getMembers() const // We need to lazy-initialize it because of recursive references. if (!m_members) { - auto interfaceFunctions = m_contract.getInterfaceFunctions(); map> members; - for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it) - members[it->second->getName()] = make_shared(*it->second, false); + for (auto const& it: m_contract.getInterfaceFunctions()) + members[it.second->getName()] = make_shared(*it.second, false); m_members.reset(new MemberList(members)); } return *m_members; diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index e58d167f1..ee233111a 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(empty_contract) char const* sourceCode = "contract test {\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("iam_not_there()", bytes()).empty()); + BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()).empty()); } BOOST_AUTO_TEST_CASE(recursive_calls) From 5e77063c38366141a7fa617e94969c9ed6047e23 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 9 Jan 2015 00:58:12 +0100 Subject: [PATCH 08/10] Fixing a solAssert in getInterfacefunctions() --- libsolidity/AST.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index fd4cc57c8..1247f325a 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -60,8 +60,7 @@ map, FunctionDefinition const*> ContractDefinition::getInterfaceFun { FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); auto res = exportedFunctions.insert(std::make_pair(hash,f.get())); - if (!res.second) - solAssert(false, "Hash collision at Function Definition Hash calculation"); + solAssert(res.second, "Hash collision at Function Definition Hash calculation"); } return exportedFunctions; From e94f1ca5b29609cb0766d968cb88ee61a80016e2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 9 Jan 2015 01:25:14 +0100 Subject: [PATCH 09/10] Adjusting Solidity Optimizer Tests --- test/SolidityCompiler.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/SolidityCompiler.cpp b/test/SolidityCompiler.cpp index ea09678d9..1a9685f02 100644 --- a/test/SolidityCompiler.cpp +++ b/test/SolidityCompiler.cpp @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(smoke_test) "}\n"; bytes code = compileContract(sourceCode); - unsigned boilerplateSize = 72; + unsigned boilerplateSize = 73; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, // initialize local variable x byte(Instruction::PUSH1), 0x2, @@ -115,8 +115,8 @@ BOOST_AUTO_TEST_CASE(different_argument_numbers) " function g() returns (uint e, uint h) { h = f(1, 2, 3); }\n" "}\n"; bytes code = compileContract(sourceCode); - unsigned shift = 102; - unsigned boilerplateSize = 115; + unsigned shift = 103; + unsigned boilerplateSize = 116; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, // initialize return variable d byte(Instruction::DUP3), @@ -165,8 +165,8 @@ BOOST_AUTO_TEST_CASE(ifStatement) " function f() { bool x; if (x) 77; else if (!x) 78; else 79; }" "}\n"; bytes code = compileContract(sourceCode); - unsigned shift = 59; - unsigned boilerplateSize = 72; + unsigned shift = 60; + unsigned boilerplateSize = 73; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x0, byte(Instruction::DUP1), @@ -206,8 +206,8 @@ BOOST_AUTO_TEST_CASE(loops) " function f() { while(true){1;break;2;continue;3;return;4;} }" "}\n"; bytes code = compileContract(sourceCode); - unsigned shift = 59; - unsigned boilerplateSize = 72; + unsigned shift = 60; + unsigned boilerplateSize = 73; bytes expectation({byte(Instruction::JUMPDEST), byte(Instruction::JUMPDEST), byte(Instruction::PUSH1), 0x1, From 03b2fa387d2f4447a44c5e5831f650964be3c67f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Fri, 9 Jan 2015 09:29:19 +0100 Subject: [PATCH 10/10] Fixing SolidityABIJSON test --- test/SolidityABIJSON.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/SolidityABIJSON.cpp b/test/SolidityABIJSON.cpp index 714aa0f30..5ec7ce139 100644 --- a/test/SolidityABIJSON.cpp +++ b/test/SolidityABIJSON.cpp @@ -236,20 +236,6 @@ BOOST_AUTO_TEST_CASE(const_function) "}\n"; char const* interface = R"([ - { - "name": "boo", - "constant": true, - "inputs": [{ - "name": "a", - "type": "uint32" - }], - "outputs": [ - { - "name": "b", - "type": "uint256" - } - ] - }, { "name": "foo", "constant": false, @@ -269,6 +255,20 @@ BOOST_AUTO_TEST_CASE(const_function) "type": "uint256" } ] + }, + { + "name": "boo", + "constant": true, + "inputs": [{ + "name": "a", + "type": "uint32" + }], + "outputs": [ + { + "name": "b", + "type": "uint256" + } + ] } ])";