Skip to content

Commit

Permalink
Fix Issues of golangci-lint (#37)
Browse files Browse the repository at this point in the history
* Add golangci-lint config file, and fix lint issues.

Signed-off-by: txaty <[email protected]>
  • Loading branch information
txaty authored Nov 25, 2023
1 parent 7ab4714 commit ccfc3d2
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 70 deletions.
150 changes: 150 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
linters-settings:
gci:
local-prefixes: github.com/txaty/go-merkletree
dupl:
threshold: 100
errorlint:
errorf: true
errcheck:
check-type-assertions: true
check-blank: true
exhaustive:
check-generated: false
default-signifies-exhaustive: false
funlen:
lines: 65
statements: 40
gocognit:
min-complexity: 15
gocyclo:
min-complexity: 10
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport
- unnecessaryBlock
gofumpt:
extra-rules: true
gomnd:
settings:
mnd:
checks:
- argument
- case
- condition
- operation
- return
govet:
check-shadowing: true
misspell:
locale: US
nestif:
min-complexity: 4
nolintlint:
require-explanation: true
require-specific: true

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errorlint
- exhaustive
- exportloopref
- forbidigo
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goimports
- gomnd
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- makezero
- misspell
- nakedret
- nestif
- nlreturn
- noctx
- nolintlint
- paralleltest
- predeclared
- revive
- rowserrcheck
- sloglint
- sqlclosecheck
- staticcheck
- stylecheck
- tparallel
- thelper
- typecheck
- unconvert
- unparam
- unused
- wastedassign
- wsl
- whitespace
- goheader
- prealloc
- wrapcheck
- zerologlint

disable:
- testpackage

issues:
exclude-rules:
- path: _test\.go
linters:
- cyclop
- depguard
- dupl
- forbidigo
- funlen
- gocognit
- gocritic
- gocyclo
- goerr113
- gosec
- nestif
- nlreturn
- paralleltest
- unparam
- wsl

run:
skip-dirs:
- docs
6 changes: 5 additions & 1 deletion default_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ package merkletree
import "crypto/sha256"

// sha256Digest is the reusable digest for DefaultHashFunc.
// It is used to avoid creating a new hash digest for every call to DefaultHashFunc.
// It is used to avoid creating a new hash digest for every call to DefaultHashFunc and reduce memory allocations.
//
//nolint:gochecknoglobals // Ignoring this linting error as this has to be a global variable.
var sha256Digest = sha256.New()

// DefaultHashFunc is the default hash function used when no user-specified hash function is provided.
// It implements the SHA256 hash function and reuses sha256Digest to reduce memory allocations.
func DefaultHashFunc(data []byte) ([]byte, error) {
defer sha256Digest.Reset()
sha256Digest.Write(data)

return sha256Digest.Sum(make([]byte, 0, sha256Digest.Size())), nil
}

Expand All @@ -42,5 +45,6 @@ func DefaultHashFunc(data []byte) ([]byte, error) {
func DefaultHashFuncParallel(data []byte) ([]byte, error) {
digest := sha256.New()
digest.Write(data)

return digest.Sum(make([]byte, 0, digest.Size())), nil
}
21 changes: 18 additions & 3 deletions leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

package merkletree

import "golang.org/x/sync/errgroup"
import (
"fmt"

"golang.org/x/sync/errgroup"
)

// computeLeafNodes compute the leaf nodes from the data blocks.
func (m *MerkleTree) computeLeafNodes(blocks []DataBlock) ([][]byte, error) {
Expand All @@ -32,11 +36,13 @@ func (m *MerkleTree) computeLeafNodes(blocks []DataBlock) ([][]byte, error) {
disableLeafHashing = m.DisableLeafHashing
err error
)

for i := 0; i < m.NumLeaves; i++ {
if leaves[i], err = dataBlockToLeaf(blocks[i], hashFunc, disableLeafHashing); err != nil {
return nil, err
}
}

return leaves, nil
}

Expand All @@ -50,22 +56,28 @@ func (m *MerkleTree) computeLeafNodesParallel(blocks []DataBlock) ([][]byte, err
disableLeafHashing = m.DisableLeafHashing
eg = new(errgroup.Group)
)

numRoutines = min(numRoutines, lenLeaves)

for startIdx := 0; startIdx < numRoutines; startIdx++ {
startIdx := startIdx

eg.Go(func() error {
var err error
for i := startIdx; i < lenLeaves; i += numRoutines {
if leaves[i], err = dataBlockToLeaf(blocks[i], hashFunc, disableLeafHashing); err != nil {
return err
}
}

return nil
})
}

if err := eg.Wait(); err != nil {
return nil, err
return nil, fmt.Errorf("computeLeafNodesParallel: %w", err)
}

return leaves, nil
}

Expand All @@ -74,13 +86,16 @@ func (m *MerkleTree) computeLeafNodesParallel(blocks []DataBlock) ([][]byte, err
func dataBlockToLeaf(block DataBlock, hashFunc TypeHashFunc, disableLeafHashing bool) ([]byte, error) {
blockBytes, err := block.Serialize()
if err != nil {
return nil, err
return nil, fmt.Errorf("dataBlockToLeaf: %w", err)
}

if disableLeafHashing {
// copy the value so that the original byte slice is not modified
leaf := make([]byte, len(blockBytes))
copy(leaf, blockBytes)

return leaf, nil
}

return hashFunc(blockBytes)
}
Loading

0 comments on commit ccfc3d2

Please sign in to comment.