Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/pip/ledgerblue-0.1.31
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrotkk authored Feb 24, 2020
2 parents 05103e7 + b652b1f commit 9140f5a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
],
'dev': [
"bumpversion==0.5.3",
"pytest==5.2.1",
"pytest==5.3.5",
"click==7.0",
"twine==1.12.1",
"mock==3.0.5",
"twine==3.1.1",
"mock==4.0.1",
"when-changed",
"Random-Word==1.0.4",
"pytest-cov==2.8.1"
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 @@ -17,7 +17,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_info = skale._Skale__contracts_info
for contract_info in CONTRACTS_INFO:
Expand All @@ -32,17 +32,20 @@ def test_lib_init():
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

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 9140f5a

Please sign in to comment.