diff --git a/plugin/evm/atomic_tx_repository.go b/plugin/evm/atomic_tx_repository.go index 6bb863d5a6..ef2cf0c601 100644 --- a/plugin/evm/atomic_tx_repository.go +++ b/plugin/evm/atomic_tx_repository.go @@ -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" @@ -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) @@ -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 } diff --git a/plugin/evm/atomic_tx_repository_test.go b/plugin/evm/atomic_tx_repository_test.go index 614d9c9e94..b17b93d0ba 100644 --- a/plugin/evm/atomic_tx_repository_test.go +++ b/plugin/evm/atomic_tx_repository_test.go @@ -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" @@ -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++ { diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 825d9d81e3..89d43ed43e 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -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" @@ -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) @@ -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) diff --git a/plugin/evm/vm_test.go b/plugin/evm/vm_test.go index d5db4829f6..2587aa5e8b 100644 --- a/plugin/evm/vm_test.go +++ b/plugin/evm/vm_test.go @@ -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" @@ -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) {