diff --git a/.circleci/config.yml b/.circleci/config.yml index d59e6f2e7..acf83b9cd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -34,7 +34,7 @@ jobs: - run: name: go-lint command: | - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.44.2 + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.49.0 make lint test_1_17: diff --git a/.golangci.yml b/.golangci.yml index 1c67aa665..b79417f01 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -32,20 +32,32 @@ linters: - revive # some default golangci-lint linters - - deadcode - errcheck - gosimple - godot - ineffassign - staticcheck - - structcheck - typecheck - unused - - varcheck # extra linters # - exhaustive + # - goconst + # - goerr113 + # - gomnd + # - nonamedreturns + # - unparam + - bidichk + - bodyclose + - contextcheck + - decorder + - durationcheck + - errorlint + - exportloopref - gofmt + - misspell + - predeclared + - reassign - whitespace - goimports disable-all: true 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..1c14b7851 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -693,15 +693,15 @@ func (bc *Blockchain) Run() { } } -func (bc *Blockchain) tryRunGC(old uint32) time.Duration { +func (bc *Blockchain) tryRunGC(oldHeight uint32) time.Duration { var dur time.Duration - new := atomic.LoadUint32(&bc.persistedHeight) - var tgtBlock = int64(new) + newHeight := atomic.LoadUint32(&bc.persistedHeight) + var tgtBlock = int64(newHeight) tgtBlock -= int64(bc.config.MaxTraceableBlocks) if bc.config.P2PStateExchangeExtensions { - syncP := new / uint32(bc.config.StateSyncInterval) + syncP := newHeight / uint32(bc.config.StateSyncInterval) syncP-- syncP *= uint32(bc.config.StateSyncInterval) if tgtBlock > int64(syncP) { @@ -712,9 +712,9 @@ func (bc *Blockchain) tryRunGC(old uint32) time.Duration { tgtBlock /= int64(bc.config.GarbageCollectionPeriod) tgtBlock *= int64(bc.config.GarbageCollectionPeriod) // Count periods. - old /= bc.config.GarbageCollectionPeriod - new /= bc.config.GarbageCollectionPeriod - if tgtBlock > int64(bc.config.GarbageCollectionPeriod) && new != old { + oldHeight /= bc.config.GarbageCollectionPeriod + newHeight /= bc.config.GarbageCollectionPeriod + if tgtBlock > int64(bc.config.GarbageCollectionPeriod) && newHeight != oldHeight { tgtBlock /= int64(bc.config.GarbageCollectionPeriod) tgtBlock *= int64(bc.config.GarbageCollectionPeriod) dur = bc.stateRoot.GC(uint32(tgtBlock), bc.store) @@ -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/blockchain_core_test.go b/pkg/core/blockchain_core_test.go index a0ccae047..8ef59604e 100644 --- a/pkg/core/blockchain_core_test.go +++ b/pkg/core/blockchain_core_test.go @@ -77,7 +77,7 @@ func TestAddBlock(t *testing.T) { } func TestRemoveOldTransfers(t *testing.T) { - // Creating proper number of transfers/blocks takes unneccessary time, so emulate + // Creating proper number of transfers/blocks takes unnecessary time, so emulate // some DB with stale entries. bc := newTestChain(t) h, err := bc.GetHeader(bc.GetHeaderHash(0)) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index b9f8ced9d..cd8266bef 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 @@ -828,14 +828,14 @@ func (dao *Simple) StoreAsTransaction(tx *transaction.Transaction, index uint32, return nil } -func (dao *Simple) getKeyBuf(len int) []byte { +func (dao *Simple) getKeyBuf(l int) []byte { if dao.private { if dao.keyBuf == nil { dao.keyBuf = make([]byte, 0, 1+4+limits.MaxStorageKeyLen) // Prefix, uint32, key. } - return dao.keyBuf[:len] // Should have enough capacity. + return dao.keyBuf[:l] // Should have enough capacity. } - return make([]byte, len) + return make([]byte, l) } func (dao *Simple) getDataBuf() *io.BufBinWriter { diff --git a/pkg/core/mpt/billet.go b/pkg/core/mpt/billet.go index 86b2abeb6..cba44afa0 100644 --- a/pkg/core/mpt/billet.go +++ b/pkg/core/mpt/billet.go @@ -37,7 +37,7 @@ type Billet struct { // NewBillet returns a new billet for MPT trie restoring. It accepts a MemCachedStore // to decouple storage errors from logic errors so that all storage errors are -// processed during `store.Persist()` at the caller. Another benifit is +// processed during `store.Persist()` at the caller. Another benefit is // that every `Put` can be considered an atomic operation. func NewBillet(rootHash util.Uint256, mode TrieMode, prefix storage.KeyPrefix, store *storage.MemCachedStore) *Billet { return &Billet{ 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/neorpc/result/validator_test.go b/pkg/neorpc/result/validator_test.go index f7ecd4b68..a45a81523 100644 --- a/pkg/neorpc/result/validator_test.go +++ b/pkg/neorpc/result/validator_test.go @@ -13,8 +13,8 @@ func TestValidatorUnmarshal(t *testing.T) { require.NoError(t, json.Unmarshal(old, v)) require.Equal(t, int64(100500), v.Votes) - new := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":42}`) - require.NoError(t, json.Unmarshal(new, v)) + newV := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":42}`) + require.NoError(t, json.Unmarshal(newV, v)) require.Equal(t, int64(42), v.Votes) bad := []byte(`{"publickey":"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62","votes":"notanumber"}`) 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/rpcclient/wsclient.go b/pkg/rpcclient/wsclient.go index ab77f141d..3a71d8bb9 100644 --- a/pkg/rpcclient/wsclient.go +++ b/pkg/rpcclient/wsclient.go @@ -90,7 +90,10 @@ var errConnClosedByUser = errors.New("connection closed by user") // operating on. func NewWS(ctx context.Context, endpoint string, opts Options) (*WSClient, error) { dialer := websocket.Dialer{HandshakeTimeout: opts.DialTimeout} - ws, _, err := dialer.Dial(endpoint, nil) + ws, resp, err := dialer.DialContext(ctx, endpoint, nil) + if resp != nil && resp.Body != nil { // Can be non-nil even with error returned. + defer resp.Body.Close() // Not exactly required by websocket, but let's do this for bodyclose checker. + } if err != nil { return nil, err } 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/network_test.go b/pkg/services/oracle/network_test.go index 37d623939..de132508a 100644 --- a/pkg/services/oracle/network_test.go +++ b/pkg/services/oracle/network_test.go @@ -40,7 +40,7 @@ func TestDefaultClient_RestrictedRedirectErr(t *testing.T) { } for _, c := range testCases { t.Run(c, func(t *testing.T) { - _, err := cl.Get(c) + _, err := cl.Get(c) //nolint:bodyclose // It errors out and it's a test. require.Error(t, err) require.True(t, errors.Is(err, ErrRestrictedRedirect), err) require.True(t, strings.Contains(err.Error(), "IP is not global unicast"), err) diff --git a/pkg/services/oracle/request.go b/pkg/services/oracle/request.go index 1b6ed6197..fddc9d771 100644 --- a/pkg/services/oracle/request.go +++ b/pkg/services/oracle/request.go @@ -138,6 +138,7 @@ func (o *Oracle) processRequest(priv *keys.PrivateKey, req request) error { o.Log.Warn("oracle request failed", zap.String("url", req.Req.URL), zap.Error(err), zap.Stringer("code", resp.Code)) break } + defer r.Body.Close() switch r.StatusCode { case http.StatusOK: if !checkMediaType(r.Header.Get("Content-Type"), o.MainCfg.AllowedContentTypes) { diff --git a/pkg/services/oracle/response.go b/pkg/services/oracle/response.go index b57be2f3b..d8523b2b2 100644 --- a/pkg/services/oracle/response.go +++ b/pkg/services/oracle/response.go @@ -67,11 +67,9 @@ func (o *Oracle) AddResponse(pub *keys.PublicKey, reqID uint64, txSig []byte) { var ErrResponseTooLarge = errors.New("too big response") func readResponse(rc gio.ReadCloser, limit int) ([]byte, error) { - defer rc.Close() - 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/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index d5a9fce4a..e0e2ee86a 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2633,8 +2633,9 @@ func checkErrGetBatchResult(t *testing.T, body []byte, expectingFail bool) json. func doRPCCallOverWS(rpcCall string, url string, t *testing.T) []byte { dialer := websocket.Dialer{HandshakeTimeout: time.Second} url = "ws" + strings.TrimPrefix(url, "http") - c, _, err := dialer.Dial(url+"/ws", nil) + c, r, err := dialer.Dial(url+"/ws", nil) require.NoError(t, err) + defer r.Body.Close() err = c.SetWriteDeadline(time.Now().Add(time.Second)) require.NoError(t, err) require.NoError(t, c.WriteMessage(1, []byte(rpcCall))) @@ -2651,6 +2652,7 @@ func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte { resp, err := cl.Post(url, "application/json", strings.NewReader(rpcCall)) require.NoErrorf(t, err, "could not make a POST request") body, err := gio.ReadAll(resp.Body) + resp.Body.Close() assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall) return bytes.TrimSpace(body) } diff --git a/pkg/services/rpcsrv/subscription_test.go b/pkg/services/rpcsrv/subscription_test.go index 1a5e5a957..41a8191dc 100644 --- a/pkg/services/rpcsrv/subscription_test.go +++ b/pkg/services/rpcsrv/subscription_test.go @@ -59,8 +59,9 @@ func initCleanServerAndWSClient(t *testing.T) (*core.Blockchain, *Server, *webso dialer := websocket.Dialer{HandshakeTimeout: time.Second} url := "ws" + strings.TrimPrefix(httpSrv.URL, "http") + "/ws" - ws, _, err := dialer.Dial(url, nil) + ws, r, err := dialer.Dial(url, nil) require.NoError(t, err) + defer r.Body.Close() // Use buffered channel to read server's messages and then read expected // responses from it. @@ -520,7 +521,10 @@ func TestWSClientsLimit(t *testing.T) { wss := make([]*websocket.Conn, maxSubscribers) for i := 0; i < len(wss)+1; i++ { - ws, _, err := dialer.Dial(url, nil) + ws, r, err := dialer.Dial(url, nil) + if r != nil && r.Body != nil { + defer r.Body.Close() + } if i < maxSubscribers { require.NoError(t, err) wss[i] = ws 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