Skip to content

Commit 2c0abb9

Browse files
committed
fix(base_types): Storage methods
1 parent f1bf042 commit 2c0abb9

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/ethereum_test_base_types/composite_types.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
Base composite types for Ethereum test cases.
33
"""
44
from dataclasses import dataclass
5-
from itertools import count
6-
from typing import Any, ClassVar, Dict, Iterator, SupportsBytes, Type, TypeAlias
5+
from typing import Any, ClassVar, Dict, SupportsBytes, Type, TypeAlias
76

8-
from pydantic import Field, RootModel, TypeAdapter
7+
from pydantic import Field, PrivateAttr, RootModel, TypeAdapter
98

109
from .base_types import Address, Bytes, Hash, HashInt, HexNumber, ZeroPaddedHexNumber
1110
from .conversions import BytesConvertible, NumberConvertible
@@ -24,7 +23,7 @@ class Storage(RootModel[Dict[StorageKeyValueType, StorageKeyValueType]]):
2423

2524
root: Dict[StorageKeyValueType, StorageKeyValueType] = Field(default_factory=dict)
2625

27-
_current_slot: Iterator[int] = count(0)
26+
_current_slot: int = PrivateAttr(0)
2827

2928
StorageDictType: ClassVar[TypeAlias] = Dict[
3029
str | int | bytes | SupportsBytes, str | int | bytes | SupportsBytes
@@ -161,10 +160,23 @@ def __bool__(self) -> bool:
161160
"""Returns True if the storage is not empty"""
162161
return any(v for v in self.root.values())
163162

163+
def __add__(self, other: "Storage") -> "Storage":
164+
"""
165+
Returns a new storage that is the sum of two storages.
166+
"""
167+
return Storage({**self.root, **other.root})
168+
164169
def keys(self) -> set[StorageKeyValueType]:
165170
"""Returns the keys of the storage"""
166171
return set(self.root.keys())
167172

173+
def set_next_slot(self, slot: int) -> "Storage":
174+
"""
175+
Sets the next slot to be used by `store_next`.
176+
"""
177+
self._current_slot = slot
178+
return self
179+
168180
def store_next(
169181
self, value: StorageKeyValueTypeConvertible | StorageKeyValueType | bool
170182
) -> StorageKeyValueType:
@@ -174,10 +186,17 @@ def store_next(
174186
Increments the key counter so the next time this function is called,
175187
the next key is used.
176188
"""
177-
slot = StorageKeyValueTypeAdapter.validate_python(next(self._current_slot))
189+
slot = StorageKeyValueTypeAdapter.validate_python(self._current_slot)
190+
self._current_slot += 1
178191
self[slot] = StorageKeyValueTypeAdapter.validate_python(value)
179192
return slot
180193

194+
def peek_slot(self) -> int:
195+
"""
196+
Peeks the next slot that will be used by `store_next`.
197+
"""
198+
return self._current_slot
199+
181200
def contains(self, other: "Storage") -> bool:
182201
"""
183202
Returns True if self contains all keys with equal value as

0 commit comments

Comments
 (0)