From 72a91b901deac2628398555be90887f53f624df2 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Tue, 26 Nov 2024 04:13:14 +0000 Subject: [PATCH] feat: shrink memory size for checksum cache --- y/convert.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/y/convert.py b/y/convert.py index 898f197d5..e9a1f96d8 100644 --- a/y/convert.py +++ b/y/convert.py @@ -1,4 +1,5 @@ from functools import lru_cache +from typing import Set import brownie.convert import checksum_dict._key @@ -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))