diff --git a/.golangci.yml b/.golangci.yml index 07c29f1b1..874a27292 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,24 +1,52 @@ -run: - deadline: 1m - linters: - enable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - dupl + # - errcheck + # - funlen + # - gochecknoglobals + # - gochecknoinits + - goconst + - gocritic + # - gocyclo + # - godox + - gofmt + - goimports + # - golint + - gosec + - gosimple + - govet + - ineffassign + - interfacer + # - lll + - misspell + - maligned + - nakedret + - prealloc + - scopelint + - staticcheck + - structcheck + # - stylecheck + - typecheck + - unconvert + # - unparam + - unused + - varcheck + # - whitespace + # - wsl + # - gocognit disable: - - gocyclo - - golint - errcheck - - unparam - - lll - - gochecknoglobals - - gochecknoinits - - stylecheck - - funlen - - godox - - whitespace linters-settings: dogsled: max-blank-identifiers: 3 + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true # govet: # check-shadowing: true # golint: @@ -49,3 +77,6 @@ linters-settings: # disabled-checks: # - wrapperFunc # - commentFormatting # https://github.com/go-critic/go-critic/issues/755 + +service: + golangci-lint-version: 1.19.x diff --git a/abci/tests/test_app/main.go b/abci/tests/test_app/main.go index 8f45cec3c..ae47ac6f9 100644 --- a/abci/tests/test_app/main.go +++ b/abci/tests/test_app/main.go @@ -36,7 +36,7 @@ func ensureABCIIsUp(typ string, n int) error { } for i := 0; i < n; i++ { - cmd := exec.Command("bash", "-c", cmdString) // nolint: gas + cmd := exec.Command("bash", "-c", cmdString) _, err = cmd.CombinedOutput() if err == nil { break @@ -53,7 +53,7 @@ func testCounter() { } fmt.Printf("Running %s test with abci=%s\n", abciApp, abciType) - cmd := exec.Command("bash", "-c", fmt.Sprintf("abci-cli %s", abciApp)) // nolint: gas + cmd := exec.Command("bash", "-c", fmt.Sprintf("abci-cli %s", abciApp)) cmd.Stdout = os.Stdout if err := cmd.Start(); err != nil { log.Fatalf("starting %q err: %v", abciApp, err) diff --git a/config/config.go b/config/config.go index 3e4bf9340..b256171c3 100644 --- a/config/config.go +++ b/config/config.go @@ -140,7 +140,7 @@ func (cfg *Config) ValidateBasic() error { // BaseConfig // BaseConfig defines the base configuration for a Tendermint node -type BaseConfig struct { +type BaseConfig struct { //nolint: maligned // chainID is unexposed and immutable but here for convenience chainID string @@ -471,7 +471,7 @@ func (cfg RPCConfig) IsTLSEnabled() bool { // P2PConfig // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer -type P2PConfig struct { +type P2PConfig struct { //nolint: maligned RootDir string `mapstructure:"home"` // Address to listen for incoming connections diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index afb793fd0..49f19f7d4 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -639,7 +639,7 @@ func capture() { // Ensure basic validation of structs is functioning func TestNewRoundStepMessageValidateBasic(t *testing.T) { - testCases := []struct { + testCases := []struct { // nolint: maligned expectErr bool messageRound int messageLastCommitRound int @@ -769,7 +769,7 @@ func TestHasVoteMessageValidateBasic(t *testing.T) { invalidSignedMsgType types.SignedMsgType = 0x03 ) - testCases := []struct { + testCases := []struct { // nolint: maligned expectErr bool messageRound int messageIndex int @@ -814,7 +814,7 @@ func TestVoteSetMaj23MessageValidateBasic(t *testing.T) { }, } - testCases := []struct { + testCases := []struct { // nolint: maligned expectErr bool messageRound int messageHeight int64 @@ -860,7 +860,7 @@ func TestVoteSetBitsMessageValidateBasic(t *testing.T) { } testBitArray := cmn.NewBitArray(1) - testCases := []struct { + testCases := []struct { // nolint: maligned expectErr bool messageRound int messageHeight int64 diff --git a/consensus/types/peer_round_state.go b/consensus/types/peer_round_state.go index 16e292940..22033b469 100644 --- a/consensus/types/peer_round_state.go +++ b/consensus/types/peer_round_state.go @@ -13,12 +13,10 @@ import ( // PeerRoundState contains the known state of a peer. // NOTE: Read-only when returned by PeerState.GetRoundState(). type PeerRoundState struct { + ProposalBlockPartsHeader types.PartSetHeader `json:"proposal_block_parts_header"` // + StartTime time.Time `json:"start_time"` // Estimated start of round 0 at this height Height int64 `json:"height"` // Height peer is at Round int `json:"round"` // Round peer is at, -1 if unknown. - Step RoundStepType `json:"step"` // Step peer is at - StartTime time.Time `json:"start_time"` // Estimated start of round 0 at this height - Proposal bool `json:"proposal"` // True if peer has proposal for this round - ProposalBlockPartsHeader types.PartSetHeader `json:"proposal_block_parts_header"` // ProposalBlockParts *cmn.BitArray `json:"proposal_block_parts"` // ProposalPOLRound int `json:"proposal_pol_round"` // Proposal's POL round. -1 if none. ProposalPOL *cmn.BitArray `json:"proposal_pol"` // nil until ProposalPOLMessage received. @@ -28,6 +26,8 @@ type PeerRoundState struct { LastCommit *cmn.BitArray `json:"last_commit"` // All commit precommits of commit for last height. CatchupCommitRound int `json:"catchup_commit_round"` // Round that we have commit for. Not necessarily unique. -1 if none. CatchupCommit *cmn.BitArray `json:"catchup_commit"` // All commit precommits peer has for this height & CatchupCommitRound + Step RoundStepType `json:"step"` // Step peer is at + Proposal bool `json:"proposal"` // True if peer has proposal for this round } // String returns a string representation of the PeerRoundState diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index c4372e201..2cab83c1c 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -65,11 +65,10 @@ func (rs RoundStepType) String() string { // NOTE: Not thread safe. Should only be manipulated by functions downstream // of the cs.receiveRoutine type RoundState struct { - Height int64 `json:"height"` // Height we are working on - Round int `json:"round"` - Step RoundStepType `json:"step"` StartTime time.Time `json:"start_time"` CommitTime time.Time `json:"commit_time"` // Subjective time when +2/3 precommits for Block at Round were found + Height int64 `json:"height"` // Height we are working on + Round int `json:"round"` Validators *types.ValidatorSet `json:"validators"` Proposal *types.Proposal `json:"proposal"` ProposalBlock *types.Block `json:"proposal_block"` @@ -84,6 +83,7 @@ type RoundState struct { CommitRound int `json:"commit_round"` // LastCommit *types.VoteSet `json:"last_commit"` // Last precommits at Height-1 LastValidators *types.ValidatorSet `json:"last_validators"` + Step RoundStepType `json:"step"` TriggeredTimeoutPrecommit bool `json:"triggered_timeout_precommit"` } diff --git a/libs/flowrate/flowrate.go b/libs/flowrate/flowrate.go index 35ebfbde6..2a053805c 100644 --- a/libs/flowrate/flowrate.go +++ b/libs/flowrate/flowrate.go @@ -107,7 +107,7 @@ const timeRemLimit = 999*time.Hour + 59*time.Minute + 59*time.Second // Status represents the current Monitor status. All transfer rates are in bytes // per second rounded to the nearest byte. type Status struct { - Active bool // Flag indicating an active transfer + Start time.Time // Transfer start time Bytes int64 // Total number of bytes transferred Samples int64 // Total number of samples taken InstRate int64 // Instantaneous transfer rate @@ -115,11 +115,11 @@ type Status struct { AvgRate int64 // Average transfer rate (Bytes / Duration) PeakRate int64 // Maximum instantaneous transfer rate BytesRem int64 // Number of bytes remaining in the transfer - Start time.Time // Transfer start time Duration time.Duration // Time period covered by the statistics Idle time.Duration // Time since the last transfer of at least 1 byte TimeRem time.Duration // Estimated time to completion Progress Percent // Overall transfer progress + Active bool // Flag indicating an active transfer } // Status returns current transfer status information. The returned value diff --git a/libs/flowrate/io_test.go b/libs/flowrate/io_test.go index 0bb1a5911..4d7de417e 100644 --- a/libs/flowrate/io_test.go +++ b/libs/flowrate/io_test.go @@ -81,12 +81,12 @@ func TestReader(t *testing.T) { // Active, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, Start, Duration, Idle, TimeRem, Progress want := []Status{ - {true, 0, 0, 0, 0, 0, 0, 0, start, 0, 0, 0, 0}, - {true, 10, 1, 100, 100, 100, 100, 0, start, _100ms, 0, 0, 0}, - {true, 20, 2, 100, 100, 100, 100, 0, start, _200ms, _100ms, 0, 0}, - {true, 20, 3, 0, 90, 67, 100, 0, start, _300ms, _200ms, 0, 0}, - {false, 20, 3, 0, 0, 67, 100, 0, start, _300ms, 0, 0, 0}, - {false, 20, 3, 0, 0, 67, 100, 0, start, _300ms, 0, 0, 0}, + {start, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, true}, + {start, 10, 1, 100, 100, 100, 100, 0, _100ms, 0, 0, 0, true}, + {start, 20, 2, 100, 100, 100, 100, 0, _200ms, _100ms, 0, 0, true}, + {start, 20, 3, 0, 90, 67, 100, 0, _300ms, _200ms, 0, 0, true}, + {start, 20, 3, 0, 0, 67, 100, 0, _300ms, 0, 0, 0, false}, + {start, 20, 3, 0, 0, 67, 100, 0, _300ms, 0, 0, 0, false}, } for i, s := range status { s := s @@ -140,9 +140,10 @@ func TestWriter(t *testing.T) { // Active, Bytes, Samples, InstRate, CurRate, AvgRate, PeakRate, BytesRem, Start, Duration, Idle, TimeRem, Progress want := []Status{ - {true, 80, 4, 200, 200, 200, 200, 20, start, _400ms, 0, _100ms, 80000}, - {true, 100, 5, 200, 200, 200, 200, 0, start, _500ms, _100ms, 0, 100000}, + {start, 80, 4, 200, 200, 200, 200, 20, _400ms, 0, _100ms, 80000, true}, + {start, 100, 5, 200, 200, 200, 200, 0, _500ms, _100ms, 0, 100000, true}, } + for i, s := range status { s := s if !statusesAreEqual(&s, &want[i]) { diff --git a/node/node.go b/node/node.go index fc80c6b70..3e0a09aba 100644 --- a/node/node.go +++ b/node/node.go @@ -839,7 +839,6 @@ func (n *Node) ConfigureRPC() { pubKey := n.privValidator.GetPubKey() rpccore.SetPubKey(pubKey) rpccore.SetGenesisDoc(n.genesisDoc) - rpccore.SetAddrBook(n.addrBook) rpccore.SetProxyAppQuery(n.proxyApp.Query()) rpccore.SetTxIndexer(n.txIndexer) rpccore.SetConsensusReactor(n.consensusReactor) diff --git a/p2p/pex/addrbook.go b/p2p/pex/addrbook.go index 344df295f..3d7d08794 100644 --- a/p2p/pex/addrbook.go +++ b/p2p/pex/addrbook.go @@ -819,9 +819,9 @@ func (a *addrBook) groupKey(na *p2p.NetAddress) string { // doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes. func doubleSha256(b []byte) []byte { hasher := sha256.New() - hasher.Write(b) // nolint: errcheck, gas + hasher.Write(b) // nolint:errcheck sum := hasher.Sum(nil) hasher.Reset() - hasher.Write(sum) // nolint: errcheck, gas + hasher.Write(sum) // nolint:errcheck return hasher.Sum(nil) } diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index 55cde5a35..73abe9bb1 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -490,7 +490,7 @@ func (r *PEXReactor) ensurePeers() { peers := r.Switch.Peers().List() peersCount := len(peers) if peersCount > 0 { - peer := peers[cmn.RandInt()%peersCount] // nolint: gas + peer := peers[cmn.RandInt()%peersCount] r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer) r.RequestAddrs(peer) } diff --git a/p2p/switch_test.go b/p2p/switch_test.go index 0879acc2d..c5acd5cd3 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -354,6 +354,7 @@ func TestSwitchStopPeerForError(t *testing.T) { scrapeMetrics := func() string { resp, _ := http.Get(s.URL) buf, _ := ioutil.ReadAll(resp.Body) + resp.Body.Close() return string(buf) } diff --git a/rpc/core/pipe.go b/rpc/core/pipe.go index 19abf62f6..a3fff7b10 100644 --- a/rpc/core/pipe.go +++ b/rpc/core/pipe.go @@ -70,7 +70,6 @@ var ( // objects pubKey crypto.PubKey genDoc *types.GenesisDoc // cache the genesis structure - addrBook p2p.AddrBook txIndexer txindex.TxIndexer consensusReactor *consensus.ConsensusReactor eventBus *types.EventBus // thread safe @@ -117,10 +116,6 @@ func SetGenesisDoc(doc *types.GenesisDoc) { genDoc = doc } -func SetAddrBook(book p2p.AddrBook) { - addrBook = book -} - func SetProxyAppQuery(appConn proxy.AppConnQuery) { proxyAppQuery = appConn } diff --git a/rpc/lib/client/ws_client.go b/rpc/lib/client/ws_client.go index 52de8d133..da7c5d965 100644 --- a/rpc/lib/client/ws_client.go +++ b/rpc/lib/client/ws_client.go @@ -27,7 +27,7 @@ const ( // WSClient is a WebSocket client. The methods of WSClient are safe for use by // multiple goroutines. -type WSClient struct { +type WSClient struct { // nolint: maligned conn *websocket.Conn cdc *amino.Codec @@ -252,7 +252,7 @@ func (c *WSClient) dial() error { Proxy: http.ProxyFromEnvironment, } rHeader := http.Header{} - conn, _, err := dialer.Dial(c.protocol+"://"+c.Address+c.Endpoint, rHeader) + conn, _, err := dialer.Dial(c.protocol+"://"+c.Address+c.Endpoint, rHeader) // nolint:bodyclose if err != nil { return err } diff --git a/rpc/lib/server/handlers_test.go b/rpc/lib/server/handlers_test.go index 9cded2953..b481a6b19 100644 --- a/rpc/lib/server/handlers_test.go +++ b/rpc/lib/server/handlers_test.go @@ -77,6 +77,7 @@ func TestRPCParams(t *testing.T) { t.Errorf("#%d: err reading body: %v", i, err) continue } + res.Body.Close() recv := new(types.RPCResponse) assert.Nil(t, json.Unmarshal(blob, recv), "#%d: expecting successful parsing of an RPCResponse:\nblob: %s", i, blob) @@ -125,6 +126,7 @@ func TestJSONRPCID(t *testing.T) { t.Errorf("#%d: err reading body: %v", i, err) continue } + res.Body.Close() recv := new(types.RPCResponse) err = json.Unmarshal(blob, recv) @@ -150,6 +152,7 @@ func TestRPCNotification(t *testing.T) { // Always expecting back a JSONRPCResponse require.True(t, statusOK(res.StatusCode), "should always return 2XX") blob, err := ioutil.ReadAll(res.Body) + res.Body.Close() require.Nil(t, err, "reading from the body should not give back an error") require.Equal(t, len(blob), 0, "a notification SHOULD NOT be responded to by the server") } @@ -189,6 +192,7 @@ func TestRPCNotificationInBatch(t *testing.T) { t.Errorf("#%d: err reading body: %v", i, err) continue } + res.Body.Close() var responses []types.RPCResponse // try to unmarshal an array first @@ -229,6 +233,7 @@ func TestUnknownRPCPath(t *testing.T) { // Always expecting back a 404 error require.Equal(t, http.StatusNotFound, res.StatusCode, "should always return 404") + res.Body.Close() } ////////////////////////////////////////////////////////////////////////////// @@ -257,6 +262,7 @@ func TestWebsocketManagerHandler(t *testing.T) { err = c.ReadJSON(&resp) require.NoError(t, err) require.Nil(t, resp.Error) + dialResp.Body.Close() } func newWSServer() *httptest.Server { diff --git a/state/state_test.go b/state/state_test.go index bd4935ea8..54604b352 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -37,7 +37,6 @@ func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) { func TestStateCopy(t *testing.T) { tearDown, _, state := setupTestCase(t) defer tearDown(t) - // nolint: vetshadow assert := assert.New(t) stateCopy := state.Copy() @@ -68,7 +67,6 @@ func TestMakeGenesisStateNilValidators(t *testing.T) { func TestStateSaveLoad(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - // nolint: vetshadow assert := assert.New(t) state.LastBlockHeight++ @@ -84,7 +82,6 @@ func TestStateSaveLoad(t *testing.T) { func TestABCIResponsesSaveLoad1(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - // nolint: vetshadow assert := assert.New(t) state.LastBlockHeight++ @@ -110,7 +107,6 @@ func TestABCIResponsesSaveLoad1(t *testing.T) { func TestABCIResponsesSaveLoad2(t *testing.T) { tearDown, stateDB, _ := setupTestCase(t) defer tearDown(t) - // nolint: vetshadow assert := assert.New(t) cases := [...]struct { @@ -181,7 +177,6 @@ func TestABCIResponsesSaveLoad2(t *testing.T) { func TestValidatorSimpleSaveLoad(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - // nolint: vetshadow assert := assert.New(t) // Can't load anything for height 0. diff --git a/tools/tm-bench/transacter.go b/tools/tm-bench/transacter.go index 3347fdfb0..57324de35 100644 --- a/tools/tm-bench/transacter.go +++ b/tools/tm-bench/transacter.go @@ -72,7 +72,7 @@ func (t *transacter) Start() error { rand.Seed(time.Now().Unix()) for i := 0; i < t.Connections; i++ { - c, _, err := connect(t.Target) + c, _, err := connect(t.Target) // nolint:bodyclose if err != nil { return err }