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

fix: checksum cache race condition #42

Merged
merged 3 commits into from
Dec 30, 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
13 changes: 12 additions & 1 deletion evmspec/data/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def _decode_hook(cls, typ: Type["Address"], obj: str):
return cls.checksum(obj)

@classmethod
@ttl_cache(maxsize=None, ttl=600)
def checksum(cls, address: str) -> Self:
"""Returns the checksummed version of the address.

Expand All @@ -111,6 +110,18 @@ def checksum(cls, address: str) -> Self:
See Also:
- `cchecksum.to_checksum_address`: Function used for checksum conversion.
"""
# there is a race condition in the cache expire function due to
# my hacky removal of the threading.Lock from ttl_cache, but
# the race condition can be ignored for our use-case
while True:
try:
return cls.__checksum(address)
except KeyError:
pass

@classmethod
@ttl_cache(maxsize=None, ttl=600)
def __checksum(cls, address: str) -> Self:
return cls(address)


Expand Down
10 changes: 1 addition & 9 deletions evmspec/structs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,7 @@ def as_address(self) -> Address:
f"This {type(self).__name__} does not represent an address", self
)

# there is a race condition in the cache expire function due to
# my hacky removal of the threading.Lock from ttl_cache, but
# the race condition can be ignored for our use-case
# TODO move this fix somewhere more appropriate
while True:
try:
return Address.checksum(self[-20:].hex())
except KeyError:
pass
return Address.checksum(self[-20:].hex())

@property
def as_uint8(self) -> uints.uint8:
Expand Down
Loading