Skip to content
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

VM, Common: Implement EIP 1153, Transient storage opcodes #1768

Merged
merged 16 commits into from
Mar 25, 2022

Conversation

tynes
Copy link
Contributor

@tynes tynes commented Mar 7, 2022

This is a draft PR that implements EIP 1153. I'm opening it to get feedback on its structure, to make sure its going in the correct direction and to see if its something that is desired to be upstreamed.

Its only slightly tested and there is a problem where the opcodes for simple subroutines clash with the opcodes used in our forks of geth and solc. This must be fixed before merging is considered

Ideally if this was merged and [1] is implemented in hardhat, then users will be able to very easily develop EIP 1153 based smart contracts in hardhat

[1] - NomicFoundation/hardhat#2463

If it is something desired, I will clean up the commits and add more tests

@ryanio
Copy link
Contributor

ryanio commented Mar 7, 2022

pretty cool, thanks for taking the initiative! looks like a pretty clean implementation so far on first look :)

@jochem-brouwer jochem-brouwer changed the title feat: eip1153 VM: Implement EIP 1153, Transient storage opcodes Mar 7, 2022
{
// These numbers differ than what is currently implemented
// in geth due to other eips being included here that are
// not in geth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the EIP also does not yet state whatever opcodes numbers we should use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now have some numbers that we can use: ethereum/EIPs@b6b3857

Copy link
Member

@jochem-brouwer jochem-brouwer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start so far! Some comments. The state manager tests are great, but we should also add tests for the VM (so running the TLOAD / TSTORE opcodes).

@tynes
Copy link
Contributor Author

tynes commented Mar 7, 2022

Great start so far! Some comments. The state manager tests are great, but we should also add tests for the VM (so running the TLOAD / TSTORE opcodes).

Thank you for the quick review! I'll address the comments when I have some time over the next couple of days

@tynes
Copy link
Contributor Author

tynes commented Mar 7, 2022

This pr also needs to take into account the debug logging that shows what values are being stored/read still

@holgerd77
Copy link
Member

Some context: Tweet from Tim Beiko on ACD from January 2022

@codecov
Copy link

codecov bot commented Mar 9, 2022

Codecov Report

Merging #1768 (6856771) into master (7330c4a) will decrease coverage by 0.10%.
The diff coverage is n/a.

Impacted file tree graph

Flag Coverage Δ
block 85.57% <ø> (ø)
blockchain 83.82% <ø> (ø)
client 75.82% <ø> (ø)
common 94.19% <ø> (ø)
devp2p 82.49% <ø> (-0.27%) ⬇️
ethash ?
trie 80.02% <ø> (-0.63%) ⬇️
tx 88.20% <ø> (ø)
util 92.62% <ø> (ø)
vm ?

Flags with carried forward coverage won't be shown. Click here to find out more.

@ryanio
Copy link
Contributor

ryanio commented Mar 9, 2022

looks like coverage is down a lot, let's make sure we have all new lines covered with this PR as to not affect overall VM code coverage. probably just need a few more test cases to test the uncovered lines.

@tynes
Copy link
Contributor Author

tynes commented Mar 12, 2022

looks like coverage is down a lot, let's make sure we have all new lines covered with this PR as to not affect overall VM code coverage. probably just need a few more test cases to test the uncovered lines.

Working on adding more coverage still, will squash my commits once I get some more coverage

@tynes tynes force-pushed the eip1153 branch 2 times, most recently from cfeb752 to e66da59 Compare March 13, 2022 17:58
@tynes tynes marked this pull request as ready for review March 13, 2022 18:02
@tynes
Copy link
Contributor Author

tynes commented Mar 13, 2022

looks like coverage is down a lot, let's make sure we have all new lines covered with this PR as to not affect overall VM code coverage. probably just need a few more test cases to test the uncovered lines.

Based on https://app.codecov.io/gh/ethereumjs/ethereumjs-monorepo/compare/1768/overview it looks like codecov is still working off of older code. Is there any way to refresh codecov? I've just been running the coverage locally to be able to add more coverage

@tynes
Copy link
Contributor Author

tynes commented Mar 13, 2022

code-cov

There are a couple branches inside the transient storage class that aren't covered, they are when optional chaining is used to make typescript remove the "object may be undefined" error in cases where it is known that the object will not be undefined. 100% of lines are covered

@tynes
Copy link
Contributor Author

tynes commented Mar 18, 2022

We pushed a new approach and added some additional test coverage, would love to hear what you think of the new approach. Now there are no changes to the state manager and the changes are encapsulated in the eei

if (!changeset) {
throw new Error('cannot revert without a changeset')
}
const reversed = changeset.slice().reverse()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The slice copies the array, right? And the reversal seems like it can be optimized, for each key we would only need the first entry in the changeset? As that is the original value which we should revert to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs, slice creates a shallow copy
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
I can refactor this to use a simple for loop that decrements

You need to iterate through the changeset and apply each diff. Perhaps some better naming could help here.

As things are added to transient storage, a TransientStorageModification is pushed to the latest changeset array. On a commit, the latest changeset is discarded and on a revert the latest changeset is used to set each value that was changed to its previous value and then discarded

export type Changeset = TransientStorageModification[]

You only need to touch the latest changeset and all items in that changeset.

Copy link
Contributor

@moodysalem moodysalem Mar 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the array of modifications could be optimized to be a map, since we only care about the first previous value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i addressed this in aa79dcd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean, thanks for the commit @moodysalem

@holgerd77 holgerd77 changed the title VM: Implement EIP 1153, Transient storage opcodes VM, Common: Implement EIP 1153, Transient storage opcodes Mar 25, 2022
Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for the CI update! I will now merge this in then. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants