aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-04-05 21:52:25 +0800
committerchriseth <chris@ethereum.org>2018-04-12 22:37:16 +0800
commit75b88286667690ffb4a5e079665ed8b70bcaeb87 (patch)
treeee0952b521768895b329f3e91dfa0a98eb2b4cef
parent7343c4028365d3fbc9fa46fca6078971a2bed426 (diff)
downloaddexon-solidity-75b88286667690ffb4a5e079665ed8b70bcaeb87.tar.gz
dexon-solidity-75b88286667690ffb4a5e079665ed8b70bcaeb87.tar.zst
dexon-solidity-75b88286667690ffb4a5e079665ed8b70bcaeb87.zip
Allow struct encoding with new encoder.
-rw-r--r--libsolidity/analysis/TypeChecker.cpp25
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp106
-rw-r--r--test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs.sol17
-rw-r--r--test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs_abiv2.sol18
-rw-r--r--test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs.sol9
-rw-r--r--test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs_abiv2.sol16
6 files changed, 179 insertions, 12 deletions
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index df08598c..b95fee38 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -1723,6 +1723,8 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
}
else if (isPositionalCall)
{
+ bool const abiEncodeV2 = m_scope->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::ABIEncoderV2);
+
for (size_t i = 0; i < arguments.size(); ++i)
{
auto const& argType = type(*arguments[i]);
@@ -1735,13 +1737,22 @@ bool TypeChecker::visit(FunctionCall const& _functionCall)
m_errorReporter.typeError(arguments[i]->location(), "Invalid rational number (too large or division by zero).");
errored = true;
}
- if (!errored && !(
- argType->mobileType() &&
- argType->mobileType()->interfaceType(false) &&
- argType->mobileType()->interfaceType(false)->encodingType() &&
- !(dynamic_cast<StructType const*>(argType->mobileType()->interfaceType(false)->encodingType().get()))
- ))
- m_errorReporter.typeError(arguments[i]->location(), "This type cannot be encoded.");
+ if (!errored)
+ {
+ TypePointer encodingType;
+ if (
+ argType->mobileType() &&
+ argType->mobileType()->interfaceType(false) &&
+ argType->mobileType()->interfaceType(false)->encodingType()
+ )
+ encodingType = argType->mobileType()->interfaceType(false)->encodingType();
+ // Structs are fine as long as ABIV2 is activated and we do not do packed encoding.
+ if (!encodingType || (
+ dynamic_cast<StructType const*>(encodingType.get()) &&
+ !(abiEncodeV2 && functionType->padArguments())
+ ))
+ m_errorReporter.typeError(arguments[i]->location(), "This type cannot be encoded.");
+ }
}
else if (!type(*arguments[i])->isImplicitlyConvertibleTo(*parameterTypes[i]))
m_errorReporter.typeError(
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 8b0e3f83..49cb5d4d 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -11234,6 +11234,53 @@ BOOST_AUTO_TEST_CASE(abi_encode_with_selector)
ABI_CHECK(callContractFunction("f3()"), expectation);
}
+BOOST_AUTO_TEST_CASE(abi_encode_with_selectorv2)
+{
+ char const* sourceCode = R"(
+ pragma experimental ABIEncoderV2;
+ contract C {
+ function f0() public pure returns (bytes) {
+ return abi.encodeWithSelector(0x12345678);
+ }
+ function f1() public pure returns (bytes) {
+ return abi.encodeWithSelector(0x12345678, "abc");
+ }
+ function f2() public pure returns (bytes) {
+ bytes4 x = 0x12345678;
+ return abi.encodeWithSelector(x, "abc");
+ }
+ function f3() public pure returns (bytes) {
+ bytes4 x = 0x12345678;
+ return abi.encodeWithSelector(x, uint(-1));
+ }
+ struct S { uint a; string b; uint16 c; }
+ function f4() public pure returns (bytes) {
+ bytes4 x = 0x12345678;
+ S memory s;
+ s.a = 0x1234567;
+ s.b = "Lorem ipsum dolor sit ethereum........";
+ s.c = 0x1234;
+ return abi.encodeWithSelector(x, uint(-1), s, uint(3));
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "C");
+ ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\x12\x34\x56\x78"));
+ bytes expectation;
+ expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f1()"), expectation);
+ expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f2()"), expectation);
+ expectation = encodeArgs(0x20, 4 + 0x20) + bytes{0x12, 0x34, 0x56, 0x78} + encodeArgs(u256(-1)) + bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f3()"), expectation);
+ expectation =
+ encodeArgs(0x20, 4 + 0x120) +
+ bytes{0x12, 0x34, 0x56, 0x78} +
+ encodeArgs(u256(-1), 0x60, u256(3), 0x1234567, 0x60, 0x1234, 38, "Lorem ipsum dolor sit ethereum........") +
+ bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f4()"), expectation);
+}
+
BOOST_AUTO_TEST_CASE(abi_encode_with_signature)
{
char const* sourceCode = R"T(
@@ -11277,6 +11324,65 @@ BOOST_AUTO_TEST_CASE(abi_encode_with_signature)
ABI_CHECK(callContractFunction("f2()"), expectation);
}
+BOOST_AUTO_TEST_CASE(abi_encode_with_signaturev2)
+{
+ char const* sourceCode = R"T(
+ pragma experimental ABIEncoderV2;
+ contract C {
+ function f0() public pure returns (bytes) {
+ return abi.encodeWithSignature("f(uint256)");
+ }
+ function f1() public pure returns (bytes) {
+ string memory x = "f(uint256)";
+ return abi.encodeWithSignature(x, "abc");
+ }
+ string xstor;
+ function f1s() public returns (bytes) {
+ xstor = "f(uint256)";
+ return abi.encodeWithSignature(xstor, "abc");
+ }
+ function f2() public pure returns (bytes r, uint[] ar) {
+ string memory x = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
+ uint[] memory y = new uint[](4);
+ y[0] = uint(-1);
+ y[1] = uint(-2);
+ y[2] = uint(-3);
+ y[3] = uint(-4);
+ r = abi.encodeWithSignature(x, y);
+ // The hash uses temporary memory. This allocation re-uses the memory
+ // and should initialize it properly.
+ ar = new uint[](2);
+ }
+ struct S { uint a; string b; uint16 c; }
+ function f4() public pure returns (bytes) {
+ bytes4 x = 0x12345678;
+ S memory s;
+ s.a = 0x1234567;
+ s.b = "Lorem ipsum dolor sit ethereum........";
+ s.c = 0x1234;
+ return abi.encodeWithSignature(s.b, uint(-1), s, uint(3));
+ }
+ }
+ )T";
+ compileAndRun(sourceCode, 0, "C");
+ ABI_CHECK(callContractFunction("f0()"), encodeArgs(0x20, 4, "\xb3\xde\x64\x8b"));
+ bytes expectation;
+ expectation = encodeArgs(0x20, 4 + 0x60) + bytes{0xb3, 0xde, 0x64, 0x8b} + encodeArgs(0x20, 3, "abc") + bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f1()"), expectation);
+ ABI_CHECK(callContractFunction("f1s()"), expectation);
+ expectation =
+ encodeArgs(0x40, 0x140, 4 + 0xc0) +
+ (bytes{0xe9, 0xc9, 0x21, 0xcd} + encodeArgs(0x20, 4, u256(-1), u256(-2), u256(-3), u256(-4)) + bytes(0x20 - 4)) +
+ encodeArgs(2, 0, 0);
+ ABI_CHECK(callContractFunction("f2()"), expectation);
+ expectation =
+ encodeArgs(0x20, 4 + 0x120) +
+ bytes{0x7c, 0x79, 0x30, 0x02} +
+ encodeArgs(u256(-1), 0x60, u256(3), 0x1234567, 0x60, 0x1234, 38, "Lorem ipsum dolor sit ethereum........") +
+ bytes(0x20 - 4);
+ ABI_CHECK(callContractFunction("f4()"), expectation);
+}
+
BOOST_AUTO_TEST_CASE(abi_encode_call)
{
char const* sourceCode = R"T(
diff --git a/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs.sol b/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs.sol
new file mode 100644
index 00000000..d9eebee4
--- /dev/null
+++ b/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs.sol
@@ -0,0 +1,17 @@
+contract C {
+ struct S { uint x; }
+ S s;
+ struct T { uint y; }
+ T t;
+ function f() public view {
+ abi.encode(s, t);
+ }
+ function g() public view {
+ abi.encodePacked(s, t);
+ }
+}
+// ----
+// TypeError: (131-132): This type cannot be encoded.
+// TypeError: (134-135): This type cannot be encoded.
+// TypeError: (200-201): This type cannot be encoded.
+// TypeError: (203-204): This type cannot be encoded.
diff --git a/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs_abiv2.sol b/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs_abiv2.sol
new file mode 100644
index 00000000..d6cf60e4
--- /dev/null
+++ b/test/libsolidity/syntaxTests/specialFunctions/abi_encode_structs_abiv2.sol
@@ -0,0 +1,18 @@
+pragma experimental ABIEncoderV2;
+
+contract C {
+ struct S { uint x; }
+ S s;
+ struct T { uint y; }
+ T t;
+ function f() public view {
+ abi.encode(s, t);
+ }
+ function g() public view {
+ abi.encodePacked(s, t);
+ }
+}
+// ----
+// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
+// TypeError: (235-236): This type cannot be encoded.
+// TypeError: (238-239): This type cannot be encoded.
diff --git a/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs.sol b/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs.sol
index cc354819..fa910260 100644
--- a/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs.sol
+++ b/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs.sol
@@ -1,14 +1,13 @@
contract C {
struct S { uint x; }
S s;
- struct T { }
+ struct T { uint y; }
T t;
- function f() public pure {
+ function f() public view {
bytes32 a = sha256(s, t);
a;
}
}
// ----
-// Warning: (51-63): Defining empty structs is deprecated.
-// TypeError: (131-132): This type cannot be encoded.
-// TypeError: (134-135): This type cannot be encoded.
+// TypeError: (139-140): This type cannot be encoded.
+// TypeError: (142-143): This type cannot be encoded.
diff --git a/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs_abiv2.sol b/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs_abiv2.sol
new file mode 100644
index 00000000..1187ce4a
--- /dev/null
+++ b/test/libsolidity/syntaxTests/specialFunctions/types_with_unspecified_encoding_structs_abiv2.sol
@@ -0,0 +1,16 @@
+pragma experimental ABIEncoderV2;
+
+contract C {
+ struct S { uint x; }
+ S s;
+ struct T { uint y; }
+ T t;
+ function f() public view {
+ bytes32 a = sha256(s, t);
+ a;
+ }
+}
+// ----
+// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
+// TypeError: (174-175): This type cannot be encoded.
+// TypeError: (177-178): This type cannot be encoded.