Skip to content

Commit e512062

Browse files
committed
fix(utils/submit_api): improve resubmission logging and error messages
- Add attempt counters to resubmission log messages for better traceability. - Use named `attempts` variables instead of hardcoded values for clarity. - Update error messages to include the number of attempts made before failure. - Refactor loop indices for consistency and readability in `post_cbor` and `submit_tx`.
1 parent 303c29b commit e512062

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

cardano_node_tests/utils/submit_api.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ def post_cbor(*, cbor_file: clusterlib.FileType, url: str) -> requests.Response:
6262
with open(cbor_file, "rb") as in_fp:
6363
cbor_binary = in_fp.read()
6464

65-
i = 0
66-
for i in range(1, 6):
67-
if i > 1:
68-
LOGGER.warning("Resubmitting transaction to submit-api.")
65+
r = 0
66+
attempts = 6
67+
for r in range(1, attempts + 1):
68+
if r > 1:
69+
LOGGER.warning(f"Resubmitting transaction to submit-api. Attempt {r}/{attempts}.")
6970

7071
response = None
7172
with contextlib.suppress(requests.exceptions.ReadTimeout):
@@ -81,7 +82,7 @@ def post_cbor(*, cbor_file: clusterlib.FileType, url: str) -> requests.Response:
8182

8283
time.sleep(random.random())
8384
else:
84-
err = f"Failed to submit the tx after {i} attempts."
85+
err = f"Failed to submit the tx after {attempts} attempts."
8586
raise SubmitApiError(err)
8687

8788
return response
@@ -131,16 +132,17 @@ def submit_tx(
131132
"""
132133
txid = ""
133134
err = None
134-
for r in range(20):
135-
err = None
136-
137-
if r == 0:
135+
attempts = 20
136+
for r in range(1, attempts + 1):
137+
if r == 1:
138138
txid = submit_tx_bare(tx_file=tx_file).txid
139139
else:
140140
if not txid:
141141
msg = "The TxId is not known."
142142
raise SubmitApiError(msg)
143-
LOGGER.warning(f"Resubmitting transaction '{txid}' (from '{tx_file}').")
143+
LOGGER.warning(
144+
f"Resubmitting transaction '{txid}' (from '{tx_file}'). Attempt {r}/{attempts}."
145+
)
144146
try:
145147
submit_tx_bare(tx_file=tx_file)
146148
except SubmitApiError as exc:
@@ -169,10 +171,16 @@ def submit_tx(
169171
if err is not None:
170172
# Submitting the TX raised an exception as if the input was already
171173
# spent, but it was either not the case, or the TX is still in mempool.
172-
msg = f"Failed to resubmit the transaction '{txid}' (from '{tx_file}')."
174+
msg = (
175+
f"Failed to resubmit the transaction '{txid}' "
176+
f"after {attempts} attempts (from '{tx_file}')."
177+
)
173178
raise SubmitApiError(msg) from err
174179

175-
msg = f"Transaction '{txid}' didn't make it to the chain (from '{tx_file}')."
180+
msg = (
181+
f"Transaction '{txid}' didn't make it to the chain "
182+
f"after {attempts} attempts (from '{tx_file}')."
183+
)
176184
raise SubmitApiError(msg)
177185

178186
# Create a `.submitted` status file when the Tx was successfully submitted

0 commit comments

Comments
 (0)