Skip to content

Commit 32d537c

Browse files
all: replace fmt.Errorf with errors.New (ethereum#32286)
The errors.new function does not require string formatting, so its performance is better than that of fmt.Errorf.
1 parent a7aed7b commit 32d537c

File tree

16 files changed

+31
-28
lines changed

16 files changed

+31
-28
lines changed

cmd/devp2p/internal/ethtest/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (c *Conn) Write(proto Proto, code uint64, msg any) error {
129129
return err
130130
}
131131

132-
var errDisc error = fmt.Errorf("disconnect")
132+
var errDisc error = errors.New("disconnect")
133133

134134
// ReadEth reads an Eth sub-protocol wire message.
135135
func (c *Conn) ReadEth() (any, error) {

cmd/devp2p/internal/ethtest/suite.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ethtest
1919
import (
2020
"context"
2121
"crypto/rand"
22+
"errors"
2223
"fmt"
2324
"reflect"
2425
"sync"
@@ -1092,7 +1093,7 @@ func (s *Suite) testBadBlobTx(t *utesting.T, tx *types.Transaction, badTx *types
10921093
return
10931094
}
10941095
if !readUntilDisconnect(conn) {
1095-
errc <- fmt.Errorf("expected bad peer to be disconnected")
1096+
errc <- errors.New("expected bad peer to be disconnected")
10961097
return
10971098
}
10981099
stage3.Done()
@@ -1139,7 +1140,7 @@ func (s *Suite) testBadBlobTx(t *utesting.T, tx *types.Transaction, badTx *types
11391140
}
11401141

11411142
if req.GetPooledTransactionsRequest[0] != tx.Hash() {
1142-
errc <- fmt.Errorf("requested unknown tx hash")
1143+
errc <- errors.New("requested unknown tx hash")
11431144
return
11441145
}
11451146

@@ -1149,7 +1150,7 @@ func (s *Suite) testBadBlobTx(t *utesting.T, tx *types.Transaction, badTx *types
11491150
return
11501151
}
11511152
if readUntilDisconnect(conn) {
1152-
errc <- fmt.Errorf("unexpected disconnect")
1153+
errc <- errors.New("unexpected disconnect")
11531154
return
11541155
}
11551156
close(errc)

cmd/geth/chaincmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ func downloadEra(ctx *cli.Context) error {
716716
case ctx.IsSet(utils.SepoliaFlag.Name):
717717
network = "sepolia"
718718
default:
719-
return fmt.Errorf("unsupported network, no known era1 checksums")
719+
return errors.New("unsupported network, no known era1 checksums")
720720
}
721721
}
722722

cmd/workload/testsuite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package main
1818

1919
import (
2020
"embed"
21-
"fmt"
21+
"errors"
2222
"io/fs"
2323
"os"
2424

@@ -97,7 +97,7 @@ type testConfig struct {
9797
traceTestFile string
9898
}
9999

100-
var errPrunedHistory = fmt.Errorf("attempt to access pruned history")
100+
var errPrunedHistory = errors.New("attempt to access pruned history")
101101

102102
// validateHistoryPruneErr checks whether the given error is caused by access
103103
// to history before the pruning threshold block (it is an rpc.Error with code 4444).
@@ -109,7 +109,7 @@ func validateHistoryPruneErr(err error, blockNum uint64, historyPruneBlock *uint
109109
if err != nil {
110110
if rpcErr, ok := err.(rpc.Error); ok && rpcErr.ErrorCode() == 4444 {
111111
if historyPruneBlock != nil && blockNum > *historyPruneBlock {
112-
return fmt.Errorf("pruned history error returned after pruning threshold")
112+
return errors.New("pruned history error returned after pruning threshold")
113113
}
114114
return errPrunedHistory
115115
}

core/state/snapshot/journal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func iterateJournal(db ethdb.KeyValueReader, callback journalCallback) error {
350350
}
351351
if len(destructs) > 0 {
352352
log.Warn("Incompatible legacy journal detected", "version", journalV0)
353-
return fmt.Errorf("incompatible legacy journal detected")
353+
return errors.New("incompatible legacy journal detected")
354354
}
355355
}
356356
if err := r.Decode(&accounts); err != nil {

core/tracing/journal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package tracing
1818

1919
import (
20-
"fmt"
20+
"errors"
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
@@ -39,14 +39,14 @@ type entry interface {
3939
// WrapWithJournal wraps the given tracer with a journaling layer.
4040
func WrapWithJournal(hooks *Hooks) (*Hooks, error) {
4141
if hooks == nil {
42-
return nil, fmt.Errorf("wrapping nil tracer")
42+
return nil, errors.New("wrapping nil tracer")
4343
}
4444
// No state change to journal, return the wrapped hooks as is
4545
if hooks.OnBalanceChange == nil && hooks.OnNonceChange == nil && hooks.OnNonceChangeV2 == nil && hooks.OnCodeChange == nil && hooks.OnStorageChange == nil {
4646
return hooks, nil
4747
}
4848
if hooks.OnNonceChange != nil && hooks.OnNonceChangeV2 != nil {
49-
return nil, fmt.Errorf("cannot have both OnNonceChange and OnNonceChangeV2")
49+
return nil, errors.New("cannot have both OnNonceChange and OnNonceChangeV2")
5050
}
5151

5252
// Create a new Hooks instance and copy all hooks

core/txpool/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
145145
}
146146
if tx.Type() == types.SetCodeTxType {
147147
if len(tx.SetCodeAuthorizations()) == 0 {
148-
return fmt.Errorf("set code tx must have at least one authorization tuple")
148+
return errors.New("set code tx must have at least one authorization tuple")
149149
}
150150
}
151151
return nil

core/types/bal/bal_encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (e *AccountAccess) validate() error {
169169
// Convert code change
170170
if len(e.Code) == 1 {
171171
if len(e.Code[0].Code) > params.MaxCodeSize {
172-
return fmt.Errorf("code change contained oversized code")
172+
return errors.New("code change contained oversized code")
173173
}
174174
}
175175
return nil

eth/catalyst/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ func checkEqualBody(a *types.Body, b *engine.ExecutionPayloadBody) error {
14971497
}
14981498
}
14991499
if !reflect.DeepEqual(a.Withdrawals, b.Withdrawals) {
1500-
return fmt.Errorf("withdrawals mismatch")
1500+
return errors.New("withdrawals mismatch")
15011501
}
15021502
return nil
15031503
}

eth/ethconfig/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package ethconfig
1919

2020
import (
21-
"fmt"
21+
"errors"
2222
"time"
2323

2424
"github.com/ethereum/go-ethereum/common"
@@ -171,7 +171,7 @@ type Config struct {
171171
func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database) (consensus.Engine, error) {
172172
if config.TerminalTotalDifficulty == nil {
173173
log.Error("Geth only supports PoS networks. Please transition legacy networks using Geth v1.13.x.")
174-
return nil, fmt.Errorf("'terminalTotalDifficulty' is not set in genesis block")
174+
return nil, errors.New("'terminalTotalDifficulty' is not set in genesis block")
175175
}
176176
// Wrap previously supported consensus engines into their post-merge counterpart
177177
if config.Clique != nil {

0 commit comments

Comments
 (0)