Skip to content

Commit 6c73276

Browse files
authored
core,console: replace noarg fmt.Errorf with errors.New (ethereum#27332)
* core: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa <delweng@gmail.com> * console: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa <delweng@gmail.com> * core: go autoimport Signed-off-by: jsvisa <delweng@gmail.com> * core: dry Signed-off-by: jsvisa <delweng@gmail.com> --------- Signed-off-by: jsvisa <delweng@gmail.com>
1 parent 690249d commit 6c73276

10 files changed

+47
-41
lines changed

console/bridge.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package console
1818

1919
import (
2020
"encoding/json"
21+
"errors"
2122
"fmt"
2223
"io"
2324
"reflect"
@@ -77,18 +78,18 @@ func (b *bridge) NewAccount(call jsre.Call) (goja.Value, error) {
7778
return nil, err
7879
}
7980
if password != confirm {
80-
return nil, fmt.Errorf("passwords don't match!")
81+
return nil, errors.New("passwords don't match!")
8182
}
8283
// A single string password was specified, use that
8384
case len(call.Arguments) == 1 && call.Argument(0).ToString() != nil:
8485
password = call.Argument(0).ToString().String()
8586
default:
86-
return nil, fmt.Errorf("expected 0 or 1 string argument")
87+
return nil, errors.New("expected 0 or 1 string argument")
8788
}
8889
// Password acquired, execute the call and return
8990
newAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("newAccount"))
9091
if !callable {
91-
return nil, fmt.Errorf("jeth.newAccount is not callable")
92+
return nil, errors.New("jeth.newAccount is not callable")
9293
}
9394
ret, err := newAccount(goja.Null(), call.VM.ToValue(password))
9495
if err != nil {
@@ -102,7 +103,7 @@ func (b *bridge) NewAccount(call jsre.Call) (goja.Value, error) {
102103
func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) {
103104
// Make sure we have a wallet specified to open
104105
if call.Argument(0).ToObject(call.VM).ClassName() != "String" {
105-
return nil, fmt.Errorf("first argument must be the wallet URL to open")
106+
return nil, errors.New("first argument must be the wallet URL to open")
106107
}
107108
wallet := call.Argument(0)
108109

@@ -115,7 +116,7 @@ func (b *bridge) OpenWallet(call jsre.Call) (goja.Value, error) {
115116
// Open the wallet and return if successful in itself
116117
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
117118
if !callable {
118-
return nil, fmt.Errorf("jeth.openWallet is not callable")
119+
return nil, errors.New("jeth.openWallet is not callable")
119120
}
120121
val, err := openWallet(goja.Null(), wallet, passwd)
121122
if err == nil {
@@ -198,7 +199,7 @@ func (b *bridge) readPassphraseAndReopenWallet(call jsre.Call) (goja.Value, erro
198199
}
199200
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
200201
if !callable {
201-
return nil, fmt.Errorf("jeth.openWallet is not callable")
202+
return nil, errors.New("jeth.openWallet is not callable")
202203
}
203204
return openWallet(goja.Null(), wallet, call.VM.ToValue(input))
204205
}
@@ -219,7 +220,7 @@ func (b *bridge) readPinAndReopenWallet(call jsre.Call) (goja.Value, error) {
219220
}
220221
openWallet, callable := goja.AssertFunction(getJeth(call.VM).Get("openWallet"))
221222
if !callable {
222-
return nil, fmt.Errorf("jeth.openWallet is not callable")
223+
return nil, errors.New("jeth.openWallet is not callable")
223224
}
224225
return openWallet(goja.Null(), wallet, call.VM.ToValue(input))
225226
}
@@ -230,13 +231,13 @@ func (b *bridge) readPinAndReopenWallet(call jsre.Call) (goja.Value, error) {
230231
// the RPC call.
231232
func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
232233
if len(call.Arguments) < 1 {
233-
return nil, fmt.Errorf("usage: unlockAccount(account, [ password, duration ])")
234+
return nil, errors.New("usage: unlockAccount(account, [ password, duration ])")
234235
}
235236

236237
account := call.Argument(0)
237238
// Make sure we have an account specified to unlock.
238239
if goja.IsUndefined(account) || goja.IsNull(account) || account.ExportType().Kind() != reflect.String {
239-
return nil, fmt.Errorf("first argument must be the account to unlock")
240+
return nil, errors.New("first argument must be the account to unlock")
240241
}
241242

242243
// If password is not given or is the null value, prompt the user for it.
@@ -250,7 +251,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
250251
passwd = call.VM.ToValue(input)
251252
} else {
252253
if call.Argument(1).ExportType().Kind() != reflect.String {
253-
return nil, fmt.Errorf("password must be a string")
254+
return nil, errors.New("password must be a string")
254255
}
255256
passwd = call.Argument(1)
256257
}
@@ -259,15 +260,15 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
259260
duration := goja.Null()
260261
if !goja.IsUndefined(call.Argument(2)) && !goja.IsNull(call.Argument(2)) {
261262
if !isNumber(call.Argument(2)) {
262-
return nil, fmt.Errorf("unlock duration must be a number")
263+
return nil, errors.New("unlock duration must be a number")
263264
}
264265
duration = call.Argument(2)
265266
}
266267

267268
// Send the request to the backend and return.
268269
unlockAccount, callable := goja.AssertFunction(getJeth(call.VM).Get("unlockAccount"))
269270
if !callable {
270-
return nil, fmt.Errorf("jeth.unlockAccount is not callable")
271+
return nil, errors.New("jeth.unlockAccount is not callable")
271272
}
272273
return unlockAccount(goja.Null(), account, passwd, duration)
273274
}
@@ -277,7 +278,7 @@ func (b *bridge) UnlockAccount(call jsre.Call) (goja.Value, error) {
277278
// jeth.sign) with it to actually execute the RPC call.
278279
func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
279280
if nArgs := len(call.Arguments); nArgs < 2 {
280-
return nil, fmt.Errorf("usage: sign(message, account, [ password ])")
281+
return nil, errors.New("usage: sign(message, account, [ password ])")
281282
}
282283
var (
283284
message = call.Argument(0)
@@ -286,10 +287,10 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
286287
)
287288

288289
if goja.IsUndefined(message) || message.ExportType().Kind() != reflect.String {
289-
return nil, fmt.Errorf("first argument must be the message to sign")
290+
return nil, errors.New("first argument must be the message to sign")
290291
}
291292
if goja.IsUndefined(account) || account.ExportType().Kind() != reflect.String {
292-
return nil, fmt.Errorf("second argument must be the account to sign with")
293+
return nil, errors.New("second argument must be the account to sign with")
293294
}
294295

295296
// if the password is not given or null ask the user and ensure password is a string
@@ -301,25 +302,25 @@ func (b *bridge) Sign(call jsre.Call) (goja.Value, error) {
301302
}
302303
passwd = call.VM.ToValue(input)
303304
} else if passwd.ExportType().Kind() != reflect.String {
304-
return nil, fmt.Errorf("third argument must be the password to unlock the account")
305+
return nil, errors.New("third argument must be the password to unlock the account")
305306
}
306307

307308
// Send the request to the backend and return
308309
sign, callable := goja.AssertFunction(getJeth(call.VM).Get("sign"))
309310
if !callable {
310-
return nil, fmt.Errorf("jeth.sign is not callable")
311+
return nil, errors.New("jeth.sign is not callable")
311312
}
312313
return sign(goja.Null(), message, account, passwd)
313314
}
314315

