chore(node/cmd): replace `fmt.Errorf` without parameters with `errors.New` (#4030)

This commit is contained in:
yukionfire 2024-07-24 22:05:48 +08:00 committed by GitHub
parent cdc23d152a
commit 450b41c891
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
@ -183,7 +184,7 @@ func getTransactor(ethC *ethclient.Client) (*abi.AbiTransactor, error) {
addr := common.HexToAddress(contractAddress) addr := common.HexToAddress(contractAddress)
emptyAddr := common.Address{} emptyAddr := common.Address{}
if addr == emptyAddr { if addr == emptyAddr {
return nil, fmt.Errorf("invalid contract address") return nil, errors.New("invalid contract address")
} }
t, err := abi.NewAbiTransactor(addr, ethC) t, err := abi.NewAbiTransactor(addr, ethC)
@ -196,7 +197,7 @@ func getTransactor(ethC *ethclient.Client) (*abi.AbiTransactor, error) {
func getGovernanceVaaAction(payload []byte) (uint8, error) { func getGovernanceVaaAction(payload []byte) (uint8, error) {
if len(payload) < 32+2+1 { if len(payload) < 32+2+1 {
return 0, fmt.Errorf("VAA payload does not contain a governance header") return 0, errors.New("VAA payload does not contain a governance header")
} }
return payload[32], nil return payload[32], nil

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
@ -274,7 +275,7 @@ func runP2P(
failedQueriesByUser.WithLabelValues(pendingResponse.userName).Inc() failedQueriesByUser.WithLabelValues(pendingResponse.userName).Inc()
delete(responses, requestSignature) delete(responses, requestSignature)
select { select {
case pendingResponse.errCh <- &ErrorEntry{err: fmt.Errorf("quorum not met"), status: http.StatusBadRequest}: case pendingResponse.errCh <- &ErrorEntry{err: errors.New("quorum not met"), status: http.StatusBadRequest}:
logger.Info("query failed, quorum not met", logger.Info("query failed, quorum not met",
zap.String("peerId", peerId), zap.String("peerId", peerId),
zap.String("userId", pendingResponse.userName), zap.String("userId", pendingResponse.userName),

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
@ -30,12 +31,12 @@ func FetchCurrentGuardianSet(rpcUrl, coreAddr string) (*common.GuardianSet, erro
ethContract := eth_common.HexToAddress(coreAddr) ethContract := eth_common.HexToAddress(coreAddr)
rawClient, err := ethRpc.DialContext(ctx, rpcUrl) rawClient, err := ethRpc.DialContext(ctx, rpcUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to connect to ethereum") return nil, errors.New("failed to connect to ethereum")
} }
client := ethClient.NewClient(rawClient) client := ethClient.NewClient(rawClient)
caller, err := ethAbi.NewAbiCaller(ethContract, client) caller, err := ethAbi.NewAbiCaller(ethContract, client)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create caller") return nil, errors.New("failed to create caller")
} }
currentIndex, err := caller.GetCurrentGuardianSetIndex(&ethBind.CallOpts{Context: ctx}) currentIndex, err := caller.GetCurrentGuardianSetIndex(&ethBind.CallOpts{Context: ctx})
if err != nil { if err != nil {
@ -57,7 +58,7 @@ func validateRequest(logger *zap.Logger, env common.Environment, perms *Permissi
if !exists { if !exists {
logger.Debug("invalid api key", zap.String("apiKey", apiKey)) logger.Debug("invalid api key", zap.String("apiKey", apiKey))
invalidQueryRequestReceived.WithLabelValues("invalid_api_key").Inc() invalidQueryRequestReceived.WithLabelValues("invalid_api_key").Inc()
return http.StatusForbidden, nil, fmt.Errorf("invalid api key") return http.StatusForbidden, nil, errors.New("invalid api key")
} }
// TODO: Should we verify the signatures? // TODO: Should we verify the signatures?
@ -70,7 +71,7 @@ func validateRequest(logger *zap.Logger, env common.Environment, perms *Permissi
zap.Bool("signerKeyConfigured", signerKey != nil), zap.Bool("signerKeyConfigured", signerKey != nil),
) )
invalidQueryRequestReceived.WithLabelValues("request_not_signed").Inc() invalidQueryRequestReceived.WithLabelValues("request_not_signed").Inc()
return http.StatusBadRequest, nil, fmt.Errorf("request not signed") return http.StatusBadRequest, nil, errors.New("request not signed")
} }
// Sign the request using our key. // Sign the request using our key.
@ -117,7 +118,7 @@ func validateRequest(logger *zap.Logger, env common.Environment, perms *Permissi
default: default:
logger.Debug("unsupported query type", zap.String("userName", permsForUser.userName), zap.Any("type", pcq.Query)) logger.Debug("unsupported query type", zap.String("userName", permsForUser.userName), zap.Any("type", pcq.Query))
invalidQueryRequestReceived.WithLabelValues("unsupported_query_type").Inc() invalidQueryRequestReceived.WithLabelValues("unsupported_query_type").Inc()
return http.StatusBadRequest, nil, fmt.Errorf("unsupported query type") return http.StatusBadRequest, nil, errors.New("unsupported query type")
} }
if err != nil { if err != nil {
@ -142,7 +143,7 @@ func validateCallData(logger *zap.Logger, permsForUser *permissionEntry, callTag
if len(cd.Data) < ETH_CALL_SIG_LENGTH { if len(cd.Data) < ETH_CALL_SIG_LENGTH {
logger.Debug("eth call data must be at least four bytes", zap.String("userName", permsForUser.userName), zap.String("data", hex.EncodeToString(cd.Data))) logger.Debug("eth call data must be at least four bytes", zap.String("userName", permsForUser.userName), zap.String("data", hex.EncodeToString(cd.Data)))
invalidQueryRequestReceived.WithLabelValues("bad_call_data").Inc() invalidQueryRequestReceived.WithLabelValues("bad_call_data").Inc()
return http.StatusBadRequest, fmt.Errorf("eth call data must be at least four bytes") return http.StatusBadRequest, errors.New("eth call data must be at least four bytes")
} }
if !permsForUser.allowAnything { if !permsForUser.allowAnything {
call := hex.EncodeToString(cd.Data[0:ETH_CALL_SIG_LENGTH]) call := hex.EncodeToString(cd.Data[0:ETH_CALL_SIG_LENGTH])

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/csv" "encoding/csv"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -238,7 +239,7 @@ func runSignWormchainValidatorAddress(cmd *cobra.Command, args []string) error {
guardianKeyPath := args[0] guardianKeyPath := args[0]
wormchainAddress := args[1] wormchainAddress := args[1]
if !strings.HasPrefix(wormchainAddress, "wormhole") || strings.HasPrefix(wormchainAddress, "wormholeval") { if !strings.HasPrefix(wormchainAddress, "wormhole") || strings.HasPrefix(wormchainAddress, "wormholeval") {
return fmt.Errorf("must provide a bech32 address that has 'wormhole' prefix") return errors.New("must provide a bech32 address that has 'wormhole' prefix")
} }
gk, err := common.LoadGuardianKey(guardianKeyPath, *unsafeDevnetMode) gk, err := common.LoadGuardianKey(guardianKeyPath, *unsafeDevnetMode)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package guardiand
import ( import (
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"log" "log"
"math/big" "math/big"
@ -1214,7 +1215,7 @@ func parseAddress(s string) (string, error) {
func leftPadAddress(a []byte) (string, error) { func leftPadAddress(a []byte) (string, error) {
if len(a) > 32 { if len(a) > 32 {
return "", fmt.Errorf("address longer than 32 bytes") return "", errors.New("address longer than 32 bytes")
} }
return hex.EncodeToString(common.LeftPadBytes(a, 32)), nil return hex.EncodeToString(common.LeftPadBytes(a, 32)), nil
} }
@ -1246,7 +1247,7 @@ func isValidUint256(s string) (bool, error) {
// Check if i is within the range [0, 2^256 - 1] // Check if i is within the range [0, 2^256 - 1]
if i.Cmp(big.NewInt(0)) < 0 || i.Cmp(upperLimit) > 0 { if i.Cmp(big.NewInt(0)) < 0 || i.Cmp(upperLimit) > 0 {
return false, fmt.Errorf("value is not a valid uint256") return false, errors.New("value is not a valid uint256")
} }
return true, nil return true, nil