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

feat: shrink memory size for checksum cache #795

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
23 changes: 20 additions & 3 deletions y/convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import lru_cache
from typing import Set

import brownie.convert
import checksum_dict._key
Expand All @@ -14,12 +15,28 @@


@lru_cache(maxsize=ENVS.CHECKSUM_CACHE_MAXSIZE)
def to_address(address: AnyAddressType) -> ChecksumAddress:
if type(address) == int:
address = _int_to_address(address)
def checksum(address: AnyAddressType) -> ChecksumAddress:
return str(EthAddress(address))


def to_address(address: AnyAddressType) -> ChecksumAddress:
address = _int_to_address(address) if type(address) == int else str(address)
if address in _is_checksummed:
return address
elif address in _is_not_checksummed:
return checksum(address)
checksummed = str(EthAddress(address))
if address == checksummed:
_is_checksummed.add(address)
else:
_is_not_checksummed.add(address)
return checksummed


_is_checksummed: Set[HexAddress] = set()
_is_not_checksummed: Set[HexAddress] = set()


def _int_to_address(int_address: int) -> HexAddress:
hex_value = HexBytes(int_address).hex()[2:]
padding = "0" * (40 - len(hex_value))
Expand Down
Loading