mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 21:08:10 -05:00
Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
This commit is contained in:
@@ -30,7 +30,7 @@ var (
|
||||
)
|
||||
|
||||
// NewSignedBeaconBlock creates a signed beacon block from a protobuf signed beacon block.
|
||||
func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
|
||||
func NewSignedBeaconBlock(i any) (interfaces.SignedBeaconBlock, error) {
|
||||
switch b := i.(type) {
|
||||
case nil:
|
||||
return nil, ErrNilObject
|
||||
@@ -88,7 +88,7 @@ func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
|
||||
}
|
||||
|
||||
// NewBeaconBlock creates a beacon block from a protobuf beacon block.
|
||||
func NewBeaconBlock(i interface{}) (interfaces.ReadOnlyBeaconBlock, error) {
|
||||
func NewBeaconBlock(i any) (interfaces.ReadOnlyBeaconBlock, error) {
|
||||
switch b := i.(type) {
|
||||
case nil:
|
||||
return nil, ErrNilObject
|
||||
@@ -144,7 +144,7 @@ func NewBeaconBlock(i interface{}) (interfaces.ReadOnlyBeaconBlock, error) {
|
||||
}
|
||||
|
||||
// NewBeaconBlockBody creates a beacon block body from a protobuf beacon block body.
|
||||
func NewBeaconBlockBody(i interface{}) (interfaces.ReadOnlyBeaconBlockBody, error) {
|
||||
func NewBeaconBlockBody(i any) (interfaces.ReadOnlyBeaconBlockBody, error) {
|
||||
switch b := i.(type) {
|
||||
case nil:
|
||||
return nil, ErrNilObject
|
||||
@@ -265,7 +265,7 @@ func BuildSignedBeaconBlock(blk interfaces.ReadOnlyBeaconBlock, signature []byte
|
||||
}
|
||||
}
|
||||
|
||||
func getWrappedPayload(payload interface{}) (wrappedPayload interfaces.ExecutionData, wrapErr error) {
|
||||
func getWrappedPayload(payload any) (wrappedPayload interfaces.ExecutionData, wrapErr error) {
|
||||
switch p := payload.(type) {
|
||||
case *enginev1.ExecutionPayload:
|
||||
wrappedPayload, wrapErr = WrappedExecutionPayload(p)
|
||||
@@ -308,7 +308,7 @@ func checkPayloadAgainstHeader(wrappedPayload, payloadHeader interfaces.Executio
|
||||
// BuildSignedBeaconBlockFromExecutionPayload takes a signed, blinded beacon block and converts into
|
||||
// a full, signed beacon block by specifying an execution payload.
|
||||
// nolint:gocognit
|
||||
func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBeaconBlock, payload interface{}) (interfaces.SignedBeaconBlock, error) {
|
||||
func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBeaconBlock, payload any) (interfaces.SignedBeaconBlock, error) {
|
||||
if err := BeaconBlockIsNil(blk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -338,7 +338,7 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea
|
||||
graffiti := b.Body().Graffiti()
|
||||
sig := blk.Signature()
|
||||
|
||||
var fullBlock interface{}
|
||||
var fullBlock any
|
||||
switch blk.Version() {
|
||||
case version.Bellatrix:
|
||||
p, ok := payload.(*enginev1.ExecutionPayload)
|
||||
|
||||
@@ -230,8 +230,8 @@ func Benchmark_MerkleProofKZGCommitment(b *testing.B) {
|
||||
body, err := NewBeaconBlockBody(pbBody)
|
||||
require.NoError(b, err)
|
||||
index := 1
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
for b.Loop() {
|
||||
_, err := MerkleProofKZGCommitment(body, index)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
|
||||
@@ -444,14 +444,14 @@ func FuzzKmpIndex(f *testing.F) {
|
||||
// Parse comma-separated strings into int slices
|
||||
var source, target []int
|
||||
if sourceStr != "" {
|
||||
for _, s := range strings.Split(sourceStr, ",") {
|
||||
for s := range strings.SplitSeq(sourceStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
source = append(source, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
if targetStr != "" {
|
||||
for _, s := range strings.Split(targetStr, ",") {
|
||||
for s := range strings.SplitSeq(targetStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
target = append(target, val)
|
||||
}
|
||||
@@ -508,7 +508,7 @@ func FuzzComputeLPS(f *testing.F) {
|
||||
// Parse comma-separated string into int slice
|
||||
var pattern []int
|
||||
if patternStr != "" {
|
||||
for _, s := range strings.Split(patternStr, ",") {
|
||||
for s := range strings.SplitSeq(patternStr, ",") {
|
||||
if val, err := strconv.Atoi(strings.TrimSpace(s)); err == nil {
|
||||
pattern = append(pattern, val)
|
||||
}
|
||||
|
||||
@@ -47,10 +47,11 @@ func FuzzPropertyRoundTrip(f *testing.F) {
|
||||
ctx := t.Context()
|
||||
|
||||
// Create source state with reasonable size
|
||||
validatorCount := uint64(len(validatorChanges) + 8) // Minimum 8 validators
|
||||
if validatorCount > 64 {
|
||||
validatorCount = 64 // Cap at 64 for performance
|
||||
}
|
||||
validatorCount := min(
|
||||
// Minimum 8 validators
|
||||
uint64(len(validatorChanges)+8),
|
||||
// Cap at 64 for performance
|
||||
64)
|
||||
source, _ := util.DeterministicGenesisStateElectra(t, validatorCount)
|
||||
|
||||
// Create target state with modifications
|
||||
|
||||
@@ -63,7 +63,7 @@ func TestReasonablePerformance(t *testing.T) {
|
||||
// Make realistic changes
|
||||
_ = target.SetSlot(source.Slot() + 32) // One epoch
|
||||
validators := target.Validators()
|
||||
for i := 0; i < 100; i++ { // 10% of validators changed
|
||||
for i := range 100 { // 10% of validators changed
|
||||
validators[i].EffectiveBalance += 1000000000 // 1 ETH change
|
||||
}
|
||||
_ = target.SetValidators(validators)
|
||||
@@ -129,7 +129,7 @@ func TestStateTransitionValidation(t *testing.T) {
|
||||
|
||||
// Some validators get rewards, others get penalties
|
||||
balances := target.Balances()
|
||||
for i := 0; i < len(balances); i++ {
|
||||
for i := range balances {
|
||||
if i%2 == 0 {
|
||||
balances[i] += 100000000 // 0.1 ETH reward
|
||||
} else {
|
||||
@@ -331,12 +331,12 @@ func TestConcurrencySafety(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, numGoroutines*iterations)
|
||||
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
for i := range numGoroutines {
|
||||
wg.Add(1)
|
||||
go func(workerID int) {
|
||||
defer wg.Done()
|
||||
|
||||
for j := 0; j < iterations; j++ {
|
||||
for j := range iterations {
|
||||
_, err := Diff(source, target)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf("worker %d iteration %d: %v", workerID, j, err)
|
||||
@@ -367,7 +367,7 @@ func TestConcurrencySafety(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
errors := make(chan error, numGoroutines)
|
||||
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
for i := range numGoroutines {
|
||||
wg.Add(1)
|
||||
go func(workerID int) {
|
||||
defer wg.Done()
|
||||
|
||||
@@ -752,7 +752,7 @@ func (ret *stateDiff) readProposerLookahead(data *[]byte) error {
|
||||
// Read the proposer lookahead (2 * SlotsPerEpoch uint64 values)
|
||||
numProposers := 2 * fieldparams.SlotsPerEpoch
|
||||
ret.proposerLookahead = make([]uint64, numProposers)
|
||||
for i := 0; i < numProposers; i++ {
|
||||
for i := range numProposers {
|
||||
ret.proposerLookahead[i] = binary.LittleEndian.Uint64((*data)[i*8 : (i+1)*8])
|
||||
}
|
||||
*data = (*data)[proposerLookaheadLength:]
|
||||
|
||||
@@ -42,7 +42,7 @@ func Test_diffToState(t *testing.T) {
|
||||
|
||||
func Test_kmpIndex(t *testing.T) {
|
||||
intSlice := make([]*int, 10)
|
||||
for i := 0; i < len(intSlice); i++ {
|
||||
for i := range intSlice {
|
||||
intSlice[i] = new(int)
|
||||
*intSlice[i] = i
|
||||
}
|
||||
@@ -544,7 +544,7 @@ func Test_diffToBalances(t *testing.T) {
|
||||
}
|
||||
|
||||
targetBals := target.Balances()
|
||||
for i := 0; i < len(sourceBals); i++ {
|
||||
for i := range sourceBals {
|
||||
require.Equal(t, targetBals[i], sourceBals[i], "balance mismatch at index %d", i)
|
||||
}
|
||||
})
|
||||
@@ -665,7 +665,7 @@ func Test_applyStateDiff(t *testing.T) {
|
||||
// Test_computeLPS tests the LPS array computation for KMP algorithm
|
||||
func Test_computeLPS(t *testing.T) {
|
||||
intSlice := make([]*int, 10)
|
||||
for i := 0; i < len(intSlice); i++ {
|
||||
for i := range intSlice {
|
||||
intSlice[i] = new(int)
|
||||
*intSlice[i] = i
|
||||
}
|
||||
@@ -955,8 +955,7 @@ func BenchmarkGetDiff(b *testing.B) {
|
||||
source, target, err := getMainnetStates()
|
||||
require.NoError(b, err)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
hdiff, err := Diff(source, target)
|
||||
b.Log("Diff size:", len(hdiff.StateDiff)+len(hdiff.BalancesDiff)+len(hdiff.ValidatorDiffs))
|
||||
require.NoError(b, err)
|
||||
@@ -971,8 +970,8 @@ func BenchmarkApplyDiff(b *testing.B) {
|
||||
require.NoError(b, err)
|
||||
hdiff, err := Diff(source, target)
|
||||
require.NoError(b, err)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
for b.Loop() {
|
||||
source, err = ApplyDiff(b.Context(), source, hdiff)
|
||||
require.NoError(b, err)
|
||||
}
|
||||
@@ -998,7 +997,7 @@ func BenchmarkDiffCreation(b *testing.B) {
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_, err := Diff(source, target)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
@@ -1026,7 +1025,7 @@ func BenchmarkDiffApplication(b *testing.B) {
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
// Need fresh source for each iteration
|
||||
freshSource := source.Copy()
|
||||
_, err := ApplyDiff(ctx, freshSource, diff)
|
||||
@@ -1049,8 +1048,7 @@ func BenchmarkSerialization(b *testing.B) {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_ = hdiff.serialize()
|
||||
}
|
||||
}
|
||||
@@ -1067,8 +1065,7 @@ func BenchmarkDeserialization(b *testing.B) {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_, err := newHdiff(diff)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
@@ -1093,7 +1090,7 @@ func BenchmarkBalanceDiff(b *testing.B) {
|
||||
_ = target.SetBalances(balances)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_, err := diffToBalances(source, target)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
@@ -1123,7 +1120,7 @@ func BenchmarkValidatorDiff(b *testing.B) {
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_, err := diffToVals(source, target)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
@@ -1172,7 +1169,7 @@ func BenchmarkKMPAlgorithm(b *testing.B) {
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
_ = kmpIndex(len(pattern), text, intEquals)
|
||||
}
|
||||
})
|
||||
@@ -1201,7 +1198,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
|
||||
name: "balance_changes",
|
||||
modifier: func(target state.BeaconState) {
|
||||
balances := target.Balances()
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := range 10 {
|
||||
if i < len(balances) {
|
||||
balances[i] += 1000
|
||||
}
|
||||
@@ -1213,7 +1210,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
|
||||
name: "validator_changes",
|
||||
modifier: func(target state.BeaconState) {
|
||||
validators := target.Validators()
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := range 10 {
|
||||
if i < len(validators) {
|
||||
validators[i].EffectiveBalance += 1000
|
||||
}
|
||||
@@ -1235,7 +1232,7 @@ func BenchmarkCompressionRatio(b *testing.B) {
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
for i := 0; b.Loop(); i++ {
|
||||
diff, err := Diff(source, testTarget)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
@@ -1262,7 +1259,7 @@ func BenchmarkMemoryUsage(b *testing.B) {
|
||||
|
||||
// Modify some data
|
||||
validators := target.Validators()
|
||||
for i := 0; i < 25; i++ {
|
||||
for i := range 25 {
|
||||
if i < len(validators) {
|
||||
validators[i].EffectiveBalance += 1000
|
||||
}
|
||||
@@ -1270,9 +1267,8 @@ func BenchmarkMemoryUsage(b *testing.B) {
|
||||
_ = target.SetValidators(validators)
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
for b.Loop() {
|
||||
diff, err := Diff(source, target)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
|
||||
@@ -30,7 +30,7 @@ var (
|
||||
)
|
||||
|
||||
// New returns a new payload attribute with the given input object.
|
||||
func New(i interface{}) (Attributer, error) {
|
||||
func New(i any) (Attributer, error) {
|
||||
switch a := i.(type) {
|
||||
case nil:
|
||||
return nil, blocks.ErrNilObject
|
||||
|
||||
@@ -25,7 +25,7 @@ func (u *Uint64) UnmarshalJSON(bs []byte) error {
|
||||
}
|
||||
|
||||
// UnmarshalYAML custom unmarshal function for yaml
|
||||
func (u *Uint64) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
func (u *Uint64) UnmarshalYAML(unmarshal func(any) error) error {
|
||||
var str string
|
||||
err := unmarshal(&str)
|
||||
if err != nil {
|
||||
|
||||
@@ -42,7 +42,7 @@ func (m MetadataV0) CustodyGroupCount() uint64 {
|
||||
}
|
||||
|
||||
// InnerObject returns the underlying metadata protobuf structure.
|
||||
func (m MetadataV0) InnerObject() interface{} {
|
||||
func (m MetadataV0) InnerObject() any {
|
||||
return m.md
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ func (m MetadataV1) CustodyGroupCount() uint64 {
|
||||
}
|
||||
|
||||
// InnerObject returns the underlying metadata protobuf structure.
|
||||
func (m MetadataV1) InnerObject() interface{} {
|
||||
func (m MetadataV1) InnerObject() any {
|
||||
return m.md
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ func (m MetadataV2) CustodyGroupCount() uint64 {
|
||||
}
|
||||
|
||||
// InnerObject returns the underlying metadata protobuf structure.
|
||||
func (m MetadataV2) InnerObject() interface{} {
|
||||
func (m MetadataV2) InnerObject() any {
|
||||
return m.md
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user