From 12dba45c7755cbd09f8c8886da8d022e85615839 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Tue, 7 May 2024 10:19:49 +0100 Subject: [PATCH] Linting and workflows. --- .github/workflows/golangci-lint.yml | 9 ++- .github/workflows/test.yml | 3 + .golangci.yml | 94 ++++++++++++++++++++++++----- blake2b/hash.go | 6 +- dot.go | 4 ++ formatter.go | 6 +- go.mod | 9 ++- go.sum | 8 +-- keccak256/hash.go | 6 +- poseidon/hash.go | 6 +- sha3/sha256.go | 6 +- sha3/sha512.go | 6 +- 12 files changed, 118 insertions(+), 45 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 768ceb4..d96b088 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -7,18 +7,21 @@ on: permissions: contents: read + pull-requests: read jobs: golangci: name: lint runs-on: ubuntu-22.04 steps: + - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: cache: false go-version: '1.20' - - uses: actions/checkout@v4 - name: golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v5 with: - args: --timeout=60m + version: 'latest' + args: '--timeout=60m' + skip-cache: true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33584be..4eafaa9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,3 +14,6 @@ jobs: go-version: '1.20' - uses: actions/checkout@v4 - uses: n8maninger/action-golang-test@v2 + with: + args: "-race;-timeout=30m" + diff --git a/.golangci.yml b/.golangci.yml index d5cb8d4..9b152e0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,16 @@ # This file is not a configuration example, # it contains the exhaustive configuration with explanations of the options. +issues: + # Which files to exclude: they will be analyzed, but issues from them won't be reported. + # There is no need to include all autogenerated files, + # we confidently recognize autogenerated files. + # If it's not, please let us know. + # "/" will be replaced by current OS file path separator to properly work on Windows. + # Default: [] + exclude-files: + - ".*_ssz\\.go$" + # Options for analysis running. run: # The default concurrency value is the number of available CPU. @@ -39,15 +49,6 @@ run: # Default: true # skip-dirs-use-default: false - # 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. - # "/" will be replaced by current OS file path separator to properly work on Windows. - skip-files: - - ".*_ssz\\.go$" - # If set we pass it to "go list -mod={option}". From "go help modules": # If invoked with -mod=readonly, the go command is disallowed from the implicit # automatic updating of go.mod described above. Instead, it fails when any changes @@ -68,7 +69,7 @@ run: # Define the Go version limit. # Mainly related to generics support since go1.18. # Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.18 - go: '1.19' + # go: '1.20' # output configuration options @@ -108,6 +109,56 @@ linters-settings: lll: line-length: 132 + revive: + ignore-generated-header: true + severity: error + confidence: 0.1 + errorCode: 0 + warningCode: 0 + rules: + - name: atomic + - name: blank-imports + - name: comment-spacings + - name: confusing-results + - name: constant-logical-expr + - name: context-as-argument + - name: context-keys-type + - name: datarace + - name: defer + - name: dot-imports + - name: duplicated-imports + - name: early-return + - name: error-strings + - name: errorf + - name: exported + - name: file-header + - name: identical-branches + - name: import-alias-naming + - name: import-shadowing + - name: increment-decrement + - name: indent-error-flow + - name: modifies-value-receiver + - name: range-val-address + - name: range-val-in-closure + - name: range + - name: receiver-naming + - name: string-of-int + - name: superfluous-else + - name: time-equal + - name: time-naming + - name: unchecked-type-assertion + - name: unhandled-error + arguments: + - "fmt.Fprint" + - "fmt.Fprintf" + - name: unnecessary-stmt + - name: unused-parameter + - name: unused-receiver + - name: use-any + - name: useless-break + - name: var-naming + - name: waitgroup-by-value + stylecheck: checks: [ "all", "-ST1000" ] @@ -118,10 +169,6 @@ linters-settings: json: snake yaml: snake - nlreturn: - # Allow two-line blocks without requiring a newline - block-size: 3 - linters: # Enable all available linters. # Default: false @@ -129,20 +176,37 @@ linters: # Disable specific linter # https://golangci-lint.run/usage/linters/#disabled-by-default disable: + - cyclop - deadcode - depguard + - dupl + - execinquery + - exhaustive - exhaustivestruct - exhaustruct - - goerr113 + - forcetypeassert + - funlen + - gci + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - err113 - golint - gomnd - ifshort - interfacer - ireturn + - lll + - maintidx - maligned + - mnd + - musttag - nosnakecase + - perfsprint - scopelint - structcheck - varcheck - varnamelen + - wrapcheck - wsl diff --git a/blake2b/hash.go b/blake2b/hash.go index 74ba690..3e9e22a 100644 --- a/blake2b/hash.go +++ b/blake2b/hash.go @@ -29,17 +29,17 @@ func New() *BLAKE2b { } // HashLength returns the length of hashes generated by Hash() in bytes. -func (h *BLAKE2b) HashLength() int { +func (*BLAKE2b) HashLength() int { return _hashlength } // HashName returns the name of this hash. -func (h *BLAKE2b) HashName() string { +func (*BLAKE2b) HashName() string { return "blake2b" } // Hash generates a BLAKE2b hash from input byte arrays. -func (h *BLAKE2b) Hash(data ...[]byte) []byte { +func (*BLAKE2b) Hash(data ...[]byte) []byte { var hash [_hashlength]byte if len(data) == 1 { hash = blake2b.Sum256(data[0]) diff --git a/dot.go b/dot.go index 8745408..d7b50b2 100644 --- a/dot.go +++ b/dot.go @@ -103,6 +103,7 @@ func (t *MerkleTree) DOTMultiProof(multiProof *MultiProof, lf Formatter, bf Form return t.dot(rootIndices, valueIndices, proofIndices, lf, bf) } +//nolint:revive func (t *MerkleTree) dot(rootIndices, valueIndices, proofIndices map[uint64]int, lf, bf Formatter) string { if lf == nil { lf = new(TruncatedHexFormatter) @@ -139,6 +140,7 @@ func (t *MerkleTree) dot(rootIndices, valueIndices, proofIndices map[uint64]int, return builder.String() } +//nolint:revive func (t *MerkleTree) dotBranches(rootIndices, proofIndices map[uint64]int, bf Formatter, builder *strings.Builder) { valuesOffset := int(math.Ceil(float64(len(t.Nodes)) / 2)) for valueIndex := valuesOffset - 1; valueIndex > 0; valueIndex-- { @@ -155,6 +157,7 @@ func (t *MerkleTree) dotBranches(rootIndices, proofIndices map[uint64]int, bf Fo } } +//nolint:revive func (t *MerkleTree) dotLeaf(builder *strings.Builder, nodeBuilder *strings.Builder, i int, @@ -193,6 +196,7 @@ func (t *MerkleTree) dotLeaf(builder *strings.Builder, } } +//nolint:revive func (t *MerkleTree) dotEmptyLeaf(builder *strings.Builder, nodeBuilder *strings.Builder, offset int, diff --git a/formatter.go b/formatter.go index dda5fe5..5e2ac37 100644 --- a/formatter.go +++ b/formatter.go @@ -26,7 +26,7 @@ type Formatter interface { type TruncatedHexFormatter struct{} // Format formats a value as truncated hex, showing the first and last four characers of the hex string. -func (f *TruncatedHexFormatter) Format(data []byte) string { +func (*TruncatedHexFormatter) Format(data []byte) string { return fmt.Sprintf("%4x…%4x", data[0:2], data[len(data)-2:]) } @@ -34,7 +34,7 @@ func (f *TruncatedHexFormatter) Format(data []byte) string { type HexFormatter struct{} // Format formats a value as a full hex string. -func (f *HexFormatter) Format(data []byte) string { +func (*HexFormatter) Format(data []byte) string { return fmt.Sprintf("%0x", data) } @@ -42,6 +42,6 @@ func (f *HexFormatter) Format(data []byte) string { type StringFormatter struct{} // Format formats a value as a UTF-8 string. -func (f *StringFormatter) Format(data []byte) string { +func (*StringFormatter) Format(data []byte) string { return string(data) } diff --git a/go.mod b/go.mod index 00485a6..89581fe 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,15 @@ module github.com/wealdtech/go-merkletree/v2 go 1.20 require ( + github.com/iden3/go-iden3-crypto v0.0.16 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.2 - golang.org/x/crypto v0.18.0 + golang.org/x/crypto v0.23.0 ) -require gopkg.in/yaml.v3 v3.0.1 // indirect - require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/iden3/go-iden3-crypto v0.0.16 github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.16.0 // indirect + golang.org/x/sys v0.20.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c28742e..84a8f73 100644 --- a/go.sum +++ b/go.sum @@ -15,10 +15,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= -golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= -golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= -golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/keccak256/hash.go b/keccak256/hash.go index 8e60b2b..7bab043 100644 --- a/keccak256/hash.go +++ b/keccak256/hash.go @@ -29,17 +29,17 @@ func New() *Keccak256 { } // HashLength returns the length of hashes generated by Hash() in bytes. -func (h *Keccak256) HashLength() int { +func (*Keccak256) HashLength() int { return _hashlength } // HashName returns the name of this hash. -func (h *Keccak256) HashName() string { +func (*Keccak256) HashName() string { return "keccak256" } // Hash generates a Keccak-256 hash from a byte array. -func (h *Keccak256) Hash(data ...[]byte) []byte { +func (*Keccak256) Hash(data ...[]byte) []byte { hash := sha3.NewLegacyKeccak256() for _, d := range data { hash.Write(d) diff --git a/poseidon/hash.go b/poseidon/hash.go index 9267d73..799ab5d 100644 --- a/poseidon/hash.go +++ b/poseidon/hash.go @@ -29,7 +29,7 @@ func New() *Poseidon { } // Hash generates a Poseidon hash from a byte array. -func (ph *Poseidon) Hash(data ...[]byte) []byte { +func (*Poseidon) Hash(data ...[]byte) []byte { var hash []byte if len(data) == 1 { hash = poseidon.Sum(data[0]) @@ -51,11 +51,11 @@ func (ph *Poseidon) Hash(data ...[]byte) []byte { } // HashLength returns the length of hashes generated by Hash() in bytes. -func (ph *Poseidon) HashLength() int { +func (*Poseidon) HashLength() int { return _hashlength } // HashName returns the name of this hash. -func (ph *Poseidon) HashName() string { +func (*Poseidon) HashName() string { return "poseidon" } diff --git a/sha3/sha256.go b/sha3/sha256.go index b79e94c..4c345a5 100644 --- a/sha3/sha256.go +++ b/sha3/sha256.go @@ -29,17 +29,17 @@ func New256() *SHA256 { } // HashLength returns the length of hashes generated by Hash() in bytes. -func (h *SHA256) HashLength() int { +func (*SHA256) HashLength() int { return _256hashlength } // HashName returns the name of this hash. -func (h *SHA256) HashName() string { +func (*SHA256) HashName() string { return "sha256" } // Hash generates a SHA3 hash from input byte arrays. -func (h *SHA256) Hash(data ...[]byte) []byte { +func (*SHA256) Hash(data ...[]byte) []byte { var hash [_256hashlength]byte if len(data) == 1 { hash = sha3.Sum256(data[0]) diff --git a/sha3/sha512.go b/sha3/sha512.go index c1a88db..c71bc51 100644 --- a/sha3/sha512.go +++ b/sha3/sha512.go @@ -28,17 +28,17 @@ func New512() *SHA512 { } // HashLength returns the length of hashes generated by Hash() in bytes. -func (h *SHA512) HashLength() int { +func (*SHA512) HashLength() int { return _512hashlength } // HashName returns the name of this hash. -func (h *SHA512) HashName() string { +func (*SHA512) HashName() string { return "sha512" } // Hash generates a SHA3 hash from input byte arrays. -func (h *SHA512) Hash(data ...[]byte) []byte { +func (*SHA512) Hash(data ...[]byte) []byte { var hash [_512hashlength]byte if len(data) == 1 { hash = sha3.Sum512(data[0])