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

tools(fix): Storage iterator #369

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Test fixtures for use by clients are available for each release on the [Github r
- 🔀 Rename test fixtures names to match the corresponding pytest node ID as generated using `fill` ([#342](https://github.com/ethereum/execution-spec-tests/pull/342)).
- 💥 Replace "=" with "_" in pytest node ids and test fixture names ([#342](https://github.com/ethereum/execution-spec-tests/pull/342)).
- ✨ Fork objects used to write tests can now be compared using the `>`, `>=`, `<`, `<=` operators, to check for a fork being newer than, newer than or equal, older than, older than or equal, respectively when compared against other fork ([#367](https://github.com/ethereum/execution-spec-tests/pull/367))
- 🐞 Storage type iterator is now fixed ([#369](https://github.com/ethereum/execution-spec-tests/pull/369))

### 🔧 EVM Tools

Expand Down
6 changes: 5 additions & 1 deletion src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def key_value_to_string(value: int) -> str:
hex_str = "0" + hex_str
return "0x" + hex_str

def __init__(self, input: StorageDictType = {}, start_slot: int = 0):
def __init__(self, input: StorageDictType | "Storage" = {}, start_slot: int = 0):
"""
Initializes the storage using a given mapping which can have
keys and values either as string or int.
Expand All @@ -420,6 +420,10 @@ def __len__(self) -> int:
"""Returns number of elements in the storage"""
return len(self.data)

def __iter__(self) -> Iterator[int]:
"""Returns iterator of the storage"""
return iter(self.data)

def __contains__(self, key: str | int | bytes) -> bool:
"""Checks for an item in the storage"""
key = Storage.parse_key_value(key)
Expand Down
4 changes: 4 additions & 0 deletions src/ethereum_test_tools/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def test_storage():
assert 10 in s.data
assert s.data[10] == 10

iter_s = iter(Storage({10: 20, "11": "21"}))
assert next(iter_s) == 10
assert next(iter_s) == 11

s["10"] = "0x10"
s["0x10"] = "10"
assert s.data[10] == 16
Expand Down