Skip to content

Commit 69519b2

Browse files
committed
fix(submit_api): improve transaction submission retries and error handling
- Remove use of contextlib.suppress for ReadTimeout, handle with try/except instead. - Increase post timeout from 60s to 300s for more robust network handling. - Refactor retry logic to properly handle "MempoolTxTooSlow" and ReadTimeout cases. - Improve error tracking and reporting in submit_tx, ensuring exceptions are captured and retried appropriately.
1 parent 45f3a8b commit 69519b2

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

cardano_node_tests/utils/submit_api.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Utilities for `cardano-submit-api` REST service."""
22

33
import binascii
4-
import contextlib
54
import dataclasses
65
import json
76
import logging
@@ -68,20 +67,15 @@ def post_cbor(*, cbor_file: clusterlib.FileType, url: str) -> requests.Response:
6867
if r > 1:
6968
LOGGER.warning(f"Resubmitting transaction to submit-api. Attempt {r}/{attempts}.")
7069

71-
response = None
72-
with contextlib.suppress(requests.exceptions.ReadTimeout):
73-
response = http_client.get_session().post(
74-
url, headers=headers, data=cbor_binary, timeout=60
75-
)
76-
77-
if response is not None and not response and "MempoolTxTooSlow" in response.text:
78-
# Repeat the request as the transaction didn't make it to mempool
79-
pass
80-
elif response is not None:
81-
break
70+
response = http_client.get_session().post(
71+
url, headers=headers, data=cbor_binary, timeout=300
72+
)
8273

83-
if r < attempts:
74+
if r < attempts and not response and "MempoolTxTooSlow" in response.text:
8475
time.sleep(random.uniform(0, r))
76+
continue
77+
78+
break
8579
else:
8680
err = f"Failed to submit the tx after {attempts} attempts."
8781
raise SubmitApiError(err)
@@ -132,20 +126,22 @@ def submit_tx(
132126
wait_blocks: A number of new blocks to wait for (default = 2).
133127
"""
134128
txid = ""
135-
err = None
129+
err: Exception | None = None
136130
attempts = 20
137131
for r in range(1, attempts + 1):
138132
if r == 1:
139-
txid = submit_tx_bare(tx_file=tx_file).txid
133+
try:
134+
txid = submit_tx_bare(tx_file=tx_file).txid
135+
except requests.exceptions.ReadTimeout as exc:
136+
err = exc
140137
else:
141-
if not txid:
142-
msg = "The TxId is not known."
143-
raise SubmitApiError(msg)
138+
txid = txid or cluster_obj.g_transaction.get_txid(tx_file=tx_file)
144139
LOGGER.warning(
145140
f"Resubmitting transaction '{txid}' (from '{tx_file}'). Attempt {r}/{attempts}."
146141
)
147142
try:
148143
submit_tx_bare(tx_file=tx_file)
144+
err = None
149145
except SubmitApiError as exc:
150146
# Check if resubmitting failed because an input UTxO was already spent
151147
exc_str = str(exc)
@@ -157,6 +153,10 @@ def submit_tx(
157153
raise
158154
err = exc
159155
# If here, the TX is likely still in mempool and we need to wait
156+
except requests.exceptions.ReadTimeout as exc:
157+
# If here, the TX might have been successfully submitted, but
158+
# the timeout occurred before we got response.
159+
err = exc
160160

161161
cluster_obj.wait_for_new_block(wait_blocks)
162162

0 commit comments

Comments
 (0)