Skip to content

Commit 5f9fbd8

Browse files
chore: lint ics23 golang code (#161)
* lint * update ci config * remove unused code --------- Co-authored-by: Carlos Rodriguez <[email protected]>
1 parent e179fae commit 5f9fbd8

11 files changed

+152
-37
lines changed

.github/workflows/go.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ jobs:
2323
- uses: actions/setup-go@v4
2424
with:
2525
# ci is set to go1.19 to match developer setups
26-
go-version: 1.19.2
26+
go-version: "1.21"
2727
- uses: actions/checkout@v3
2828
- name: golangci-lint
2929
uses: golangci/golangci-lint-action@v3
3030
with:
3131
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
32-
version: v1.50.0
32+
version: v1.54.2
3333
working-directory: go
3434

3535
test:
@@ -39,7 +39,7 @@ jobs:
3939
- uses: actions/checkout@v3
4040
- uses: actions/setup-go@v4
4141
with:
42-
go-version: 1.19.2
42+
go-version: "1.21"
4343
- name: test
4444
working-directory: ./go
4545
run: make test
@@ -51,7 +51,7 @@ jobs:
5151
- uses: actions/checkout@v3
5252
- uses: actions/setup-go@v4
5353
with:
54-
go-version: 1.19.2
54+
go-version: "1.21"
5555
- name: Run coverage
5656
working-directory: ./go
5757
run: go test -race -coverprofile=coverage.txt -covermode=atomic

go/.golangci.yml

+95-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
run:
2-
tests: false
3-
# timeout for analysis, e.g. 30s, 5m, default is 1m
2+
tests: true
3+
# # timeout for analysis, e.g. 30s, 5m, default is 1m
44
timeout: 5m
55

66
linters:
77
disable-all: true
88
enable:
9-
- depguard
10-
- dogsled
119
- exportloopref
10+
- errcheck
11+
- gci
1212
- goconst
1313
- gocritic
1414
- gofumpt
@@ -18,9 +18,99 @@ linters:
1818
- ineffassign
1919
- misspell
2020
- nakedret
21-
- nolintlint
2221
- staticcheck
22+
- thelper
23+
- typecheck
2324
- stylecheck
25+
- revive
2426
- typecheck
27+
- tenv
2528
- unconvert
29+
# Prefer unparam over revive's unused param. It is more thorough in its checking.
30+
- unparam
2631
- unused
32+
- misspell
33+
34+
issues:
35+
exclude-rules:
36+
- text: 'differs only by capitalization to method'
37+
linters:
38+
- revive
39+
- text: 'Use of weak random number generator'
40+
linters:
41+
- gosec
42+
43+
max-issues-per-linter: 10000
44+
max-same-issues: 10000
45+
46+
linters-settings:
47+
gci:
48+
sections:
49+
- standard # Standard section: captures all standard packages.
50+
- default # Default section: contains all imports that could not be matched to another section type.
51+
- blank # blank imports
52+
- dot # dot imports
53+
- prefix(cosmossdk.io)
54+
- prefix(github.com/cosmos/cosmos-sdk)
55+
- prefix(github.com/cometbft/cometbft)
56+
- prefix(github.com/cosmos/ics23)
57+
custom-order: true
58+
revive:
59+
enable-all-rules: true
60+
# Do NOT whine about the following, full explanation found in:
61+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#description-of-available-rules
62+
rules:
63+
- name: use-any
64+
disabled: true
65+
- name: if-return
66+
disabled: true
67+
- name: max-public-structs
68+
disabled: true
69+
- name: cognitive-complexity
70+
disabled: true
71+
- name: argument-limit
72+
disabled: true
73+
- name: cyclomatic
74+
disabled: true
75+
- name: file-header
76+
disabled: true
77+
- name: function-length
78+
disabled: true
79+
- name: function-result-limit
80+
disabled: true
81+
- name: line-length-limit
82+
disabled: true
83+
- name: flag-parameter
84+
disabled: true
85+
- name: add-constant
86+
disabled: true
87+
- name: empty-lines
88+
disabled: true
89+
- name: banned-characters
90+
disabled: true
91+
- name: deep-exit
92+
disabled: true
93+
- name: confusing-results
94+
disabled: true
95+
- name: unused-parameter
96+
disabled: true
97+
- name: modifies-value-receiver
98+
disabled: true
99+
- name: early-return
100+
disabled: true
101+
- name: nested-structs
102+
disabled: true
103+
- name: confusing-naming
104+
disabled: true
105+
- name: defer
106+
disabled: true
107+
# Disabled in favour of unparam.
108+
- name: unused-parameter
109+
disabled: true
110+
- name: unhandled-error
111+
disabled: false
112+
arguments:
113+
- 'fmt.Printf'
114+
- 'fmt.Print'
115+
- 'fmt.Println'
116+
- 'myFunction'

go/fuzz_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ func init() {
6161
batchVectorDataSeeds = bsL
6262
}
6363

64-
var specVectorTestData = VectorsTestData()
65-
6664
func FuzzVerifyNonMembership(f *testing.F) {
6765
if testing.Short() {
6866
f.Skip("in -short mode")

go/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/cosmos/ics23/go
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/cosmos/gogoproto v1.4.11

go/ops.go

+22-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
_ "crypto/sha256"
1313
// adds sha512 capability to crypto.SHA512
1414
_ "crypto/sha512"
15-
1615
// adds ripemd160 capability to crypto.RIPEMD160
1716
_ "golang.org/x/crypto/ripemd160" //nolint:staticcheck
1817
)
@@ -164,15 +163,24 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
164163
case HashOp_BITCOIN:
165164
// ripemd160(sha256(x))
166165
sha := crypto.SHA256.New()
167-
sha.Write(preimage)
166+
_, err := sha.Write(preimage)
167+
if err != nil {
168+
return nil, err
169+
}
168170
tmp := sha.Sum(nil)
169-
hash := crypto.RIPEMD160.New()
170-
hash.Write(tmp)
171-
return hash.Sum(nil), nil
171+
bitcoinHash := crypto.RIPEMD160.New()
172+
_, err = bitcoinHash.Write(tmp)
173+
if err != nil {
174+
return nil, err
175+
}
176+
return bitcoinHash.Sum(nil), nil
172177
case HashOp_SHA512_256:
173-
hash := crypto.SHA512_256.New()
174-
hash.Write(preimage)
175-
return hash.Sum(nil), nil
178+
shaHash := crypto.SHA512_256.New()
179+
_, err := shaHash.Write(preimage)
180+
if err != nil {
181+
return nil, err
182+
}
183+
return shaHash.Sum(nil), nil
176184
}
177185
return nil, fmt.Errorf("unsupported hashop: %d", hashOp)
178186
}
@@ -183,7 +191,10 @@ type hasher interface {
183191

184192
func hashBz(h hasher, preimage []byte) ([]byte, error) {
185193
hh := h.New()
186-
hh.Write(preimage)
194+
_, err := hh.Write(preimage)
195+
if err != nil {
196+
return nil, err
197+
}
187198
return hh.Sum(nil), nil
188199
}
189200

@@ -193,8 +204,8 @@ func prepareLeafData(hashOp HashOp, lengthOp LengthOp, data []byte) ([]byte, err
193204
if err != nil {
194205
return nil, err
195206
}
196-
ldata, err := doLengthOp(lengthOp, hdata)
197-
return ldata, err
207+
208+
return doLengthOp(lengthOp, hdata)
198209
}
199210

200211
func validateSpec(spec *ProofSpec) bool {

go/ops_data_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type LeafOpTestStruct struct {
1717
}
1818

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

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

6264
func DoHashTestData(t *testing.T) map[string]DoHashTestStruct {
65+
t.Helper()
6366
fname := filepath.Join("..", "testdata", "TestDoHashData.json")
6467
ffile, err := os.Open(fname)
6568
if err != nil {

go/ops_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func TestLeafOp(t *testing.T) {
1414
res, err := tc.Op.Apply(tc.Key, tc.Value)
1515
// short-circuit with error case
1616
if tc.IsErr && err == nil {
17-
t.Fatal("Expected error, but got none")
17+
t.Fatal("expected error, but got none")
1818
}
19-
if tc.IsErr == false && err != nil {
19+
if !tc.IsErr && err != nil {
2020
t.Fatal(err)
2121
}
2222
if !bytes.Equal(res, tc.Expected) {
23-
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))
23+
t.Errorf("bad result: %s vs %s", toHex(res), toHex(tc.Expected))
2424
}
2525
})
2626
}
@@ -35,7 +35,7 @@ func TestInnerOp(t *testing.T) {
3535
if tc.IsErr && err == nil {
3636
t.Fatal("Expected error, but got none")
3737
}
38-
if tc.IsErr == false && err != nil {
38+
if !tc.IsErr && err != nil {
3939
t.Fatal(err)
4040
}
4141
if !bytes.Equal(res, tc.Expected) {

go/proof.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
200200
if err := inner.CheckAgainstSpec(spec, layerNum); err != nil {
201201
return fmt.Errorf("inner, %w", err)
202202
}
203-
layerNum += 1
203+
layerNum++
204204
}
205205
return nil
206206
}
@@ -367,7 +367,7 @@ func getPadding(spec *InnerSpec, branch int32) (minPrefix, maxPrefix, suffix int
367367

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

373373
// leftBranchesAreEmpty returns true if the padding bytes correspond to all empty siblings

go/proof_data_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type ExistenceProofTestStruct struct {
1414
}
1515

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

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

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

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

99103
return []EmptyBranchTestStruct{

go/proof_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ func TestExistenceProof(t *testing.T) {
1212
t.Run(name, func(t *testing.T) {
1313
res, err := tc.Proof.Calculate()
1414
// short-circuit with error case
15-
if tc.IsErr && err == nil {
16-
t.Fatal("Expected error, but got none")
17-
}
18-
if tc.IsErr == false && err != nil {
19-
t.Fatal(err)
15+
if tc.IsErr {
16+
if err == nil {
17+
t.Fatal("Expected error, but got none")
18+
}
19+
} else {
20+
if err != nil {
21+
t.Fatal(err)
22+
}
2023
}
2124
if !bytes.Equal(res, tc.Expected) {
2225
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))

0 commit comments

Comments
 (0)