From b6d9171c9954a171f50eceae9aac7241f0313c70 Mon Sep 17 00:00:00 2001 From: F4ever <1590415904a@gmail.com> Date: Tue, 3 Oct 2023 18:14:40 +0200 Subject: [PATCH 1/2] fix: Unhandled exception in oracle transaction submission --- src/web3py/extensions/tx_utils.py | 11 ++++++++--- tests/web3_extentions/test_tx_utils.py | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/web3py/extensions/tx_utils.py b/src/web3py/extensions/tx_utils.py index 7932c8377..a14162a9b 100644 --- a/src/web3py/extensions/tx_utils.py +++ b/src/web3py/extensions/tx_utils.py @@ -3,8 +3,9 @@ from eth_account.signers.local import LocalAccount from web3 import Web3 +from hexbytes import HexBytes from web3.contract.contract import ContractFunction -from web3.exceptions import ContractLogicError +from web3.exceptions import ContractLogicError, TimeExhausted from web3.module import Module from web3.types import TxReceipt, Wei, TxParams, BlockData @@ -12,6 +13,7 @@ from src.metrics.prometheus.basic import TRANSACTIONS_COUNT, Status from src.utils.input import prompt + logger = logging.getLogger(__name__) @@ -118,9 +120,12 @@ def _sign_and_send_transaction( tx_hash = self.w3.eth.send_raw_transaction(signed_tx.rawTransaction) logger.info({"msg": "Transaction sent.", "value": tx_hash.hex()}) - tx_receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash) + return self._handle_sent_transaction(tx_hash) - if not tx_receipt: + def _handle_sent_transaction(self, transaction_hash: HexBytes) -> Optional[TxReceipt]: + try: + tx_receipt = self.w3.eth.wait_for_transaction_receipt(transaction_hash) + except TimeExhausted: TRANSACTIONS_COUNT.labels(status=Status.FAILURE).inc() logger.warning({"msg": "Transaction was not found in blockchain after 120 seconds."}) return None diff --git a/tests/web3_extentions/test_tx_utils.py b/tests/web3_extentions/test_tx_utils.py index 670f83d73..e8ff2a945 100644 --- a/tests/web3_extentions/test_tx_utils.py +++ b/tests/web3_extentions/test_tx_utils.py @@ -1,7 +1,8 @@ +from collections import defaultdict from unittest.mock import Mock import pytest -from web3.exceptions import ContractLogicError +from web3.exceptions import ContractLogicError, TimeExhausted from src import variables from src.constants import MAX_BLOCK_GAS_LIMIT @@ -100,3 +101,21 @@ def test_daemon_check_and_send_transaction(web3, tx_utils, tx, account, monkeypa web3.transaction._check_transaction = Mock(return_value=True) web3.transaction.check_and_send_transaction(tx, account) web3.transaction._sign_and_send_transaction.assert_not_called() + + +def test_find_transaction_timeout(web3, tx_utils, tx, account, monkeypatch): + web3.eth.wait_for_transaction_receipt = Mock(side_effect=TimeExhausted()) + + assert not web3.transaction._handle_sent_transaction('0x000001') + + web3.eth.wait_for_transaction_receipt = Mock(return_value={ + 'blockHash': b'', + 'blockNumber': '', + 'gasUsed': '', + 'effectiveGasPrice': '', + 'status': '', + 'transactionHash': b'', + 'transactionIndex': '', + }) + + assert web3.transaction._handle_sent_transaction('0x000001') From c5e81eb8c0a03bcf903295d2e27d9b37eb1643fe Mon Sep 17 00:00:00 2001 From: F4ever <1590415904a@gmail.com> Date: Tue, 3 Oct 2023 18:25:10 +0200 Subject: [PATCH 2/2] black formatting --- tests/web3_extentions/test_tx_utils.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/web3_extentions/test_tx_utils.py b/tests/web3_extentions/test_tx_utils.py index e8ff2a945..7b82a1909 100644 --- a/tests/web3_extentions/test_tx_utils.py +++ b/tests/web3_extentions/test_tx_utils.py @@ -108,14 +108,16 @@ def test_find_transaction_timeout(web3, tx_utils, tx, account, monkeypatch): assert not web3.transaction._handle_sent_transaction('0x000001') - web3.eth.wait_for_transaction_receipt = Mock(return_value={ - 'blockHash': b'', - 'blockNumber': '', - 'gasUsed': '', - 'effectiveGasPrice': '', - 'status': '', - 'transactionHash': b'', - 'transactionIndex': '', - }) + web3.eth.wait_for_transaction_receipt = Mock( + return_value={ + 'blockHash': b'', + 'blockNumber': '', + 'gasUsed': '', + 'effectiveGasPrice': '', + 'status': '', + 'transactionHash': b'', + 'transactionIndex': '', + } + ) assert web3.transaction._handle_sent_transaction('0x000001')