cmd/devp2p: use slices package for sorting (#27487)
This commit is contained in:
parent
311b742c84
commit
4544dc5f32
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -33,6 +32,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -288,11 +288,11 @@ func makeDeletionChanges(records map[string]recordSet, keep map[string]string) [
|
|||||||
// sortChanges ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order.
|
// sortChanges ensures DNS changes are in leaf-added -> root-changed -> leaf-deleted order.
|
||||||
func sortChanges(changes []types.Change) {
|
func sortChanges(changes []types.Change) {
|
||||||
score := map[string]int{"CREATE": 1, "UPSERT": 2, "DELETE": 3}
|
score := map[string]int{"CREATE": 1, "UPSERT": 2, "DELETE": 3}
|
||||||
sort.Slice(changes, func(i, j int) bool {
|
slices.SortFunc(changes, func(a, b types.Change) bool {
|
||||||
if changes[i].Action == changes[j].Action {
|
if a.Action == b.Action {
|
||||||
return *changes[i].ResourceRecordSet.Name < *changes[j].ResourceRecordSet.Name
|
return *a.ResourceRecordSet.Name < *b.ResourceRecordSet.Name
|
||||||
}
|
}
|
||||||
return score[string(changes[i].Action)] < score[string(changes[j].Action)]
|
return score[string(a.Action)] < score[string(b.Action)]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,11 +21,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
const jsonIndent = " "
|
const jsonIndent = " "
|
||||||
@ -77,8 +77,8 @@ func (ns nodeSet) nodes() []*enode.Node {
|
|||||||
result = append(result, n.N)
|
result = append(result, n.N)
|
||||||
}
|
}
|
||||||
// Sort by ID.
|
// Sort by ID.
|
||||||
sort.Slice(result, func(i, j int) bool {
|
slices.SortFunc(result, func(a, b *enode.Node) bool {
|
||||||
return bytes.Compare(result[i].ID().Bytes(), result[j].ID().Bytes()) < 0
|
return bytes.Compare(a.ID().Bytes(), b.ID().Bytes()) < 0
|
||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -103,8 +103,8 @@ func (ns nodeSet) topN(n int) nodeSet {
|
|||||||
for _, v := range ns {
|
for _, v := range ns {
|
||||||
byscore = append(byscore, v)
|
byscore = append(byscore, v)
|
||||||
}
|
}
|
||||||
sort.Slice(byscore, func(i, j int) bool {
|
slices.SortFunc(byscore, func(a, b nodeJSON) bool {
|
||||||
return byscore[i].Score >= byscore[j].Score
|
return a.Score >= b.Score
|
||||||
})
|
})
|
||||||
result := make(nodeSet, n)
|
result := make(nodeSet, n)
|
||||||
for _, v := range byscore[:n] {
|
for _, v := range byscore[:n] {
|
||||||
|
Loading…
Reference in New Issue
Block a user