From de77918d05a6d4951e0a51bae867455c638f2332 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 9 Jan 2015 00:58:32 +0100 Subject: [PATCH] Padding for ABI types. --- libsolidity/Compiler.cpp | 13 +-- libsolidity/CompilerUtils.cpp | 19 ++-- libsolidity/CompilerUtils.h | 15 +++- libsolidity/ExpressionCompiler.cpp | 27 +++--- libsolidity/ExpressionCompiler.h | 6 +- libsolidity/Types.h | 3 +- test/SolidityEndToEndTest.cpp | 135 ++++++++++++++--------------- test/solidityExecutionFramework.h | 41 ++++++--- 8 files changed, 143 insertions(+), 116 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 4e5b7f558..c7d063eab 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -95,7 +95,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor) // copy constructor arguments from code to memory and then to stack, they are supplied after the actual program unsigned argumentSize = 0; for (ASTPointer const& var: _constructor.getParameters()) - argumentSize += var->getType()->getCalldataEncodedSize(); + argumentSize += CompilerUtils::getPaddedSize(var->getType()->getCalldataEncodedSize()); if (argumentSize > 0) { m_context << u256(argumentSize); @@ -159,9 +159,9 @@ unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, b BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(var->getLocation()) << errinfo_comment("Type " + var->getType()->toString() + " not yet supported.")); - bool leftAligned = var->getType()->getCategory() == Type::Category::STRING; - CompilerUtils(m_context).loadFromMemory(dataOffset, numBytes, leftAligned, !_fromMemory); - dataOffset += numBytes; + bool const leftAligned = var->getType()->getCategory() == Type::Category::STRING; + bool const padToWords = true; + dataOffset += CompilerUtils(m_context).loadFromMemory(dataOffset, numBytes, leftAligned, !_fromMemory, padToWords); } return dataOffset; } @@ -181,10 +181,11 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function) << errinfo_sourceLocation(parameters[i]->getLocation()) << errinfo_comment("Type " + paramType.toString() + " not yet supported.")); CompilerUtils(m_context).copyToStackTop(stackDepth, paramType); + ExpressionCompiler::appendTypeConversion(m_context, paramType, paramType, true); bool const leftAligned = paramType.getCategory() == Type::Category::STRING; - CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); + bool const padToWords = true; + dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); stackDepth -= paramType.getSizeOnStack(); - dataOffset += numBytes; } // note that the stack is not cleaned up here m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN; diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp index a5254b421..3101c1b44 100644 --- a/libsolidity/CompilerUtils.cpp +++ b/libsolidity/CompilerUtils.cpp @@ -33,17 +33,21 @@ namespace solidity const unsigned int CompilerUtils::dataStartOffset = 4; -void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata) +unsigned CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, + bool _fromCalldata, bool _padToWordBoundaries) { if (_bytes == 0) { m_context << u256(0); - return; + return 0; } eth::Instruction load = _fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD; solAssert(_bytes <= 32, "Memory load of more than 32 bytes requested."); - if (_bytes == 32) + if (_bytes == 32 || _padToWordBoundaries) + { m_context << u256(_offset) << load; + return 32; + } else { // load data and add leading or trailing zeros by dividing/multiplying depending on alignment @@ -54,21 +58,24 @@ void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _left m_context << u256(_offset) << load << eth::Instruction::DIV; if (_leftAligned) m_context << eth::Instruction::MUL; + return _bytes; } } -void CompilerUtils::storeInMemory(unsigned _offset, unsigned _bytes, bool _leftAligned) +unsigned CompilerUtils::storeInMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, + bool _padToWordBoundaries) { if (_bytes == 0) { m_context << eth::Instruction::POP; - return; + return 0; } solAssert(_bytes <= 32, "Memory store of more than 32 bytes requested."); - if (_bytes != 32 && !_leftAligned) + if (_bytes != 32 && !_leftAligned && !_padToWordBoundaries) // shift the value accordingly before storing m_context << (u256(1) << ((32 - _bytes) * 8)) << eth::Instruction::MUL; m_context << u256(_offset) << eth::Instruction::MSTORE; + return _padToWordBoundaries ? 32 : _bytes; } void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h index 6bd8d3155..b5a695282 100644 --- a/libsolidity/CompilerUtils.h +++ b/libsolidity/CompilerUtils.h @@ -40,12 +40,23 @@ public: /// @param _bytes number of bytes to load /// @param _leftAligned if true, store left aligned on stack (otherwise right aligned) /// @param _fromCalldata if true, load from calldata, not from memory - void loadFromMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false, bool _fromCalldata = false); + /// @param _padToWordBoundaries if true, assume the data is padded to word (32 byte) boundaries + /// @returns the number of bytes consumed in memory (can be different from _bytes if + /// _padToWordBoundaries is true) + unsigned loadFromMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false, + bool _fromCalldata = false, bool _padToWordBoundaries = false); /// Stores data from stack in memory. /// @param _offset offset in memory /// @param _bytes number of bytes to store /// @param _leftAligned if true, data is left aligned on stack (otherwise right aligned) - void storeInMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false); + /// @param _padToWordBoundaries if true, pad the data to word (32 byte) boundaries + /// @returns the number of bytes written to memory (can be different from _bytes if + /// _padToWordBoundaries is true) + unsigned storeInMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false, + bool _padToWordBoundaries = false); + /// @returns _size rounded up to the next multiple of 32 (the number of bytes occupied in the + /// padded calldata) + static unsigned getPaddedSize(unsigned _size) { return ((_size + 31) / 32) * 32; } /// Moves the value that is at the top of the stack to a stack variable. void moveToStackVariable(VariableDeclaration const& _variable); diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 5667098db..b12b8e675 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -41,11 +41,11 @@ void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression _expression.accept(compiler); } -void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, - Type const& _typeOnStack, Type const& _targetType) +void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, + Type const& _targetType, bool _cleanupNeeded) { ExpressionCompiler compiler(_context); - compiler.appendTypeConversion(_typeOnStack, _targetType); + compiler.appendTypeConversion(_typeOnStack, _targetType, _cleanupNeeded); } bool ExpressionCompiler::visit(Assignment const& _assignment) @@ -295,7 +295,6 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) FunctionCallOptions options; options.bare = true; options.obtainAddress = [&]() { m_context << contractAddress; }; - options.packDensely = false; appendExternalFunctionCall(function, arguments, options); break; } @@ -327,15 +326,15 @@ bool ExpressionCompiler::visit(NewExpression const& _newExpression) for (unsigned i = 0; i < arguments.size(); ++i) { arguments[i]->accept(*this); - appendTypeConversion(*arguments[i]->getType(), *types[i]); + appendTypeConversion(*arguments[i]->getType(), *types[i], true); unsigned const numBytes = types[i]->getCalldataEncodedSize(); if (numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(arguments[i]->getLocation()) << errinfo_comment("Type " + types[i]->toString() + " not yet supported.")); bool const leftAligned = types[i]->getCategory() == Type::Category::STRING; - CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); - dataOffset += numBytes; + bool const padToWords = true; + dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); } // size, offset, endowment m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE; @@ -634,22 +633,20 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio { _arguments[i]->accept(*this); Type const& type = *_functionType.getParameterTypes()[i]; - appendTypeConversion(*_arguments[i]->getType(), type); - unsigned const numBytes = _options.packDensely ? type.getCalldataEncodedSize() : 32; + appendTypeConversion(*_arguments[i]->getType(), type, true); + unsigned const numBytes = type.getCalldataEncodedSize(); if (numBytes == 0 || numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_arguments[i]->getLocation()) << errinfo_comment("Type " + type.toString() + " not yet supported.")); bool const leftAligned = type.getCategory() == Type::Category::STRING; - CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); - dataOffset += numBytes; + bool const padToWords = true; + dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned, padToWords); } //@todo only return the first return value for now Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr : _functionType.getReturnParameterTypes().front().get(); - unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0; - if (!_options.packDensely && retSize > 0) - retSize = 32; + unsigned retSize = firstType ? CompilerUtils::getPaddedSize(firstType->getCalldataEncodedSize()) : 0; // CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top) m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0); if (_options.obtainValue) @@ -666,7 +663,7 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio if (retSize > 0) { bool const leftAligned = firstType->getCategory() == Type::Category::STRING; - CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned); + CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned, false, true); } } diff --git a/libsolidity/ExpressionCompiler.h b/libsolidity/ExpressionCompiler.h index abd7f7750..98f58c854 100644 --- a/libsolidity/ExpressionCompiler.h +++ b/libsolidity/ExpressionCompiler.h @@ -51,7 +51,8 @@ public: static void compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize = false); /// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type. - static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, Type const& _targetType); + static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, + Type const& _targetType, bool _cleanupNeeded = false); private: explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false): @@ -96,9 +97,6 @@ private: std::function obtainValue; /// If true, do not prepend function index to call data bool bare = false; - /// If false, use calling convention that all arguments and return values are packed as - /// 32 byte values with padding. - bool packDensely = true; }; /// Appends code to call a function of the given type with the given arguments. diff --git a/libsolidity/Types.h b/libsolidity/Types.h index 8c800fa0d..69f69ef61 100644 --- a/libsolidity/Types.h +++ b/libsolidity/Types.h @@ -115,7 +115,8 @@ public: virtual bool operator!=(Type const& _other) const { return !this->operator ==(_other); } /// @returns number of bytes used by this type when encoded for CALL, or 0 if the encoding - /// is not a simple big-endian encoding or the type cannot be stored on the stack. + /// is not a simple big-endian encoding or the type cannot be stored in calldata. + /// Note that irrespective of this size, each calldata element is padded to a multiple of 32 bytes. virtual unsigned getCalldataEncodedSize() const { return 0; } /// @returns number of bytes required to hold this value in storage. /// For dynamically "allocated" types, it returns the size of the statically allocated head, diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index bc184dfca..7ed7ad594 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -331,10 +331,23 @@ BOOST_AUTO_TEST_CASE(packing_unpacking_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0")) + BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", true, fromHex("0f0f0f0f"), fromHex("f0f0f0f0f0f0f0f0")) == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f")); } +BOOST_AUTO_TEST_CASE(packing_signed_types) +{ + char const* sourceCode = "contract test {\n" + " function run() returns(int8 y) {\n" + " uint8 x = 0xfa;\n" + " return int8(x);\n" + " }\n" + "}\n"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("run()") + == fromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa")); +} + BOOST_AUTO_TEST_CASE(multiple_return_values) { char const* sourceCode = "contract test {\n" @@ -343,8 +356,7 @@ BOOST_AUTO_TEST_CASE(multiple_return_values) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(bool,uint256)", bytes(1, 1) + toBigEndian(u256(0xcd))) - == toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0))); + BOOST_CHECK(callContractFunction("run(bool,uint256)", true, 0xcd) == encodeArgs(0xcd, true, 0)); } BOOST_AUTO_TEST_CASE(short_circuiting) @@ -450,20 +462,8 @@ BOOST_AUTO_TEST_CASE(strings) " }\n" "}\n"; compileAndRun(sourceCode); - bytes expectation(32, 0); - expectation[0] = byte('a'); - expectation[1] = byte('b'); - expectation[2] = byte('c'); - expectation[3] = byte(0); - expectation[4] = byte(0xff); - expectation[5] = byte('_'); - expectation[6] = byte('_'); - BOOST_CHECK(callContractFunction("fixed()", bytes()) == expectation); - expectation = bytes(17, 0); - expectation[0] = 0; - expectation[1] = 2; - expectation[16] = 1; - BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", bytes({0x00, 0x02, 0x01})) == expectation); + BOOST_CHECK(callContractFunction("fixed()") == encodeArgs(string("abc\0\xff__", 7))); + BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true)); } BOOST_AUTO_TEST_CASE(empty_string_on_stack) @@ -477,7 +477,7 @@ BOOST_AUTO_TEST_CASE(empty_string_on_stack) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("run(string0,uint8)", bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00})); + BOOST_CHECK(callContractFunction("run(string0,uint8)", string(), byte(0x02)) == encodeArgs(0x2, string(""), string("abc\0"))); } BOOST_AUTO_TEST_CASE(state_smoke_test) @@ -495,14 +495,14 @@ BOOST_AUTO_TEST_CASE(state_smoke_test) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0))); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes()); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes()); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x1234))); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0x8765))); - BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes()); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x3))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0)); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x1234) == encodeArgs()); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x01), 0x8765) == encodeArgs()); + BOOST_CHECK(callContractFunction("get(uint8)", byte( 0x00)) == encodeArgs(0x1234)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0x8765)); + BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x3) == encodeArgs()); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0x3)); } BOOST_AUTO_TEST_CASE(compound_assign) @@ -553,22 +553,21 @@ BOOST_AUTO_TEST_CASE(simple_mapping) "}"; compileAndRun(sourceCode); - // msvc seems to have problems with initializer-list, when there is only 1 param in the list - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x01, 0xa1})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x00, 0xef})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); - callContractFunction("set(uint8,uint8)", bytes({0x01, 0x05})); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x05)); - BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x01), byte(0xa1)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0x00))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x00), byte(0xef)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); + callContractFunction("set(uint8,uint8)", byte(0x01), byte(0x05)); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x05))); + BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00))); } BOOST_AUTO_TEST_CASE(mapping_state) @@ -736,9 +735,9 @@ BOOST_AUTO_TEST_CASE(structs) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(false)); BOOST_CHECK(callContractFunction("set()") == bytes()); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(true)); } BOOST_AUTO_TEST_CASE(struct_reference) @@ -764,9 +763,9 @@ BOOST_AUTO_TEST_CASE(struct_reference) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(false)); BOOST_CHECK(callContractFunction("set()") == bytes()); - BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01)); + BOOST_CHECK(callContractFunction("check()") == encodeArgs(true)); } BOOST_AUTO_TEST_CASE(constructor) @@ -799,7 +798,7 @@ BOOST_AUTO_TEST_CASE(balance) " }\n" "}\n"; compileAndRun(sourceCode, 23); - BOOST_CHECK(callContractFunction("getBalance()") == toBigEndian(u256(23))); + BOOST_CHECK(callContractFunction("getBalance()") == encodeArgs(23)); } BOOST_AUTO_TEST_CASE(blockchain) @@ -812,7 +811,7 @@ BOOST_AUTO_TEST_CASE(blockchain) " }\n" "}\n"; compileAndRun(sourceCode, 27); - BOOST_CHECK(callContractFunction("someInfo()", bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1))); + BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0, 1)); } BOOST_AUTO_TEST_CASE(function_types) @@ -831,8 +830,8 @@ BOOST_AUTO_TEST_CASE(function_types) " }\n" "}\n"; compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("a(bool)", bytes{0}) == toBigEndian(u256(11))); - BOOST_CHECK(callContractFunction("a(bool)", bytes{1}) == toBigEndian(u256(12))); + BOOST_CHECK(callContractFunction("a(bool)", false) == encodeArgs(11)); + BOOST_CHECK(callContractFunction("a(bool)", true) == encodeArgs(12)); } BOOST_AUTO_TEST_CASE(send_ether) @@ -846,7 +845,7 @@ BOOST_AUTO_TEST_CASE(send_ether) u256 amount(130); compileAndRun(sourceCode, amount + 1); u160 address(23); - BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == toBigEndian(u256(1))); + BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == encodeArgs(1)); BOOST_CHECK_EQUAL(m_state.balance(address), amount); } @@ -1031,7 +1030,7 @@ BOOST_AUTO_TEST_CASE(ecrecover) u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f"); u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549"); u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"); - BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == toBigEndian(addr)); + BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == encodeArgs(addr)); } BOOST_AUTO_TEST_CASE(inter_contract_calls) @@ -1058,10 +1057,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) @@ -1088,11 +1087,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == toBigEndian(a * 3)); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == toBigEndian(b * 3)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == encodeArgs(b * 3)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) @@ -1119,8 +1118,8 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); - BOOST_REQUIRE(callContractFunction("callHelper()") == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(helperAddress)); } BOOST_AUTO_TEST_CASE(calls_to_this) @@ -1150,10 +1149,10 @@ BOOST_AUTO_TEST_CASE(calls_to_this) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 10)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10)); } BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) @@ -1185,10 +1184,10 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); u256 a(3456789); u256 b("0x282837623374623234aa74"); - BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 9)); + BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9)); } BOOST_AUTO_TEST_CASE(strings_in_calls) @@ -1215,8 +1214,8 @@ BOOST_AUTO_TEST_CASE(strings_in_calls) u160 const helperAddress = m_contractAddress; compileAndRun(sourceCode, 0, "Main"); BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes()); - BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress)); - BOOST_CHECK(callContractFunction("callHelper(string2,bool)", bytes({0, 'a', 1})) == bytes({0, 'a', 0, 0, 0})); + BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == encodeArgs(helperAddress)); + BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); } BOOST_AUTO_TEST_CASE(constructor_arguments) @@ -1241,8 +1240,8 @@ BOOST_AUTO_TEST_CASE(constructor_arguments) function getName() returns (string3 ret) { return h.getName(); } })"; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("getFlag()") == bytes({byte(0x01)})); - BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); } BOOST_AUTO_TEST_CASE(functions_called_by_constructor) @@ -1259,7 +1258,7 @@ BOOST_AUTO_TEST_CASE(functions_called_by_constructor) function setName(string3 _name) { name = _name; } })"; compileAndRun(sourceCode); - BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'})); + BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/solidityExecutionFramework.h b/test/solidityExecutionFramework.h index 5ef23fdb1..8d3c7e77c 100644 --- a/test/solidityExecutionFramework.h +++ b/test/solidityExecutionFramework.h @@ -32,9 +32,6 @@ namespace dev { -/// Provides additional overloads for toBigEndian to encode arguments and return values. -inline bytes toBigEndian(byte _value) { return bytes({_value}); } -inline bytes toBigEndian(bool _value) { return bytes({byte(_value)}); } namespace solidity { @@ -56,18 +53,20 @@ public: return m_output; } - bytes const& callContractFunction(std::string _sig, bytes const& _data = bytes(), - u256 const& _value = 0) + template + bytes const& callContractFunctionWithValue(std::string _sig, u256 const& _value, + Args const&... _arguments) { + FixedHash<4> hash(dev::sha3(_sig)); - sendMessage(hash.asBytes() + _data, false, _value); + sendMessage(hash.asBytes() + encodeArgs(_arguments...), false, _value); return m_output; } template bytes const& callContractFunction(std::string _sig, Args const&... _arguments) { - return callContractFunction(_sig, argsToBigEndian(_arguments...)); + return callContractFunctionWithValue(_sig, 0, _arguments...); } template @@ -89,19 +88,33 @@ public: bytes cppResult = callCppAndEncodeResult(_cppFunction, argument); BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match." "\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult) + - "\nArgument: " + toHex(toBigEndian(argument))); + "\nArgument: " + toHex(encode(argument))); } } -private: - template - bytes argsToBigEndian(FirstArg const& _firstArg, Args const&... _followingArgs) const + static bytes encode(bool _value) { return encode(byte(_value)); } + static bytes encode(int _value) { return encode(u256(_value)); } + static bytes encode(char const* _value) { return encode(std::string(_value)); } + static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } + static bytes encode(u256 const& _value) { return toBigEndian(_value); } + static bytes encode(bytes const& _value, bool _padLeft = true) { - return toBigEndian(_firstArg) + argsToBigEndian(_followingArgs...); + bytes padding = bytes((32 - _value.size() % 32) % 32, 0); + return _padLeft ? padding + _value : _value + padding; } + static bytes encode(std::string const& _value) { return encode(asBytes(_value), false); } - bytes argsToBigEndian() const { 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 @@ -113,7 +126,7 @@ private: auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments) -> typename std::enable_if::value, bytes>::type { - return toBigEndian(_cppFunction(_arguments...)); + return encode(_cppFunction(_arguments...)); } void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0)