eth/fetcher: using slices.Clone to simplify codes

This commit is contained in:
cuiweixie 2024-03-29 10:40:57 +08:00
parent a3829178af
commit b2e2fd067f

View file

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"math" "math"
mrand "math/rand" mrand "math/rand"
"slices"
"sort" "sort"
"time" "time"
@ -973,8 +974,7 @@ func (f *TxFetcher) forEachAnnounce(announces map[common.Hash]*txMetadata, do fu
// rotateStrings rotates the contents of a slice by n steps. This method is only // rotateStrings rotates the contents of a slice by n steps. This method is only
// used in tests to simulate random map iteration but keep it deterministic. // used in tests to simulate random map iteration but keep it deterministic.
func rotateStrings(slice []string, n int) { func rotateStrings(slice []string, n int) {
orig := make([]string, len(slice)) orig := slices.Clone(slice)
copy(orig, slice)
for i := 0; i < len(orig); i++ { for i := 0; i < len(orig); i++ {
slice[i] = orig[(i+n)%len(orig)] slice[i] = orig[(i+n)%len(orig)]
@ -996,8 +996,7 @@ func sortHashes(slice []common.Hash) {
// rotateHashes rotates the contents of a slice by n steps. This method is only // rotateHashes rotates the contents of a slice by n steps. This method is only
// used in tests to simulate random map iteration but keep it deterministic. // used in tests to simulate random map iteration but keep it deterministic.
func rotateHashes(slice []common.Hash, n int) { func rotateHashes(slice []common.Hash, n int) {
orig := make([]common.Hash, len(slice)) orig := slices.Clone(slice)
copy(orig, slice)
for i := 0; i < len(orig); i++ { for i := 0; i < len(orig); i++ {
slice[i] = orig[(i+n)%len(orig)] slice[i] = orig[(i+n)%len(orig)]