Skip to content

Commit

Permalink
Make keccak and cheksum caches unlimited
Browse files Browse the repository at this point in the history
- Can be limited via environment variable
  • Loading branch information
Uxio0 committed Mar 14, 2024
1 parent 3761629 commit 6469435
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions gnosis/eth/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from functools import lru_cache
from typing import Union

Expand All @@ -21,15 +22,19 @@ def get_empty_tx_params() -> TxParams:
}


@lru_cache(maxsize=8192)
@lru_cache(maxsize=os.environ.get("CACHE_KECCAK", None))
def _keccak_256(value: bytes) -> bytes:
return keccak_256(value)


def fast_keccak(value: bytes) -> Hash32:
"""
Calculates ethereum keccak256 using fast library `pysha3`
:param value:
:return: Keccak256 used by ethereum as `HexBytes`
"""
return HexBytes(keccak_256(value).digest())
return HexBytes(_keccak_256(value).digest())


def fast_keccak_text(value: str) -> Hash32:
Expand All @@ -50,7 +55,7 @@ def fast_keccak_hex(value: bytes) -> HexStr:
:param value:
:return: Keccak256 used by ethereum as a hex string (not 0x prefixed)
"""
return HexStr(keccak_256(value).hexdigest())
return HexStr(_keccak_256(value).hexdigest())


def _build_checksum_address(
Expand Down Expand Up @@ -82,7 +87,7 @@ def _build_checksum_address(
)


@lru_cache(maxsize=8192)
@lru_cache(maxsize=os.environ.get("CACHE_CHECKSUM_ADDRESS", None))
def _fast_to_checksum_address(address: HexAddress):
address_hash = fast_keccak_hex(address.encode())
return _build_checksum_address(address, address_hash)
Expand Down

0 comments on commit 6469435

Please sign in to comment.