mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #1326 from chriseth/sol_packedStorage
Let Solidity pack its storage
This commit is contained in:
commit
6e2464487f
@ -534,6 +534,27 @@ BOOST_AUTO_TEST_CASE(empty_string_on_stack)
|
||||
BOOST_CHECK(callContractFunction("run(bytes0,uint8)", string(), byte(0x02)) == encodeArgs(0x2, string(""), string("abc\0")));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(inc_dec_operators)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract test {
|
||||
uint8 x;
|
||||
uint v;
|
||||
function f() returns (uint r) {
|
||||
uint a = 6;
|
||||
r = a;
|
||||
r += (a++) * 0x10;
|
||||
r += (++a) * 0x100;
|
||||
v = 3;
|
||||
r += (v++) * 0x1000;
|
||||
r += (++v) * 0x10000;
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f()") == encodeArgs(0x53866));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(state_smoke_test)
|
||||
{
|
||||
char const* sourceCode = "contract test {\n"
|
||||
@ -970,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));
|
||||
@ -2502,7 +2525,7 @@ BOOST_AUTO_TEST_CASE(struct_copy)
|
||||
contract c {
|
||||
struct Nested { uint x; uint y; }
|
||||
struct Struct { uint a; mapping(uint => Struct) b; Nested nested; uint c; }
|
||||
mapping(uint => Struct) public data;
|
||||
mapping(uint => Struct) data;
|
||||
function set(uint k) returns (bool) {
|
||||
data[k].a = 1;
|
||||
data[k].nested.x = 3;
|
||||
@ -3248,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()
|
||||
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(unused_expressions)
|
||||
data;
|
||||
}
|
||||
})";
|
||||
compileBothVersions(33, sourceCode);
|
||||
compileBothVersions(36, sourceCode);
|
||||
compareVersions("f()");
|
||||
}
|
||||
|
||||
|
82
SolidityTypes.cpp
Normal file
82
SolidityTypes.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
This file is part of cpp-ethereum.
|
||||
|
||||
cpp-ethereum is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
cpp-ethereum is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* @author Christian <c@ethdev.com>
|
||||
* @date 2015
|
||||
* Unit tests for the type system of Solidity.
|
||||
*/
|
||||
|
||||
#include <libsolidity/Types.h>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace dev
|
||||
{
|
||||
namespace solidity
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(SolidityTypes)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_layout_simple)
|
||||
{
|
||||
MemberList members(MemberList::MemberMap({
|
||||
{string("first"), Type::fromElementaryTypeName("uint128")},
|
||||
{string("second"), Type::fromElementaryTypeName("uint120")},
|
||||
{string("wraps"), Type::fromElementaryTypeName("uint16")}
|
||||
}));
|
||||
BOOST_REQUIRE_EQUAL(u256(2), members.getStorageSize());
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr);
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr);
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("wraps") != nullptr);
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(0), unsigned(16)));
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("wraps") == make_pair(u256(1), unsigned(0)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(storage_layout_mapping)
|
||||
{
|
||||
MemberList members(MemberList::MemberMap({
|
||||
{string("first"), Type::fromElementaryTypeName("uint128")},
|
||||
{string("second"), make_shared<MappingType>(
|
||||
Type::fromElementaryTypeName("uint8"),
|
||||
Type::fromElementaryTypeName("uint8")
|
||||
)},
|
||||
{string("third"), Type::fromElementaryTypeName("uint16")},
|
||||
{string("final"), make_shared<MappingType>(
|
||||
Type::fromElementaryTypeName("uint8"),
|
||||
Type::fromElementaryTypeName("uint8")
|
||||
)},
|
||||
}));
|
||||
BOOST_REQUIRE_EQUAL(u256(4), members.getStorageSize());
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr);
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr);
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("third") != nullptr);
|
||||
BOOST_REQUIRE(members.getMemberStorageOffset("final") != nullptr);
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0)));
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(1), unsigned(0)));
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("third") == make_pair(u256(2), unsigned(0)));
|
||||
BOOST_CHECK(*members.getMemberStorageOffset("final") == make_pair(u256(3), unsigned(0)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user