Skip to content

Commit be65b47

Browse files
authored
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 0ce331f commit be65b47

38 files changed

+144
-113
lines changed

accounts/keystore/account_cache.go

+2-2
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

+9-3
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

+10-4
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

+6-6
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

+1-1
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

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ func (bc *BlockChain) procFutureBlocks() {
10531053
}
10541054
}
10551055
if len(blocks) > 0 {
1056-
slices.SortFunc(blocks, func(a, b *types.Block) bool {
1057-
return a.NumberU64() < b.NumberU64()
1056+
slices.SortFunc(blocks, func(a, b *types.Block) int {
1057+
return a.Number().Cmp(b.Number())
10581058
})
10591059
// Insert one by one as chain insertion needs contiguous ancestry between blocks
10601060
for i := range blocks {

core/rawdb/accessors_chain.go

+2-2
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

+3-5
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

+2-2
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

core/state/snapshot/iterator_fast.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,25 @@ type weightedIterator struct {
3333
priority int
3434
}
3535

36-
func (it *weightedIterator) Less(other *weightedIterator) bool {
36+
func (it *weightedIterator) Cmp(other *weightedIterator) int {
3737
// Order the iterators primarily by the account hashes
3838
hashI := it.it.Hash()
3939
hashJ := other.it.Hash()
4040

4141
switch bytes.Compare(hashI[:], hashJ[:]) {
4242
case -1:
43-
return true
43+
return -1
4444
case 1:
45-
return false
45+
return 1
4646
}
4747
// Same account/storage-slot in multiple layers, split by priority
48-
return it.priority < other.priority
48+
if it.priority < other.priority {
49+
return -1
50+
}
51+
if it.priority > other.priority {
52+
return 1
53+
}
54+
return 0
4955
}
5056

5157
// fastIterator is a more optimized multi-layer iterator which maintains a
@@ -155,9 +161,7 @@ func (fi *fastIterator) init() {
155161
}
156162
}
157163
// Re-sort the entire list
158-
slices.SortFunc(fi.iterators, func(a, b *weightedIterator) bool {
159-
return a.Less(b)
160-
})
164+
slices.SortFunc(fi.iterators, func(a, b *weightedIterator) int { return a.Cmp(b) })
161165
fi.initiated = false
162166
}
163167

eth/api_debug_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestAccountRange(t *testing.T) {
105105
}
106106
// Test to see if it's possible to recover from the middle of the previous
107107
// set and get an even split between the first and second sets.
108-
slices.SortFunc(hList, common.Hash.Less)
108+
slices.SortFunc(hList, common.Hash.Cmp)
109109
middleH := hList[AccountRangeMaxResults/2]
110110
middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
111111
missing, infirst, insecond := 0, 0, 0

eth/gasprice/feehistory.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
111111
reward, _ := tx.EffectiveGasTip(bf.block.BaseFee())
112112
sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
113113
}
114-
slices.SortStableFunc(sorter, func(a, b txGasAndReward) bool {
115-
return a.reward.Cmp(b.reward) < 0
114+
slices.SortStableFunc(sorter, func(a, b txGasAndReward) int {
115+
return a.reward.Cmp(b.reward)
116116
})
117117

118118
var txIndex int

eth/gasprice/gasprice.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
208208
}
209209
price := lastPrice
210210
if len(results) > 0 {
211-
slices.SortFunc(results, func(a, b *big.Int) bool { return a.Cmp(b) < 0 })
211+
slices.SortFunc(results, func(a, b *big.Int) int { return a.Cmp(b) })
212212
price = results[(len(results)-1)*oracle.percentile/100]
213213
}
214214
if price.Cmp(oracle.maxPrice) > 0 {
@@ -247,12 +247,12 @@ func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit
247247
sortedTxs := make([]*types.Transaction, len(txs))
248248
copy(sortedTxs, txs)
249249
baseFee := block.BaseFee()
250-
slices.SortFunc(sortedTxs, func(a, b *types.Transaction) bool {
250+
slices.SortFunc(sortedTxs, func(a, b *types.Transaction) int {
251251
// It's okay to discard the error because a tx would never be
252252
// accepted into a block with an invalid effective tip.
253253
tip1, _ := a.EffectiveGasTip(baseFee)
254254
tip2, _ := b.EffectiveGasTip(baseFee)
255-
return tip1.Cmp(tip2) < 0
255+
return tip1.Cmp(tip2)
256256
})
257257

258258
var prices []*big.Int

eth/protocols/snap/sync_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,8 @@ type kv struct {
14171417
k, v []byte
14181418
}
14191419

1420-
func (k *kv) less(other *kv) bool {
1421-
return bytes.Compare(k.k, other.k) < 0
1420+
func (k *kv) cmp(other *kv) int {
1421+
return bytes.Compare(k.k, other.k)
14221422
}
14231423

14241424
func key32(i uint64) []byte {
@@ -1478,7 +1478,7 @@ func makeAccountTrieNoStorage(n int, scheme string) (string, *trie.Trie, []*kv)
14781478
accTrie.MustUpdate(elem.k, elem.v)
14791479
entries = append(entries, elem)
14801480
}
1481-
slices.SortFunc(entries, (*kv).less)
1481+
slices.SortFunc(entries, (*kv).cmp)
14821482

14831483
// Commit the state changes into db and re-create the trie
14841484
// for accessing later.
@@ -1540,7 +1540,7 @@ func makeBoundaryAccountTrie(scheme string, n int) (string, *trie.Trie, []*kv) {
15401540
accTrie.MustUpdate(elem.k, elem.v)
15411541
entries = append(entries, elem)
15421542
}
1543-
slices.SortFunc(entries, (*kv).less)
1543+
slices.SortFunc(entries, (*kv).cmp)
15441544

15451545
// Commit the state changes into db and re-create the trie
15461546
// for accessing later.
@@ -1587,7 +1587,7 @@ func makeAccountTrieWithStorageWithUniqueStorage(scheme string, accounts, slots
15871587
storageRoots[common.BytesToHash(key)] = stRoot
15881588
storageEntries[common.BytesToHash(key)] = stEntries
15891589
}
1590-
slices.SortFunc(entries, (*kv).less)
1590+
slices.SortFunc(entries, (*kv).cmp)
15911591

15921592
// Commit account trie
15931593
root, set, _ := accTrie.Commit(true)
@@ -1652,7 +1652,7 @@ func makeAccountTrieWithStorage(scheme string, accounts, slots int, code, bounda
16521652
storageRoots[common.BytesToHash(key)] = stRoot
16531653
storageEntries[common.BytesToHash(key)] = stEntries
16541654
}
1655-
slices.SortFunc(entries, (*kv).less)
1655+
slices.SortFunc(entries, (*kv).cmp)
16561656

16571657
// Commit account trie
16581658
root, set, _ := accTrie.Commit(true)
@@ -1696,7 +1696,7 @@ func makeStorageTrieWithSeed(owner common.Hash, n, seed uint64, db *trie.Databas
16961696
trie.MustUpdate(elem.k, elem.v)
16971697
entries = append(entries, elem)
16981698
}
1699-
slices.SortFunc(entries, (*kv).less)
1699+
slices.SortFunc(entries, (*kv).cmp)
17001700
root, nodes, _ := trie.Commit(false)
17011701
return root, nodes, entries
17021702
}
@@ -1747,7 +1747,7 @@ func makeBoundaryStorageTrie(owner common.Hash, n int, db *trie.Database) (commo
17471747
trie.MustUpdate(elem.k, elem.v)
17481748
entries = append(entries, elem)
17491749
}
1750-
slices.SortFunc(entries, (*kv).less)
1750+
slices.SortFunc(entries, (*kv).cmp)
17511751
root, nodes, _ := trie.Commit(false)
17521752
return root, nodes, entries
17531753
}

eth/tracers/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ func newAccounts(n int) (accounts []Account) {
790790
addr := crypto.PubkeyToAddress(key.PublicKey)
791791
accounts = append(accounts, Account{key: key, addr: addr})
792792
}
793-
slices.SortFunc(accounts, func(a, b Account) bool { return a.addr.Less(b.addr) })
793+
slices.SortFunc(accounts, func(a, b Account) int { return a.addr.Cmp(b.addr) })
794794
return accounts
795795
}
796796

ethdb/dbtest/testsuite.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func makeDataset(size, ksize, vsize int, order bool) ([][]byte, [][]byte) {
527527
vals = append(vals, randBytes(vsize))
528528
}
529529
if order {
530-
slices.SortFunc(keys, func(a, b []byte) bool { return bytes.Compare(a, b) < 0 })
530+
slices.SortFunc(keys, func(a, b []byte) int { return bytes.Compare(a, b) })
531531
}
532532
return keys, vals
533533
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ require (
6363
github.com/urfave/cli/v2 v2.24.1
6464
go.uber.org/automaxprocs v1.5.2
6565
golang.org/x/crypto v0.9.0
66-
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
66+
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad
6767
golang.org/x/sync v0.3.0
6868
golang.org/x/sys v0.9.0
6969
golang.org/x/text v0.9.0
@@ -125,7 +125,7 @@ require (
125125
github.com/tklauser/go-sysconf v0.3.5 // indirect
126126
github.com/tklauser/numcpus v0.2.2 // indirect
127127
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
128-
golang.org/x/mod v0.10.0 // indirect
128+
golang.org/x/mod v0.11.0 // indirect
129129
golang.org/x/net v0.10.0 // indirect
130130
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
131131
google.golang.org/protobuf v1.28.1 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
479479
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
480480
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
481481
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
482-
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
483-
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
482+
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad h1:g0bG7Z4uG+OgH2QDODnjp6ggkk1bJDsINcuWmJN1iJU=
483+
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
484484
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
485485
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
486486
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@@ -490,8 +490,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
490490
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
491491
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
492492
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
493-
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
494-
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
493+
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
494+
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
495495
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
496496
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
497497
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

internal/ethapi/api_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func newAccounts(n int) (accounts []Account) {
817817
addr := crypto.PubkeyToAddress(key.PublicKey)
818818
accounts = append(accounts, Account{key: key, addr: addr})
819819
}
820-
slices.SortFunc(accounts, func(a, b Account) bool { return a.addr.Less(b.addr) })
820+
slices.SortFunc(accounts, func(a, b Account) int { return a.addr.Cmp(b.addr) })
821821
return accounts
822822
}
823823

0 commit comments

Comments
 (0)