-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate transient storage layout #15247
Conversation
828652c
to
39f0be8
Compare
8da9441
to
09890a7
Compare
libsolidity/ast/Types.cpp
Outdated
VariableDeclaration::Location location = [&]() { | ||
switch (_location) | ||
{ | ||
case DataLocation::Storage: return VariableDeclaration::Location::Unspecified; // By default state variables have unspecified (storage) data location |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be refactored in a separate PR so variables at storage are explicitly assigned VariableDeclaration::Location::Storage
(note this is a different enum from DataLocation
which is in Types.h
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even need two enums for this? It makes it significantly more annoying in the first place. Adding an Unspecified
option to the DataLocation
enum would be enough; plus it's not a class member enum so it can be aliased as well (unlike VariableDeclaration::Location
). But yeah, a problem for another time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even need two enums for this?
DataLocation
specifies where the data is stored while VariableDeclaration::Location
indicates which modifier is on the variable.
I think this should be refactored in a separate PR so variables at storage are explicitly assigned
VariableDeclaration::Location::Storage
Unspecified
does not necessarily mean Storage
. That's the case only for state variables. And even then it may also represent a constant or an immutable.
I think changing the way it works would just make things confusing in a different way. Then we'd be asking why it doesn't reflect what's on the variable :) The current usage does have some logic behind it. This is just another case of things being too vaguely named, omitting important context. Maybe something like DeclaredLocation
or LocationModifier
would be clearer.
Adding an
Unspecified
option to theDataLocation
enum would be enough;
It would be misleading because this Unspecified
would have a different meaning than the one in VariableDeclaration::Location
and it still would not be a 1:1 translation, IMO nullopt
is a better choice.
9545849
to
e4961e0
Compare
@@ -1103,8 +1115,9 @@ void IRGenerator::resetContext(ContractDefinition const& _contract, ExecutionCon | |||
m_context = std::move(newContext); | |||
|
|||
m_context.setMostDerivedContract(_contract); | |||
for (auto const& var: ContractType(_contract).stateVariables()) | |||
m_context.addStateVariable(*std::get<0>(var), std::get<1>(var), std::get<2>(var)); | |||
for (auto const& location: {DataLocation::Storage, DataLocation::Transient}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might make sense to make the required data location std::optional
and set to std::nullopt
by default - this would alleviate then need to change both this section of code as well as the one in ContractCompiler
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought about that at first but expected it to be more complicated than what I have actually come up with.
libsolidity/ast/Types.cpp
Outdated
VariableDeclaration::Location location = [&]() { | ||
switch (_location) | ||
{ | ||
case DataLocation::Storage: return VariableDeclaration::Location::Unspecified; // By default state variables have unspecified (storage) data location |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even need two enums for this? It makes it significantly more annoying in the first place. Adding an Unspecified
option to the DataLocation
enum would be enough; plus it's not a class member enum so it can be aliased as well (unlike VariableDeclaration::Location
). But yeah, a problem for another time.
test/cmdlineTests/transient_storage_layout_smoke_two_contracts/input.json
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_value_types_interleaved_with_storage/in.sol
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_value_types_interleaved_with_storage/input.json
Outdated
Show resolved
Hide resolved
b58bd32
to
de3872e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cameel please take over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are a few initial remarks after going through tests.
So far only minor stuff, other than the fact that we haven't even one test covering CLI output. And that docs have not been updated.
test/cmdlineTests/standard_outputs_on_compilation_error/output.json
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_smoke_empty/input.json
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_smoke_two_contracts/input.json
Outdated
Show resolved
Hide resolved
contract A { | ||
uint transient x; | ||
uint y; | ||
int transient z; | ||
int w; | ||
address transient addr; | ||
address d; | ||
bool transient b; | ||
bool c; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also add some constants and immutables here to make sure they're not included in either layout.
Also different visibilities (public
, private
, etc.).
Could also be separate tests, but IMO it would be fine to keep it in one test since CLI tests are pretty verbose.
test/cmdlineTests/transient_storage_layout_value_types_interleaved_with_storage/input.json
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through it all now and did not find anything broken. Just a few more testing/styling/wording remarks.
So overall this is mostly fine, just needs docs, CLI coverage, some small tweaks and then we can merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You haven't added a cmdline test for --transient-storage-layout
yet, right?
No I haven't. I wrote a test case in |
I meant something like this - https://github.com/ethereum/solidity/tree/develop/test/cmdlineTests/ir_subobject_order |
6b6407c
to
fc21366
Compare
Duh, yeah, I forgot we can do that 😁 |
We have this section in the docs describing the storage layout and it mentions |
b18cf12
to
7660610
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of tests have syntax errors and we could use a tiny bit more detail in the docs.
I didn't find anything more serious and these are trivial to fix so you can consider it soft-approved.
docs/internals/layout_in_storage.rst
Outdated
@@ -6,6 +6,9 @@ Layout of State Variables in Storage | |||
|
|||
.. _storage-inplace-encoding: | |||
|
|||
.. note:: | |||
The rules described in this section apply for both storage and transient storage data locations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still add a few things:
- Maybe change title to "Layout of State Variables in Storage and Transient Storage"?
- Say that the layouts are completely independent, i.e. storage variables have zero effect on locations of transient storage variables and vice-versa. They can be safely interleaved.
- I'd mention that only value types are supported for transient storage.
- There's a note saying that we consider storage layout a part of the external interface. Is that still true for transient storage? If so, we should say that explicitly.
- Add an index entry for
transient storage
, next tostorage
above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, there's a note way below, saying:
The JSON output format of a contract's storage layout is still considered experimental and is subject to change in non-breaking releases of Solidity.
I don't think it's true any more. When we once broke the layout accidentally in 0.8.x cycle, we considered it very breaking and released a new version a few days later to correct it.
Given that, I wonder if we should now say that about transient storage or is it automatically considered stable by the virtue of being tied to storage layout?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have applied all suggestions, but not sure about the notes regarding external interface.
What do you think @ekpyron?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH this is already wrong on develop
so it's fine to merge the PR regardless and then fix it separately in a smaller doc-only PR.
test/cmdlineTests/transient_storage_layout_smoke_two_contracts/input.json
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_value_types_interleaved_with_storage/output.json
Outdated
Show resolved
Hide resolved
test/cmdlineTests/transient_storage_layout_value_types_packed/in.sol
Outdated
Show resolved
Hide resolved
I generally agree in that we don't have to create a separate section and just repeat everything verbatim, but I think we could use a tiny bit more mentions than just a single note :) See #15247 (comment) |
7660610
to
6ee0b7f
Compare
Gonna need to squash the commits - still got some fixups in there. |
7d13d99
to
c5685e7
Compare
No description provided.