Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Aug 24, 2023
1 parent 9089921 commit 91f6a79
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 36 deletions.
100 changes: 95 additions & 5 deletions go/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
run:
tests: false
# timeout for analysis, e.g. 30s, 5m, default is 1m
tests: true
# # timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

linters:
disable-all: true
enable:
- depguard
- dogsled
- exportloopref
- errcheck
- gci
- goconst
- gocritic
- gofumpt
Expand All @@ -18,9 +18,99 @@ linters:
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- thelper
- typecheck
- stylecheck
- revive
- typecheck
- tenv
- unconvert
# Prefer unparam over revive's unused param. It is more thorough in its checking.
- unparam
- unused
- misspell

issues:
exclude-rules:
- text: 'differs only by capitalization to method'
linters:
- revive
- text: 'Use of weak random number generator'
linters:
- gosec

max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- blank # blank imports
- dot # dot imports
- prefix(cosmossdk.io)
- prefix(github.com/cosmos/cosmos-sdk)
- prefix(github.com/cometbft/cometbft)
- prefix(github.com/cosmos/ics23)
custom-order: true
revive:
enable-all-rules: true
# Do NOT whine about the following, full explanation found in:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#description-of-available-rules
rules:
- name: use-any
disabled: true
- name: if-return
disabled: true
- name: max-public-structs
disabled: true
- name: cognitive-complexity
disabled: true
- name: argument-limit
disabled: true
- name: cyclomatic
disabled: true
- name: file-header
disabled: true
- name: function-length
disabled: true
- name: function-result-limit
disabled: true
- name: line-length-limit
disabled: true
- name: flag-parameter
disabled: true
- name: add-constant
disabled: true
- name: empty-lines
disabled: true
- name: banned-characters
disabled: true
- name: deep-exit
disabled: true
- name: confusing-results
disabled: true
- name: unused-parameter
disabled: true
- name: modifies-value-receiver
disabled: true
- name: early-return
disabled: true
- name: nested-structs
disabled: true
- name: confusing-naming
disabled: true
- name: defer
disabled: true
# Disabled in favour of unparam.
- name: unused-parameter
disabled: true
- name: unhandled-error
disabled: false
arguments:
- 'fmt.Printf'
- 'fmt.Print'
- 'fmt.Println'
- 'myFunction'
4 changes: 2 additions & 2 deletions go/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func compressStep(step *InnerOp, lookup *[]*InnerOp, registry map[string]int32)
}

// load from cache if there
if num, ok := registry[string(bz)]; ok {
if num, ok := registry[string(bz)]; ok {
return num
}

// create new step if not there
num := int32(len(*lookup))
*lookup = append(*lookup, step)
registry[string(bz)] = num
registry[string(bz)] = num
return num
}

Expand Down
10 changes: 8 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module github.com/cosmos/ics23/go

go 1.19
go 1.21

require (
github.com/cosmos/gogoproto v1.4.3
github.com/cosmos/gogoproto v1.4.11
golang.org/x/crypto v0.2.0
)

require (
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

// subject to the dragonberry vulnerability
retract [v0.0.0, v0.7.0]
14 changes: 12 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/cosmos/gogoproto v1.4.3 h1:RP3yyVREh9snv/lsOvmsAPQt8f44LgL281X0IOIhhcI=
github.com/cosmos/gogoproto v1.4.3/go.mod h1:0hLIG5TR7IvV1fme1HCFKjfzW9X2x0Mo+RooWXCnOWU=
github.com/cosmos/gogoproto v1.4.11 h1:LZcMHrx4FjUgrqQSWeaGC1v/TeuVFqSLa43CC6aWR2g=
github.com/cosmos/gogoproto v1.4.11/go.mod h1:/g39Mh8m17X8Q/GDEs5zYTSNaNnInBSohtaxzQnYq1Y=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
33 changes: 22 additions & 11 deletions go/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
_ "crypto/sha256"
// adds sha512 capability to crypto.SHA512
_ "crypto/sha512"

// adds ripemd160 capability to crypto.RIPEMD160
_ "golang.org/x/crypto/ripemd160" //nolint:staticcheck
)
Expand Down Expand Up @@ -164,15 +163,24 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
case HashOp_BITCOIN:
// ripemd160(sha256(x))
sha := crypto.SHA256.New()
sha.Write(preimage)
_, err := sha.Write(preimage)
if err != nil {
return nil, err
}

Check warning on line 169 in go/ops.go

View check run for this annotation

Codecov / codecov/patch

go/ops.go#L168-L169

Added lines #L168 - L169 were not covered by tests
tmp := sha.Sum(nil)
hash := crypto.RIPEMD160.New()
hash.Write(tmp)
return hash.Sum(nil), nil
bitcoinHash := crypto.RIPEMD160.New()
_, err = bitcoinHash.Write(tmp)
if err != nil {
return nil, err
}

Check warning on line 175 in go/ops.go

View check run for this annotation

Codecov / codecov/patch

go/ops.go#L174-L175

