Skip to content

Commit f7c96b2

Browse files
authored
chore: add golangci-lint config (celestiaorg#107)
While working on a different PR, my editor complained about failed go-staticcheck warnings. For consistency across codebases, I adopted the celestia-app golangci-lint config and fixed the lint errors in the screenshot below. This repo already has a lint workflow that runs [golangci-lint](https://golangci-lint.run/) so CI will run with these linters moving forward (confirmed via verbose logging [here](https://github.com/celestiaorg/nmt/actions/runs/4207167400/jobs/7301618009#step:4:33)). ## Screenshot <img width="1441" alt="Screenshot 2023-02-17 at 2 48 43 PM" src="https://user-images.githubusercontent.com/3699047/219778350-ebaac63e-c7e9-407e-8f73-7ca1f3d77786.png">
1 parent d40e66c commit f7c96b2

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ jobs:
2424
with:
2525
version: latest
2626
args: --timeout 10m
27-
if: env.GIT_DIFF
27+
if: env.GIT_DIFF

.golangci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
run:
2+
timeout: 5m
3+
modules-download-mode: readonly
4+
5+
linters:
6+
enable:
7+
- exportloopref
8+
- gofumpt
9+
- misspell
10+
- revive
11+
- prealloc

hasher_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package nmt
33
import (
44
"crypto"
55
"crypto/sha256"
6-
_ "crypto/sha256"
76
"reflect"
87
"testing"
98

nmt_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,8 @@ func TestNMT_forgedNamespaceEmptinessProof(t *testing.T) {
619619
append(namespace.ID{1}, []byte("leaf_0")...),
620620
append(namespace.ID{1}, []byte("leaf_1")...),
621621
append(namespace.ID{2}, []byte("leaf_2")...),
622-
append(namespace.ID{2}, []byte("leaf_3")...)}
622+
append(namespace.ID{2}, []byte("leaf_3")...),
623+
}
623624
// Init a tree with the namespace size as well as
624625
// the underlying hash function:
625626
tree := New(sha256.New(), NamespaceIDSize(1))

proof.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ func NewAbsenceProof(proofStart, proofEnd int, proofNodes [][]byte, leafHash []b
120120
// `end-1` of the tree.
121121
//
122122
// `root` is the root of the NMT against which the `proof` is verified.
123-
func (proof Proof) VerifyNamespace(
124-
h hash.Hash, nID namespace.ID, data [][]byte, root []byte) bool {
123+
func (proof Proof) VerifyNamespace(h hash.Hash, nID namespace.ID, data [][]byte, root []byte) bool {
125124
nth := NewNmtHasher(h, nID.Size(), proof.isMaxNamespaceIDIgnored)
126125
min := namespace.ID(MinNamespace(root, nID.Size()))
127126
max := namespace.ID(MaxNamespace(root, nID.Size()))

subrootpaths.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
var (
9-
srpNotPowerOf2 = errors.New("GetSubrootPaths: Supplied square size is not a power of 2")
10-
srpInvalidShareCount = errors.New("GetSubrootPaths: Can't compute path for 0 share count slice")
11-
srpPastSquareSize = errors.New("GetSubrootPaths: Share slice can't be past the square size")
12-
srpInvalidIdxEnd = errors.New("GetSubrootPaths: idxEnd must be larger than idxStart and shareCount")
9+
errNotPowerOf2 = errors.New("GetSubrootPaths: Supplied square size is not a power of 2")
10+
errInvalidShareCount = errors.New("GetSubrootPaths: Can't compute path for 0 share count slice")
11+
errPastSquareSize = errors.New("GetSubrootPaths: Share slice can't be past the square size")
12+
errInvalidIdxEnd = errors.New("GetSubrootPaths: idxEnd must be larger than idxStart and shareCount")
1313
)
1414

1515
// merkle path to a node is equivalent to the index's binary representation
@@ -51,9 +51,8 @@ func prune(idxStart uint, idxEnd uint, maxWidth uint) [][]int {
5151
if idxStart+1 >= idxEnd {
5252
if idxStart%2 == 1 {
5353
return [][]int{pathStart, pathEnd}
54-
} else {
55-
return [][]int{pathStart[:len(pathStart)-1]}
5654
}
55+
return [][]int{pathStart[:len(pathStart)-1]}
5756
}
5857

5958
var prunedPaths [][]int
@@ -127,24 +126,24 @@ func GetSubrootPaths(squareSize uint, idxStart uint, shareCount uint) ([][][]int
127126
// check squareSize is at least 2 and that it's
128127
// a power of 2 by checking that only 1 bit is on
129128
if squareSize < 2 || bits.OnesCount(squareSize) != 1 {
130-
return nil, srpNotPowerOf2
129+
return nil, errNotPowerOf2
131130
}
132131

133132
// no path exists for 0 count slice
134133
if shareCount == 0 {
135-
return nil, srpInvalidShareCount
134+
return nil, errInvalidShareCount
136135
}
137136

138137
idxEnd := idxStart + shareCount
139138
if idxEnd < idxStart || idxEnd < shareCount {
140-
return nil, srpInvalidIdxEnd
139+
return nil, errInvalidIdxEnd
141140
}
142141

143142
shares := squareSize * squareSize
144143

145144
// sanity checking
146145
if idxStart >= shares || idxEnd > shares {
147-
return nil, srpPastSquareSize
146+
return nil, errPastSquareSize
148147
}
149148

150149
startRow := idxStart / squareSize

subrootpaths_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ func TestArgValidation(t *testing.T) {
2020
}
2121

2222
tests := []test{
23-
{input: pathSpan{squareSize: 0, startNode: 0, length: 0}, want: srpNotPowerOf2},
24-
{input: pathSpan{squareSize: 1, startNode: 0, length: 1}, want: srpNotPowerOf2},
25-
{input: pathSpan{squareSize: 20, startNode: 0, length: 1}, want: srpNotPowerOf2},
26-
{input: pathSpan{squareSize: 4, startNode: 0, length: 17}, want: srpPastSquareSize},
27-
{input: pathSpan{squareSize: 4, startNode: 0, length: 0}, want: srpInvalidShareCount},
28-
{input: pathSpan{squareSize: 128, startNode: 1, length: 18446744073709551615}, want: srpInvalidIdxEnd},
23+
{input: pathSpan{squareSize: 0, startNode: 0, length: 0}, want: errNotPowerOf2},
24+
{input: pathSpan{squareSize: 1, startNode: 0, length: 1}, want: errNotPowerOf2},
25+
{input: pathSpan{squareSize: 20, startNode: 0, length: 1}, want: errNotPowerOf2},
26+
{input: pathSpan{squareSize: 4, startNode: 0, length: 17}, want: errPastSquareSize},
27+
{input: pathSpan{squareSize: 4, startNode: 0, length: 0}, want: errInvalidShareCount},
28+
{input: pathSpan{squareSize: 128, startNode: 1, length: 18446744073709551615}, want: errInvalidIdxEnd},
2929
}
3030

3131
for _, tc := range tests {

0 commit comments

Comments
 (0)