Skip to content

Commit

Permalink
Linting and workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed May 7, 2024
1 parent 8170082 commit 12dba45
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 45 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ jobs:
go-version: '1.20'
- uses: actions/checkout@v4
- uses: n8maninger/action-golang-test@v2
with:
args: "-race;-timeout=30m"

94 changes: 79 additions & 15 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" ]

Expand All @@ -118,31 +169,44 @@ 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
enable-all: true
# 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
6 changes: 3 additions & 3 deletions blake2b/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 4 additions & 0 deletions dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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-- {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ 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:])
}

// HexFormatter shows the entire value.
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)
}

// StringFormatter shows the entire value as a 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)
}
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions keccak256/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions poseidon/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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"
}
6 changes: 3 additions & 3 deletions sha3/sha256.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions sha3/sha512.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 12dba45

Please sign in to comment.