diff --git a/.changelog/4429.bugfix.md b/.changelog/4429.bugfix.md new file mode 100644 index 00000000000..1986a78ce6c --- /dev/null +++ b/.changelog/4429.bugfix.md @@ -0,0 +1,4 @@ +go/runtime/transaction: Return transactions in batch order when queried + +Previously when runtime transactions were queried via a GetTransactions call, +they were returned ordered by transaction hash instead of in execution order. diff --git a/go/runtime/transaction/transaction.go b/go/runtime/transaction/transaction.go index bce7ba72dc5..771843f748b 100644 --- a/go/runtime/transaction/transaction.go +++ b/go/runtime/transaction/transaction.go @@ -406,8 +406,7 @@ func (t *Tree) GetInputBatch(ctx context.Context, maxBatchSize, maxBatchSizeByte return bo.batch, nil } -// GetTransactions returns a list of all transaction artifacts in the tree -// in a stable order (transactions are ordered by their hash). +// GetTransactions returns a list of all transaction artifacts in batch order. func (t *Tree) GetTransactions(ctx context.Context) ([]*Transaction, error) { it := t.tree.NewIterator(ctx, mkvs.IteratorPrefetch(prefetchArtifactCount)) defer it.Close() @@ -455,6 +454,11 @@ func (t *Tree) GetTransactions(ctx context.Context) ([]*Transaction, error) { return nil, fmt.Errorf("transaction: get transactions failed: %w", it.Err()) } + // Reorder transactions so they are in batch order (how they were executed). + sort.SliceStable(txs, func(i, j int) bool { + return txs[i].BatchOrder < txs[j].BatchOrder + }) + return txs, nil } diff --git a/go/runtime/transaction/transaction_test.go b/go/runtime/transaction/transaction_test.go index af002ef7eb0..21a95707144 100644 --- a/go/runtime/transaction/transaction_test.go +++ b/go/runtime/transaction/transaction_test.go @@ -72,9 +72,14 @@ func TestTransaction(t *testing.T) { var txHashes []hash.Hash txnsByHash := make(map[hash.Hash]*Transaction) - for _, tx := range txns { + for i, tx := range txns { txnsByHash[tx.Hash()] = tx txHashes = append(txHashes, tx.Hash()) + + // Make sure the transactions are returned in batch order. + if i > 0 { + require.EqualValues(t, testTxns[i-1], *tx, "transactions must be returned in batch order") + } } for _, checkTx := range testTxns {