Skip to content

Commit 8a78a4f

Browse files
authored
eth,consensus: replace noarg fmt.Errorf with errors.New (ethereum#27330)
* eth: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa <delweng@gmail.com> * consensus: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa <delweng@gmail.com> --------- Signed-off-by: jsvisa <delweng@gmail.com>
1 parent b21ba66 commit 8a78a4f

File tree

11 files changed

+32
-24
lines changed

11 files changed

+32
-24
lines changed

consensus/clique/clique.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
299299
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, params.MaxGasLimit)
300300
}
301301
if chain.Config().IsShanghai(header.Number, header.Time) {
302-
return fmt.Errorf("clique does not support shanghai fork")
302+
return errors.New("clique does not support shanghai fork")
303303
}
304304
if chain.Config().IsCancun(header.Number, header.Time) {
305-
return fmt.Errorf("clique does not support cancun fork")
305+
return errors.New("clique does not support cancun fork")
306306
}
307307
// All basic checks passed, verify cascading fields
308308
return c.verifyCascadingFields(chain, header, parents)

consensus/ethash/consensus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, pa
263263
return consensus.ErrInvalidNumber
264264
}
265265
if chain.Config().IsShanghai(header.Number, header.Time) {
266-
return fmt.Errorf("ethash does not support shanghai fork")
266+
return errors.New("ethash does not support shanghai fork")
267267
}
268268
if chain.Config().IsCancun(header.Number, header.Time) {
269-
return fmt.Errorf("ethash does not support cancun fork")
269+
return errors.New("ethash does not support cancun fork")
270270
}
271271
// Add some fake checks for tests
272272
if ethash.fakeDelay != nil {

consensus/misc/eip1559.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package misc
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/big"
2223

@@ -40,7 +41,7 @@ func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Heade
4041
}
4142
// Verify the header is not malformed
4243
if header.BaseFee == nil {
43-
return fmt.Errorf("header is missing baseFee")
44+
return errors.New("header is missing baseFee")
4445
}
4546
// Verify the baseFee is correct based on the parent header.
4647
expectedBaseFee := CalcBaseFee(config, parent)

eth/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func (api *DebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64, error
554554
if num.Int64() < 0 {
555555
block := api.eth.blockchain.CurrentBlock()
556556
if block == nil {
557-
return 0, fmt.Errorf("current block missing")
557+
return 0, errors.New("current block missing")
558558
}
559559
return block.Number.Uint64(), nil
560560
}
@@ -574,7 +574,7 @@ func (api *DebugAPI) GetAccessibleState(from, to rpc.BlockNumber) (uint64, error
574574
return 0, err
575575
}
576576
if start == end {
577-
return 0, fmt.Errorf("from and to needs to be different")
577+
return 0, errors.New("from and to needs to be different")
578578
}
579579
if start > end {
580580
delta = -1

eth/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (s *Ethereum) Etherbase() (eb common.Address, err error) {
323323
if etherbase != (common.Address{}) {
324324
return etherbase, nil
325325
}
326-
return common.Address{}, fmt.Errorf("etherbase must be explicitly specified")
326+
return common.Address{}, errors.New("etherbase must be explicitly specified")
327327
}
328328

329329
// isLocalBlock checks whether the specified block is mined

eth/catalyst/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ func NewConsensusAPI(eth *eth.Ethereum) *ConsensusAPI {
166166
func (api *ConsensusAPI) ForkchoiceUpdatedV1(update engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes) (engine.ForkChoiceResponse, error) {
167167
if payloadAttributes != nil {
168168
if payloadAttributes.Withdrawals != nil {
169-
return engine.STATUS_INVALID, engine.InvalidParams.With(fmt.Errorf("withdrawals not supported in V1"))
169+
return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("withdrawals not supported in V1"))
170170
}
171171
if api.eth.BlockChain().Config().IsShanghai(api.eth.BlockChain().Config().LondonBlock, payloadAttributes.Timestamp) {
172-
return engine.STATUS_INVALID, engine.InvalidParams.With(fmt.Errorf("forkChoiceUpdateV1 called post-shanghai"))
172+
return engine.STATUS_INVALID, engine.InvalidParams.With(errors.New("forkChoiceUpdateV1 called post-shanghai"))
173173
}
174174
}
175175
return api.forkchoiceUpdated(update, payloadAttributes)
@@ -386,7 +386,7 @@ func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config engine.Transit
386386
TerminalBlockNumber: config.TerminalBlockNumber,
387387
}, nil
388388
}
389-
return nil, fmt.Errorf("invalid terminal block hash")
389+
return nil, errors.New("invalid terminal block hash")
390390
}
391391
return &engine.TransitionConfigurationV1{TerminalTotalDifficulty: (*hexutil.Big)(ttd)}, nil
392392
}
@@ -417,7 +417,7 @@ func (api *ConsensusAPI) getPayload(payloadID engine.PayloadID) (*engine.Executi
417417
// NewPayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
418418
func (api *ConsensusAPI) NewPayloadV1(params engine.ExecutableData) (engine.PayloadStatusV1, error) {
419419
if params.Withdrawals != nil {
420-
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(fmt.Errorf("withdrawals not supported in V1"))
420+
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("withdrawals not supported in V1"))
421421
}
422422
return api.newPayload(params)
423423
}
@@ -426,10 +426,10 @@ func (api *ConsensusAPI) NewPayloadV1(params engine.ExecutableData) (engine.Payl
426426
func (api *ConsensusAPI) NewPayloadV2(params engine.ExecutableData) (engine.PayloadStatusV1, error) {
427427
if api.eth.BlockChain().Config().IsShanghai(new(big.Int).SetUint64(params.Number), params.Timestamp) {
428428
if params.Withdrawals == nil {
429-
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(fmt.Errorf("nil withdrawals post-shanghai"))
429+
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("nil withdrawals post-shanghai"))
430430
}
431431
} else if params.Withdrawals != nil {
432-
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(fmt.Errorf("non-nil withdrawals pre-shanghai"))
432+
return engine.PayloadStatusV1{Status: engine.INVALID}, engine.InvalidParams.With(errors.New("non-nil withdrawals pre-shanghai"))
433433
}
434434
return api.newPayload(params)
435435
}

