Skip to content

Commit

Permalink
Merge pull request #107 from skalenetwork/enhancement/SKALE-2106-skal…
Browse files Browse the repository at this point in the history
…e.py-high-memory-usage

Remove redundant data saving from skale object
  • Loading branch information
dmytrotkk authored Feb 25, 2020
2 parents c64ceca + b22b7c1 commit 50ee175
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
7 changes: 2 additions & 5 deletions skale/contracts/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def transaction_method(transaction):
@wraps(transaction_method)
def wrapper(self, *args, wait_for=False, timeout=4, blocks_to_wait=50, retries=1, **kwargs):
for retry in range(retries):
logging.info(
logger.info(
f'transaction_method: {transaction.__name__}, try {retry+1}/{retries}, '
f'wallet: {self.skale.wallet.__class__.__name__}, '
f'sender: {self.skale.wallet.address}'
Expand All @@ -61,9 +61,6 @@ def wrapper(self, *args, wait_for=False, timeout=4, blocks_to_wait=50, retries=1
class BaseContract:
def __init__(self, skale, name, address, abi):
self.skale = skale
self.web3 = skale.web3
self.name = name
self.address = Web3.toChecksumAddress(address)
self.abi = abi
self.contract = self.web3.eth.contract(
address=self.address, abi=self.abi)
self.contract = skale.web3.eth.contract(address=self.address, abi=abi)
7 changes: 7 additions & 0 deletions skale/contracts_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@
ContractInfo('token_state', 'TokenState', contracts.TokenState,
ContractTypes.API, False)
]


def get_contracts_info():
contracts_info = {}
for contract_info in CONTRACTS_INFO:
contracts_info[contract_info.name] = contract_info
return contracts_info
42 changes: 16 additions & 26 deletions skale/manager_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import skale.contracts as contracts
from skale.wallets import BaseWallet
from skale.contracts_info import CONTRACTS_INFO
from skale.contracts_info import get_contracts_info
from skale.utils.helper import get_abi
from skale.utils.web3_utils import get_provider
from skale.utils.exceptions import InvalidWalletError, EmptyWalletError
Expand All @@ -48,14 +48,11 @@ def __init__(self, endpoint, abi_filepath, wallet=None, provider_timeout=30):
self._endpoint = endpoint
self.web3 = Web3(provider)
self.web3.middleware_onion.inject(geth_poa_middleware, layer=0) # todo: may cause issues
self.abi = get_abi(abi_filepath)
self.__contracts = {}
self.__contracts_info = {}
self.nonces = {}
if wallet:
self.wallet = wallet
self.__init_contracts_info()
self.__init_contracts()
self.__init_contracts(get_abi(abi_filepath), get_contracts_info())

@property
def gas_price(self):
Expand All @@ -75,35 +72,28 @@ def wallet(self, wallet):
raise InvalidWalletError(f'Wrong wallet class: {type(wallet).__name__}. \
Must be one of the BaseWallet subclasses')

def __init_contracts_info(self):
for contract_info in CONTRACTS_INFO:
self.__add_contract_info(contract_info)

def __add_contract_info(self, contract_info):
self.__contracts_info[contract_info.name] = contract_info

def __init_contracts(self):
self.add_lib_contract('contract_manager', contracts.ContractManager)
for name in self.__contracts_info:
info = self.__contracts_info[name]
def __init_contracts(self, abi, contracts_info):
self.add_lib_contract('contract_manager', contracts.ContractManager, abi)
for name in contracts_info:
info = contracts_info[name]
if info.upgradeable:
self.init_upgradeable_contract(info)
self.init_upgradeable_contract(info, abi)
else:
self.add_lib_contract(info.name, info.contract_class)
self.add_lib_contract(info.name, info.contract_class, abi)

def init_upgradeable_contract(self, contract_info):
def init_upgradeable_contract(self, contract_info, abi):
address = self.get_contract_address(contract_info.contract_name)
self.add_lib_contract(contract_info.name, contract_info.contract_class,
address)
abi, address)

def add_lib_contract(self, name, contract_class, contract_address=None):
address = contract_address or self.get_contract_address_by_name(
self.abi, name)
def add_lib_contract(self, name, contract_class, abi, contract_address=None):
address = contract_address or self.get_contract_address_by_name(abi, name)
logger.info(f'Initialized: {name} at {address}')
abi = self.get_contract_abi_by_name(self.abi, name)
if name == 'dkg': # todo: tmp fix
abi = self.get_contract_abi_by_name(self.abi, 'd_k_g')
self.add_contract(name, contract_class(self, name, address, abi))
contract_abi = self.get_contract_abi_by_name(abi, 'd_k_g')
else:
contract_abi = self.get_contract_abi_by_name(abi, name)
self.add_contract(name, contract_class(self, name, address, contract_abi))

def get_contract_address_by_name(self, abi, name):
if name == 'dkg': # todo: tmp fix
Expand Down
8 changes: 0 additions & 8 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from skale.contracts import BaseContract
from skale.contracts.functionality.nodes import Nodes
from skale.contracts_info import CONTRACTS_INFO
from skale.utils.contract_info import ContractInfo
from tests.constants import TEST_CONTRACT_NAME, ENDPOINT, TEST_ABI_FILEPATH, ETH_PRIVATE_KEY


Expand All @@ -19,10 +18,6 @@ def test_lib_init():
wallet = Web3Wallet(ETH_PRIVATE_KEY, web3)
skale = Skale(ENDPOINT, TEST_ABI_FILEPATH, wallet, provider_timeout=20)

lib_contracts_info = skale._Skale__contracts_info
for contract_info in CONTRACTS_INFO:
assert isinstance(lib_contracts_info[contract_info.name], ContractInfo)

lib_contracts = skale._Skale__contracts
assert len(lib_contracts) == len(CONTRACTS_INFO)

Expand All @@ -31,11 +26,8 @@ def test_lib_init():
assert lib_contract.address is not None
assert int(lib_contract.address, 16) != 0
assert web3.eth.getCode(lib_contract.address)
assert lib_contract.abi is not None
assert skale.web3.provider._request_kwargs == {'timeout': 20}

assert skale.abi is not None

isinstance(skale.web3.provider, HTTPProvider)

ws_endpoint = 'ws://localhost:8080'
Expand Down

0 comments on commit 50ee175

Please sign in to comment.