From b241223c7ed1da57dd090dea97214909832cef59 Mon Sep 17 00:00:00 2001 From: Vlad <13818348+walldiss@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:37:18 +0400 Subject: [PATCH] chore(linters): add errorlint (#3334) Adds errorlint linter and fixes related issues --- .golangci.yml | 1 + api/gateway/availability.go | 7 ++-- blob/service.go | 6 +-- cmd/rpc.go | 6 +-- das/subscriber.go | 3 +- nodebuilder/blob/cmd/blob.go | 20 +++++----- nodebuilder/header/cmd/header.go | 4 +- nodebuilder/header/service.go | 3 +- nodebuilder/p2p/addrs.go | 6 +-- nodebuilder/p2p/config.go | 2 +- nodebuilder/state/cmd/state.go | 68 ++++++++++++++++---------------- pruner/checkpoint.go | 3 +- share/eds/ods.go | 4 +- share/eds/store.go | 6 +-- share/p2p/shrexeds/client.go | 2 +- share/p2p/shrexnd/client.go | 2 +- 16 files changed, 74 insertions(+), 69 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e9fc3713de..7ebd04f913 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,7 @@ linters: - dogsled - dupl - errcheck + - errorlint # - funlen # - gochecknoglobals # - gochecknoinits diff --git a/api/gateway/availability.go b/api/gateway/availability.go index 70341b34f2..e8e96083d5 100644 --- a/api/gateway/availability.go +++ b/api/gateway/availability.go @@ -2,6 +2,7 @@ package gateway import ( "encoding/json" + "errors" "net/http" "strconv" @@ -33,8 +34,8 @@ func (h *Handler) handleHeightAvailabilityRequest(w http.ResponseWriter, r *http } err = h.share.SharesAvailable(r.Context(), header) - switch err { - case nil: + switch { + case err == nil: resp, err := json.Marshal(&AvailabilityResponse{Available: true}) if err != nil { writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err) @@ -44,7 +45,7 @@ func (h *Handler) handleHeightAvailabilityRequest(w http.ResponseWriter, r *http if werr != nil { log.Errorw("serving request", "endpoint", heightAvailabilityEndpoint, "err", err) } - case share.ErrNotAvailable: + case errors.Is(err, share.ErrNotAvailable): resp, err := json.Marshal(&AvailabilityResponse{Available: false}) if err != nil { writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err) diff --git a/blob/service.go b/blob/service.go index 7445c05054..1a81a1ccd4 100644 --- a/blob/service.go +++ b/blob/service.go @@ -239,9 +239,9 @@ func (s *Service) Included( return blob.compareCommitments(commitment) }} _, resProof, err := s.retrieve(ctx, height, namespace, sharesParser) - switch err { - case nil: - case ErrBlobNotFound: + switch { + case err == nil: + case errors.Is(err, ErrBlobNotFound): return false, nil default: return false, err diff --git a/cmd/rpc.go b/cmd/rpc.go index 2a6c267d54..230b51508b 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -48,12 +48,12 @@ func InitClient(cmd *cobra.Command, _ []string) error { storePath, err := getStorePath(cmd) if err != nil { - return fmt.Errorf("%s: %v", rootErrMsg, err) + return fmt.Errorf("%s: %w", rootErrMsg, err) } cfg, err := nodebuilder.LoadConfig(filepath.Join(storePath, "config.toml")) if err != nil { - return fmt.Errorf("%s: root directory was not specified: %v", rootErrMsg, err) + return fmt.Errorf("%s: root directory was not specified: %w", rootErrMsg, err) } if requestURL == "" { @@ -66,7 +66,7 @@ func InitClient(cmd *cobra.Command, _ []string) error { } else { token, err := getToken(storePath) if err != nil { - return fmt.Errorf("%s: %v", rootErrMsg, err) + return fmt.Errorf("%s: %w", rootErrMsg, err) } authTokenFlag = token diff --git a/das/subscriber.go b/das/subscriber.go index 6af894d2dc..dc185dcab7 100644 --- a/das/subscriber.go +++ b/das/subscriber.go @@ -2,6 +2,7 @@ package das import ( "context" + "errors" libhead "github.com/celestiaorg/go-header" @@ -25,7 +26,7 @@ func (s *subscriber) run(ctx context.Context, sub libhead.Subscription[*header.E for { h, err := sub.NextHeader(ctx) if err != nil { - if err == context.Canceled { + if errors.Is(err, context.Canceled) { return } diff --git a/nodebuilder/blob/cmd/blob.go b/nodebuilder/blob/cmd/blob.go index 78dd57f250..9d18c38e10 100644 --- a/nodebuilder/blob/cmd/blob.go +++ b/nodebuilder/blob/cmd/blob.go @@ -73,17 +73,17 @@ var getCmd = &cobra.Command{ height, err := strconv.ParseUint(args[0], 10, 64) if err != nil { - return fmt.Errorf("error parsing a height:%v", err) + return fmt.Errorf("error parsing a height: %w", err) } namespace, err := cmdnode.ParseV0Namespace(args[1]) if err != nil { - return fmt.Errorf("error parsing a namespace:%v", err) + return fmt.Errorf("error parsing a namespace: %w", err) } commitment, err := base64.StdEncoding.DecodeString(args[2]) if err != nil { - return fmt.Errorf("error parsing a commitment:%v", err) + return fmt.Errorf("error parsing a commitment: %w", err) } blob, err := client.Blob.Get(cmd.Context(), height, namespace, commitment) @@ -109,12 +109,12 @@ var getAllCmd = &cobra.Command{ height, err := strconv.ParseUint(args[0], 10, 64) if err != nil { - return fmt.Errorf("error parsing a height:%v", err) + return fmt.Errorf("error parsing a height: %w", err) } namespace, err := cmdnode.ParseV0Namespace(args[1]) if err != nil { - return fmt.Errorf("error parsing a namespace:%v", err) + return fmt.Errorf("error parsing a namespace: %w", err) } blobs, err := client.Blob.GetAll(cmd.Context(), height, []share.Namespace{namespace}) @@ -224,12 +224,12 @@ var submitCmd = &cobra.Command{ func getBlobFromArguments(namespaceArg, blobArg string) (*blob.Blob, error) { namespace, err := cmdnode.ParseV0Namespace(namespaceArg) if err != nil { - return nil, fmt.Errorf("error parsing a namespace:%v", err) + return nil, fmt.Errorf("error parsing a namespace: %w", err) } parsedBlob, err := blob.NewBlobV0(namespace, []byte(blobArg)) if err != nil { - return nil, fmt.Errorf("error creating a blob:%v", err) + return nil, fmt.Errorf("error creating a blob: %w", err) } return parsedBlob, nil @@ -248,17 +248,17 @@ var getProofCmd = &cobra.Command{ height, err := strconv.ParseUint(args[0], 10, 64) if err != nil { - return fmt.Errorf("error parsing a height:%v", err) + return fmt.Errorf("error parsing a height: %w", err) } namespace, err := cmdnode.ParseV0Namespace(args[1]) if err != nil { - return fmt.Errorf("error parsing a namespace:%v", err) + return fmt.Errorf("error parsing a namespace: %w", err) } commitment, err := base64.StdEncoding.DecodeString(args[2]) if err != nil { - return fmt.Errorf("error parsing a commitment:%v", err) + return fmt.Errorf("error parsing a commitment: %w", err) } proof, err := client.Blob.GetProof(cmd.Context(), height, namespace, commitment) diff --git a/nodebuilder/header/cmd/header.go b/nodebuilder/header/cmd/header.go index 6bcf84d414..4e884050aa 100644 --- a/nodebuilder/header/cmd/header.go +++ b/nodebuilder/header/cmd/header.go @@ -72,7 +72,7 @@ var getByHashCmd = &cobra.Command{ hash, err := hex.DecodeString(args[0]) if err != nil { - return fmt.Errorf("error decoding a hash: expected a hex encoded string:%v", err) + return fmt.Errorf("error decoding a hash: expected a hex encoded string: %w", err) } header, err := client.Header.GetByHash(cmd.Context(), hash) return cmdnode.PrintOutput(header, err, nil) @@ -92,7 +92,7 @@ var getByHeightCmd = &cobra.Command{ height, err := strconv.ParseUint(args[0], 10, 64) if err != nil { - return fmt.Errorf("error parsing a height:%v", err) + return fmt.Errorf("error parsing a height: %w", err) } header, err := client.Header.GetByHeight(cmd.Context(), height) diff --git a/nodebuilder/header/service.go b/nodebuilder/header/service.go index 55bbc6e209..e769cd5299 100644 --- a/nodebuilder/header/service.go +++ b/nodebuilder/header/service.go @@ -2,6 +2,7 @@ package header import ( "context" + "errors" "fmt" libhead "github.com/celestiaorg/go-header" @@ -125,7 +126,7 @@ func (s *Service) Subscribe(ctx context.Context) (<-chan *header.ExtendedHeader, for { h, err := subscription.NextHeader(ctx) if err != nil { - if err != context.DeadlineExceeded && err != context.Canceled { + if !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) { log.Errorw("fetching header from subscription", "err", err) } return diff --git a/nodebuilder/p2p/addrs.go b/nodebuilder/p2p/addrs.go index 0fb29008e0..28f6e44407 100644 --- a/nodebuilder/p2p/addrs.go +++ b/nodebuilder/p2p/addrs.go @@ -15,7 +15,7 @@ func Listen(listen []string) func(h hst.Host) (err error) { for i, addr := range listen { maListen[i], err = ma.NewMultiaddr(addr) if err != nil { - return fmt.Errorf("failure to parse config.P2P.ListenAddresses: %s", err) + return fmt.Errorf("failure to parse config.P2P.ListenAddresses: %w", err) } } return h.Network().Listen(maListen...) @@ -30,7 +30,7 @@ func addrsFactory(announce, noAnnounce []string) func() (_ p2pconfig.AddrsFactor for i, addr := range announce { maAnnounce[i], err = ma.NewMultiaddr(addr) if err != nil { - return nil, fmt.Errorf("failure to parse config.P2P.AnnounceAddresses: %s", err) + return nil, fmt.Errorf("failure to parse config.P2P.AnnounceAddresses: %w", err) } } @@ -40,7 +40,7 @@ func addrsFactory(announce, noAnnounce []string) func() (_ p2pconfig.AddrsFactor for _, addr := range noAnnounce { maddr, err := ma.NewMultiaddr(addr) if err != nil { - return nil, fmt.Errorf("failure to parse config.P2P.NoAnnounceAddresses: %s", err) + return nil, fmt.Errorf("failure to parse config.P2P.NoAnnounceAddresses: %w", err) } maNoAnnounce[string(maddr.Bytes())] = true } diff --git a/nodebuilder/p2p/config.go b/nodebuilder/p2p/config.go index 99f3fe8879..c95f5a98cf 100644 --- a/nodebuilder/p2p/config.go +++ b/nodebuilder/p2p/config.go @@ -71,7 +71,7 @@ func (cfg *Config) mutualPeers() (_ []peer.AddrInfo, err error) { for i, addr := range cfg.MutualPeers { maddrs[i], err = ma.NewMultiaddr(addr) if err != nil { - return nil, fmt.Errorf("failure to parse config.P2P.MutualPeers: %s", err) + return nil, fmt.Errorf("failure to parse config.P2P.MutualPeers: %w", err) } } diff --git a/nodebuilder/state/cmd/state.go b/nodebuilder/state/cmd/state.go index 833f4afc06..7915cf382d 100644 --- a/nodebuilder/state/cmd/state.go +++ b/nodebuilder/state/cmd/state.go @@ -95,7 +95,7 @@ var balanceForAddressCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } balance, err := client.State.BalanceForAddress(cmd.Context(), addr) @@ -116,20 +116,20 @@ var transferCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } amount, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing an amount:%v", err) + return fmt.Errorf("error parsing an amount: %w", err) } fee, err := strconv.ParseInt(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[3], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.Transfer( @@ -155,7 +155,7 @@ var submitTxCmd = &cobra.Command{ decoded, err := hex.DecodeString(args[0]) if err != nil { - return fmt.Errorf("failed to decode tx: %v", err) + return fmt.Errorf("failed to decode tx: %w", err) } txResponse, err := client.State.SubmitTx( cmd.Context(), @@ -178,27 +178,27 @@ var cancelUnbondingDelegationCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } amount, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing an amount:%v", err) + return fmt.Errorf("error parsing an amount: %w", err) } height, err := strconv.ParseInt(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } fee, err := strconv.ParseInt(args[3], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[4], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.CancelUnbondingDelegation( @@ -226,26 +226,26 @@ var beginRedelegateCmd = &cobra.Command{ srcAddr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } dstAddr, err := parseAddressFromString(args[1]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } amount, err := strconv.ParseInt(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing an amount:%v", err) + return fmt.Errorf("error parsing an amount: %w", err) } fee, err := strconv.ParseInt(args[3], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[4], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.BeginRedelegate( @@ -273,20 +273,20 @@ var undelegateCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } amount, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing an amount:%v", err) + return fmt.Errorf("error parsing an amount: %w", err) } fee, err := strconv.ParseInt(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[3], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.Undelegate( @@ -313,22 +313,22 @@ var delegateCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } amount, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing an amount:%v", err) + return fmt.Errorf("error parsing an amount: %w", err) } fee, err := strconv.ParseInt(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[3], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.Delegate( @@ -355,7 +355,7 @@ var queryDelegationCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } balance, err := client.State.QueryDelegation(cmd.Context(), addr.Address.(state.ValAddress)) @@ -376,7 +376,7 @@ var queryUnbondingCmd = &cobra.Command{ addr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } response, err := client.State.QueryUnbonding(cmd.Context(), addr.Address.(state.ValAddress)) @@ -397,12 +397,12 @@ var queryRedelegationCmd = &cobra.Command{ srcAddr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing a src address:%v", err) + return fmt.Errorf("error parsing a src address: %w", err) } dstAddr, err := parseAddressFromString(args[1]) if err != nil { - return fmt.Errorf("error parsing a dst address:%v", err) + return fmt.Errorf("error parsing a dst address: %w", err) } response, err := client.State.QueryRedelegations( @@ -428,16 +428,16 @@ var grantFeeCmd = &cobra.Command{ granteeAddr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } fee, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.GrantFee( @@ -462,16 +462,16 @@ var revokeGrantFeeCmd = &cobra.Command{ granteeAddr, err := parseAddressFromString(args[0]) if err != nil { - return fmt.Errorf("error parsing an address:%v", err) + return fmt.Errorf("error parsing an address: %w", err) } fee, err := strconv.ParseInt(args[1], 10, 64) if err != nil { - return fmt.Errorf("error parsing a fee:%v", err) + return fmt.Errorf("error parsing a fee: %w", err) } gasLimit, err := strconv.ParseUint(args[2], 10, 64) if err != nil { - return fmt.Errorf("error parsing a gas limit:%v", err) + return fmt.Errorf("error parsing a gas limit: %w", err) } txResponse, err := client.State.RevokeGrantFee( diff --git a/pruner/checkpoint.go b/pruner/checkpoint.go index 10db918cb5..ef9f777b6e 100644 --- a/pruner/checkpoint.go +++ b/pruner/checkpoint.go @@ -3,6 +3,7 @@ package pruner import ( "context" "encoding/json" + "errors" "fmt" "github.com/ipfs/go-datastore" @@ -31,7 +32,7 @@ func (s *Service) initializeCheckpoint(ctx context.Context) error { func (s *Service) loadCheckpoint(ctx context.Context) error { bin, err := s.ds.Get(ctx, checkpointKey) if err != nil { - if err == datastore.ErrNotFound { + if errors.Is(err, datastore.ErrNotFound) { return s.initializeCheckpoint(ctx) } return fmt.Errorf("failed to load checkpoint: %w", err) diff --git a/share/eds/ods.go b/share/eds/ods.go index aa1219d41a..a924e1c235 100644 --- a/share/eds/ods.go +++ b/share/eds/ods.go @@ -38,7 +38,7 @@ func ODSReader(carReader io.Reader) (io.Reader, error) { // first LdRead reads the full CAR header to determine amount of shares in the ODS data, err := util.LdRead(odsR.carReader) if err != nil { - return nil, fmt.Errorf("reading header: %v", err) + return nil, fmt.Errorf("reading header: %w", err) } var header car.CarHeader @@ -79,7 +79,7 @@ func (r *bufferedODSReader) readLeaf() error { l, err := binary.ReadUvarint(r.carReader) if err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { return io.ErrUnexpectedEOF // don't silently pretend this is a clean EOF } return err diff --git a/share/eds/store.go b/share/eds/store.go index 8a73f9bf07..72d1e0bc82 100644 --- a/share/eds/store.go +++ b/share/eds/store.go @@ -566,10 +566,10 @@ func (s *Store) Has(ctx context.Context, root share.DataHash) (has bool, err err func (s *Store) has(_ context.Context, root share.DataHash) (bool, error) { key := root.String() info, err := s.dgstr.GetShardInfo(shard.KeyFromString(key)) - switch err { - case nil: + switch { + case err == nil: return true, info.Error - case dagstore.ErrShardUnknown: + case errors.Is(err, dagstore.ErrShardUnknown): return false, info.Error default: return false, err diff --git a/share/p2p/shrexeds/client.go b/share/p2p/shrexeds/client.go index 7602bb5fb0..d56e0e20f5 100644 --- a/share/p2p/shrexeds/client.go +++ b/share/p2p/shrexeds/client.go @@ -68,7 +68,7 @@ func (c *Client) RequestEDS( return nil, context.DeadlineExceeded } } - if err != p2p.ErrNotFound { + if !errors.Is(err, p2p.ErrNotFound) { log.Warnw("client: eds request to peer failed", "peer", peer.String(), "hash", dataHash.String(), diff --git a/share/p2p/shrexnd/client.go b/share/p2p/shrexnd/client.go index 86c5150095..4fb6c8904b 100644 --- a/share/p2p/shrexnd/client.go +++ b/share/p2p/shrexnd/client.go @@ -73,7 +73,7 @@ func (c *Client) RequestND( return nil, context.DeadlineExceeded } } - if err != p2p.ErrNotFound && err != p2p.ErrRateLimited { + if !errors.Is(err, p2p.ErrNotFound) && !errors.Is(err, p2p.ErrRateLimited) { log.Warnw("client-nd: peer returned err", "err", err) } return nil, err