Skip to content
Merged
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
39 changes: 30 additions & 9 deletions algosdk/future/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .. import error
from .. import logic
from .. import transaction
from ..v2client import algod
from nacl.signing import SigningKey, VerifyKey
from nacl.exceptions import BadSignatureError

Expand Down Expand Up @@ -3010,31 +3011,51 @@ def assign_group_id(txns, address=None):
return result


def wait_for_confirmation(algod_client, txid, wait_rounds=0, **kwargs):
def wait_for_confirmation(
algod_client: algod.AlgodClient, txid: str, wait_rounds: int = 0, **kwargs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the default value here instead of checking for 0 and overriding it.

A smaller default like 5 would probably be better default than 1000.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my attempt at maintaining backwards compatibility, since we already shipped a version with this function (unlike the other SDKs).

There might be existing code that passes 0 for wait_rounds and expects the function to wait indefinitely, so I though 1000 was the best replacement for it.

):
"""
Block until a pending transaction is confirmed by the network.

Args:
algod_client (algod.AlgodClient): Instance of the `algod` client
txid (str): transaction ID
wait_rounds (int, optional): The number of rounds to block for before
exiting with an Exception. If not supplied, there is no timeout.
exiting with an Exception. If not supplied, this will be 1000.
"""
last_round = algod_client.status()["last-round"]
current_round = last_round + 1

if wait_rounds == 0:
wait_rounds = 1000

while True:
# Check that the `wait_rounds` has not passed
if wait_rounds > 0 and current_round > last_round + wait_rounds:
if current_round > last_round + wait_rounds:
raise error.ConfirmationTimeoutError(
f"Wait for transaction id {txid} timed out"
"Wait for transaction id {} timed out".format(txid)
)

tx_info = algod_client.pending_transaction_info(txid, **kwargs)

# The transaction has been confirmed
if "confirmed-round" in tx_info:
return tx_info
try:
tx_info = algod_client.pending_transaction_info(txid, **kwargs)

# The transaction has been rejected
if "pool-error" in tx_info and len(tx_info["pool-error"]) != 0:
raise error.TransactionRejectedError(
"Transaction rejected: " + tx_info["pool-error"]
)

# The transaction has been confirmed
if (
"confirmed-round" in tx_info
and tx_info["confirmed-round"] != 0
):
return tx_info
except error.AlgodHTTPError as e:
# Ignore HTTP errors from pending_transaction_info, since it may return 404 if the algod
# instance is behind a load balancer and the request goes to a different algod than the
# one we submitted the transaction to
pass

# Wait until the block for the `current_round` is confirmed
algod_client.status_after_block(current_round)
Expand Down