Skip to content

Commit

Permalink
Use slices methods instead of sort methods (#261)
Browse files Browse the repository at this point in the history
* use slices.Sort instead of sort.Slice

* import ordering
  • Loading branch information
Dan Laine authored Jun 15, 2023
1 parent b0c29b8 commit fc74798
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
9 changes: 6 additions & 3 deletions plugin/evm/atomic_tx_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"encoding/binary"
"errors"
"fmt"
"sort"
"time"

"golang.org/x/exp/slices"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"

Expand Down Expand Up @@ -272,7 +273,9 @@ func (a *atomicTxRepository) write(height uint64, txs []*Tx, bonus bool) error {
// with txs initialized from the txID index.
copyTxs := make([]*Tx, len(txs))
copy(copyTxs, txs)
sort.Slice(copyTxs, func(i, j int) bool { return copyTxs[i].ID().Hex() < copyTxs[j].ID().Hex() })
slices.SortFunc(copyTxs, func(i, j *Tx) bool {
return i.Less(j)
})
txs = copyTxs
}
heightBytes := make([]byte, wrappers.LongLen)
Expand Down Expand Up @@ -452,6 +455,6 @@ func getAtomicRepositoryRepairHeights(bonusBlocks map[uint64]ids.ID, canonicalBl
repairHeights = append(repairHeights, height)
}
}
sort.Slice(repairHeights, func(i, j int) bool { return repairHeights[i] < repairHeights[j] })
slices.Sort(repairHeights)
return repairHeights
}
12 changes: 5 additions & 7 deletions plugin/evm/atomic_tx_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package evm
import (
"encoding/binary"
"fmt"
"sort"
"testing"

"golang.org/x/exp/slices"

"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
Expand Down Expand Up @@ -98,17 +99,14 @@ func writeTxs(t testing.TB, repo AtomicTxRepository, fromHeight uint64, toHeight
// verifyTxs asserts [repo] can find all txs in [txMap] by height and txID
func verifyTxs(t testing.TB, repo AtomicTxRepository, txMap map[uint64][]*Tx) {
// We should be able to fetch indexed txs by height:
getComparator := func(txs []*Tx) func(int, int) bool {
return func(i, j int) bool {
return txs[i].ID().Hex() < txs[j].ID().Hex()
}
}
for height, expectedTxs := range txMap {
txs, err := repo.GetByHeight(height)
assert.NoErrorf(t, err, "unexpected error on GetByHeight at height=%d", height)
assert.Lenf(t, txs, len(expectedTxs), "wrong len of txs at height=%d", height)
// txs should be stored in order of txID
sort.Slice(expectedTxs, getComparator(expectedTxs))
slices.SortFunc(expectedTxs, func(i, j *Tx) bool {
return i.Less(j)
})

txIDs := set.Set[ids.ID]{}
for i := 0; i < len(txs); i++ {
Expand Down
10 changes: 9 additions & 1 deletion plugin/evm/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"math/big"
"sort"

"golang.org/x/exp/slices"

"github.com/ethereum/go-ethereum/common"

"github.com/ava-labs/coreth/core/state"
Expand Down Expand Up @@ -141,6 +143,10 @@ type Tx struct {
Creds []verify.Verifiable `serialize:"true" json:"credentials"`
}

func (tx *Tx) Less(other *Tx) bool {
return tx.ID().Hex() < other.ID().Hex()
}

// Sign this transaction with the provided signers
func (tx *Tx) Sign(c codec.Manager, signers [][]*secp256k1.PrivateKey) error {
unsignedBytes, err := c.Marshal(codecVersion, &tx.UnsignedAtomicTx)
Expand Down Expand Up @@ -261,7 +267,9 @@ func mergeAtomicOps(txs []*Tx) (map[ids.ID]*atomic.Requests, error) {
// with txs initialized from the txID index.
copyTxs := make([]*Tx, len(txs))
copy(copyTxs, txs)
sort.Slice(copyTxs, func(i, j int) bool { return copyTxs[i].ID().Hex() < copyTxs[j].ID().Hex() })
slices.SortFunc(copyTxs, func(i, j *Tx) bool {
return i.Less(j)
})
txs = copyTxs
}
output := make(map[ids.ID]*atomic.Requests)
Expand Down
6 changes: 3 additions & 3 deletions plugin/evm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"math/big"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"

"golang.org/x/exp/slices"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -4027,8 +4028,7 @@ func TestExtraStateChangeAtomicGasLimitExceeded(t *testing.T) {
func TestGetAtomicRepositoryRepairHeights(t *testing.T) {
mainnetHeights := getAtomicRepositoryRepairHeights(bonusBlockMainnetHeights, canonicalBlockMainnetHeights)
assert.Len(t, mainnetHeights, 76)
sorted := sort.SliceIsSorted(mainnetHeights, func(i, j int) bool { return mainnetHeights[i] < mainnetHeights[j] })
assert.True(t, sorted)
assert.True(t, slices.IsSorted(mainnetHeights))
}

func TestSkipChainConfigCheckCompatible(t *testing.T) {
Expand Down

0 comments on commit fc74798

Please sign in to comment.