From 21968123e3d5230b3592203c5e79fba90e9c4373 Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 15 Jun 2022 14:30:20 -0600 Subject: [PATCH 1/2] Add black to web3's public utils --- tox.ini | 2 +- web3/utils/async_exception_handling.py | 63 ++++++++++++------------ web3/utils/exception_handling.py | 66 ++++++++++++++------------ 3 files changed, 70 insertions(+), 61 deletions(-) diff --git a/tox.ini b/tox.ini index 08cd9a0e7e..8da6ff5e69 100644 --- a/tox.ini +++ b/tox.ini @@ -64,7 +64,7 @@ basepython=python extras=linter commands= flake8 {toxinidir}/web3 {toxinidir}/ens {toxinidir}/ethpm {toxinidir}/tests --exclude {toxinidir}/ethpm/ethpm-spec - black {toxinidir}/ethpm --exclude {toxinidir}/ethpm/ethpm-spec --check + black {toxinidir}/ethpm {toxinidir}/web3/utils --exclude {toxinidir}/ethpm/ethpm-spec --check isort --recursive --check-only --diff {toxinidir}/web3/ {toxinidir}/ens/ {toxinidir}/ethpm/ {toxinidir}/tests/ mypy -p web3 -p ethpm -p ens --config-file {toxinidir}/mypy.ini diff --git a/web3/utils/async_exception_handling.py b/web3/utils/async_exception_handling.py index 28094d32c8..1782e87b01 100644 --- a/web3/utils/async_exception_handling.py +++ b/web3/utils/async_exception_handling.py @@ -32,61 +32,64 @@ async def async_handle_offchain_lookup( offchain_lookup_payload: Dict[str, Any], transaction: TxParams, ) -> bytes: - formatted_sender = to_hex_if_bytes(offchain_lookup_payload['sender']).lower() - formatted_data = to_hex_if_bytes(offchain_lookup_payload['callData']).lower() + formatted_sender = to_hex_if_bytes(offchain_lookup_payload["sender"]).lower() + formatted_data = to_hex_if_bytes(offchain_lookup_payload["callData"]).lower() - if formatted_sender != to_hex_if_bytes(transaction['to']).lower(): + if formatted_sender != to_hex_if_bytes(transaction["to"]).lower(): raise ValidationError( - 'Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does ' - 'not equal `to` address in transaction.' + "Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does " + "not equal `to` address in transaction." ) - for url in offchain_lookup_payload['urls']: + for url in offchain_lookup_payload["urls"]: formatted_url = URI( str(url) - .replace('{sender}', str(formatted_sender)) - .replace('{data}', str(formatted_data)) + .replace("{sender}", str(formatted_sender)) + .replace("{data}", str(formatted_data)) ) try: - if '{data}' in url and '{sender}' in url: + if "{data}" in url and "{sender}" in url: response = await async_get_response_from_get_request(formatted_url) - elif '{sender}' in url: - response = await async_get_response_from_post_request(formatted_url, data={ - "data": formatted_data, - "sender": formatted_sender - }) + elif "{sender}" in url: + response = await async_get_response_from_post_request( + formatted_url, + data={"data": formatted_data, "sender": formatted_sender}, + ) else: - raise ValidationError('url not formatted properly.') + raise ValidationError("url not formatted properly.") except Exception: continue # try next url if timeout or issues making the request - if 400 <= response.status <= 499: # if request returns 400 error, raise exception + if ( + 400 <= response.status <= 499 + ): # if request returns 400 error, raise exception response.raise_for_status() if not 200 <= response.status <= 299: # if not 400 error, try next url continue result = await async_get_json_from_client_response(response) - if 'data' not in result.keys(): + if "data" not in result.keys(): raise ValidationError( "Improperly formatted response for offchain lookup HTTP request - missing 'data' " "field." ) - encoded_data_with_function_selector = b''.join([ - # 4-byte callback function selector - to_bytes_if_hex(offchain_lookup_payload['callbackFunction']), - - # encode the `data` from the result and the `extraData` as bytes - encode_abi( - ['bytes', 'bytes'], - [ - to_bytes_if_hex(result['data']), - to_bytes_if_hex(offchain_lookup_payload['extraData']), - ] - ) - ]) + encoded_data_with_function_selector = b"".join( + [ + # 4-byte callback function selector + to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]), + # encode the `data` from the result and the `extraData` as bytes + encode_abi( + ["bytes", "bytes"], + [ + to_bytes_if_hex(result["data"]), + to_bytes_if_hex(offchain_lookup_payload["extraData"]), + ], + ), + ] + ) return encoded_data_with_function_selector raise MultipleFailedRequests("Offchain lookup failed for supplied urls.") diff --git a/web3/utils/exception_handling.py b/web3/utils/exception_handling.py index dddde1d5eb..6b72242469 100644 --- a/web3/utils/exception_handling.py +++ b/web3/utils/exception_handling.py @@ -31,61 +31,67 @@ def handle_offchain_lookup( offchain_lookup_payload: Dict[str, Any], transaction: TxParams, ) -> bytes: - formatted_sender = to_hex_if_bytes(offchain_lookup_payload['sender']).lower() - formatted_data = to_hex_if_bytes(offchain_lookup_payload['callData']).lower() + formatted_sender = to_hex_if_bytes(offchain_lookup_payload["sender"]).lower() + formatted_data = to_hex_if_bytes(offchain_lookup_payload["callData"]).lower() - if formatted_sender != to_hex_if_bytes(transaction['to']).lower(): + if formatted_sender != to_hex_if_bytes(transaction["to"]).lower(): raise ValidationError( - 'Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does ' - 'not equal `to` address in transaction.' + "Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does " + "not equal `to` address in transaction." ) - for url in offchain_lookup_payload['urls']: + for url in offchain_lookup_payload["urls"]: formatted_url = URI( str(url) - .replace('{sender}', str(formatted_sender)) - .replace('{data}', str(formatted_data)) + .replace("{sender}", str(formatted_sender)) + .replace("{data}", str(formatted_data)) ) try: - if '{data}' in url and '{sender}' in url: + if "{data}" in url and "{sender}" in url: response = get_response_from_get_request(formatted_url) - elif '{sender}' in url: - response = get_response_from_post_request(formatted_url, data={ - "data": formatted_data, - "sender": formatted_sender, - }) + elif "{sender}" in url: + response = get_response_from_post_request( + formatted_url, + data={ + "data": formatted_data, + "sender": formatted_sender, + }, + ) else: - raise ValidationError('url not formatted properly.') + raise ValidationError("url not formatted properly.") except Exception: continue # try next url if timeout or issues making the request - if 400 <= response.status_code <= 499: # if request returns 400 error, raise exception + if ( + 400 <= response.status_code <= 499 + ): # if request returns 400 error, raise exception response.raise_for_status() if not 200 <= response.status_code <= 299: # if not 400 error, try next url continue result = response.json() - if 'data' not in result.keys(): + if "data" not in result.keys(): raise ValidationError( "Improperly formatted response for offchain lookup HTTP request - missing 'data' " "field." ) - encoded_data_with_function_selector = b''.join([ - # 4-byte callback function selector - to_bytes_if_hex(offchain_lookup_payload['callbackFunction']), - - # encode the `data` from the result and the `extraData` as bytes - encode_abi( - ['bytes', 'bytes'], - [ - to_bytes_if_hex(result['data']), - to_bytes_if_hex(offchain_lookup_payload['extraData']), - ] - ) - ]) + encoded_data_with_function_selector = b"".join( + [ + # 4-byte callback function selector + to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]), + # encode the `data` from the result and the `extraData` as bytes + encode_abi( + ["bytes", "bytes"], + [ + to_bytes_if_hex(result["data"]), + to_bytes_if_hex(offchain_lookup_payload["extraData"]), + ], + ), + ] + ) return encoded_data_with_function_selector raise MultipleFailedRequests("Offchain lookup failed for supplied urls.") From 4a8d53a39bb9dcf482cc79c81a89018139663470 Mon Sep 17 00:00:00 2001 From: kclowes Date: Wed, 15 Jun 2022 14:44:45 -0600 Subject: [PATCH 2/2] Add newsfragment --- newsfragments/2513.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/2513.misc.rst diff --git a/newsfragments/2513.misc.rst b/newsfragments/2513.misc.rst new file mode 100644 index 0000000000..ed01581cd8 --- /dev/null +++ b/newsfragments/2513.misc.rst @@ -0,0 +1 @@ +Add black checks to ``web3/utils`` directory