Skip to content

Commit

Permalink
Enable perfsprint linter (#11379)
Browse files Browse the repository at this point in the history
Not sure if I agree that string concatenation is more readable than
using `fmt.Sprintf`. I could try to configure the linter to avoid this
and revert if it's interesting?
  • Loading branch information
estensen authored Jul 29, 2024
1 parent 1cd18e7 commit 85ce691
Show file tree
Hide file tree
Showing 249 changed files with 957 additions and 850 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ linters:
- errchkjson #TODO: enable me
- unused #TODO: enable me
- testifylint #TODO: enable me
- perfsprint #TODO: enable me
- gocheckcompilerdirectives
- protogetter
enable:
Expand All @@ -36,6 +35,7 @@ linters:
- wastedassign
- gofmt
- gocritic
- perfsprint
# - revive
# - forcetypeassert
# - stylecheck
Expand Down
7 changes: 4 additions & 3 deletions accounts/abi/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package abi

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (arguments Arguments) isTuple() bool {
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
if len(data) == 0 {
if len(arguments) != 0 {
return nil, fmt.Errorf("abi: attempting to unmarshall an empty string while arguments are expected")
return nil, errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
}
// Nothing to unmarshal, return default variables
nonIndexedArgs := arguments.NonIndexed()
Expand All @@ -99,11 +100,11 @@ func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) {
func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error {
// Make sure map is not nil
if v == nil {
return fmt.Errorf("abi: cannot unpack into a nil map")
return errors.New("abi: cannot unpack into a nil map")
}
if len(data) == 0 {
if len(arguments) != 0 {
return fmt.Errorf("abi: attempting to unmarshall an empty string while arguments are expected")
return errors.New("abi: attempting to unmarshall an empty string while arguments are expected")
}
return nil // Nothing to unmarshal, return
}
Expand Down
4 changes: 2 additions & 2 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *libcommon.Address
if opts.Value != nil {
overflow := value.SetFromBig(opts.Value)
if overflow {
return nil, fmt.Errorf("opts.Value higher than 2^256-1")
return nil, errors.New("opts.Value higher than 2^256-1")
}
}
var nonce uint64
Expand All @@ -240,7 +240,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *libcommon.Address
}
gasPrice, overflow := uint256.FromBig(gasPriceBig)
if overflow {
return nil, fmt.Errorf("gasPriceBig higher than 2^256-1")
return nil, errors.New("gasPriceBig higher than 2^256-1")
}
gasLimit := opts.GasLimit
if gasLimit == 0 {
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[stri
structFieldName := ToCamelCase(argName)

if structFieldName == "" {
return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct")
return nil, errors.New("abi: purely underscored output cannot unpack to struct")
}

// this abi has already been paired, skip it... unless there exists another, yet unassigned
Expand Down
4 changes: 2 additions & 2 deletions accounts/abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var (
func NewType(t string, internalType string, components []ArgumentMarshaling) (typ Type, err error) {
// check that array brackets are equal if they exist
if strings.Count(t, "[") != strings.Count(t, "]") {
return Type{}, fmt.Errorf("invalid arg type in abi")
return Type{}, errors.New("invalid arg type in abi")
}
typ.stringKind = t

Expand Down Expand Up @@ -110,7 +110,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}
typ.stringKind = embeddedType.stringKind + sliced
} else {
return Type{}, fmt.Errorf("invalid formatting of array type")
return Type{}, errors.New("invalid formatting of array type")
}
return typ, err
}
Expand Down
7 changes: 4 additions & 3 deletions accounts/abi/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package abi

import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"reflect"
Expand Down Expand Up @@ -98,7 +99,7 @@ func readBool(word []byte) (bool, error) {
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
if t.T != FunctionTy {
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array")
}
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
Expand All @@ -111,7 +112,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
// ReadFixedBytes uses reflection to create a fixed array to be read from.
func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
if t.T != FixedBytesTy {
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
return nil, errors.New("abi: invalid type in call to make fixed byte array")
}
// convert
array := reflect.New(t.GetType()).Elem()
Expand Down Expand Up @@ -140,7 +141,7 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
// declare our array
refSlice = reflect.New(t.GetType()).Elem()
} else {
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
}

// Arrays have packed elements, resulting in longer unpack steps.
Expand Down
6 changes: 3 additions & 3 deletions cl/aggregation/pool_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package aggregation

import (
"context"
"fmt"
"errors"
"sync"
"time"

Expand All @@ -30,7 +30,7 @@ import (
"github.com/erigontech/erigon/cl/utils/eth_clock"
)

var ErrIsSuperset = fmt.Errorf("attestation is superset of existing attestation")
var ErrIsSuperset = errors.New("attestation is superset of existing attestation")

var (
blsAggregate = bls.AggregateSignatures
Expand Down Expand Up @@ -89,7 +89,7 @@ func (p *aggregationPoolImpl) AddAttestation(inAtt *solid.Attestation) error {
return err
}
if len(merged) != 96 {
return fmt.Errorf("merged signature is too long")
return errors.New("merged signature is too long")
}
var mergedSig [96]byte
copy(mergedSig[:], merged)
Expand Down
2 changes: 1 addition & 1 deletion cl/antiquary/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func GetBellatrixRandom() ([]*cltypes.SignedBeaconBlock, *state.CachingBeaconSta
for i := 0; i < 96; i++ {
block := cltypes.NewSignedBeaconBlock(&clparams.MainnetBeaconConfig)
// Lets do te
b, err := bellatrixFS.ReadFile("test_data/bellatrix/blocks_" + strconv.FormatInt(int64(i), 10) + ".ssz_snappy")
b, err := bellatrixFS.ReadFile("test_data/bellatrix/blocks_" + strconv.Itoa(i) + ".ssz_snappy")
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/beaconhttp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func HandleEndpoint[T any](h EndpointHandler[T]) http.HandlerFunc {
case strings.Contains(contentType, "text/event-stream"):
return
default:
http.Error(w, fmt.Sprintf("content type must include application/json, application/octet-stream, or text/event-stream, got %s", contentType), http.StatusBadRequest)
http.Error(w, "content type must include application/json, application/octet-stream, or text/event-stream, got "+contentType, http.StatusBadRequest)
}
})
}
Expand Down
14 changes: 7 additions & 7 deletions cl/beacon/beaconhttp/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package beaconhttp

import (
"fmt"
"errors"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -73,7 +73,7 @@ func EpochFromRequest(r *http.Request) (uint64, error) {
regex := regexp.MustCompile(`^\d+$`)
epoch := chi.URLParam(r, "epoch")
if !regex.MatchString(epoch) {
return 0, fmt.Errorf("invalid path variable: {epoch}")
return 0, errors.New("invalid path variable: {epoch}")
}
epochMaybe, err := strconv.ParseUint(epoch, 10, 64)
if err != nil {
Expand All @@ -95,7 +95,7 @@ func BlockIdFromRequest(r *http.Request) (*SegmentID, error) {

blockId := chi.URLParam(r, "block_id")
if !regex.MatchString(blockId) {
return nil, fmt.Errorf("invalid path variable: {block_id}")
return nil, errors.New("invalid path variable: {block_id}")
}

if blockId == "head" {
Expand All @@ -122,7 +122,7 @@ func StateIdFromRequest(r *http.Request) (*SegmentID, error) {

stateId := chi.URLParam(r, "state_id")
if !regex.MatchString(stateId) {
return nil, fmt.Errorf("invalid path variable: {state_id}")
return nil, errors.New("invalid path variable: {state_id}")
}

if stateId == "head" {
Expand Down Expand Up @@ -154,17 +154,17 @@ func HashFromQueryParams(r *http.Request, name string) (*common.Hash, error) {
}
// check if hashstr is an hex string
if len(hashStr) != 2+2*32 {
return nil, fmt.Errorf("invalid hash length")
return nil, errors.New("invalid hash length")
}
if hashStr[:2] != "0x" {
return nil, fmt.Errorf("invalid hash prefix")
return nil, errors.New("invalid hash prefix")
}
notHex, err := regexp.MatchString("[^0-9A-Fa-f]", hashStr[2:])
if err != nil {
return nil, err
}
if notHex {
return nil, fmt.Errorf("invalid hash characters")
return nil, errors.New("invalid hash characters")
}

hash := common.HexToHash(hashStr)
Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/builder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
var _ BuilderClient = &builderClient{}

var (
ErrNoContent = fmt.Errorf("no http content")
ErrNoContent = errors.New("no http content")
)

type builderClient struct {
Expand Down
22 changes: 11 additions & 11 deletions cl/beacon/handler/attestation_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package handler

import (
"encoding/json"
"fmt"
"errors"
"io"
"net/http"

Expand Down Expand Up @@ -94,7 +94,7 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
}
headEpoch := headSlot / a.beaconChainCfg.SlotsPerEpoch
if epoch > headEpoch {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("epoch is in the future"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("epoch is in the future"))
}
// Few cases to handle:
// 1) finalized data
Expand All @@ -115,39 +115,39 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
continue
}
if version == clparams.Phase0Version {
return nil, beaconhttp.NewEndpointError(http.StatusHTTPVersionNotSupported, fmt.Errorf("phase0 state is not supported when there is no antiquation"))
return nil, beaconhttp.NewEndpointError(http.StatusHTTPVersionNotSupported, errors.New("phase0 state is not supported when there is no antiquation"))
}
inactivityScores, err := a.forkchoiceStore.GetInactivitiesScores(blockRoot)
if err != nil {
return nil, err
}
if inactivityScores == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no inactivity scores found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no inactivity scores found for this epoch"))
}

prevPartecipation, err := a.forkchoiceStore.GetPreviousPartecipationIndicies(blockRoot)
if err != nil {
return nil, err
}
if prevPartecipation == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no previous partecipation found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no previous partecipation found for this epoch"))
}
validatorSet, err := a.forkchoiceStore.GetValidatorSet(blockRoot)
if err != nil {
return nil, err
}
if validatorSet == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no validator set found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no validator set found for this epoch"))
}

ok, finalizedCheckpoint, _, _ := a.forkchoiceStore.GetFinalityCheckpoints(blockRoot)
if !ok {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no finalized checkpoint found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no finalized checkpoint found for this epoch"))
}

return a.computeAttestationsRewardsForAltair(validatorSet, inactivityScores, prevPartecipation, a.isInactivityLeaking(epoch, finalizedCheckpoint), filterIndicies, epoch)
}
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no block found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no block found for this epoch"))
}

root, err := a.findEpochRoot(tx, epoch)
Expand All @@ -159,7 +159,7 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
return nil, err
}
if lastSlotPtr == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no block found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no block found for this epoch"))
}
lastSlot := *lastSlotPtr

Expand All @@ -168,7 +168,7 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
return nil, err
}
if lastSlot > stateProgress {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("requested range is not yet processed or the node is not archivial"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("requested range is not yet processed or the node is not archivial"))
}

epochData, err := state_accessors.ReadEpochData(tx, a.beaconChainCfg.RoundSlotToEpoch(lastSlot))
Expand All @@ -181,7 +181,7 @@ func (a *ApiHandler) PostEthV1BeaconRewardsAttestations(w http.ResponseWriter, r
return nil, err
}
if validatorSet == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("no validator set found for this epoch"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("no validator set found for this epoch"))
}

_, previousIdx, err := a.stateReader.ReadPartecipations(tx, lastSlot)
Expand Down
4 changes: 2 additions & 2 deletions cl/beacon/handler/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package handler

import (
"fmt"
"errors"
"net/http"
"strconv"

Expand Down Expand Up @@ -50,7 +50,7 @@ func (a *ApiHandler) GetEthV1BeaconBlobSidecars(w http.ResponseWriter, r *http.R
return nil, err
}
if slot == nil {
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("block not found"))
return nil, beaconhttp.NewEndpointError(http.StatusNotFound, errors.New("block not found"))
}
if a.caplinSnapshots != nil && *slot <= a.caplinSnapshots.FrozenBlobs() {
out, err := a.caplinSnapshots.ReadBlobSidecars(*slot)
Expand Down
Loading

0 comments on commit 85ce691

Please sign in to comment.