Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Transaction submission unhandled exception #399

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/web3py/extensions/tx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

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

from src import variables, constants
from src.metrics.prometheus.basic import TRANSACTIONS_COUNT, Status
from src.utils.input import prompt


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -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)
vgorkavenko marked this conversation as resolved.
Show resolved Hide resolved
except TimeExhausted:
TRANSACTIONS_COUNT.labels(status=Status.FAILURE).inc()
logger.warning({"msg": "Transaction was not found in blockchain after 120 seconds."})
return None
Expand Down
23 changes: 22 additions & 1 deletion tests/web3_extentions/test_tx_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -100,3 +101,23 @@ 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')
Loading