Skip to content

Commit 1ca7d76

Browse files
authored
Merge pull request #557 from JukLee0ira/reps-fmtErrorf
WIP: use errrors.New instead of empty fmt.Errorf (ethereum#27329)
2 parents b718f35 + 2d89951 commit 1ca7d76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+359
-318
lines changed

XDCxlending/lendingstate/lendingcontract.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package lendingstate
22

33
import (
4-
"fmt"
4+
"errors"
55
"math/big"
66

77
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
@@ -273,10 +273,10 @@ func GetAllLendingBooks(statedb *state.StateDB) (mapLendingBook map[common.Hash]
273273
baseTokens := GetSupportedBaseToken(statedb)
274274
terms := GetSupportedTerms(statedb)
275275
if len(baseTokens) == 0 {
276-
return nil, fmt.Errorf("GetAllLendingBooks: empty baseToken list")
276+
return nil, errors.New("GetAllLendingBooks: empty baseToken list")
277277
}
278278
if len(terms) == 0 {
279-
return nil, fmt.Errorf("GetAllLendingPairs: empty term list")
279+
return nil, errors.New("GetAllLendingPairs: empty term list")
280280
}
281281
for _, baseToken := range baseTokens {
282282
for _, term := range terms {
@@ -295,10 +295,10 @@ func GetAllLendingPairs(statedb *state.StateDB) (allPairs []LendingPair, err err
295295
baseTokens := GetSupportedBaseToken(statedb)
296296
collaterals := GetAllCollateral(statedb)
297297
if len(baseTokens) == 0 {
298-
return allPairs, fmt.Errorf("GetAllLendingPairs: empty baseToken list")
298+
return allPairs, errors.New("GetAllLendingPairs: empty baseToken list")
299299
}
300300
if len(collaterals) == 0 {
301-
return allPairs, fmt.Errorf("GetAllLendingPairs: empty collateral list")
301+
return allPairs, errors.New("GetAllLendingPairs: empty collateral list")
302302
}
303303
for _, baseToken := range baseTokens {
304304
for _, collateral := range collaterals {

XDCxlending/lendingstate/lendingitem.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lendingstate
22

33
import (
4+
"errors"
45
"fmt"
56
"math/big"
67
"strconv"
@@ -359,7 +360,7 @@ func (l *LendingItem) VerifyLendingSignature() error {
359360
tx.ImportSignature(V, R, S)
360361
from, _ := types.LendingSender(types.LendingTxSigner{}, tx)
361362
if from != tx.UserAddress() {
362-
return fmt.Errorf("verify lending item: invalid signature")
363+
return errors.New("verify lending item: invalid signature")
363364
}
364365
return nil
365366
}
@@ -473,10 +474,10 @@ func VerifyBalance(isXDCXLendingFork bool, statedb *state.StateDB, lendingStateD
473474
}
474475
return nil
475476
default:
476-
return fmt.Errorf("VerifyBalance: unknown lending side")
477+
return errors.New("VerifyBalance: unknown lending side")
477478
}
478479
default:
479-
return fmt.Errorf("VerifyBalance: unknown lending type")
480+
return errors.New("VerifyBalance: unknown lending type")
480481
}
481482
return nil
482483
}

XDCxlending/order_processor.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package XDCxlending
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"math/big"
78

@@ -264,7 +265,7 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address
264265
borrowFee = lendingstate.GetFee(statedb, oldestOrder.Relayer)
265266
}
266267
if collateralToken.IsZero() {
267-
return nil, nil, nil, fmt.Errorf("empty collateral")
268+
return nil, nil, nil, errors.New("empty collateral")
268269
}
269270
depositRate, liquidationRate, recallRate := lendingstate.GetCollateralDetail(statedb, collateralToken)
270271
if depositRate == nil || depositRate.Sign() <= 0 {
@@ -282,10 +283,10 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address
282283
return nil, nil, nil, err
283284
}
284285
if lendTokenXDCPrice == nil || lendTokenXDCPrice.Sign() <= 0 {
285-
return nil, nil, nil, fmt.Errorf("invalid lendToken price")
286+
return nil, nil, nil, errors.New("invalid lendToken price")
286287
}
287288
if collateralPrice == nil || collateralPrice.Sign() <= 0 {
288-
return nil, nil, nil, fmt.Errorf("invalid collateral price")
289+
return nil, nil, nil, errors.New("invalid collateral price")
289290
}
290291
tradedQuantity, collateralLockedAmount, rejectMaker, settleBalanceResult, err := l.getLendQuantity(lendTokenXDCPrice, collateralPrice, depositRate, borrowFee, coinbase, chain, header, statedb, order, &oldestOrder, maxTradedQuantity)
291292
if err != nil && err == lendingstate.ErrQuantityTradeTooSmall && tradedQuantity != nil && tradedQuantity.Sign() >= 0 {

accounts/abi/abi.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
7979
// Unpack output in v according to the abi specification
8080
func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
8181
if len(output) == 0 {
82-
return fmt.Errorf("abi: unmarshalling empty output")
82+
return errors.New("abi: unmarshalling empty output")
8383
}
8484
// since there can't be naming collisions with contracts and events,
8585
// we need to decide whether we're calling a method or an event
8686
if method, ok := abi.Methods[name]; ok {
8787
if len(output)%32 != 0 {
88-
return fmt.Errorf("abi: improperly formatted output")
88+
return errors.New("abi: improperly formatted output")
8989
}
9090
return method.Outputs.Unpack(v, output)
9191
} else if event, ok := abi.Events[name]; ok {
9292
return event.Inputs.Unpack(v, output)
9393
}
94-
return fmt.Errorf("abi: could not locate named method or event")
94+
return errors.New("abi: could not locate named method or event")
9595
}
9696

9797
// UnmarshalJSON implements json.Unmarshaler interface

accounts/abi/bind/base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
335335
return errNoEventSignature
336336
}
337337
if log.Topics[0] != c.abi.Events[event].Id() {
338-
return fmt.Errorf("event signature mismatch")
338+
return errors.New("event signature mismatch")
339339
}
340340
if len(log.Data) > 0 {
341341
if err := c.abi.Unpack(out, event, log.Data); err != nil {

accounts/abi/bind/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package bind
1818

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

2424
"github.com/XinFinOrg/XDPoSChain/common"
@@ -56,14 +56,14 @@ func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*ty
5656
// contract address when it is mined. It stops waiting when ctx is canceled.
5757
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
5858
if tx.To() != nil {
59-
return common.Address{}, fmt.Errorf("tx is not contract creation")
59+
return common.Address{}, errors.New("tx is not contract creation")
6060
}
6161
receipt, err := WaitMined(ctx, b, tx)
6262
if err != nil {
6363
return common.Address{}, err
6464
}
6565
if receipt.ContractAddress == (common.Address{}) {
66-
return common.Address{}, fmt.Errorf("zero address")
66+
return common.Address{}, errors.New("zero address")
6767
}
6868
// Check that code has indeed been deployed at the address.
6969
// This matters on pre-Homestead chains: OOG in the constructor

accounts/abi/reflect.go

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

1919
import (
20+
"errors"
2021
"fmt"
2122
"reflect"
2223
)
@@ -117,7 +118,7 @@ func requireUniqueStructFieldNames(args Arguments) error {
117118
for _, arg := range args {
118119
field := capitalise(arg.Name)
119120
if field == "" {
120-
return fmt.Errorf("abi: purely underscored output cannot unpack to struct")
121+
return errors.New("abi: purely underscored output cannot unpack to struct")
121122
}
122123
if exists[field] {
123124
return fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", field)

accounts/abi/type.go

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

1919
import (
20+
"errors"
2021
"fmt"
2122
"reflect"
2223
"regexp"
@@ -61,7 +62,7 @@ var (
6162
func NewType(t string) (typ Type, err error) {
6263
// check that array brackets are equal if they exist
6364
if strings.Count(t, "[") != strings.Count(t, "]") {
64-
return Type{}, fmt.Errorf("invalid arg type in abi")
65+
return Type{}, errors.New("invalid arg type in abi")
6566
}
6667

6768
typ.stringKind = t
@@ -98,7 +99,7 @@ func NewType(t string) (typ Type, err error) {
9899
}
99100
typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type)
100101
} else {
101-
return Type{}, fmt.Errorf("invalid formatting of array type")
102+
return Type{}, errors.New("invalid formatting of array type")
102103
}
103104
return typ, err
104105
}

accounts/abi/unpack.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package abi
1818

1919
import (
2020
"encoding/binary"
21+
"errors"
2122
"fmt"
2223
"math/big"
2324
"reflect"
@@ -70,7 +71,7 @@ func readBool(word []byte) (bool, error) {
7071
// This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
7172
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
7273
if t.T != FunctionTy {
73-
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
74+
return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array")
7475
}
7576
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
7677
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
@@ -83,7 +84,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
8384
// through reflection, creates a fixed array to be read from
8485
func readFixedBytes(t Type, word []byte) (interface{}, error) {
8586
if t.T != FixedBytesTy {
86-
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
87+
return nil, errors.New("abi: invalid type in call to make fixed byte array")
8788
}
8889
// convert
8990
array := reflect.New(t.Type).Elem()
@@ -123,7 +124,7 @@ func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error)
123124
// declare our array
124125
refSlice = reflect.New(t.Type).Elem()
125126
} else {
126-
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
127+
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
127128
}
128129

129130
// Arrays have packed elements, resulting in longer unpack steps.

accounts/keystore/account_cache_test.go

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

1919
import (
20+
"errors"
2021
"fmt"
2122
"math/rand"
2223
"os"
@@ -305,7 +306,7 @@ func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error {
305306
select {
306307
case <-ks.changes:
307308
default:
308-
return fmt.Errorf("wasn't notified of new accounts")
309+
return errors.New("wasn't notified of new accounts")
309310
}
310311
return nil
311312
}

accounts/keystore/keystore.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"crypto/ecdsa"
2525
crand "crypto/rand"
2626
"errors"
27-
"fmt"
2827
"math/big"
2928
"os"
3029
"path/filepath"
@@ -455,7 +454,7 @@ func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (ac
455454
func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (accounts.Account, error) {
456455
key := newKeyFromECDSA(priv)
457456
if ks.cache.hasAddress(key.Address) {
458-
return accounts.Account{}, fmt.Errorf("account already exists")
457+
return accounts.Account{}, errors.New("account already exists")
459458
}
460459
return ks.importKey(key, passphrase)
461460
}

bmt/bmt_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package bmt
1919
import (
2020
"bytes"
2121
crand "crypto/rand"
22+
"errors"
2223
"fmt"
2324
"hash"
2425
"io"
@@ -288,7 +289,7 @@ func TestHasherConcurrency(t *testing.T) {
288289
var err error
289290
select {
290291
case <-time.NewTimer(5 * time.Second).C:
291-
err = fmt.Errorf("timed out")
292+
err = errors.New("timed out")
292293
case err = <-errc:
293294
}
294295
if err != nil {
@@ -321,7 +322,7 @@ func testHasherCorrectness(bmt hash.Hash, hasher BaseHasher, d []byte, n, count
321322
}()
322323
select {
323324
case <-timeout.C:
324-
err = fmt.Errorf("BMT hash calculation timed out")
325+
err = errors.New("BMT hash calculation timed out")
325326
case err = <-c:
326327
}
327328
return err

cmd/utils/cmd.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package utils
1919

2020
import (
2121
"compress/gzip"
22+
"errors"
2223
"fmt"
2324
"io"
2425
"os"
@@ -130,7 +131,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
130131
for batch := 0; ; batch++ {
131132
// Load a batch of RLP blocks.
132133
if checkInterrupt() {
133-
return fmt.Errorf("interrupted")
134+
return errors.New("interrupted")
134135
}
135136
i := 0
136137
for ; i < importBatchSize; i++ {
@@ -153,7 +154,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
153154
}
154155
// Import the batch.
155156
if checkInterrupt() {
156-
return fmt.Errorf("interrupted")
157+
return errors.New("interrupted")
157158
}
158159
missing := missingBlocks(chain, blocks[:i])
159160
if len(missing) == 0 {

common/countdown/countdown_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package countdown
22

33
import (
4-
"fmt"
4+
"errors"
55
"testing"
66
"time"
77

@@ -76,7 +76,7 @@ func TestCountdownShouldResetEvenIfErrored(t *testing.T) {
7676
called := make(chan int)
7777
OnTimeoutFn := func(time.Time, interface{}) error {
7878
called <- 1
79-
return fmt.Errorf("ERROR!")
79+
return errors.New("ERROR!")
8080
}
8181

8282
countdown := NewCountDown(5000 * time.Millisecond)

consensus/XDPoS/XDPoS.go

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

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

@@ -438,7 +439,7 @@ func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *type
438439
case params.ConsensusEngineVersion2:
439440
return x.EngineV2.CalculateMissingRounds(chain, header)
440441
default: // Default "v1"
441-
return nil, fmt.Errorf("Not supported in the v1 consensus")
442+
return nil, errors.New("Not supported in the v1 consensus")
442443
}
443444
}
444445

consensus/XDPoS/engines/engine_v1/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ func (x *XDPoS_v1) GetValidator(creator common.Address, chain consensus.ChainRea
686686
if no%epoch == 0 {
687687
cpHeader = header
688688
} else {
689-
return common.Address{}, fmt.Errorf("couldn't find checkpoint header")
689+
return common.Address{}, errors.New("couldn't find checkpoint header")
690690
}
691691
}
692692
m, err := getM1M2FromCheckpointHeader(cpHeader, header, chain.Config())

0 commit comments

Comments
 (0)