Skip to content

fix: return waitForIndexer when latest lastValid is reached #408

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

Merged
merged 3 commits into from
Jun 9, 2025
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
44 changes: 38 additions & 6 deletions docs/code/classes/testing.TransactionLogger.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Useful for automated tests.

### Properties

- [\_latestLastValidRound](testing.TransactionLogger.md#_latestlastvalidround)
- [\_sentTransactionIds](testing.TransactionLogger.md#_senttransactionids)

### Accessors
Expand All @@ -23,6 +24,7 @@ Useful for automated tests.

### Methods

- [\_pushTxn](testing.TransactionLogger.md#_pushtxn)
- [capture](testing.TransactionLogger.md#capture)
- [clear](testing.TransactionLogger.md#clear)
- [logRawTransaction](testing.TransactionLogger.md#lograwtransaction)
Expand All @@ -40,13 +42,23 @@ Useful for automated tests.

## Properties

### \_latestLastValidRound

• `Private` `Optional` **\_latestLastValidRound**: `bigint`

#### Defined in

[src/testing/transaction-logger.ts:14](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L14)

___

### \_sentTransactionIds

• `Private` **\_sentTransactionIds**: `string`[] = `[]`

#### Defined in

[src/testing/transaction-logger.ts:12](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L12)
[src/testing/transaction-logger.ts:13](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L13)

## Accessors

Expand All @@ -62,10 +74,30 @@ readonly `string`[]

#### Defined in

[src/testing/transaction-logger.ts:17](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L17)
[src/testing/transaction-logger.ts:27](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L27)

## Methods

### \_pushTxn

▸ **_pushTxn**(`stxn`): `void`

#### Parameters

| Name | Type |
| :------ | :------ |
| `stxn` | `Uint8Array` |

#### Returns

`void`

#### Defined in

[src/testing/transaction-logger.ts:16](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L16)

___

### capture

▸ **capture**(`algod`): `AlgodClient`
Expand All @@ -86,7 +118,7 @@ The wrapped `Algodv2`, any transactions sent using this algod instance will be l

#### Defined in

[src/testing/transaction-logger.ts:48](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L48)
[src/testing/transaction-logger.ts:54](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L54)

___

Expand All @@ -102,7 +134,7 @@ Clear all logged IDs.

#### Defined in

[src/testing/transaction-logger.ts:24](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L24)
[src/testing/transaction-logger.ts:34](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L34)

___

Expand All @@ -124,7 +156,7 @@ The method that captures raw transactions and stores the transaction IDs.

#### Defined in

[src/testing/transaction-logger.ts:31](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L31)
[src/testing/transaction-logger.ts:41](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L41)

___

Expand All @@ -146,4 +178,4 @@ Wait until all logged transactions IDs appear in the given `Indexer`.

#### Defined in

[src/testing/transaction-logger.ts:53](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L53)
[src/testing/transaction-logger.ts:59](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/transaction-logger.ts#L59)
35 changes: 28 additions & 7 deletions src/testing/transaction-logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import algosdk from 'algosdk'
import { Config } from '../config'
import { runWhenIndexerCaughtUp } from './indexer'
import Algodv2 = algosdk.Algodv2
import decodeSignedTransaction = algosdk.decodeSignedTransaction
Expand All @@ -10,6 +11,15 @@ import Indexer = algosdk.Indexer
*/
export class TransactionLogger {
private _sentTransactionIds: string[] = []
private _latestLastValidRound?: bigint

private _pushTxn(stxn: Uint8Array) {
const decoded = decodeSignedTransaction(stxn)
if (decoded.txn.lastValid > (this._latestLastValidRound ?? BigInt(0))) {
this._latestLastValidRound = BigInt(decoded.txn.lastValid)
}
this._sentTransactionIds.push(decoded.txn.txID())
}

/**
* The list of transaction IDs that has been logged thus far.
Expand All @@ -30,13 +40,9 @@ export class TransactionLogger {
*/
logRawTransaction(signedTransactions: Uint8Array | Uint8Array[]) {
if (Array.isArray(signedTransactions)) {
for (const stxn of signedTransactions) {
const decoded = decodeSignedTransaction(stxn)
this._sentTransactionIds.push(decoded.txn.txID())
}
signedTransactions.forEach((stxn) => this._pushTxn(stxn))
} else {
const decoded = decodeSignedTransaction(signedTransactions)
this._sentTransactionIds.push(decoded.txn.txID())
this._pushTxn(signedTransactions)
}
}

Expand All @@ -53,7 +59,22 @@ export class TransactionLogger {
async waitForIndexer(indexer: Indexer) {
if (this._sentTransactionIds.length === 0) return
const lastTxId = this._sentTransactionIds[this._sentTransactionIds.length - 1]
await runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(lastTxId).do())
await runWhenIndexerCaughtUp(async () => {
try {
await indexer.lookupTransactionByID(lastTxId).do()
} catch (e) {
// If the txid lookup failed, then try to look up the last valid round
// If that round exists, then we know indexer is caught up
if (this._latestLastValidRound) {
await indexer.lookupBlock(this._latestLastValidRound).do()
Config.getLogger().debug(
`waitForIndexer has waited until the last valid round ${this._latestLastValidRound} was indexed, but did not find transaction ${lastTxId} in the indexer.`,
)
} else {
throw e
}
}
})
}
}

Expand Down