Skip to content

Commit

Permalink
go/runtime/transaction: Ensure consistent batch order indices
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Sep 4, 2020
1 parent 306b571 commit 625f4b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion go/runtime/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const (

// MarshalBinary encodes an artifact kind into binary form.
func (ak artifactKind) MarshalBinary() (data []byte, err error) {
// kindInvalid should not be marshaleld.
// kindInvalid should not be marshaled.
if ak == kindInvalid {
return nil, errMalformedArtifactKind
}
Expand Down Expand Up @@ -258,6 +258,13 @@ func (t *Tree) GetInputBatch(ctx context.Context, maxBatchSize, maxBatchSizeByte

// Sort transactions to be in batch order.
sort.Stable(bo)

// Make sure that item orders are consistent.
for i, v := range bo.order {
if uint32(i) != v {
return nil, fmt.Errorf("transaction: inconsistent order: item %d has batch order %d", i, v)
}
}
bo.order = nil

return bo.batch, nil
Expand Down
21 changes: 21 additions & 0 deletions go/runtime/transaction/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,24 @@ func TestTransaction(t *testing.T) {
require.True(t, txnsByHash[checkTx.Hash()].Equal(&checkTx), "transaction should have the correct artifacts") // nolint: gosec
}
}

func TestTransactionInvalidBatchOrder(t *testing.T) {
ctx := context.Background()
store := mkvs.New(nil, nil)

var emptyRoot node.Root
emptyRoot.Empty()

tree := NewTree(store, emptyRoot)

tx := Transaction{
Input: []byte("this goes in"),
Output: []byte("and this comes out"),
BatchOrder: 1, // Invalid batch order as the first transaction should be at index zero.
}
err := tree.AddTransaction(ctx, tx, nil)
require.NoError(t, err, "AddTransaction")

_, err = tree.GetInputBatch(ctx, 0, 0)
require.Error(t, err, "GetInputBatch should fail with inconsistent order")
}

0 comments on commit 625f4b2

Please sign in to comment.