Merge pull request #6211 from ethereum/fixFormatting

Fix formatting and compilation error.
This commit is contained in:
chriseth 2019-03-06 22:55:02 +01:00 committed by GitHub
commit 8d9be8bb6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 209 deletions

View File

@ -203,7 +203,7 @@ public:
} }
template<typename Range> template<typename Range>
bytes encodeArray(bool _dynamicallySized, bool _dynamicallyEncoded, Range const& _elements) static bytes encodeArray(bool _dynamicallySized, bool _dynamicallyEncoded, Range const& _elements)
{ {
bytes result; bytes result;
if (_dynamicallySized) if (_dynamicallySized)

View File

@ -8055,11 +8055,11 @@ BOOST_AUTO_TEST_CASE(struct_named_constructor)
BOOST_AUTO_TEST_CASE(calldata_array) BOOST_AUTO_TEST_CASE(calldata_array)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(uint[2] calldata s) external pure returns (uint256 a, uint256 b) { function f(uint[2] calldata s) external pure returns (uint256 a, uint256 b) {
a = s[0]; a = s[0];
b = s[1]; b = s[1];
} }
} }
)"; )";
@ -8071,12 +8071,12 @@ BOOST_AUTO_TEST_CASE(calldata_array)
BOOST_AUTO_TEST_CASE(calldata_struct) BOOST_AUTO_TEST_CASE(calldata_struct)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(S calldata s) external pure returns (uint256 a, uint256 b) { function f(S calldata s) external pure returns (uint256 a, uint256 b) {
a = s.a; a = s.a;
b = s.b; b = s.b;
} }
} }
)"; )";
@ -8088,11 +8088,11 @@ BOOST_AUTO_TEST_CASE(calldata_struct)
BOOST_AUTO_TEST_CASE(calldata_struct_and_ints) BOOST_AUTO_TEST_CASE(calldata_struct_and_ints)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(uint256 a, S calldata s, uint256 b) external pure returns (uint256, uint256, uint256, uint256) { function f(uint256 a, S calldata s, uint256 b) external pure returns (uint256, uint256, uint256, uint256) {
return (a, s.a, s.b, b); return (a, s.a, s.b, b);
} }
} }
)"; )";
@ -8105,17 +8105,17 @@ BOOST_AUTO_TEST_CASE(calldata_struct_and_ints)
BOOST_AUTO_TEST_CASE(calldata_structs) BOOST_AUTO_TEST_CASE(calldata_structs)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S1 { uint256 a; uint256 b; } struct S1 { uint256 a; uint256 b; }
struct S2 { uint256 a; } struct S2 { uint256 a; }
function f(S1 calldata s1, S2 calldata s2, S1 calldata s3) function f(S1 calldata s1, S2 calldata s2, S1 calldata s3)
external pure returns (uint256 a, uint256 b, uint256 c, uint256 d, uint256 e) { external pure returns (uint256 a, uint256 b, uint256 c, uint256 d, uint256 e) {
a = s1.a; a = s1.a;
b = s1.b; b = s1.b;
c = s2.a; c = s2.a;
d = s3.a; d = s3.a;
e = s3.b; e = s3.b;
} }
} }
)"; )";
@ -8127,14 +8127,14 @@ BOOST_AUTO_TEST_CASE(calldata_structs)
BOOST_AUTO_TEST_CASE(calldata_struct_array_member) BOOST_AUTO_TEST_CASE(calldata_struct_array_member)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256[2] b; uint256 c; } struct S { uint256 a; uint256[2] b; uint256 c; }
function f(S calldata s) external pure returns (uint256 a, uint256 b0, uint256 b1, uint256 c) { function f(S calldata s) external pure returns (uint256 a, uint256 b0, uint256 b1, uint256 c) {
a = s.a; a = s.a;
b0 = s.b[0]; b0 = s.b[0];
b1 = s.b[1]; b1 = s.b[1];
c = s.c; c = s.c;
} }
} }
)"; )";
@ -8146,15 +8146,15 @@ BOOST_AUTO_TEST_CASE(calldata_struct_array_member)
BOOST_AUTO_TEST_CASE(calldata_array_of_struct) BOOST_AUTO_TEST_CASE(calldata_array_of_struct)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(S[] calldata s) external pure returns (uint256 l, uint256 a, uint256 b, uint256 c, uint256 d) { function f(S[] calldata s) external pure returns (uint256 l, uint256 a, uint256 b, uint256 c, uint256 d) {
l = s.length; l = s.length;
a = s[0].a; a = s[0].a;
b = s[0].b; b = s[0].b;
c = s[1].a; c = s[1].a;
d = s[1].b; d = s[1].b;
} }
} }
)"; )";
@ -8166,16 +8166,16 @@ BOOST_AUTO_TEST_CASE(calldata_array_of_struct)
BOOST_AUTO_TEST_CASE(calldata_array_of_struct_to_memory) BOOST_AUTO_TEST_CASE(calldata_array_of_struct_to_memory)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(S[] calldata s) external pure returns (uint256 l, uint256 a, uint256 b, uint256 c, uint256 d) { function f(S[] calldata s) external pure returns (uint256 l, uint256 a, uint256 b, uint256 c, uint256 d) {
S[] memory m = s; S[] memory m = s;
l = m.length; l = m.length;
a = m[0].a; a = m[0].a;
b = m[0].b; b = m[0].b;
c = m[1].a; c = m[1].a;
d = m[1].b; d = m[1].b;
} }
} }
)"; )";
@ -8188,12 +8188,12 @@ BOOST_AUTO_TEST_CASE(calldata_array_of_struct_to_memory)
BOOST_AUTO_TEST_CASE(calldata_struct_to_memory) BOOST_AUTO_TEST_CASE(calldata_struct_to_memory)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(S calldata s) external pure returns (uint256, uint256) { function f(S calldata s) external pure returns (uint256, uint256) {
S memory m = s; S memory m = s;
return (m.a, m.b); return (m.a, m.b);
} }
} }
)"; )";
@ -8205,12 +8205,12 @@ BOOST_AUTO_TEST_CASE(calldata_struct_to_memory)
BOOST_AUTO_TEST_CASE(nested_calldata_struct) BOOST_AUTO_TEST_CASE(nested_calldata_struct)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S1 { uint256 a; uint256 b; } struct S1 { uint256 a; uint256 b; }
struct S2 { uint256 a; uint256 b; S1 s; uint256 c; } struct S2 { uint256 a; uint256 b; S1 s; uint256 c; }
function f(S2 calldata s) external pure returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c) { function f(S2 calldata s) external pure returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c) {
return (s.a, s.b, s.s.a, s.s.b, s.c); return (s.a, s.b, s.s.a, s.s.b, s.c);
} }
} }
)"; )";
@ -8222,13 +8222,13 @@ BOOST_AUTO_TEST_CASE(nested_calldata_struct)
BOOST_AUTO_TEST_CASE(nested_calldata_struct_to_memory) BOOST_AUTO_TEST_CASE(nested_calldata_struct_to_memory)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S1 { uint256 a; uint256 b; } struct S1 { uint256 a; uint256 b; }
struct S2 { uint256 a; uint256 b; S1 s; uint256 c; } struct S2 { uint256 a; uint256 b; S1 s; uint256 c; }
function f(S2 calldata s) external pure returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c) { function f(S2 calldata s) external pure returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c) {
S2 memory m = s; S2 memory m = s;
return (m.a, m.b, m.s.a, m.s.b, m.c); return (m.a, m.b, m.s.a, m.s.b, m.c);
} }
} }
)"; )";
@ -8240,11 +8240,11 @@ BOOST_AUTO_TEST_CASE(nested_calldata_struct_to_memory)
BOOST_AUTO_TEST_CASE(calldata_struct_short) BOOST_AUTO_TEST_CASE(calldata_struct_short)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint256 a; uint256 b; } struct S { uint256 a; uint256 b; }
function f(S calldata) external pure returns (uint256) { function f(S calldata) external pure returns (uint256) {
return msg.data.length; return msg.data.length;
} }
} }
)"; )";
@ -8263,12 +8263,12 @@ BOOST_AUTO_TEST_CASE(calldata_struct_short)
BOOST_AUTO_TEST_CASE(calldata_struct_cleaning) BOOST_AUTO_TEST_CASE(calldata_struct_cleaning)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
struct S { uint8 a; bytes1 b; } struct S { uint8 a; bytes1 b; }
function f(S calldata s) external pure returns (uint256 a, bytes32 b) { function f(S calldata s) external pure returns (uint256 a, bytes32 b) {
uint8 tmp1 = s.a; uint8 tmp1 = s.a;
bytes1 tmp2 = s.b; bytes1 tmp2 = s.b;
assembly { assembly {
a := tmp1 a := tmp1
b := tmp2 b := tmp2
@ -8279,7 +8279,7 @@ BOOST_AUTO_TEST_CASE(calldata_struct_cleaning)
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
// double check that the valid case goes through // double check that the valid case goes through
ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(0x12), bytes{0x34} + bytes(31,0)), encodeArgs(0x12, bytes{0x34} + bytes(31,0))); ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(0x12), bytes{0x34} + bytes(31,0)), encodeArgs(0x12, bytes{0x34} + bytes(31,0)));
ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(0x1234), bytes{0x56, 0x78} + bytes(30,0)), encodeArgs(0x34, bytes{0x56} + bytes(31,0))); ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(0x1234), bytes{0x56, 0x78} + bytes(30,0)), encodeArgs(0x34, bytes{0x56} + bytes(31,0)));
ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(-1), u256(-1)), encodeArgs(0xFF, bytes{0xFF} + bytes(31,0))); ABI_CHECK(callContractFunction("f((uint8,bytes1))", u256(-1), u256(-1)), encodeArgs(0xFF, bytes{0xFF} + bytes(31,0)));
@ -8295,10 +8295,10 @@ BOOST_AUTO_TEST_CASE(calldata_struct_function_type)
return s.fn(42); return s.fn(42);
} }
function g(uint256 a) external returns (uint256) { function g(uint256 a) external returns (uint256) {
return a * 3; return a * 3;
} }
function h(uint256 a) external returns (uint256) { function h(uint256 a) external returns (uint256) {
return 23; return 23;
} }
} }
)"; )";
@ -8331,29 +8331,29 @@ BOOST_AUTO_TEST_CASE(calldata_array_dynamic_bytes)
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
bytes bytes010203 = bytes{1,2,3}+bytes(29,0); bytes bytes010203 = bytes{1,2,3}+bytes(29,0);
bytes bytes040506 = bytes{4,5,6}+bytes(29,0); bytes bytes040506 = bytes{4,5,6}+bytes(29,0);
bytes bytes0102 = bytes{1,2}+bytes(30,0); bytes bytes0102 = bytes{1,2}+bytes(30,0);
ABI_CHECK( ABI_CHECK(
callContractFunction("f1(bytes[1])", 0x20, 0x20, 3, bytes010203), callContractFunction("f1(bytes[1])", 0x20, 0x20, 3, bytes010203),
encodeArgs(3, 1, 2, 3) encodeArgs(3, 1, 2, 3)
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f2(bytes[1],bytes[1])", 0x40, 0xA0, 0x20, 3, bytes010203, 0x20, 2, bytes0102), callContractFunction("f2(bytes[1],bytes[1])", 0x40, 0xA0, 0x20, 3, bytes010203, 0x20, 2, bytes0102),
encodeArgs(3, 1, 2, 3, 2, 1, 2) encodeArgs(3, 1, 2, 3, 2, 1, 2)
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("g1(bytes[2])", 0x20, 0x40, 0x80, 3, bytes010203, 3, bytes040506), callContractFunction("g1(bytes[2])", 0x20, 0x40, 0x80, 3, bytes010203, 3, bytes040506),
encodeArgs(3, 1, 2, 3, 3, 4, 5, 6) encodeArgs(3, 1, 2, 3, 3, 4, 5, 6)
); );
// same offset for both arrays // same offset for both arrays
ABI_CHECK( ABI_CHECK(
callContractFunction("g1(bytes[2])", 0x20, 0x40, 0x40, 3, bytes010203), callContractFunction("g1(bytes[2])", 0x20, 0x40, 0x40, 3, bytes010203),
encodeArgs(3, 1, 2, 3, 3, 1, 2, 3) encodeArgs(3, 1, 2, 3, 3, 1, 2, 3)
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("g2(bytes[])", 0x20, 2, 0x40, 0x80, 2, bytes0102, 3, bytes040506), callContractFunction("g2(bytes[])", 0x20, 2, 0x40, 0x80, 2, bytes0102, 3, bytes040506),
encodeArgs(2, 2, 1, 2, 3, 4, 5, 6) encodeArgs(2, 2, 1, 2, 3, 4, 5, 6)
); );
} }
@ -8363,16 +8363,16 @@ BOOST_AUTO_TEST_CASE(calldata_dynamic_array_to_memory)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(uint256[][] calldata a) external returns (uint, uint256[] memory) { function f(uint256[][] calldata a) external returns (uint, uint256[] memory) {
uint256[] memory m = a[0]; uint256[] memory m = a[0];
return (a.length, m); return (a.length, m);
} }
} }
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
ABI_CHECK( ABI_CHECK(
callContractFunction("f(uint256[][])", 0x20, 1, 0x20, 2, 23, 42), callContractFunction("f(uint256[][])", 0x20, 1, 0x20, 2, 23, 42),
encodeArgs(1, 0x40, 2, 23, 42) encodeArgs(1, 0x40, 2, 23, 42)
); );
} }
@ -8382,36 +8382,36 @@ BOOST_AUTO_TEST_CASE(calldata_bytes_array_to_memory)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(bytes[] calldata a) external returns (uint, uint, bytes memory) { function f(bytes[] calldata a) external returns (uint, uint, bytes memory) {
bytes memory m = a[0]; bytes memory m = a[0];
return (a.length, m.length, m); return (a.length, m.length, m);
} }
} }
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[])", 0x20, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), callContractFunction("f(bytes[])", 0x20, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)),
encodeArgs(1, 2, 0x60, 2, bytes{'a','b'} + bytes(30, 0)) encodeArgs(1, 2, 0x60, 2, bytes{'a','b'} + bytes(30, 0))
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, bytes(32, 'x')), callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, bytes(32, 'x')),
encodeArgs(1, 32, 0x60, 32, bytes(32, 'x')) encodeArgs(1, 32, 0x60, 32, bytes(32, 'x'))
); );
bytes x_zero_a = bytes{'x'} + bytes(30, 0) + bytes{'a'}; bytes x_zero_a = bytes{'x'} + bytes(30, 0) + bytes{'a'};
bytes a_zero_x = bytes{'a'} + bytes(30, 0) + bytes{'x'}; bytes a_zero_x = bytes{'a'} + bytes(30, 0) + bytes{'x'};
bytes a_m_x = bytes{'a'} + bytes(30, 'm') + bytes{'x'}; bytes a_m_x = bytes{'a'} + bytes(30, 'm') + bytes{'x'};
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, x_zero_a), callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, x_zero_a),
encodeArgs(1, 32, 0x60, 32, x_zero_a) encodeArgs(1, 32, 0x60, 32, x_zero_a)
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, a_zero_x), callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, a_zero_x),
encodeArgs(1, 32, 0x60, 32, a_zero_x) encodeArgs(1, 32, 0x60, 32, a_zero_x)
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, a_m_x), callContractFunction("f(bytes[])", 0x20, 1, 0x20, 32, a_m_x),
encodeArgs(1, 32, 0x60, 32, a_m_x) encodeArgs(1, 32, 0x60, 32, a_m_x)
); );
} }
@ -8421,23 +8421,23 @@ BOOST_AUTO_TEST_CASE(calldata_bytes_array_bounds)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(bytes[] calldata a, uint256 i) external returns (uint) { function f(bytes[] calldata a, uint256 i) external returns (uint) {
return uint8(a[0][i]); return uint8(a[0][i]);
} }
} }
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[],uint256)", 0x40, 0, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), callContractFunction("f(bytes[],uint256)", 0x40, 0, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)),
encodeArgs('a') encodeArgs('a')
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[],uint256)", 0x40, 1, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), callContractFunction("f(bytes[],uint256)", 0x40, 1, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)),
encodeArgs('b') encodeArgs('b')
); );
ABI_CHECK( ABI_CHECK(
callContractFunction("f(bytes[],uint256)", 0x40, 2, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), callContractFunction("f(bytes[],uint256)", 0x40, 2, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)),
encodeArgs() encodeArgs()
); );
} }
@ -8447,8 +8447,8 @@ BOOST_AUTO_TEST_CASE(calldata_string_array)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(string[] calldata a) external returns (uint, uint, uint, string memory) { function f(string[] calldata a) external returns (uint, uint, uint, string memory) {
string memory s1 = a[0]; string memory s1 = a[0];
bytes memory m1 = bytes(s1); bytes memory m1 = bytes(s1);
return (a.length, m1.length, uint8(m1[0]), s1); return (a.length, m1.length, uint8(m1[0]), s1);
} }
} }
@ -8456,136 +8456,136 @@ BOOST_AUTO_TEST_CASE(calldata_string_array)
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
ABI_CHECK( ABI_CHECK(
callContractFunction("f(string[])", 0x20, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), callContractFunction("f(string[])", 0x20, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)),
encodeArgs(1, 2, 'a', 0x80, 2, bytes{'a', 'b'} + bytes(30, 0)) encodeArgs(1, 2, 'a', 0x80, 2, bytes{'a', 'b'} + bytes(30, 0))
); );
} }
BOOST_AUTO_TEST_CASE(calldata_array_two_dimensional) BOOST_AUTO_TEST_CASE(calldata_array_two_dimensional)
{ {
vector<vector<u256>> data { vector<vector<u256>> data {
{ 0x0A01, 0x0A02, 0x0A03 }, { 0x0A01, 0x0A02, 0x0A03 },
{ 0x0B01, 0x0B02, 0x0B03, 0x0B04 } { 0x0B01, 0x0B02, 0x0B03, 0x0B04 }
}; };
for (bool outerDynamicallySized: { true, false }) for (bool outerDynamicallySized: { true, false })
{ {
string arrayType = outerDynamicallySized ? "uint256[][]" : "uint256[][2]"; string arrayType = outerDynamicallySized ? "uint256[][]" : "uint256[][2]";
string sourceCode = R"( string sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function test()" + arrayType + R"( calldata a) external returns (uint256) { function test()" + arrayType + R"( calldata a) external returns (uint256) {
return a.length; return a.length;
} }
function test()" + arrayType + R"( calldata a, uint256 i) external returns (uint256) { function test()" + arrayType + R"( calldata a, uint256 i) external returns (uint256) {
return a[i].length; return a[i].length;
} }
function test()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) { function test()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) {
return a[i][j]; return a[i][j];
} }
} }
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
bytes encoding = encodeArray( bytes encoding = encodeArray(
outerDynamicallySized, outerDynamicallySized,
true, true,
data | boost::adaptors::transformed([&](vector<u256> const& _values) { data | boost::adaptors::transformed([&](vector<u256> const& _values) {
return encodeArray(true, false, _values); return encodeArray(true, false, _values);
}) })
); );
ABI_CHECK(callContractFunction("test(" + arrayType + ")", 0x20, encoding), encodeArgs(data.size())); ABI_CHECK(callContractFunction("test(" + arrayType + ")", 0x20, encoding), encodeArgs(data.size()));
for (size_t i = 0; i < data.size(); i++) for (size_t i = 0; i < data.size(); i++)
{ {
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, i, encoding), encodeArgs(data[i].size())); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, i, encoding), encodeArgs(data[i].size()));
for (size_t j = 0; j < data[i].size(); j++) for (size_t j = 0; j < data[i].size(); j++)
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j])); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j]));
// out of bounds access // out of bounds access
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, data[i].size(), encoding), encodeArgs()); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, data[i].size(), encoding), encodeArgs());
} }
// out of bounds access // out of bounds access
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, data.size(), encoding), encodeArgs()); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, data.size(), encoding), encodeArgs());
} }
} }
BOOST_AUTO_TEST_CASE(calldata_array_dynamic_three_dimensional) BOOST_AUTO_TEST_CASE(calldata_array_dynamic_three_dimensional)
{ {
vector<vector<vector<u256>>> data { vector<vector<vector<u256>>> data {
{ {
{ 0x010A01, 0x010A02, 0x010A03 }, { 0x010A01, 0x010A02, 0x010A03 },
{ 0x010B01, 0x010B02, 0x010B03 } { 0x010B01, 0x010B02, 0x010B03 }
}, },
{ {
{ 0x020A01, 0x020A02, 0x020A03 }, { 0x020A01, 0x020A02, 0x020A03 },
{ 0x020B01, 0x020B02, 0x020B03 } { 0x020B01, 0x020B02, 0x020B03 }
} }
}; };
for (bool outerDynamicallySized: { true, false }) for (bool outerDynamicallySized: { true, false })
for (bool middleDynamicallySized: { true, false }) for (bool middleDynamicallySized: { true, false })
for (bool innerDynamicallySized: { true, false }) for (bool innerDynamicallySized: { true, false })
{ {
// only test dynamically encoded arrays // only test dynamically encoded arrays
if (!outerDynamicallySized && !middleDynamicallySized && !innerDynamicallySized) if (!outerDynamicallySized && !middleDynamicallySized && !innerDynamicallySized)
continue; continue;
string arrayType = "uint256"; string arrayType = "uint256";
arrayType += innerDynamicallySized ? "[]" : "[3]"; arrayType += innerDynamicallySized ? "[]" : "[3]";
arrayType += middleDynamicallySized ? "[]" : "[2]"; arrayType += middleDynamicallySized ? "[]" : "[2]";
arrayType += outerDynamicallySized ? "[]" : "[2]"; arrayType += outerDynamicallySized ? "[]" : "[2]";
string sourceCode = R"( string sourceCode = R"(
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function test()" + arrayType + R"( calldata a) external returns (uint256) { function test()" + arrayType + R"( calldata a) external returns (uint256) {
return a.length; return a.length;
} }
function test()" + arrayType + R"( calldata a, uint256 i) external returns (uint256) { function test()" + arrayType + R"( calldata a, uint256 i) external returns (uint256) {
return a[i].length; return a[i].length;
} }
function test()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) { function test()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) {
return a[i][j].length; return a[i][j].length;
} }
function test()" + arrayType + R"( calldata a, uint256 i, uint256 j, uint256 k) external returns (uint256) { function test()" + arrayType + R"( calldata a, uint256 i, uint256 j, uint256 k) external returns (uint256) {
return a[i][j][k]; return a[i][j][k];
} }
} }
)"; )";
compileAndRun(sourceCode, 0, "C"); compileAndRun(sourceCode, 0, "C");
bytes encoding = encodeArray( bytes encoding = encodeArray(
outerDynamicallySized, outerDynamicallySized,
middleDynamicallySized || innerDynamicallySized, middleDynamicallySized || innerDynamicallySized,
data | boost::adaptors::transformed([&](auto const& _middleData) { data | boost::adaptors::transformed([&](auto const& _middleData) {
return encodeArray( return encodeArray(
middleDynamicallySized, middleDynamicallySized,
innerDynamicallySized, innerDynamicallySized,
_middleData | boost::adaptors::transformed([&](auto const& _values) { _middleData | boost::adaptors::transformed([&](auto const& _values) {
return encodeArray(innerDynamicallySized, false, _values); return encodeArray(innerDynamicallySized, false, _values);
}) })
); );
}) })
); );
ABI_CHECK(callContractFunction("test(" + arrayType + ")", 0x20, encoding), encodeArgs(data.size())); ABI_CHECK(callContractFunction("test(" + arrayType + ")", 0x20, encoding), encodeArgs(data.size()));
for (size_t i = 0; i < data.size(); i++) for (size_t i = 0; i < data.size(); i++)
{ {
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, i, encoding), encodeArgs(data[i].size())); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, i, encoding), encodeArgs(data[i].size()));
for (size_t j = 0; j < data[i].size(); j++) for (size_t j = 0; j < data[i].size(); j++)
{ {
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j].size())); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j].size()));
for (size_t k = 0; k < data[i][j].size(); k++) for (size_t k = 0; k < data[i][j].size(); k++)
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256,uint256)", 0x80, i, j, k, encoding), encodeArgs(data[i][j][k])); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256,uint256)", 0x80, i, j, k, encoding), encodeArgs(data[i][j][k]));
// out of bounds access // out of bounds access
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256,uint256)", 0x80, i, j, data[i][j].size(), encoding), encodeArgs()); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256,uint256)", 0x80, i, j, data[i][j].size(), encoding), encodeArgs());
} }
// out of bounds access // out of bounds access
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, data[i].size(), encoding), encodeArgs()); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, data[i].size(), encoding), encodeArgs());
} }
// out of bounds access // out of bounds access
ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, data.size(), encoding), encodeArgs()); ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, data.size(), encoding), encodeArgs());
} }
} }
BOOST_AUTO_TEST_CASE(calldata_array_dynamic_invalid) BOOST_AUTO_TEST_CASE(calldata_array_dynamic_invalid)
@ -8594,11 +8594,11 @@ BOOST_AUTO_TEST_CASE(calldata_array_dynamic_invalid)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(uint256[][] calldata a) external returns (uint) { function f(uint256[][] calldata a) external returns (uint) {
return 42; return 42;
} }
function g(uint256[][] calldata a) external returns (uint) { function g(uint256[][] calldata a) external returns (uint) {
a[0]; a[0];
return 42; return 42;
} }
} }
)"; )";
@ -8622,15 +8622,15 @@ BOOST_AUTO_TEST_CASE(calldata_array_dynamic_invalid_static_middle)
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
contract C { contract C {
function f(uint256[][1][] calldata a) external returns (uint) { function f(uint256[][1][] calldata a) external returns (uint) {
return 42; return 42;
} }
function g(uint256[][1][] calldata a) external returns (uint) { function g(uint256[][1][] calldata a) external returns (uint) {
a[0]; a[0];
return 42; return 42;
} }
function h(uint256[][1][] calldata a) external returns (uint) { function h(uint256[][1][] calldata a) external returns (uint) {
a[0][0]; a[0][0];
return 42; return 42;
} }
} }
)"; )";