Skip to content
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

go/runtime/transaction: Return transactions in batch order when queried #4429

Merged
merged 1 commit into from
Jan 17, 2022
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
4 changes: 4 additions & 0 deletions .changelog/4429.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 6 additions & 2 deletions go/runtime/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion go/runtime/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down