From b21ba668e605cee87b34fe2b9b86bd541cc7ccf1 Mon Sep 17 00:00:00 2001 From: Delweng Date: Thu, 25 May 2023 14:54:28 +0800 Subject: [PATCH] internal,tests: replace noarg fmt.Errorf with errors.New (#27335) * internal: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa * tests: replace noarg fmt.Errorf with errors.New Signed-off-by: jsvisa * tests: go autoimport Signed-off-by: jsvisa * tests: go autoimport Signed-off-by: jsvisa --------- Signed-off-by: jsvisa --- internal/build/archive.go | 3 ++- internal/ethapi/api.go | 26 ++++++++++++------------ internal/ethapi/transaction_args.go | 2 +- internal/ethapi/transaction_args_test.go | 16 +++++++-------- tests/fuzzers/trie/trie-fuzzer.go | 3 ++- tests/init_test.go | 3 ++- tests/rlp_test_util.go | 4 ++-- tests/state_test_util.go | 3 ++- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/internal/build/archive.go b/internal/build/archive.go index c16246070..5b37c0edf 100644 --- a/internal/build/archive.go +++ b/internal/build/archive.go @@ -20,6 +20,7 @@ import ( "archive/tar" "archive/zip" "compress/gzip" + "errors" "fmt" "io" "os" @@ -90,7 +91,7 @@ func WriteArchive(name string, files []string) (err error) { }() archive, basename := NewArchive(archfd) if archive == nil { - return fmt.Errorf("unknown archive extension") + return errors.New("unknown archive extension") } fmt.Println(name) if err := archive.Directory(basename); err != nil { diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 9a821be0c..69b5bf130 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -478,16 +478,16 @@ func (s *PersonalAccountAPI) SignTransaction(ctx context.Context, args Transacti // No need to obtain the noncelock mutex, since we won't be sending this // tx into the transaction pool, but right back to the user if args.From == nil { - return nil, fmt.Errorf("sender not specified") + return nil, errors.New("sender not specified") } if args.Gas == nil { - return nil, fmt.Errorf("gas not specified") + return nil, errors.New("gas not specified") } if args.GasPrice == nil && (args.MaxFeePerGas == nil || args.MaxPriorityFeePerGas == nil) { - return nil, fmt.Errorf("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas") + return nil, errors.New("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas") } if args.Nonce == nil { - return nil, fmt.Errorf("nonce not specified") + return nil, errors.New("nonce not specified") } // Before actually signing the transaction, ensure the transaction fee is reasonable. tx := args.toTransaction() @@ -548,7 +548,7 @@ func (s *PersonalAccountAPI) EcRecover(ctx context.Context, data, sig hexutil.By return common.Address{}, fmt.Errorf("signature must be %d bytes long", crypto.SignatureLength) } if sig[crypto.RecoveryIDOffset] != 27 && sig[crypto.RecoveryIDOffset] != 28 { - return common.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)") + return common.Address{}, errors.New("invalid Ethereum signature (V is not 27 or 28)") } sig[crypto.RecoveryIDOffset] -= 27 // Transform yellow paper V from 27/28 to 0/1 @@ -582,7 +582,7 @@ func (s *PersonalAccountAPI) InitializeWallet(ctx context.Context, url string) ( case *scwallet.Wallet: return mnemonic, wallet.Initialize(seed) default: - return "", fmt.Errorf("specified wallet does not support initialization") + return "", errors.New("specified wallet does not support initialization") } } @@ -597,7 +597,7 @@ func (s *PersonalAccountAPI) Unpair(ctx context.Context, url string, pin string) case *scwallet.Wallet: return wallet.Unpair([]byte(pin)) default: - return fmt.Errorf("specified wallet does not support pairing") + return errors.New("specified wallet does not support pairing") } } @@ -722,10 +722,10 @@ func decodeHash(s string) (common.Hash, error) { } b, err := hex.DecodeString(s) if err != nil { - return common.Hash{}, fmt.Errorf("hex string invalid") + return common.Hash{}, errors.New("hex string invalid") } if len(b) > 32 { - return common.Hash{}, fmt.Errorf("hex string too long, want at most 32 bytes") + return common.Hash{}, errors.New("hex string too long, want at most 32 bytes") } return common.BytesToHash(b), nil } @@ -1833,13 +1833,13 @@ type SignTransactionResult struct { // the given from address and it needs to be unlocked. func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { if args.Gas == nil { - return nil, fmt.Errorf("gas not specified") + return nil, errors.New("gas not specified") } if args.GasPrice == nil && (args.MaxPriorityFeePerGas == nil || args.MaxFeePerGas == nil) { - return nil, fmt.Errorf("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas") + return nil, errors.New("missing gasPrice or maxFeePerGas/maxPriorityFeePerGas") } if args.Nonce == nil { - return nil, fmt.Errorf("nonce not specified") + return nil, errors.New("nonce not specified") } if err := args.setDefaults(ctx, s.b); err != nil { return nil, err @@ -1888,7 +1888,7 @@ func (s *TransactionAPI) PendingTransactions() ([]*RPCTransaction, error) { // the given transaction from the pool and reinsert it with the new gas price and limit. func (s *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error) { if sendArgs.Nonce == nil { - return common.Hash{}, fmt.Errorf("missing transaction nonce in transaction spec") + return common.Hash{}, errors.New("missing transaction nonce in transaction spec") } if err := sendArgs.setDefaults(ctx, s.b); err != nil { return common.Hash{}, err diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index c74f540b7..6e2690e09 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -157,7 +157,7 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro } } else { if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { - return fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active") + return errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active") } // London not active, set gas price. price, err := b.SuggestGasTipCap(ctx) diff --git a/internal/ethapi/transaction_args_test.go b/internal/ethapi/transaction_args_test.go index 4da98718b..0868f8762 100644 --- a/internal/ethapi/transaction_args_test.go +++ b/internal/ethapi/transaction_args_test.go @@ -18,7 +18,7 @@ package ethapi import ( "context" - "fmt" + "errors" "math/big" "reflect" "testing" @@ -138,28 +138,28 @@ func TestSetFeeDefaults(t *testing.T) { false, &TransactionArgs{MaxFeePerGas: maxFee}, nil, - fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), + errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), }, { "dynamic fee tx pre-London, priorityFee set", false, &TransactionArgs{MaxPriorityFeePerGas: fortytwo}, nil, - fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), + errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"), }, { "dynamic fee tx, maxFee < priorityFee", true, &TransactionArgs{MaxFeePerGas: maxFee, MaxPriorityFeePerGas: (*hexutil.Big)(big.NewInt(1000))}, nil, - fmt.Errorf("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"), + errors.New("maxFeePerGas (0x3e) < maxPriorityFeePerGas (0x3e8)"), }, { "dynamic fee tx, maxFee < priorityFee while setting default", true, &TransactionArgs{MaxFeePerGas: (*hexutil.Big)(big.NewInt(7))}, nil, - fmt.Errorf("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"), + errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"), }, // Misc @@ -168,21 +168,21 @@ func TestSetFeeDefaults(t *testing.T) { false, &TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, nil, - fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), + errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), }, { "set gas price and maxPriorityFee", false, &TransactionArgs{GasPrice: fortytwo, MaxPriorityFeePerGas: fortytwo}, nil, - fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), + errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), }, { "set gas price and maxFee", true, &TransactionArgs{GasPrice: fortytwo, MaxFeePerGas: maxFee}, nil, - fmt.Errorf("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), + errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), }, } diff --git a/tests/fuzzers/trie/trie-fuzzer.go b/tests/fuzzers/trie/trie-fuzzer.go index 41baf67ec..7be41b57c 100644 --- a/tests/fuzzers/trie/trie-fuzzer.go +++ b/tests/fuzzers/trie/trie-fuzzer.go @@ -19,6 +19,7 @@ package trie import ( "bytes" "encoding/binary" + "errors" "fmt" "github.com/ethereum/go-ethereum/core/rawdb" @@ -183,7 +184,7 @@ func runRandTest(rt randTest) error { checktr.MustUpdate(it.Key, it.Value) } if tr.Hash() != checktr.Hash() { - return fmt.Errorf("hash mismatch in opItercheckhash") + return errors.New("hash mismatch in opItercheckhash") } case opProve: rt[i].err = tr.Prove(step.key, 0, proofDb{}) diff --git a/tests/init_test.go b/tests/init_test.go index 9d315f951..7d8743efc 100644 --- a/tests/init_test.go +++ b/tests/init_test.go @@ -18,6 +18,7 @@ package tests import ( "encoding/json" + "errors" "fmt" "io" "os" @@ -180,7 +181,7 @@ func (tm *testMatcher) checkFailure(t *testing.T, err error) error { t.Logf("error: %v", err) return nil } - return fmt.Errorf("test succeeded unexpectedly") + return errors.New("test succeeded unexpectedly") } return err } diff --git a/tests/rlp_test_util.go b/tests/rlp_test_util.go index 15acb3a24..e4bd5450a 100644 --- a/tests/rlp_test_util.go +++ b/tests/rlp_test_util.go @@ -59,7 +59,7 @@ func FromHex(s string) ([]byte, error) { func (t *RLPTest) Run() error { outb, err := FromHex(t.Out) if err != nil { - return fmt.Errorf("invalid hex in Out") + return errors.New("invalid hex in Out") } // Handle simple decoding tests with no actual In value. @@ -87,7 +87,7 @@ func checkDecodeInterface(b []byte, isValid bool) error { case isValid && err != nil: return fmt.Errorf("decoding failed: %v", err) case !isValid && err == nil: - return fmt.Errorf("decoding of invalid value succeeded") + return errors.New("decoding of invalid value succeeded") } return nil } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index d9b646918..eec91b67a 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -19,6 +19,7 @@ package tests import ( "encoding/hex" "encoding/json" + "errors" "fmt" "math/big" "strconv" @@ -395,7 +396,7 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Mess tx.MaxFeePerGas) } if gasPrice == nil { - return nil, fmt.Errorf("no gas price provided") + return nil, errors.New("no gas price provided") } msg := &core.Message{