-
Notifications
You must be signed in to change notification settings - Fork 176
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
[EVM] Adding account/slot/code iterators to the base storage #6555
Open
ramtinms
wants to merge
7
commits into
master
Choose a base branch
from
ramtin/evm-add-iterators-to-base-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+548
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6016c9
add iterators to the base storage
ramtinms 7a62d50
apply PR feedback
ramtinms f1b8c6f
improve code iterator
ramtinms 2c61c26
improve storage iterator
ramtinms 31293cf
test improvement
ramtinms d9c783c
Merge branch 'master' into ramtin/evm-add-iterators-to-base-storage
ramtinms 9696e41
add exporter
ramtinms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -302,6 +302,164 @@ func TestBaseView(t *testing.T) { | |||||||||||||
require.Equal(t, uint64(1), view.NumberOfAccounts()) | ||||||||||||||
}) | ||||||||||||||
|
||||||||||||||
t.Run("test account iterator", func(t *testing.T) { | ||||||||||||||
ledger := testutils.GetSimpleValueStore() | ||||||||||||||
rootAddr := flow.Address{1, 2, 3, 4, 5, 6, 7, 8} | ||||||||||||||
view, err := state.NewBaseView(ledger, rootAddr) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
accountCounts := 10 | ||||||||||||||
nonces := make(map[gethCommon.Address]uint64) | ||||||||||||||
balances := make(map[gethCommon.Address]*uint256.Int) | ||||||||||||||
codeHashes := make(map[gethCommon.Address]gethCommon.Hash) | ||||||||||||||
for i := 0; i < accountCounts; i++ { | ||||||||||||||
addr := testutils.RandomCommonAddress(t) | ||||||||||||||
balance := testutils.RandomUint256Int(1000) | ||||||||||||||
nonce := testutils.RandomBigInt(1000).Uint64() | ||||||||||||||
code := testutils.RandomData(t) | ||||||||||||||
codeHash := testutils.RandomCommonHash(t) | ||||||||||||||
|
||||||||||||||
err = view.CreateAccount(addr, balance, nonce, code, codeHash) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
nonces[addr] = nonce | ||||||||||||||
balances[addr] = balance | ||||||||||||||
codeHashes[addr] = codeHash | ||||||||||||||
} | ||||||||||||||
err = view.Commit() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
ai, err := view.AccountIterator() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
counter := 0 | ||||||||||||||
for { | ||||||||||||||
acc, err := ai.Next() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
if acc == nil { | ||||||||||||||
break | ||||||||||||||
} | ||||||||||||||
require.Equal(t, nonces[acc.Address], acc.Nonce) | ||||||||||||||
require.Equal(t, balances[acc.Address].Uint64(), acc.Balance.Uint64()) | ||||||||||||||
require.Equal(t, codeHashes[acc.Address], acc.CodeHash) | ||||||||||||||
counter += 1 | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
require.Equal(t, accountCounts, counter) | ||||||||||||||
}) | ||||||||||||||
|
||||||||||||||
t.Run("test code iterator", func(t *testing.T) { | ||||||||||||||
ledger := testutils.GetSimpleValueStore() | ||||||||||||||
rootAddr := flow.Address{1, 2, 3, 4, 5, 6, 7, 8} | ||||||||||||||
view, err := state.NewBaseView(ledger, rootAddr) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
codeCounts := 10 | ||||||||||||||
codeByCodeHash := make(map[gethCommon.Hash][]byte) | ||||||||||||||
refCountByCodeHash := make(map[gethCommon.Hash]uint64) | ||||||||||||||
for i := 0; i < codeCounts; i++ { | ||||||||||||||
|
||||||||||||||
code := testutils.RandomData(t) | ||||||||||||||
codeHash := testutils.RandomCommonHash(t) | ||||||||||||||
refCount := 0 | ||||||||||||||
// we add each code couple of times through different accounts | ||||||||||||||
for j := 1; j <= i+1; j++ { | ||||||||||||||
addr := testutils.RandomCommonAddress(t) | ||||||||||||||
balance := testutils.RandomUint256Int(1000) | ||||||||||||||
nonce := testutils.RandomBigInt(1000).Uint64() | ||||||||||||||
err = view.CreateAccount(addr, balance, nonce, code, codeHash) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
refCount += 1 | ||||||||||||||
} | ||||||||||||||
codeByCodeHash[codeHash] = code | ||||||||||||||
refCountByCodeHash[codeHash] = uint64(refCount) | ||||||||||||||
} | ||||||||||||||
err = view.Commit() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
ci, err := view.CodeIterator() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
counter := 0 | ||||||||||||||
for { | ||||||||||||||
cic, err := ci.Next() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
if cic == nil { | ||||||||||||||
break | ||||||||||||||
} | ||||||||||||||
require.Equal(t, codeByCodeHash[cic.Hash], cic.Code) | ||||||||||||||
require.Equal(t, refCountByCodeHash[cic.Hash], cic.RefCounts) | ||||||||||||||
Comment on lines
+390
to
+391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
counter += 1 | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
require.Equal(t, codeCounts, counter) | ||||||||||||||
}) | ||||||||||||||
|
||||||||||||||
t.Run("test account storage iterator", func(t *testing.T) { | ||||||||||||||
ledger := testutils.GetSimpleValueStore() | ||||||||||||||
rootAddr := flow.Address{1, 2, 3, 4, 5, 6, 7, 8} | ||||||||||||||
view, err := state.NewBaseView(ledger, rootAddr) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
addr := testutils.RandomCommonAddress(t) | ||||||||||||||
code := []byte("code") | ||||||||||||||
balance := testutils.RandomUint256Int(1000) | ||||||||||||||
nonce := testutils.RandomBigInt(1000).Uint64() | ||||||||||||||
codeHash := gethCrypto.Keccak256Hash(code) | ||||||||||||||
err = view.CreateAccount(addr, balance, nonce, code, codeHash) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
slotCounts := 10 | ||||||||||||||
values := make(map[gethCommon.Hash]gethCommon.Hash) | ||||||||||||||
|
||||||||||||||
for i := 0; i < slotCounts; i++ { | ||||||||||||||
key := testutils.RandomCommonHash(t) | ||||||||||||||
value := testutils.RandomCommonHash(t) | ||||||||||||||
|
||||||||||||||
err = view.UpdateSlot( | ||||||||||||||
types.SlotAddress{ | ||||||||||||||
Address: addr, | ||||||||||||||
Key: key, | ||||||||||||||
}, value) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
values[key] = value | ||||||||||||||
} | ||||||||||||||
err = view.Commit() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
asi, err := view.AccountStorageIterator(addr) | ||||||||||||||
ramtinms marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
counter := 0 | ||||||||||||||
for { | ||||||||||||||
slot, err := asi.Next() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
if slot == nil { | ||||||||||||||
break | ||||||||||||||
} | ||||||||||||||
require.Equal(t, addr, slot.Address) | ||||||||||||||
require.Equal(t, values[slot.Key], slot.Value) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||||||||||||||
counter += 1 | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
require.Equal(t, slotCounts, counter) | ||||||||||||||
|
||||||||||||||
// test non existing address | ||||||||||||||
addr2 := testutils.RandomCommonAddress(t) | ||||||||||||||
_, err = view.AccountStorageIterator(addr2) | ||||||||||||||
require.Error(t, err) | ||||||||||||||
|
||||||||||||||
// test address without storage | ||||||||||||||
err = view.CreateAccount(addr2, balance, nonce, code, codeHash) | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
err = view.Commit() | ||||||||||||||
require.NoError(t, err) | ||||||||||||||
|
||||||||||||||
_, err = view.AccountStorageIterator(addr2) | ||||||||||||||
require.Error(t, err) | ||||||||||||||
}) | ||||||||||||||
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func checkAccount(t *testing.T, | ||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The deletion ensures we see all unique account. Otherwise, returning the same account would also pass the tests.