Skip to content

Commit

Permalink
chore: clarify sort for blobs (#888)
Browse files Browse the repository at this point in the history
* chore: clarify sort for blobs

* make proto-gen
  • Loading branch information
rootulp authored Nov 16, 2022
1 parent b7a7c1a commit 142e06b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
3 changes: 2 additions & 1 deletion proto/tendermint/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 5 additions & 16 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"
"fmt"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -1047,18 +1046,18 @@ func (data *Data) Hash() tmbytes.HexBytes {
return data.hash
}

// ByNamespace implements sort.Interface for Blob
type Blobs []Blob
// BlobsByNamespace implements sort.Interface for Blob
type BlobsByNamespace []Blob

func (b Blobs) Len() int {
func (b BlobsByNamespace) Len() int {
return len(b)
}

func (b Blobs) Swap(i, j int) {
func (b BlobsByNamespace) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}

func (b Blobs) Less(i, j int) bool {
func (b BlobsByNamespace) Less(i, j int) bool {
// The following comparison is `<` and not `<=` because bytes.Compare returns 0 for if a == b.
// We want this comparison to return `false` if a == b because:
// If both Less(i, j) and Less(j, i) are false,
Expand All @@ -1067,16 +1066,6 @@ func (b Blobs) Less(i, j int) bool {
return bytes.Compare(b[i].NamespaceID, b[j].NamespaceID) < 0
}

// SortMessages sorts messages by ascending namespace id
func (b *Blobs) SortMessages() {
sort.Sort(b)
}

// IsSorted returns whether the messages are sorted by namespace id
func (b *Blobs) IsSorted() bool {
return sort.IsSorted(b)
}

type Blob struct {
// NamespaceID defines the namespace of this message, i.e. the
// namespace it will use in the namespaced Merkle tree.
Expand Down
5 changes: 3 additions & 2 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"os"
"reflect"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -900,8 +901,8 @@ func TestMessagesIsSorted(t *testing.T) {

for _, tc := range tests {
t.Run(tc.descripton, func(t *testing.T) {
bs := Blobs(tc.blobs)
assert.Equal(t, tc.want, bs.IsSorted())
bs := tc.blobs
assert.Equal(t, tc.want, sort.IsSorted(BlobsByNamespace(bs)))
})
}
}

0 comments on commit 142e06b

Please sign in to comment.