Skip to content

Commit

Permalink
*: Replace sort package with slices for sorting
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Aug 19, 2024
1 parent 672bc26 commit 397e5f1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog for NeoFS Contract

### Changed
- Minimal Go version to 1.21 (#353)
- Replaced sort package with slices for sorting (#353)

### Updated

Expand Down
6 changes: 4 additions & 2 deletions deploy/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"slices"

"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
Expand Down Expand Up @@ -141,7 +141,9 @@ func Contract(ctx context.Context, prm ContractPrm) (util.Uint160, error) {
return util.Uint160{}, fmt.Errorf("get Neo committee of the network: %w", err)
}

sort.Sort(committee)
slices.SortFunc(committee, func(a, b *keys.PublicKey) int {
return a.Cmp(b)
})

// determine a leader
localPrivateKey := prm.LocalAccount.PrivateKey()
Expand Down
6 changes: 4 additions & 2 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"math"
"math/big"
"sort"
"slices"
"strconv"

"github.com/nspcc-dev/neo-go/pkg/core/block"
Expand Down Expand Up @@ -178,7 +178,9 @@ func Deploy(ctx context.Context, prm Prm) error {
return fmt.Errorf("get Neo committee of the network: %w", err)
}

sort.Sort(committee)
slices.SortFunc(committee, func(a, b *keys.PublicKey) int {
return a.Cmp(b)
})

// determine a leader
localPrivateKey := prm.LocalAccount.PrivateKey()
Expand Down
14 changes: 5 additions & 9 deletions tests/neofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests
import (
"bytes"
"path"
"sort"
"slices"
"testing"

"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
Expand Down Expand Up @@ -49,10 +49,8 @@ func newNeoFSInvoker(t *testing.T, n int, config ...any) (*neotest.ContractInvok
accounts[i] = acc
}

sort.Slice(accounts, func(i, j int) bool {
p1 := accounts[i].PrivateKey().PublicKey()
p2 := accounts[j].PrivateKey().PublicKey()
return p1.Cmp(p2) == -1
slices.SortFunc(accounts, func(a, b *wallet.Account) int {
return a.PublicKey().Cmp(b.PublicKey())
})

pubs := make(keys.PublicKeys, n)
Expand Down Expand Up @@ -105,10 +103,8 @@ func TestNeoFS_InnerRingCandidate(t *testing.T) {

arr := make([]stackitem.Item, candidateCount)
pubs := make([][]byte, candidateCount)
sort.Slice(accs, func(i, j int) bool {
s1 := accs[i].Script()
s2 := accs[j].Script()
return bytes.Compare(s1, s2) == -1
slices.SortFunc(accs, func(a, b neotest.Signer) int {
return bytes.Compare(a.Script(), b.Script())
})

for i, acc := range accs {
Expand Down
10 changes: 5 additions & 5 deletions tests/neofsid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tests
import (
"bytes"
"path"
"sort"
"slices"
"testing"

"github.com/mr-tron/base58"
Expand Down Expand Up @@ -49,8 +49,8 @@ func TestNeoFSID_AddKey(t *testing.T) {
e.Invoke(t, stackitem.Null{}, "addKey", owner,
[]any{pubs[0], pubs[1]})

sort.Slice(pubs[:2], func(i, j int) bool {
return bytes.Compare(pubs[i], pubs[j]) == -1
slices.SortFunc(pubs[:2], func(a, b []byte) int {
return bytes.Compare(a, b)
})
arr := []stackitem.Item{
stackitem.NewBuffer(pubs[0]),
Expand All @@ -65,8 +65,8 @@ func TestNeoFSID_AddKey(t *testing.T) {
e.CheckHalt(t, tx1.Hash(), stackitem.Null{})
e.CheckHalt(t, tx2.Hash(), stackitem.Null{})

sort.Slice(pubs[:5], func(i, j int) bool {
return bytes.Compare(pubs[i], pubs[j]) == -1
slices.SortFunc(pubs[:5], func(a, b []byte) int {
return bytes.Compare(a, b)
})
arr = []stackitem.Item{
stackitem.NewBuffer(pubs[0]),
Expand Down

0 comments on commit 397e5f1

Please sign in to comment.