diff --git a/setup.py b/setup.py index 00e88663..8301a524 100644 --- a/setup.py +++ b/setup.py @@ -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" ] } @@ -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", diff --git a/skale/manager_client.py b/skale/manager_client.py index 443f9c0b..b2b62c3b 100644 --- a/skale/manager_client.py +++ b/skale/manager_client.py @@ -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) diff --git a/skale/utils/web3_utils.py b/skale/utils/web3_utils.py index ba51b139..39c78690 100644 --- a/skale/utils/web3_utils.py +++ b/skale/utils/web3_utils.py @@ -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' diff --git a/tests/constants.py b/tests/constants.py index 9ab00eb0..e04af0b0 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -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'] diff --git a/tests/main_test.py b/tests/main_test.py index da3a02fa..ba7c4552 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -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) @@ -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):