From 1d44a8999e2dbbd5547f511958bcf744267938f7 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 13 Mar 2015 19:48:24 +0100 Subject: [PATCH] Fetch and store packed values. --- SolidityEndToEndTest.cpp | 150 +++++++++++++++++++++++++++++++++++---- SolidityOptimizer.cpp | 2 +- 2 files changed, 139 insertions(+), 13 deletions(-) diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index de6f43b77..a07b78872 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -991,18 +991,20 @@ BOOST_AUTO_TEST_CASE(multiple_elementary_accessors) BOOST_AUTO_TEST_CASE(complex_accessors) { - char const* sourceCode = "contract test {\n" - " mapping(uint256 => bytes4) public to_string_map;\n" - " mapping(uint256 => bool) public to_bool_map;\n" - " mapping(uint256 => uint256) public to_uint_map;\n" - " mapping(uint256 => mapping(uint256 => uint256)) public to_multiple_map;\n" - " function test() {\n" - " to_string_map[42] = \"24\";\n" - " to_bool_map[42] = false;\n" - " to_uint_map[42] = 12;\n" - " to_multiple_map[42][23] = 31;\n" - " }\n" - "}\n"; + char const* sourceCode = R"( + contract test { + mapping(uint256 => bytes4) public to_string_map; + mapping(uint256 => bool) public to_bool_map; + mapping(uint256 => uint256) public to_uint_map; + mapping(uint256 => mapping(uint256 => uint256)) public to_multiple_map; + function test() { + to_string_map[42] = "24"; + to_bool_map[42] = false; + to_uint_map[42] = 12; + to_multiple_map[42][23] = 31; + } + } + )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("to_string_map(uint256)", 42) == encodeArgs("24")); BOOST_CHECK(callContractFunction("to_bool_map(uint256)", 42) == encodeArgs(false)); @@ -3269,6 +3271,130 @@ BOOST_AUTO_TEST_CASE(constant_variables) })"; compileAndRun(sourceCode); } + +BOOST_AUTO_TEST_CASE(packed_storage_structs_uint) +{ + char const* sourceCode = R"( + contract C { + struct str { uint8 a; uint16 b; uint248 c; } + str data; + function test() returns (uint) { + data.a = 2; + if (data.a != 2) return 2; + data.b = 0xabcd; + if (data.b != 0xabcd) return 3; + data.c = 0x1234567890; + if (data.c != 0x1234567890) return 4; + if (data.a != 2) return 5; + data.a = 8; + if (data.a != 8) return 6; + if (data.b != 0xabcd) return 7; + data.b = 0xdcab; + if (data.b != 0xdcab) return 8; + if (data.c != 0x1234567890) return 9; + data.c = 0x9876543210; + if (data.c != 0x9876543210) return 10; + return 1; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); +} + +BOOST_AUTO_TEST_CASE(packed_storage_structs_enum) +{ + char const* sourceCode = R"( + contract C { + enum small { A, B, C, D } + enum larger { A, B, C, D, E} + struct str { small a; small b; larger c; larger d; } + str data; + function test() returns (uint) { + data.a = small.B; + if (data.a != small.B) return 2; + data.b = small.C; + if (data.b != small.C) return 3; + data.c = larger.D; + if (data.c != larger.D) return 4; + if (data.a != small.B) return 5; + data.a = small.C; + if (data.a != small.C) return 6; + if (data.b != small.C) return 7; + data.b = small.D; + if (data.b != small.D) return 8; + if (data.c != larger.D) return 9; + data.c = larger.B; + if (data.c != larger.B) return 10; + return 1; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); +} + +BOOST_AUTO_TEST_CASE(packed_storage_structs_bytes) +{ + char const* sourceCode = R"( + contract C { + struct s1 { byte a; byte b; bytes10 c; bytes9 d; bytes10 e; } + struct s2 { byte a; s1 inner; byte b; byte c; } + byte x; + s2 data; + byte y; + function test() returns (bool) { + x = 1; + data.a = 2; + data.inner.a = 3; + data.inner.b = 4; + data.inner.c = "1234567890"; + data.inner.d = "123456789"; + data.inner.e = "abcdefghij"; + data.b = 5; + data.c = 6; + y = 7; + return x == 1 && data.a == 2 && data.inner.a == 3 && data.inner.b == 4 && + data.inner.c == "1234567890" && data.inner.d == "123456789" && + data.inner.e == "abcdefghij" && data.b == 5 && data.c == 6 && y == 7; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); +} + +BOOST_AUTO_TEST_CASE(packed_storage_structs_delete) +{ + char const* sourceCode = R"( + contract C { + struct str { uint8 a; uint16 b; uint8 c; } + uint8 x; + uint16 y; + str data; + function test() returns (uint) { + x = 1; + y = 2; + data.a = 2; + data.b = 0xabcd; + data.c = 0xfa; + if (x != 1 || y != 2 || data.a != 2 || data.b != 0xabcd || data.c != 0xfa) + return 2; + delete y; + delete data.b; + if (x != 1 || y != 0 || data.a != 2 || data.b != 0 || data.c != 0xfa) + return 3; + delete x; + delete data; + return 1; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); + BOOST_CHECK(m_state.storage(m_contractAddress).empty()); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/SolidityOptimizer.cpp b/SolidityOptimizer.cpp index 161d1f898..85b77d210 100644 --- a/SolidityOptimizer.cpp +++ b/SolidityOptimizer.cpp @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(unused_expressions) data; } })"; - compileBothVersions(29, sourceCode); + compileBothVersions(36, sourceCode); compareVersions("f()"); }