Added lines #L174 - L175 were not covered by tests
return bitcoinHash.Sum(nil), nil
case HashOp_SHA512_256:
hash := crypto.SHA512_256.New()
hash.Write(preimage)
return hash.Sum(nil), nil
shaHash := crypto.SHA512_256.New()
_, err := shaHash.Write(preimage)
if err != nil {
return nil, err
}

Check warning on line 182 in go/ops.go

View check run for this annotation

Codecov / codecov/patch

go/ops.go#L181-L182

Added lines #L181 - L182 were not covered by tests
return shaHash.Sum(nil), nil
}
return nil, fmt.Errorf("unsupported hashop: %d", hashOp)
}
Expand All @@ -183,7 +191,10 @@ type hasher interface {

func hashBz(h hasher, preimage []byte) ([]byte, error) {
hh := h.New()
hh.Write(preimage)
_, err := hh.Write(preimage)
if err != nil {
return nil, err
}

Check warning on line 197 in go/ops.go

View check run for this annotation

Codecov / codecov/patch

go/ops.go#L196-L197

Added lines #L196 - L197 were not covered by tests
return hh.Sum(nil), nil
}

Expand All @@ -193,8 +204,8 @@ func prepareLeafData(hashOp HashOp, lengthOp LengthOp, data []byte) ([]byte, err
if err != nil {
return nil, err
}
ldata, err := doLengthOp(lengthOp, hdata)
return ldata, err

return doLengthOp(lengthOp, hdata)
}

func validateSpec(spec *ProofSpec) bool {
Expand Down
3 changes: 3 additions & 0 deletions go/ops_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type LeafOpTestStruct struct {
}

func LeafOpTestData(t *testing.T) map[string]LeafOpTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestLeafOpData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand All @@ -39,6 +40,7 @@ type InnerOpTestStruct struct {
}

func InnerOpTestData(t *testing.T) map[string]InnerOpTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestInnerOpData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand All @@ -60,6 +62,7 @@ type DoHashTestStruct struct {
}

func DoHashTestData(t *testing.T) map[string]DoHashTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestDoHashData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions go/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func TestLeafOp(t *testing.T) {
res, err := tc.Op.Apply(tc.Key, tc.Value)
// short-circuit with error case
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got none")
t.Fatal("expected error, but got none")
}
if tc.IsErr == false && err != nil {
if !tc.IsErr && err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, tc.Expected) {
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))
t.Errorf("bad result: %s vs %s", toHex(res), toHex(tc.Expected))
}
})
}
Expand All @@ -35,7 +35,7 @@ func TestInnerOp(t *testing.T) {
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got none")
}
if tc.IsErr == false && err != nil {
if !tc.IsErr && err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, tc.Expected) {
Expand Down
4 changes: 2 additions & 2 deletions go/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
if err := inner.CheckAgainstSpec(spec, layerNum); err != nil {
return fmt.Errorf("inner, %w", err)
}
layerNum += 1
layerNum++
}
return nil
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func getPadding(spec *InnerSpec, branch int32) (minPrefix, maxPrefix, suffix int

// count how many children are in the suffix
suffix = (len(spec.ChildOrder) - 1 - idx) * int(spec.ChildSize)
return
return minPrefix, maxPrefix, suffix
}

// leftBranchesAreEmpty returns true if the padding bytes correspond to all empty siblings
Expand Down
4 changes: 4 additions & 0 deletions go/proof_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ExistenceProofTestStruct struct {
}

func ExistenceProofTestData(t *testing.T) map[string]ExistenceProofTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestExistenceProofData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand All @@ -35,6 +36,7 @@ type CheckLeafTestStruct struct {
}

func CheckLeafTestData(t *testing.T) map[string]CheckLeafTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestCheckLeafData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand All @@ -56,6 +58,7 @@ type CheckAgainstSpecTestStruct struct {
}

func CheckAgainstSpecTestData(t *testing.T) map[string]CheckAgainstSpecTestStruct {
t.Helper()
fname := filepath.Join("..", "testdata", "TestCheckAgainstSpecData.json")
ffile, err := os.Open(fname)
if err != nil {
Expand Down Expand Up @@ -94,6 +97,7 @@ type EmptyBranchTestStruct struct {
}

func EmptyBranchTestData(t *testing.T) []EmptyBranchTestStruct {
t.Helper()
emptyChild := SpecWithEmptyChild.InnerSpec.EmptyChild

return []EmptyBranchTestStruct{
Expand Down
13 changes: 8 additions & 5 deletions go/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ func TestExistenceProof(t *testing.T) {
t.Run(name, func(t *testing.T) {
res, err := tc.Proof.Calculate()
// short-circuit with error case
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got none")
}
if tc.IsErr == false && err != nil {
t.Fatal(err)
if tc.IsErr {
if err == nil {
t.Fatal("Expected error, but got none")
}
} else {
if err != nil {
t.Fatal(err)
}
}
if !bytes.Equal(res, tc.Expected) {
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))
Expand Down
Loading

0 comments on commit 91f6a79

Please sign in to comment.