From a4db0b890263ea433b87058db357105197c41de7 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 6 Dec 2023 17:03:46 -0500 Subject: [PATCH 1/6] add golangci-lint --- .github/workflows/test.yml | 4 + .golangci.yml | 163 +++++++++++++++++++++++++++++++++++++ types/types.go | 2 +- 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 .golangci.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75507132..3225bf53 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,10 @@ jobs: - uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} + - name: Lint + uses: golangci/golangci-lint-action@v3 + with: + skip-cache: true - name: test uses: n8maninger/action-golang-test@v1 with: diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..e977b71e --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,163 @@ +# Based off of the example file at https://github.com/golangci/golangci-lint + +# options for analysis running +run: + # default concurrency is a available CPU number + concurrency: 4 + + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 600s + + # exit code when at least one issue was found, default is 1 + issues-exit-code: 1 + + # include test files or not, default is true + tests: true + + # list of build tags, all linters use it. Default is empty list. + build-tags: [] + + # which dirs to skip: issues from them won't be reported; + # can use regexp here: generated.*, regexp is applied on full path; + # default value is empty list, but default dirs are skipped independently + # from this option's value (see skip-dirs-use-default). + skip-dirs: + - cover + + # default is true. Enables skipping of directories: + # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ + skip-dirs-use-default: true + + # which files to skip: they will be analyzed, but issues from them + # won't be reported. Default value is empty list, but there is + # no need to include all autogenerated files, we confidently recognize + # autogenerated files. If it's not please let us know. + skip-files: [] + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + format: colored-line-number + + # print lines of code with issue, default is true + print-issued-lines: true + + # print linter name in the end of issue text, default is true + print-linter-name: true + +# all available settings of specific linters +linters-settings: + ## Enabled linters: + govet: + # report about shadowed variables + check-shadowing: false + disable-all: false + + tagliatelle: + case: + rules: + json: goCamel + yaml: goCamel + + + gocritic: + # Which checks should be enabled; can't be combined with 'disabled-checks'; + # See https://go-critic.github.io/overview#checks-overview + # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` + # By default list of stable checks is used. + enabled-checks: + - argOrder # Diagnostic options + - badCond + - caseOrder + - dupArg + - dupBranchBody + - dupCase + - dupSubExpr + - nilValReturn + - offBy1 + - weakCond + - boolExprSimplify # Style options here and below. + - builtinShadow + - emptyFallthrough + - hexLiteral + - underef + - equalFold + revive: + ignore-generated-header: true + rules: + - name: blank-imports + disabled: false + - name: bool-literal-in-expr + disabled: false + - name: confusing-naming + disabled: false + - name: confusing-results + disabled: false + - name: constant-logical-expr + disabled: false + - name: context-as-argument + disabled: false + - name: exported + disabled: false + - name: errorf + disabled: false + - name: if-return + disabled: false + - name: indent-error-flow + disabled: false + - name: increment-decrement + disabled: false + - name: modifies-value-receiver + disabled: false + - name: optimize-operands-order + disabled: false + - name: range-val-in-closure + disabled: false + - name: struct-tag + disabled: false + - name: superfluous-else + disabled: false + - name: time-equal + disabled: false + - name: unexported-naming + disabled: false + - name: unexported-return + disabled: false + - name: unnecessary-stmt + disabled: false + - name: unreachable-code + disabled: false + - name: package-comments + disabled: true + +linters: + disable-all: true + fast: false + enable: + - tagliatelle + - gocritic + - gofmt + - revive + - govet + - misspell + - typecheck + - whitespace + +issues: + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. + max-issues-per-linter: 0 + + # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. + max-same-issues: 0 + + # List of regexps of issue texts to exclude, empty list by default. + # But independently from this option we use default exclude patterns, + # it can be disabled by `exclude-use-default: false`. To list all + # excluded by default patterns execute `golangci-lint run --help` + exclude: [] + + # Independently from option `exclude` we use default exclude patterns, + # it can be disabled by this option. To list all + # excluded by default patterns execute `golangci-lint run --help`. + # Default value for this option is true. + exclude-use-default: false diff --git a/types/types.go b/types/types.go index 59ca0864..fe0857f5 100644 --- a/types/types.go +++ b/types/types.go @@ -179,7 +179,7 @@ type TransactionID Hash256 // A ChainIndex pairs a block's height with its ID. type ChainIndex struct { Height uint64 `json:"height"` - ID BlockID `json:"ID"` + ID BlockID `json:"id"` } // A SiacoinOutput is the recipient of some of the siacoins spent in a From 10dfc6ea4c1cd1fe1a14e8c0d889715918656832 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 6 Dec 2023 17:15:54 -0500 Subject: [PATCH 2/6] fix linter errors --- chain/db.go | 1 - chain/manager.go | 2 +- consensus/state.go | 2 +- consensus/validation_test.go | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/chain/db.go b/chain/db.go index a864ca07..4d1c16d2 100644 --- a/chain/db.go +++ b/chain/db.go @@ -44,7 +44,6 @@ type versionedState struct { func (vs versionedState) EncodeTo(e *types.Encoder) { e.WriteUint8(2) vs.State.EncodeTo(e) - } func (vs *versionedState) DecodeFrom(d *types.Decoder) { diff --git a/chain/manager.go b/chain/manager.go index 4ac889f8..a2a87fd3 100644 --- a/chain/manager.go +++ b/chain/manager.go @@ -858,7 +858,7 @@ func (m *Manager) RecommendedFee() types.Currency { Mul64(estPoolWeight).Div64(targetWeight).Mul64(estPoolWeight). Div64(targetWeight).Mul64(estPoolWeight).Div64(targetWeight) - // finally, an absolute minumum fee: 1 SC / 100 KB + // finally, an absolute minimum fee: 1 SC / 100 KB minFee := types.Siacoins(1).Div64(100e3) // use the largest of all calculated fees diff --git a/consensus/state.go b/consensus/state.go index 8b7b822e..b7510b51 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -71,7 +71,7 @@ type Network struct { Height uint64 `json:"height"` OakTime time.Duration `json:"oakTime"` OakTarget types.BlockID `json:"oakTarget"` - } `json:"hardforkASIC"` + } `json:"hardforkAsic"` HardforkFoundation struct { Height uint64 `json:"height"` PrimaryAddress types.Address `json:"primaryAddress"` diff --git a/consensus/validation_test.go b/consensus/validation_test.go index 4ea35a0d..d1802531 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -1135,7 +1135,7 @@ func TestValidateV2Block(t *testing.T) { func(b *types.Block) { txn := &b.V2.Transactions[0] rev := testFces[0].V2FileContract - rev.RevisionNumber += 1 + rev.RevisionNumber++ txn.FileContractRevisions = []types.V2FileContractRevision{{ Parent: testFces[0], Revision: rev, From bc82bb36eefa3fc8b43028213f2ce689b8e65984 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Thu, 7 Dec 2023 12:49:49 -0500 Subject: [PATCH 3/6] disable indent-error-flow and modifies-value-server in golangci and minor fixes --- .golangci.yml | 4 ++-- consensus/state.go | 1 + internal/smux/stream.go | 5 +---- rhp/v3/registry.go | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e977b71e..4cad493f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -104,11 +104,11 @@ linters-settings: - name: if-return disabled: false - name: indent-error-flow - disabled: false + disabled: true - name: increment-decrement disabled: false - name: modifies-value-receiver - disabled: false + disabled: true - name: optimize-operands-order disabled: false - name: range-val-in-closure diff --git a/consensus/state.go b/consensus/state.go index b7510b51..ada310e2 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -67,6 +67,7 @@ type Network struct { FixHeight uint64 `json:"fixHeight"` GenesisTimestamp time.Time `json:"genesisTimestamp"` } `json:"hardforkOak"` + //nolint:tagliatelle HardforkASIC struct { Height uint64 `json:"height"` OakTime time.Duration `json:"oakTime"` diff --git a/internal/smux/stream.go b/internal/smux/stream.go index 2b4d0752..d66ffe70 100644 --- a/internal/smux/stream.go +++ b/internal/smux/stream.go @@ -147,10 +147,7 @@ func (s *Stream) SetDeadline(t time.Time) error { if err := s.SetReadDeadline(t); err != nil { return err } - if err := s.SetWriteDeadline(t); err != nil { - return err - } - return nil + return s.SetWriteDeadline(t) } // session closes the stream diff --git a/rhp/v3/registry.go b/rhp/v3/registry.go index bcc066fe..feefde5e 100644 --- a/rhp/v3/registry.go +++ b/rhp/v3/registry.go @@ -94,7 +94,7 @@ func RegistryHostID(pk types.PublicKey) types.Hash256 { func ValidateRegistryEntry(re RegistryEntry) (err error) { switch re.Type { case EntryTypeArbitrary: - break // no extra validation required + // no extra validation required case EntryTypePubKey: // pub key entries have the first 20 bytes of the host's pub key hash // prefixed to the data. From 07579aaa8cea888c51ec705dc967c5da6a81ae0c Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Thu, 7 Dec 2023 13:05:14 -0500 Subject: [PATCH 4/6] add inline documentation to RPCCost, fix struct tag formatting --- consensus/validation_test.go | 8 ++++---- rhp/v2/rhp.go | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/consensus/validation_test.go b/consensus/validation_test.go index d1802531..53f1e37b 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -1030,7 +1030,7 @@ func TestValidateV2Block(t *testing.T) { } cs, testCau := consensus.ApplyBlock(cs, validBlock, dbStore.SupplementTipBlock(validBlock), time.Now()) - if dbStore.ApplyBlock(cs, testCau, true) != true { + if !dbStore.ApplyBlock(cs, testCau, true) { t.Fatal("didn't commit block") } updateProofs(testCau, sces, sfes, fces) @@ -1070,7 +1070,7 @@ func TestValidateV2Block(t *testing.T) { t.Fatal(err) } cs, cau = consensus.ApplyBlock(cs, b, dbStore.SupplementTipBlock(validBlock), time.Now()) - if dbStore.ApplyBlock(cs, cau, true) != true { + if !dbStore.ApplyBlock(cs, cau, true) { t.Fatal("didn't commit block") } @@ -1289,8 +1289,8 @@ func TestValidateV2Transaction(t *testing.T) { Index: types.ChainIndex{Height: uint64(0)}, Network: &consensus.Network{ HardforkV2: struct { - AllowHeight uint64 "json:\"allowHeight\"" - RequireHeight uint64 "json:\"requireHeight\"" + AllowHeight uint64 `json:"allowHeight"` + RequireHeight uint64 `json:"requireHeight"` }{ AllowHeight: uint64(2), }, diff --git a/rhp/v2/rhp.go b/rhp/v2/rhp.go index 8732cf4f..a74b64b9 100644 --- a/rhp/v2/rhp.go +++ b/rhp/v2/rhp.go @@ -303,6 +303,7 @@ type ( } ) +// RPCCost represents the cost of an RPC based on a hosts settings. type RPCCost struct { Base types.Currency Storage types.Currency @@ -311,6 +312,7 @@ type RPCCost struct { Collateral types.Currency } +// Add adds two RPCCosts by adding each of the constituent costs together. func (c RPCCost) Add(o RPCCost) RPCCost { return RPCCost{ Base: c.Base.Add(o.Base), @@ -321,6 +323,7 @@ func (c RPCCost) Add(o RPCCost) RPCCost { } } +// Total returns the total cost and collateral required for an RPC call. func (c RPCCost) Total() (cost, collateral types.Currency) { return c.Base.Add(c.Storage).Add(c.Ingress).Add(c.Egress), c.Collateral } From afd2099c042b41da44711ac5a67c57fb5013d894 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Thu, 7 Dec 2023 14:44:52 -0500 Subject: [PATCH 5/6] fix tag --- consensus/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/state.go b/consensus/state.go index ada310e2..10d33918 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -72,7 +72,7 @@ type Network struct { Height uint64 `json:"height"` OakTime time.Duration `json:"oakTime"` OakTarget types.BlockID `json:"oakTarget"` - } `json:"hardforkAsic"` + } `json:"hardforkASIC"` HardforkFoundation struct { Height uint64 `json:"height"` PrimaryAddress types.Address `json:"primaryAddress"` From 1d857a99285663b42475cdd3670a94865b217717 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Fri, 8 Dec 2023 12:16:38 -0500 Subject: [PATCH 6/6] fix lint on windows --- .github/workflows/test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3225bf53..d9a4db4a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,6 +15,9 @@ jobs: runs-on: ${{ matrix.os }} timeout-minutes: 10 steps: + - name: Configure git # required for golangci-lint on Windows + shell: bash + run: git config --global core.autocrlf false - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: @@ -28,4 +31,4 @@ jobs: with: args: "-race" skip-go-install: true - show-package-output: true \ No newline at end of file + show-package-output: true