Skip to content

Commit

Permalink
split layout into one dict per address space
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Apr 21, 2022
1 parent f0a470c commit 27dca0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
22 changes: 11 additions & 11 deletions tests/cli/outputs/test_storage_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,23 @@ def public_foo3():
output_formats=["layout"],
)

assert out["layout"] == {
"nonreentrant.foo": {"type": "nonreentrant lock", "location": "storage", "slot": 0},
"nonreentrant.bar": {"type": "nonreentrant lock", "location": "storage", "slot": 1},
assert out["layout"]["storage_layout"] == {
"nonreentrant.foo": {"type": "nonreentrant lock", "slot": 0},
"nonreentrant.bar": {"type": "nonreentrant lock", "slot": 1},
"foo": {
"type": "HashMap[address, uint256]",
"location": "storage",
"slot": 2,
},
"arr": {
"type": "DynArray[uint256, 3]",
"location": "storage",
"slot": 3,
},
"baz": {"type": "Bytes[65]", "location": "storage", "slot": 7},
"bar": {"type": "uint256", "location": "storage", "slot": 11},
"baz": {"type": "Bytes[65]", "slot": 7},
"bar": {"type": "uint256", "slot": 11},
}


def test_immutables_layout():
def test_storage_and_immutables_layout():
code = """
name: String[32]
SYMBOL: immutable(String[32])
Expand All @@ -74,9 +72,11 @@ def __init__():
"""

expected_layout = {
"DECIMALS": {"length": 32, "location": "code", "offset": 64, "type": "uint8"},
"SYMBOL": {"length": 64, "location": "code", "offset": 0, "type": "String[32]"},
"name": {"location": "storage", "slot": 0, "type": "String[32]"},
"code_layout": {
"DECIMALS": {"length": 32, "offset": 64, "type": "uint8"},
"SYMBOL": {"length": 64, "offset": 0, "type": "String[32]"},
},
"storage_layout": {"name": {"slot": 0, "type": "String[32]"}},
}

out = compile_code(code, output_formats=["layout"])
Expand Down
9 changes: 3 additions & 6 deletions vyper/semantics/validation/data_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def set_data_positions(
else set_storage_slots(vyper_module)
)

return dict(**code_offsets, **storage_slots)
return {"storage_layout": storage_slots, "code_layout": code_offsets}


class StorageAllocator:
Expand Down Expand Up @@ -104,7 +104,6 @@ def set_storage_slots_with_overrides(

ret[variable_name] = {
"type": "nonreentrant lock",
"location": "storage",
"slot": reentrant_slot,
}
else:
Expand Down Expand Up @@ -133,7 +132,7 @@ def set_storage_slots_with_overrides(
reserved_slots.reserve_slot_range(var_slot, storage_length, node.target.id)
type_.set_position(StorageSlot(var_slot))

ret[node.target.id] = {"type": str(type_), "location": "storage", "slot": var_slot}
ret[node.target.id] = {"type": str(type_), "slot": var_slot}
else:
raise StorageLayoutException(
f"Could not find storage_slot for {node.target.id}. "
Expand Down Expand Up @@ -176,7 +175,6 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:
# we nail down the format better
ret[variable_name] = {
"type": "nonreentrant lock",
"location": "storage",
"slot": storage_slot,
}

Expand All @@ -195,7 +193,7 @@ def set_storage_slots(vyper_module: vy_ast.Module) -> StorageLayout:

# this could have better typing but leave it untyped until
# we understand the use case better
ret[node.target.id] = {"type": str(type_), "location": "storage", "slot": storage_slot}
ret[node.target.id] = {"type": str(type_), "slot": storage_slot}

# CMC 2021-07-23 note that HashMaps get assigned a slot here.
# I'm not sure if it's safe to avoid allocating that slot
Expand Down Expand Up @@ -230,7 +228,6 @@ def set_code_offsets(vyper_module: vy_ast.Module) -> Dict:
# we understand the use case better
ret[node.target.id] = {
"type": str(type_),
"location": "code",
"offset": offset,
"length": len_,
}
Expand Down

0 comments on commit 27dca0b

Please sign in to comment.