Skip to content

Commit 811a674

Browse files
karalabefjl
authored andcommitted
all: update golang/x/ext and fix slice sorting fallout (#27909)
The Go authors updated golang/x/ext to change the function signature of the slices sort method. It's an entire shitshow now because x/ext is not tagged, so everyone's codebase just picked a new version that some other dep depends on, causing our code to fail building. This PR updates the dep on our code too and does all the refactorings to follow upstream...
1 parent 770db14 commit 811a674

38 files changed

+144
-113
lines changed

accounts/keystore/account_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ import (
4040
const minReloadInterval = 2 * time.Second
4141

4242
// byURL defines the sorting order for accounts.
43-
func byURL(a, b accounts.Account) bool {
44-
return a.URL.Cmp(b.URL) < 0
43+
func byURL(a, b accounts.Account) int {
44+
return a.URL.Cmp(b.URL)
4545
}
4646

4747
// AmbiguousAddrError is returned when attempting to unlock

cmd/devp2p/dns_route53.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,17 @@ func makeDeletionChanges(records map[string]recordSet, keep map[string]string) [
288288
// sortChanges ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order.
289289
func sortChanges(changes []types.Change) {
290290
score := map[string]int{"CREATE": 1, "UPSERT": 2, "DELETE": 3}
291-
slices.SortFunc(changes, func(a, b types.Change) bool {
291+
slices.SortFunc(changes, func(a, b types.Change) int {
292292
if a.Action == b.Action {
293-
return *a.ResourceRecordSet.Name < *b.ResourceRecordSet.Name
293+
return strings.Compare(*a.ResourceRecordSet.Name, *b.ResourceRecordSet.Name)
294294
}
295-
return score[string(a.Action)] < score[string(b.Action)]
295+
if score[string(a.Action)] < score[string(b.Action)] {
296+
return -1
297+
}
298+
if score[string(a.Action)] > score[string(b.Action)] {
299+
return 1
300+
}
301+
return 0
296302
})
297303
}
298304

cmd/devp2p/nodeset.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func (ns nodeSet) nodes() []*enode.Node {
7777
result = append(result, n.N)
7878
}
7979
// Sort by ID.
80-
slices.SortFunc(result, func(a, b *enode.Node) bool {
81-
return bytes.Compare(a.ID().Bytes(), b.ID().Bytes()) < 0
80+
slices.SortFunc(result, func(a, b *enode.Node) int {
81+
return bytes.Compare(a.ID().Bytes(), b.ID().Bytes())
8282
})
8383
return result
8484
}
@@ -103,8 +103,14 @@ func (ns nodeSet) topN(n int) nodeSet {
103103
for _, v := range ns {
104104
byscore = append(byscore, v)
105105
}
106-
slices.SortFunc(byscore, func(a, b nodeJSON) bool {
107-
return a.Score >= b.Score
106+
slices.SortFunc(byscore, func(a, b nodeJSON) int {
107+
if a.Score > b.Score {
108+
return -1
109+
}
110+
if a.Score < b.Score {
111+
return 1
112+
}
113+
return 0
108114
})
109115
result := make(nodeSet, n)
110116
for _, v := range byscore[:n] {

common/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
6565
// If b is larger than len(h), b will be cropped from the left.
6666
func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
6767

68-
// Less compares two hashes.
69-
func (h Hash) Less(other Hash) bool {
70-
return bytes.Compare(h[:], other[:]) < 0
68+
// Cmp compares two hashes.
69+
func (h Hash) Cmp(other Hash) int {
70+
return bytes.Compare(h[:], other[:])
7171
}
7272

7373
// Bytes gets the byte representation of the underlying hash.
@@ -231,9 +231,9 @@ func IsHexAddress(s string) bool {
231231
return len(s) == 2*AddressLength && isHex(s)
232232
}
233233

234-
// Less compares two addresses.
235-
func (a Address) Less(other Address) bool {
236-
return bytes.Compare(a[:], other[:]) < 0
234+
// Cmp compares two addresses.
235+
func (a Address) Cmp(other Address) int {
236+
return bytes.Compare(a[:], other[:])
237237
}
238238

239239
// Bytes gets the string representation of the underlying address.

consensus/clique/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (s *Snapshot) signers() []common.Address {
308308
for sig := range s.Signers {
309309
sigs = append(sigs, sig)
310310
}
311-
slices.SortFunc(sigs, common.Address.Less)
311+
slices.SortFunc(sigs, common.Address.Cmp)
312312
return sigs
313313
}
314314

consensus/clique/snapshot_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (ap *testerAccountPool) checkpoint(header *types.Header, signers []string)
5353
for i, signer := range signers {
5454
auths[i] = ap.address(signer)
5555
}
56-
slices.SortFunc(auths, common.Address.Less)
56+
slices.SortFunc(auths, common.Address.Cmp)
5757
for i, auth := range auths {
5858
copy(header.Extra[extraVanity+i*common.AddressLength:], auth.Bytes())
5959
}

core/blockchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,8 @@ func (bc *BlockChain) procFutureBlocks() {
996996
}
997997
}
998998
if len(blocks) > 0 {
999-
slices.SortFunc(blocks, func(a, b *types.Block) bool {
1000-
return a.NumberU64() < b.NumberU64()
999+
slices.SortFunc(blocks, func(a, b *types.Block) int {
1000+
return a.Number().Cmp(b.Number())
10011001
})
10021002
// Insert one by one as chain insertion needs contiguous ancestry between blocks
10031003
for i := range blocks {

core/rawdb/accessors_chain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,9 @@ func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
902902
Header: block.Header(),
903903
Body: block.Body(),
904904
})
905-
slices.SortFunc(badBlocks, func(a, b *badBlock) bool {
905+
slices.SortFunc(badBlocks, func(a, b *badBlock) int {
906906
// Note: sorting in descending number order.
907-
return a.Header.Number.Uint64() >= b.Header.Number.Uint64()
907+
return -a.Header.Number.Cmp(b.Header.Number)
908908
})
909909
if len(badBlocks) > badBlockToKeep {
910910
badBlocks = badBlocks[:badBlockToKeep]

core/rawdb/chain_iterator_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package rawdb
1919
import (
2020
"math/big"
2121
"reflect"
22+
"sort"
2223
"sync"
2324
"testing"
2425

2526
"github.com/ethereum/go-ethereum/common"
2627
"github.com/ethereum/go-ethereum/core/types"
27-
"golang.org/x/exp/slices"
2828
)
2929

3030
func TestChainIterator(t *testing.T) {
@@ -92,11 +92,9 @@ func TestChainIterator(t *testing.T) {
9292
}
9393
}
9494
if !c.reverse {
95-
slices.Sort(numbers)
95+
sort.Ints(numbers)
9696
} else {
97-
slices.SortFunc(numbers, func(a, b int) bool {
98-
return a > b // Sort descending
99-
})
97+
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
10098
}
10199
if !reflect.DeepEqual(numbers, c.expect) {
102100
t.Fatalf("Case %d failed, visit element mismatch, want %v, got %v", i, c.expect, numbers)

core/state/snapshot/difflayer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func (dl *diffLayer) AccountList() []common.Hash {
525525
dl.accountList = append(dl.accountList, hash)
526526
}
527527
}
528-
slices.SortFunc(dl.accountList, common.Hash.Less)
528+
slices.SortFunc(dl.accountList, common.Hash.Cmp)
529529
dl.memory += uint64(len(dl.accountList) * common.HashLength)
530530
return dl.accountList
531531
}
@@ -563,7 +563,7 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool)
563563
for k := range storageMap {
564564
storageList = append(storageList, k)
565565
}
566-
slices.SortFunc(storageList, common.Hash.Less)
566+
slices.SortFunc(storageList, common.Hash.Cmp)
567567
dl.storageList[accountHash] = storageList
568568
dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength)
569569
return storageList, destructed

0 commit comments

Comments
 (0)