Skip to content

Commit

Permalink
Merge 42491cf into 8aaf21f
Browse files Browse the repository at this point in the history
  • Loading branch information
moisses89 committed Aug 12, 2022
2 parents 8aaf21f + 42491cf commit cb7403d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
16 changes: 7 additions & 9 deletions gnosis/safe/safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import math
from enum import Enum
from logging import getLogger
from os import environ
from typing import Callable, List, NamedTuple, Optional, Union

from cachetools import LRUCache, cached
from cachetools.keys import hashkey
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_typing import ChecksumAddress
Expand Down Expand Up @@ -41,14 +44,6 @@
from .safe_creation_tx import InvalidERC20Token, SafeCreationTx
from .safe_tx import SafeTx

try:
from functools import cache
except ImportError:
from functools import lru_cache

cache = lru_cache(maxsize=None)


logger = getLogger(__name__)


Expand Down Expand Up @@ -822,7 +817,10 @@ def estimate_tx_operational_gas(self, data_bytes_length: int):
threshold = self.retrieve_threshold()
return 15000 + data_bytes_length // 32 * 100 + 5000 * threshold

@cache
@cached(
cache=LRUCache(maxsize=int(environ.get("CONTRACT_CACHE_SIZE", 1024 * 100))),
key=lambda self: hashkey(self.address),
)
def get_contract(self) -> Contract:
v_1_3_0_contract = get_safe_V1_3_0_contract(self.w3, address=self.address)
version = v_1_3_0_contract.functions.VERSION().call()
Expand Down
20 changes: 20 additions & 0 deletions gnosis/safe/tests/test_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,23 @@ def test_send_previously_approved_tx(self):

balance = self.w3.eth.get_balance(to)
self.assertEqual(value, balance)

def test_cache_size_get_contracts(self):
safe_deployed = self.deploy_test_safe(
owners=[self.ethereum_test_account.address]
)
Safe.get_contract.cache.clear()
for i in range(0, 2):
safe = Safe(safe_deployed.address, self.ethereum_client)
safe.get_contract()
self.assertEqual(Safe.get_contract.cache.currsize, 1)

safe_deployed = self.deploy_test_safe(
owners=[self.ethereum_test_account.address]
)
for i in range(0, 2):
safe = Safe(safe_deployed.address, self.ethereum_client)
safe.get_contract()
self.assertEqual(Safe.get_contract.cache.currsize, 2)

self.assertNotEqual(Safe.get_contract.cache.maxsize, None)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cached-property==1.5.2
cachetools==5.2.0
django==3.2.14
django-filter==22.1
djangorestframework==3.13.1
Expand Down

0 comments on commit cb7403d

Please sign in to comment.