Skip to content

Commit

Permalink
Merge branch 'develop' into enhancement/SKALE-2106-skale.py-high-memo…
Browse files Browse the repository at this point in the history
…ry-usage
  • Loading branch information
badrogger committed Feb 25, 2020
2 parents 331dd10 + c64ceca commit b22b7c1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"bumpversion==0.5.3",
"pytest==5.3.5",
"click==7.0",
"twine==1.12.1",
"twine==3.1.1",
"mock==4.0.1",
"when-changed",
"Random-Word==1.0.4",
"pytest-cov==2.8.1"
],
'hw-wallet': [
"ledgerblue==0.1.29"
"ledgerblue==0.1.31"
]
}

Expand All @@ -39,7 +39,7 @@
url='https://github.com/skalenetwork/skale.py',
include_package_data=True,
install_requires=[
"web3==5.2.2",
"web3==5.5.1",
"asyncio==3.4.3",
"pyyaml==5.1.2",
"sgx.py>=0.4dev3",
Expand Down
4 changes: 2 additions & 2 deletions skale/manager_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def spawn_skale_lib(skale):


class Skale:
def __init__(self, endpoint, abi_filepath, wallet=None):
def __init__(self, endpoint, abi_filepath, wallet=None, provider_timeout=30):
logger.info(f'Init skale-py, connecting to {endpoint}')
provider = get_provider(endpoint)
provider = get_provider(endpoint, timeout=provider_timeout)
self._abi_filepath = abi_filepath
self._endpoint = endpoint
self.web3 = Web3(provider)
Expand Down
14 changes: 11 additions & 3 deletions skale/utils/web3_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ class TransactionFailedError(Exception):
pass


def get_provider(endpoint):
WS_MAX_MESSAGE_DATA_BYTES = 5 * 1024 * 1024


def get_provider(endpoint, timeout=10, request_kwargs={}):
scheme = urlparse(endpoint).scheme
if scheme == 'ws' or scheme == 'wss':
return WebsocketProvider(endpoint)
kwargs = request_kwargs or {'max_size': WS_MAX_MESSAGE_DATA_BYTES}
return WebsocketProvider(endpoint, websocket_timeout=timeout,
websocket_kwargs=kwargs)

if scheme == 'http' or scheme == 'https':
return HTTPProvider(endpoint)
kwargs = {'timeout': timeout, **request_kwargs}
return HTTPProvider(endpoint, request_kwargs=kwargs)

raise Exception(
'Wrong endpoint option.'
'Supported endpoint schemes: http/https/ws/wss'
Expand Down
2 changes: 2 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
N_TEST_WALLETS = 2

ENDPOINT = os.environ['ENDPOINT']


TEST_ABI_FILEPATH = os.path.join(DIR_PATH, os.pardir, 'test_abi.json')
ETH_PRIVATE_KEY = os.environ['ETH_PRIVATE_KEY']

Expand Down
17 changes: 10 additions & 7 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def test_lib_init():
web3 = init_web3(ENDPOINT)
wallet = Web3Wallet(ETH_PRIVATE_KEY, web3)
skale = Skale(ENDPOINT, TEST_ABI_FILEPATH, wallet)
skale = Skale(ENDPOINT, TEST_ABI_FILEPATH, wallet, provider_timeout=20)

lib_contracts = skale._Skale__contracts
assert len(lib_contracts) == len(CONTRACTS_INFO)
Expand All @@ -26,15 +26,18 @@ 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 skale.web3.provider._request_kwargs == {'timeout': 20}

provider = skale.web3.provider
assert isinstance(provider, WebsocketProvider) or isinstance(provider, HTTPProvider)
isinstance(skale.web3.provider, HTTPProvider)

http_endpoint = 'http://localhost:8080'
ws_endpoint = 'ws://localhost:8080'
with mock.patch.object(Skale, '_Skale__init_contracts'):
skale = Skale(http_endpoint, TEST_ABI_FILEPATH, wallet)
provider = skale.web3.provider
assert isinstance(provider, HTTPProvider)
skale = Skale(ws_endpoint, TEST_ABI_FILEPATH, wallet)
assert skale.web3.provider.websocket_timeout == 30
assert skale.web3.provider.conn.websocket_kwargs == {
'max_size': 5 * 1024 * 1024
}
assert isinstance(skale.web3.provider, WebsocketProvider)

file_endpoint = 'file://local_file:1001'
with pytest.raises(Exception):
Expand Down

0 comments on commit b22b7c1

Please sign in to comment.