315316
// Sleep will block the console for the specified number of seconds.
316317
func (b *bridge) Sleep(call jsre.Call) (goja.Value, error) {
317318
if nArgs := len(call.Arguments); nArgs < 1 {
318-
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
319+
return nil, errors.New("usage: sleep(<number of seconds>)")
319320
}
320321
sleepObj := call.Argument(0)
321322
if goja.IsUndefined(sleepObj) || goja.IsNull(sleepObj) || !isNumber(sleepObj) {
322-
return nil, fmt.Errorf("usage: sleep(<number of seconds>)")
323+
return nil, errors.New("usage: sleep(<number of seconds>)")
323324
}
324325
sleep := sleepObj.ToFloat()
325326
time.Sleep(time.Duration(sleep * float64(time.Second)))
@@ -336,17 +337,17 @@ func (b *bridge) SleepBlocks(call jsre.Call) (goja.Value, error) {
336337
)
337338
nArgs := len(call.Arguments)
338339
if nArgs == 0 {
339-
return nil, fmt.Errorf("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
340+
return nil, errors.New("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
340341
}
341342
if nArgs >= 1 {
342343
if goja.IsNull(call.Argument(0)) || goja.IsUndefined(call.Argument(0)) || !isNumber(call.Argument(0)) {
343-
return nil, fmt.Errorf("expected number as first argument")
344+
return nil, errors.New("expected number as first argument")
344345
}
345346
blocks = call.Argument(0).ToInteger()
346347
}
347348
if nArgs >= 2 {
348349
if goja.IsNull(call.Argument(1)) || goja.IsUndefined(call.Argument(1)) || !isNumber(call.Argument(1)) {
349-
return nil, fmt.Errorf("expected number as second argument")
350+
return nil, errors.New("expected number as second argument")
350351
}
351352
sleep = call.Argument(1).ToInteger()
352353
}
@@ -421,7 +422,7 @@ func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
421422
JSON := call.VM.Get("JSON").ToObject(call.VM)
422423
parse, callable := goja.AssertFunction(JSON.Get("parse"))
423424
if !callable {
424-
return nil, fmt.Errorf("JSON.parse is not a function")
425+
return nil, errors.New("JSON.parse is not a function")
425426
}
426427
resultVal, err := parse(goja.Null(), call.VM.ToValue(string(result)))
427428
if err != nil {

core/block_validator.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package core
1818

1919
import (
20+
"errors"
2021
"fmt"
2122

2223
"github.com/ethereum/go-ethereum/consensus"
@@ -71,14 +72,14 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
7172
if header.WithdrawalsHash != nil {
7273
// Withdrawals list must be present in body after Shanghai.
7374
if block.Withdrawals() == nil {
74-
return fmt.Errorf("missing withdrawals in block body")
75+
return errors.New("missing withdrawals in block body")
7576
}
7677
if hash := types.DeriveSha(block.Withdrawals(), trie.NewStackTrie(nil)); hash != *header.WithdrawalsHash {
7778
return fmt.Errorf("withdrawals root hash mismatch (header value %x, calculated %x)", *header.WithdrawalsHash, hash)
7879
}
7980
} else if block.Withdrawals() != nil {
8081
// Withdrawals are not allowed prior to shanghai fork
81-
return fmt.Errorf("withdrawals present in block body")
82+
return errors.New("withdrawals present in block body")
8283
}
8384

8485
if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {

core/blockchain.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ var (
8787

8888
errInsertionInterrupted = errors.New("insertion is interrupted")
8989
errChainStopped = errors.New("blockchain is stopped")
90+
errInvalidOldChain = errors.New("invalid old chain")
91+
errInvalidNewChain = errors.New("invalid new chain")
9092
)
9193

9294
const (
@@ -865,7 +867,7 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
865867
return fmt.Errorf("export failed on #%d: not found", nr)
866868
}
867869
if nr > first && block.ParentHash() != parentHash {
868-
return fmt.Errorf("export failed: chain reorg during export")
870+
return errors.New("export failed: chain reorg during export")
869871
}
870872
parentHash = block.Hash()
871873
if err := block.EncodeRLP(w); err != nil {
@@ -2097,10 +2099,10 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
20972099
}
20982100
}
20992101
if oldBlock == nil {
2100-
return errors.New("invalid old chain")
2102+
return errInvalidOldChain
21012103
}
21022104
if newBlock == nil {
2103-
return errors.New("invalid new chain")
2105+
return errInvalidNewChain
21042106
}
21052107
// Both sides of the reorg are at the same number, reduce both until the common
21062108
// ancestor is found
@@ -2120,11 +2122,11 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
21202122
// Step back with both chains
21212123
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
21222124
if oldBlock == nil {
2123-
return fmt.Errorf("invalid old chain")
2125+
return errInvalidOldChain
21242126
}
21252127
newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
21262128
if newBlock == nil {
2127-
return fmt.Errorf("invalid new chain")
2129+
return errInvalidNewChain
21282130
}
21292131
}
21302132

core/chain_indexer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package core
1919
import (
2020
"context"
2121
"encoding/binary"
22+
"errors"
2223
"fmt"
2324
"sync"
2425
"sync/atomic"
@@ -403,7 +404,7 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
403404
if header == nil {
404405
return common.Hash{}, fmt.Errorf("block #%d [%x..] not found", number, hash[:4])
405406
} else if header.ParentHash != lastHead {
406-
return common.Hash{}, fmt.Errorf("chain reorged during section processing")
407+
return common.Hash{}, errors.New("chain reorged during section processing")
407408
}
408409
if err := c.backend.Process(c.ctx, header); err != nil {
409410
return common.Hash{}, err

core/genesis.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ReadGenesis(db ethdb.Database) (*Genesis, error) {
7373
}
7474
blob := rawdb.ReadGenesisStateSpec(db, stored)
7575
if blob == nil {
76-
return nil, fmt.Errorf("genesis state missing from db")
76+
return nil, errors.New("genesis state missing from db")
7777
}
7878
if len(blob) != 0 {
7979
if err := genesis.Alloc.UnmarshalJSON(blob); err != nil {
@@ -82,11 +82,11 @@ func ReadGenesis(db ethdb.Database) (*Genesis, error) {
8282
}
8383
genesis.Config = rawdb.ReadChainConfig(db, stored)
8484
if genesis.Config == nil {
85-
return nil, fmt.Errorf("genesis config missing from db")
85+
return nil, errors.New("genesis config missing from db")
8686
}
8787
genesisBlock := rawdb.ReadBlock(db, stored, 0)
8888
if genesisBlock == nil {
89-
return nil, fmt.Errorf("genesis block missing from db")
89+
return nil, errors.New("genesis block missing from db")
9090
}
9191
genesisHeader := genesisBlock.Header()
9292
genesis.Nonce = genesisHeader.Nonce.Uint64()
@@ -366,7 +366,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *trie.Database, gen
366366
// are returned to the caller unless we're already at block zero.
367367
head := rawdb.ReadHeadHeader(db)
368368
if head == nil {
369-
return newcfg, stored, fmt.Errorf("missing head header")
369+
return newcfg, stored, errors.New("missing head header")
370370
}
371371
compatErr := storedcfg.CheckCompatible(newcfg, head.Number.Uint64(), head.Time)
372372
if compatErr != nil && ((head.Number.Uint64() != 0 && compatErr.RewindToBlock != 0) || (head.Time != 0 && compatErr.RewindToTime != 0)) {

core/rawdb/freezer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func (f *Freezer) MigrateTable(kind string, convert convertLegacyFn) error {
434434
// TODO(s1na): This is a sanity-check since as of now no process does tail-deletion. But the migration
435435
// process assumes no deletion at tail and needs to be modified to account for that.
436436
if table.itemOffset.Load() > 0 || table.itemHidden.Load() > 0 {
437-
return fmt.Errorf("migration not supported for tail-deleted freezers")
437+
return errors.New("migration not supported for tail-deleted freezers")
438438
}
439439
ancientsPath := filepath.Dir(table.index.Name())
440440
// Set up new dir for the migrated table, the content of which

core/state/snapshot/journal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func iterateJournal(db ethdb.KeyValueReader, callback journalCallback) error {
305305
}
306306
if baseRoot := rawdb.ReadSnapshotRoot(db); baseRoot != parent {
307307
log.Warn("Loaded snapshot journal", "diskroot", baseRoot, "diffs", "unmatched")
308-
return fmt.Errorf("mismatched disk and diff layers")
308+
return errors.New("mismatched disk and diff layers")
309309
}
310310
for {
311311
var (

core/state_processor.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package core
1818

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

@@ -92,7 +93,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
9293
// Fail if Shanghai not enabled and len(withdrawals) is non-zero.
9394
withdrawals := block.Withdrawals()
9495
if len(withdrawals) > 0 && !p.config.IsShanghai(block.Number(), block.Time()) {
95-
return nil, nil, 0, fmt.Errorf("withdrawals before shanghai")
96+
return nil, nil, 0, errors.New("withdrawals before shanghai")
9697
}
9798
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
9899
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), withdrawals)

core/types/log_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package types
1818

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

@@ -97,7 +97,7 @@ var unmarshalLogTests = map[string]struct {
9797
},
9898
"missing data": {
9999
input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
100-
wantError: fmt.Errorf("missing required field 'data' for Log"),
100+
wantError: errors.New("missing required field 'data' for Log"),
101101
},
102102
}
103103

core/types/transaction_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func assertEqual(orig *Transaction, cpy *Transaction) error {
526526
}
527527
if orig.AccessList() != nil {
528528
if !reflect.DeepEqual(orig.AccessList(), cpy.AccessList()) {
529-
return fmt.Errorf("access list wrong!")
529+
return errors.New("access list wrong!")
530530
}
531531
}
532532
return nil

0 commit comments

Comments
 (0)