eth/filters/api.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import (
3333
"github.com/ethereum/go-ethereum/rpc"
3434
)
3535

36+
var (
37+
errInvalidTopic = errors.New("invalid topic(s)")
38+
errFilterNotFound = errors.New("filter not found")
39+
)
40+
3641
// filter is a helper struct that holds meta information over the filter type
3742
// and associated subscription in the event system.
3843
type filter struct {
@@ -376,7 +381,7 @@ func (api *FilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*types.Lo
376381
api.filtersMu.Unlock()
377382

378383
if !found || f.typ != LogsSubscription {
379-
return nil, fmt.Errorf("filter not found")
384+
return nil, errFilterNotFound
380385
}
381386

382387
var filter *Filter
@@ -452,7 +457,7 @@ func (api *FilterAPI) GetFilterChanges(id rpc.ID) (interface{}, error) {
452457
}
453458
}
454459

455-
return []interface{}{}, fmt.Errorf("filter not found")
460+
return []interface{}{}, errFilterNotFound
456461
}
457462

458463
// returnHashes is a helper that will return an empty hash array case the given hash array is nil,
@@ -491,7 +496,7 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
491496
if raw.BlockHash != nil {
492497
if raw.FromBlock != nil || raw.ToBlock != nil {
493498
// BlockHash is mutually exclusive with FromBlock/ToBlock criteria
494-
return fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock, choose one or the other")
499+
return errors.New("cannot specify both BlockHash and FromBlock/ToBlock, choose one or the other")
495500
}
496501
args.BlockHash = raw.BlockHash
497502
} else {
@@ -564,11 +569,11 @@ func (args *FilterCriteria) UnmarshalJSON(data []byte) error {
564569
}
565570
args.Topics[i] = append(args.Topics[i], parsed)
566571
} else {
567-
return fmt.Errorf("invalid topic(s)")
572+
return errInvalidTopic
568573
}
569574
}
570575
default:
571-
return fmt.Errorf("invalid topic(s)")
576+
return errInvalidTopic
572577
}
573578
}
574579
}

eth/filters/filter_system.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package filters
2020

2121
import (
2222
"context"
23+
"errors"
2324
"fmt"
2425
"sync"
2526
"sync/atomic"
@@ -331,7 +332,7 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
331332
if from >= 0 && to == rpc.LatestBlockNumber {
332333
return es.subscribeLogs(crit, logs), nil
333334
}
334-
return nil, fmt.Errorf("invalid from and to block combination: from > to")
335+
return nil, errors.New("invalid from and to block combination: from > to")
335336
}
336337

337338
// subscribeMinedPendingLogs creates a subscription that returned mined and

eth/tracers/js/goja.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
8989
b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes()
9090
return b, nil
9191
}
92-
return nil, fmt.Errorf("invalid buffer type")
92+
return nil, errors.New("invalid buffer type")
9393
}
9494

9595
// jsTracer is an implementation of the Tracer interface which evaluates

eth/tracers/logger/logger_test.go

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

1919
import (
2020
"encoding/json"
21-
"fmt"
21+
"errors"
2222
"math/big"
2323
"testing"
2424

@@ -85,7 +85,7 @@ func TestStructLogMarshalingOmitEmpty(t *testing.T) {
8585
}{
8686
{"empty err and no fields", &StructLog{},
8787
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},
88-
{"with err", &StructLog{Err: fmt.Errorf("this failed")},
88+
{"with err", &StructLog{Err: errors.New("this failed")},
8989
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memSize":0,"stack":null,"depth":0,"refund":0,"opName":"STOP","error":"this failed"}`},
9090
{"with mem", &StructLog{Memory: make([]byte, 2), MemorySize: 2},
9191
`{"pc":0,"op":0,"gas":"0x0","gasCost":"0x0","memory":"0x0000","memSize":2,"stack":null,"depth":0,"refund":0,"opName":"STOP"}`},

0 commit comments

Comments
 (0)