Skip to content

Commit

Permalink
rename Unique to Set
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jan 23, 2025
1 parent f645352 commit a99ecad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
6 changes: 3 additions & 3 deletions rpc/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func (h *Handler) StorageProof(id BlockID,
}

// Do a sanity check and remove duplicates from the keys
classes = utils.Unique(classes)
contracts = utils.Unique(contracts)
classes = utils.Set(classes)
contracts = utils.Set(contracts)

// Remove duplicates from the storage keys
mergedStorageKeys := make(map[felt.Felt][]felt.Felt)
Expand All @@ -116,7 +116,7 @@ func (h *Handler) StorageProof(id BlockID,

uniqueStorageKeys := make([]StorageKeys, 0, len(mergedStorageKeys))
for contract, keys := range mergedStorageKeys {
uniqueStorageKeys = append(uniqueStorageKeys, StorageKeys{Contract: contract, Keys: utils.Unique(keys)})
uniqueStorageKeys = append(uniqueStorageKeys, StorageKeys{Contract: contract, Keys: utils.Set(keys)})
}

classProof, err := getClassProof(classTrie, classes)
Expand Down
3 changes: 1 addition & 2 deletions utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ func AnyOf[T comparable](e T, values ...T) bool {

// Unique returns a new slice with duplicates removed.
// Panics if the slice contains pointer types.
func Unique[T comparable](slice []T) []T {
func Set[T comparable](slice []T) []T {
if len(slice) == 0 {
return slice
}

// do not support unique on pointer types, just return the slice as it is
if reflect.TypeOf(slice[0]).Kind() == reflect.Ptr {
panic("Unique does not support pointer types")
}
Expand Down
12 changes: 6 additions & 6 deletions utils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,34 @@ func TestAnyOf(t *testing.T) {
})
}

func TestUnique(t *testing.T) {
func TestSet(t *testing.T) {
t.Run("nil slice", func(t *testing.T) {
var input []int
actual := Unique(input)
actual := Set(input)
assert.Nil(t, actual)
})

t.Run("empty slice", func(t *testing.T) {
input := []int{}
actual := Unique(input)
actual := Set(input)
assert.Empty(t, actual)
})

t.Run("slice with no duplicates", func(t *testing.T) {
input := []int{1, 2, 3, 4, 5}
actual := Unique(input)
actual := Set(input)
assert.Equal(t, []int{1, 2, 3, 4, 5}, actual)
})

t.Run("slice with duplicates", func(t *testing.T) {
input := []int{1, 2, 2, 3, 3, 3, 4, 5, 5}
actual := Unique(input)
actual := Set(input)
assert.Equal(t, []int{1, 2, 3, 4, 5}, actual)
})

t.Run("slice of strings with duplicates", func(t *testing.T) {
input := []string{"a", "b", "b", "c", "c", "c"}
actual := Unique(input)
actual := Set(input)
assert.Equal(t, []string{"a", "b", "c"}, actual)
})
}

0 comments on commit a99ecad

Please sign in to comment.