Packed Encoding: Disallow types in v2 that aren't allowed in v1

This commit is contained in:
Mathias Baumann
2019-02-11 11:37:48 +01:00
committed by chriseth
parent 227addfcef
commit fe2429de9f
8 changed files with 52 additions and 31 deletions
+10 -20
View File
@@ -13715,25 +13715,22 @@ BOOST_AUTO_TEST_CASE(abi_encodePackedV2_structs)
s.d[0] = -7;
s.d[1] = -8;
}
function testStorage() public returns (bytes memory) {
function testStorage() public {
emit E(s);
return abi.encodePacked(uint8(0x33), s, uint8(0x44));
}
function testMemory() public returns (bytes memory) {
function testMemory() public {
S memory m = s;
emit E(m);
return abi.encodePacked(uint8(0x33), m, uint8(0x44));
}
}
)";
compileAndRun(sourceCode, 0, "C");
bytes structEnc = encodeArgs(int(0x12), u256(-7), int(2), int(3), u256(-7), u256(-8));
string encoding = "\x33" + asString(structEnc) + "\x44";
ABI_CHECK(callContractFunction("testStorage()"), encodeArgs(0x20, encoding.size(), encoding));
ABI_CHECK(callContractFunction("testStorage()"), encodeArgs());
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("E((uint8,int16,uint8[2],int16[]))")));
BOOST_CHECK_EQUAL(m_logs[0].topics[1], dev::keccak256(asString(structEnc)));
ABI_CHECK(callContractFunction("testMemory()"), encodeArgs(0x20, encoding.size(), encoding));
ABI_CHECK(callContractFunction("testMemory()"), encodeArgs());
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("E((uint8,int16,uint8[2],int16[]))")));
BOOST_CHECK_EQUAL(m_logs[0].topics[1], dev::keccak256(asString(structEnc)));
@@ -13749,7 +13746,7 @@ BOOST_AUTO_TEST_CASE(abi_encodePackedV2_nestedArray)
int16 b;
}
event E(S[2][][3] indexed);
function testNestedArrays() public returns (bytes memory) {
function testNestedArrays() public {
S[2][][3] memory x;
x[1] = new S[2][](2);
x[1][0][0].a = 1;
@@ -13757,14 +13754,12 @@ BOOST_AUTO_TEST_CASE(abi_encodePackedV2_nestedArray)
x[1][0][1].a = 3;
x[1][1][1].b = 4;
emit E(x);
return abi.encodePacked(uint8(0x33), x, uint8(0x44));
}
}
)";
compileAndRun(sourceCode, 0, "C");
bytes structEnc = encodeArgs(1, 2, 3, 0, 0, 0, 0, 4);
string encoding = "\x33" + asString(structEnc) + "\x44";
ABI_CHECK(callContractFunction("testNestedArrays()"), encodeArgs(0x20, encoding.size(), encoding));
ABI_CHECK(callContractFunction("testNestedArrays()"), encodeArgs());
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("E((uint8,int16)[2][][3])")));
BOOST_CHECK_EQUAL(m_logs[0].topics[1], dev::keccak256(asString(structEnc)));
@@ -13782,27 +13777,22 @@ BOOST_AUTO_TEST_CASE(abi_encodePackedV2_arrayOfStrings)
x[0] = "abc";
x[1] = "0123456789012345678901234567890123456789";
}
function testStorage() public returns (bytes memory) {
function testStorage() public {
emit E(x);
return abi.encodePacked(uint8(0x33), x, uint8(0x44));
}
function testMemory() public returns (bytes memory) {
function testMemory() public {
string[] memory y = x;
emit E(y);
return abi.encodePacked(uint8(0x33), y, uint8(0x44));
}
}
)";
compileAndRun(sourceCode, 0, "C");
bytes arrayEncoding = encodeArgs("abc", "0123456789012345678901234567890123456789");
// This pads to multiple of 32 bytes
string encoding = "\x33" + asString(arrayEncoding) + "\x44";
BOOST_CHECK_EQUAL(encoding.size(), 2 + 32 * 3);
ABI_CHECK(callContractFunction("testStorage()"), encodeArgs(0x20, encoding.size(), encoding));
ABI_CHECK(callContractFunction("testStorage()"), encodeArgs());
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("E(string[])")));
BOOST_CHECK_EQUAL(m_logs[0].topics[1], dev::keccak256(asString(arrayEncoding)));
ABI_CHECK(callContractFunction("testMemory()"), encodeArgs(0x20, encoding.size(), encoding));
ABI_CHECK(callContractFunction("testMemory()"), encodeArgs());
BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 2);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::keccak256(string("E(string[])")));
BOOST_CHECK_EQUAL(m_logs[0].topics[1], dev::keccak256(asString(arrayEncoding)));
@@ -12,3 +12,5 @@ contract C {
}
// ----
// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
// TypeError: (191-192): Type not supported in packed mode.
// TypeError: (194-195): Type not supported in packed mode.
@@ -13,5 +13,5 @@ contract C {
// ----
// 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.
// TypeError: (200-201): Type not supported in packed mode.
// TypeError: (203-204): Type not supported in packed mode.
@@ -5,6 +5,10 @@ contract C {
S s;
struct T { uint y; }
T t;
function e() public view {
S memory st;
abi.encodePacked(st);
}
function f() public view {
abi.encode(s, t);
}
@@ -14,3 +18,6 @@ contract C {
}
// ----
// Warning: (0-33): Experimental features are turned on. Do not use experimental features on live deployments.
// TypeError: (193-195): Type not supported in packed mode.
// TypeError: (323-324): Type not supported in packed mode.
// TypeError: (326-327): Type not supported in packed mode.
@@ -6,4 +6,4 @@ contract C {
}
}
// ----
// TypeError: (116-117): This type cannot be encoded.
// TypeError: (116-117): Type not supported in packed mode.
@@ -0,0 +1,13 @@
contract C {
string[] s;
function f() public pure {
string[] memory m;
abi.encodePacked(m);
}
function g() public pure {
abi.encodePacked(s);
}
}
// ----
// TypeError: (112-113): Type not supported in packed mode.
// TypeError: (178-179): Type not supported in packed mode.
@@ -9,5 +9,5 @@ contract C {
}
}
// ----
// TypeError: (156-157): This type cannot be encoded.
// TypeError: (159-160): This type cannot be encoded.
// TypeError: (156-157): Type not supported in packed mode.
// TypeError: (159-160): Type not supported in packed mode.