diff --git a/.golangci.yml b/.golangci.yml index b35bc5bd3..00ab87f67 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -44,6 +44,7 @@ linters: # - exhaustive - bodyclose - contextcheck + - errorlint - gofmt - whitespace - goimports diff --git a/cli/server_test.go b/cli/server_test.go index 56da8e262..09403c418 100644 --- a/cli/server_test.go +++ b/cli/server_test.go @@ -1,6 +1,7 @@ package main import ( + "errors" "io" "os" "path/filepath" @@ -110,7 +111,7 @@ func TestServerStart(t *testing.T) { var line string require.Eventually(t, func() bool { line, err = e.Out.ReadString('\n') - if err != nil && err != io.EOF { + if err != nil && !errors.Is(err, io.EOF) { t.Fatalf("unexpected error while reading CLI output: %s", err) } return err == nil diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index 54751fb84..09479ef71 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -947,7 +947,7 @@ func readWallet(ctx *cli.Context) (*wallet.Wallet, *string, error) { if path == "-" { w := &wallet.Wallet{} if err := json.NewDecoder(os.Stdin).Decode(w); err != nil { - return nil, nil, fmt.Errorf("js %s", err) + return nil, nil, fmt.Errorf("js %w", err) } return w, nil, nil } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 200e091e7..479f5a83d 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1702,7 +1702,7 @@ func (bc *Blockchain) HasTransaction(hash util.Uint256) bool { if bc.memPool.ContainsKey(hash) { return true } - return bc.dao.HasTransaction(hash) == dao.ErrAlreadyExists + return errors.Is(bc.dao.HasTransaction(hash), dao.ErrAlreadyExists) } // HasBlock returns true if the blockchain contains the given @@ -1761,7 +1761,7 @@ func (bc *Blockchain) HeaderHeight() uint32 { // GetContractState returns contract by its script hash. func (bc *Blockchain) GetContractState(hash util.Uint160) *state.Contract { contract, err := bc.contracts.Management.GetContract(bc.dao, hash) - if contract == nil && err != storage.ErrKeyNotFound { + if contract == nil && !errors.Is(err, storage.ErrKeyNotFound) { bc.log.Warn("failed to get contract state", zap.Error(err)) } return contract diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index b9f8ced9d..5e1f17960 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -176,7 +176,7 @@ func (dao *Simple) GetTokenTransferInfo(acc util.Uint160) (*state.TokenTransferI key := dao.makeTTIKey(acc) bs := state.NewTokenTransferInfo() err := dao.GetAndDecode(bs, key) - if err != nil && err != storage.ErrKeyNotFound { + if err != nil && !errors.Is(err, storage.ErrKeyNotFound) { return nil, err } return bs, nil @@ -257,7 +257,7 @@ func (dao *Simple) GetTokenTransferLog(acc util.Uint160, newestTimestamp uint64, key := dao.getTokenTransferLogKey(acc, newestTimestamp, index, isNEP11) value, err := dao.Store.Get(key) if err != nil { - if err == storage.ErrKeyNotFound { + if errors.Is(err, storage.ErrKeyNotFound) { return new(state.TokenTransferLog), nil } return nil, err @@ -306,7 +306,7 @@ func (dao *Simple) GetAppExecResults(hash util.Uint256, trig trigger.Type) ([]st aer := new(state.AppExecResult) aer.DecodeBinary(r) if r.Err != nil { - if r.Err == iocore.EOF { + if errors.Is(r.Err, iocore.EOF) { break } return nil, r.Err diff --git a/pkg/core/native/management.go b/pkg/core/native/management.go index 26bd855f8..c305001d2 100644 --- a/pkg/core/native/management.go +++ b/pkg/core/native/management.go @@ -173,7 +173,7 @@ func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) sta hash := toHash160(args[0]) ctr, err := m.GetContract(ic.DAO, hash) if err != nil { - if err == storage.ErrKeyNotFound { + if errors.Is(err, storage.ErrKeyNotFound) { return stackitem.Null{} } panic(err) diff --git a/pkg/core/native/notary.go b/pkg/core/native/notary.go index 6943fbf4c..93f2af788 100644 --- a/pkg/core/native/notary.go +++ b/pkg/core/native/notary.go @@ -469,7 +469,7 @@ func (n *Notary) GetDepositFor(dao *dao.Simple, acc util.Uint160) *state.Deposit if err == nil { return deposit } - if err == storage.ErrKeyNotFound { + if errors.Is(err, storage.ErrKeyNotFound) { return nil } panic(fmt.Errorf("failed to get deposit for %s from storage: %w", acc.StringBE(), err)) diff --git a/pkg/core/storage/leveldb_store.go b/pkg/core/storage/leveldb_store.go index c656f58bb..f1f4930f2 100644 --- a/pkg/core/storage/leveldb_store.go +++ b/pkg/core/storage/leveldb_store.go @@ -1,6 +1,8 @@ package storage import ( + "errors" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/filter" @@ -35,7 +37,7 @@ func NewLevelDBStore(cfg dbconfig.LevelDBOptions) (*LevelDBStore, error) { // Get implements the Store interface. func (s *LevelDBStore) Get(key []byte) ([]byte, error) { value, err := s.db.Get(key, nil) - if err == leveldb.ErrNotFound { + if errors.Is(err, leveldb.ErrNotFound) { err = ErrKeyNotFound } return value, err diff --git a/pkg/neorpc/errors.go b/pkg/neorpc/errors.go index cc42af56e..7ed90994c 100644 --- a/pkg/neorpc/errors.go +++ b/pkg/neorpc/errors.go @@ -1,6 +1,7 @@ package neorpc import ( + "errors" "fmt" ) @@ -125,9 +126,9 @@ func (e *Error) Error() string { // Is denotes whether the error matches the target one. func (e *Error) Is(target error) bool { - clTarget, ok := target.(*Error) - if !ok { - return false + var clTarget *Error + if errors.As(target, &clTarget) { + return e.Code == clTarget.Code } - return e.Code == clTarget.Code + return false } diff --git a/pkg/network/message.go b/pkg/network/message.go index 4655c3be5..2142d6a41 100644 --- a/pkg/network/message.go +++ b/pkg/network/message.go @@ -176,7 +176,7 @@ func (m *Message) decodePayload() error { } r := io.NewBinReaderFromBuf(buf) p.DecodeBinary(r) - if r.Err == nil || r.Err == payload.ErrTooManyHeaders { + if r.Err == nil || errors.Is(r.Err, payload.ErrTooManyHeaders) { m.Payload = p } diff --git a/pkg/network/server.go b/pkg/network/server.go index 5392071e7..cbcc40634 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -417,9 +417,9 @@ func (s *Server) run() { zap.Error(drop.reason), zap.Int("peerCount", s.PeerCount())) addr := drop.peer.PeerAddr().String() - if drop.reason == errIdenticalID { + if errors.Is(drop.reason, errIdenticalID) { s.discovery.RegisterBadAddr(addr) - } else if drop.reason == errAlreadyConnected { + } else if errors.Is(drop.reason, errAlreadyConnected) { // There is a race condition when peer can be disconnected twice for the this reason // which can lead to no connections to peer at all. Here we check for such a possibility. stillConnected := false @@ -1380,15 +1380,15 @@ func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []b continue } err := send(peer, blocking, pkt) - switch err { - case nil: + if err == nil { if msg.Command == CMDGetAddr { peer.AddGetAddrSent() } sentN++ - case errBusy: // Can be retried. + } else if errors.Is(err, errBusy) { + // Can be retried. continue - default: + } else { deadN++ } finished[i] = true diff --git a/pkg/network/tcp_peer.go b/pkg/network/tcp_peer.go index e6016a72f..e7684f156 100644 --- a/pkg/network/tcp_peer.go +++ b/pkg/network/tcp_peer.go @@ -170,7 +170,7 @@ func (p *TCPPeer) handleConn() { msg := &Message{StateRootInHeader: p.server.config.StateRootInHeader} err = msg.Decode(r) - if err == payload.ErrTooManyHeaders { + if errors.Is(err, payload.ErrTooManyHeaders) { p.server.log.Warn("not all headers were processed") r.Err = nil } else if err != nil { diff --git a/pkg/services/metrics/metrics.go b/pkg/services/metrics/metrics.go index e204cbe32..b7af0c3a9 100644 --- a/pkg/services/metrics/metrics.go +++ b/pkg/services/metrics/metrics.go @@ -2,6 +2,7 @@ package metrics import ( "context" + "errors" "net/http" "github.com/nspcc-dev/neo-go/pkg/config" @@ -21,7 +22,7 @@ func (ms *Service) Start() { if ms.config.Enabled { ms.log.Info("service is running", zap.String("endpoint", ms.Addr)) err := ms.ListenAndServe() - if err != nil && err != http.ErrServerClosed { + if err != nil && !errors.Is(err, http.ErrServerClosed) { ms.log.Warn("service couldn't start on configured port") } } else { diff --git a/pkg/services/oracle/response.go b/pkg/services/oracle/response.go index 7d37036e0..d8523b2b2 100644 --- a/pkg/services/oracle/response.go +++ b/pkg/services/oracle/response.go @@ -69,7 +69,7 @@ var ErrResponseTooLarge = errors.New("too big response") func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) { buf := make([]byte, limit+1) n, err := gio.ReadFull(rc, buf) - if err == gio.ErrUnexpectedEOF && n <= limit { + if errors.Is(err, gio.ErrUnexpectedEOF) && n <= limit { return buf[:n], nil } if err == nil || n > limit { diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index c984293f9..1b88b561b 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -340,7 +340,7 @@ func (s *Server) Start() { } s.https.Addr = ln.Addr().String() err = s.https.ServeTLS(ln, cfg.CertFile, cfg.KeyFile) - if err != http.ErrServerClosed { + if !errors.Is(err, http.ErrServerClosed) { s.log.Error("failed to start TLS RPC server", zap.Error(err)) s.errChan <- err } @@ -354,7 +354,7 @@ func (s *Server) Start() { s.Addr = ln.Addr().String() // set Addr to the actual address go func() { err = s.Serve(ln) - if err != http.ErrServerClosed { + if !errors.Is(err, http.ErrServerClosed) { s.log.Error("failed to start RPC server", zap.Error(err)) s.errChan <- err } diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index c538d8937..ae848670a 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -678,7 +678,7 @@ func (c *VMCLI) Run() error { l := getReadlineInstanceFromContext(c.shell) for { line, err := l.Readline() - if err == io.EOF || err == readline.ErrInterrupt { + if errors.Is(err, io.EOF) || errors.Is(err, readline.ErrInterrupt) { return nil // OK, stop execution. } if err != nil { diff --git a/pkg/vm/stackitem/json.go b/pkg/vm/stackitem/json.go index 2a077920f..10902e62c 100644 --- a/pkg/vm/stackitem/json.go +++ b/pkg/vm/stackitem/json.go @@ -169,7 +169,7 @@ func FromJSON(data []byte, maxCount int) (Item, error) { d.UseNumber() if item, err := d.decode(); err != nil { return nil, err - } else if _, err := d.Token(); err != gio.EOF { + } else if _, err := d.Token(); !errors.Is(err, gio.EOF) { return nil, fmt.Errorf("%w: unexpected items", ErrInvalidValue) } else { return item, nil