Skip to content

Commit

Permalink
Add custom linter cfg (#972)
Browse files Browse the repository at this point in the history
Add customer linter config for ocr3 offchain
  • Loading branch information
dimkouv authored Jun 6, 2024
1 parent 30ceb90 commit 9909698
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-meals-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ccip": patch
---

#updated golangci-lint with custom rules
2 changes: 1 addition & 1 deletion .github/workflows/ccip-ocr3-build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ jobs:
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.0
- name: Run linter
run: golangci-lint run
run: golangci-lint run -c .golangci.yml
- name: Run tests
run: go test -race -fullpath -shuffle on -count 20 ./...
87 changes: 87 additions & 0 deletions core/services/ocr3/plugins/ccip/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
run:
timeout: 60s
linters:
enable:
- exhaustive
- exportloopref
- revive
- goimports
- gosec
- misspell
- rowserrcheck
- errorlint
- unconvert
- sqlclosecheck
- noctx
- depguard
linters-settings:
exhaustive:
default-signifies-exhaustive: true
goimports:
local-prefixes: "github.com/smartcontractkit/ccipocr3"
gosec:
excludes:
errorlint:
errorf: true # Disallow formatting of errors without %w
revive:
confidence: 0.8
rules:
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: if-return
- name: increment-decrement
- name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
# - name: unused-parameter
- name: unreachable-code
- name: redefines-builtin-id
- name: waitgroup-by-value
- name: unconditional-recursion
- name: struct-tag
- name: string-format
- name: string-of-int
- name: range-val-address
- name: range-val-in-closure
- name: modifies-value-receiver
- name: modifies-parameter
- name: identical-branches
- name: get-return
#- name: flag-parameter
- name: early-return
- name: defer
- name: constant-logical-expr
- name: confusing-naming
- name: confusing-results
- name: bool-literal-in-expr
- name: atomic
depguard:
rules:
main:
list-mode: lax
deny:
- pkg: github.com/test-go/testify/assert
desc: Use github.com/stretchr/testify/assert instead
- pkg: github.com/test-go/testify/mock
desc: Use github.com/stretchr/testify/mock instead
- pkg: github.com/test-go/testify/require
desc: Use github.com/stretchr/testify/require instead
issues:
exclude-rules:
- path: test
text: "^G404:"
linters:
- gosec
2 changes: 1 addition & 1 deletion core/services/ocr3/plugins/ccip/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ test: ensure_go_1_21
go test -race -fullpath -shuffle on -count 10 ./...

lint: ensure_go_1_21
golangci-lint run
golangci-lint run -c .golangci.yml
3 changes: 2 additions & 1 deletion core/services/ocr3/plugins/ccip/commit/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)
Expand Down
5 changes: 3 additions & 2 deletions core/services/ocr3/plugins/ccip/commit/plugin_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"reflect"
"testing"

"github.com/smartcontractkit/ccipocr3/internal/libs/testhelpers"
"github.com/smartcontractkit/ccipocr3/internal/mocks"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/ccipocr3/internal/libs/testhelpers"
"github.com/smartcontractkit/ccipocr3/internal/mocks"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)
Expand Down
7 changes: 4 additions & 3 deletions core/services/ocr3/plugins/ccip/commit/plugin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"sort"

mapset "github.com/deckarep/golang-set/v2"
"github.com/smartcontractkit/ccipocr3/internal/libs/hashlib"
"github.com/smartcontractkit/ccipocr3/internal/libs/merklemulti"
"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"golang.org/x/sync/errgroup"

"github.com/smartcontractkit/ccipocr3/internal/libs/hashlib"
"github.com/smartcontractkit/ccipocr3/internal/libs/merklemulti"
"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"testing"

"github.com/smartcontractkit/ccipocr3/internal/libs/hashlib"
"github.com/smartcontractkit/ccipocr3/internal/libs/merklemulti/fixtures"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gonum.org/v1/gonum/stat/combin"

"github.com/smartcontractkit/ccipocr3/internal/libs/hashlib"
"github.com/smartcontractkit/ccipocr3/internal/libs/merklemulti/fixtures"
)

var (
Expand Down Expand Up @@ -138,14 +139,14 @@ func TestMerkleMultiProof(t *testing.T) {
ctx.HashInternal(ctx.HashInternal(ctx.HashInternal(a, b), ctx.HashInternal(c, d)), ctx.HashInternal(ctx.HashInternal(e, f), ctx.ZeroHash())),
}
// For every size tree from 0..len(leaves)
for len_ := 1; len_ <= len(leafHashes); len_++ {
tr, err := NewTree(ctx, leafHashes[:len_])
for length := 1; length <= len(leafHashes); length++ {
tr, err := NewTree(ctx, leafHashes[:length])
require.NoError(t, err)
expectedRoot := expectedRoots[len_-1]
expectedRoot := expectedRoots[length-1]
require.Equal(t, tr.Root(), expectedRoot)
// Prove every subset of its leaves
for k := 1; k <= len_; k++ {
gen := combin.NewCombinationGenerator(len_, k)
for k := 1; k <= length; k++ {
gen := combin.NewCombinationGenerator(length, k)
for gen.Next() {
leaveIndices := gen.Combination(nil)
proof, err := tr.Prove(leaveIndices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"fmt"
"math/rand"

"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/ccipocr3/internal/libs/slicelib"
)

var (
Expand Down Expand Up @@ -55,20 +56,20 @@ func (r *OCR3Runner[RI]) RunRound(ctx context.Context) (result RoundResult[RI],

q, err := leaderNode.Query(ctx, outcomeCtx)
if err != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err, ErrQuery)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err, ErrQuery)
}

attributedObservations := make([]types.AttributedObservation, len(r.nodes))
for i, n := range r.nodes {
obs, err2 := n.Observation(ctx, outcomeCtx, q)
if err2 != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err2, ErrObservation)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err2, ErrObservation)
}

attrObs := types.AttributedObservation{Observation: obs, Observer: r.nodeIDs[i]}
err = leaderNode.ValidateObservation(outcomeCtx, q, attrObs)
if err != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err, ErrValidateObservation)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err, ErrValidateObservation)
}

attributedObservations[i] = attrObs
Expand All @@ -78,7 +79,7 @@ func (r *OCR3Runner[RI]) RunRound(ctx context.Context) (result RoundResult[RI],
for i, n := range r.nodes {
outcome, err2 := n.Outcome(outcomeCtx, q, attributedObservations)
if err2 != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err2, ErrOutcome)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err2, ErrOutcome)
}
if len(outcome) == 0 {
return RoundResult[RI]{}, ErrEmptyOutcome
Expand All @@ -98,7 +99,7 @@ func (r *OCR3Runner[RI]) RunRound(ctx context.Context) (result RoundResult[RI],
for i, n := range r.nodes {
reportsWithInfo, err2 := n.Reports(seqNr, outcomes[0])
if err2 != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err2, ErrReports)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err2, ErrReports)
}

allReports[i] = reportsWithInfo
Expand All @@ -118,7 +119,7 @@ func (r *OCR3Runner[RI]) RunRound(ctx context.Context) (result RoundResult[RI],
for i, n := range r.nodes {
shouldAccept, err2 := n.ShouldAcceptAttestedReport(ctx, seqNr, report)
if err2 != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err2, ErrShouldAcceptAttestedReport)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err2, ErrShouldAcceptAttestedReport)
}

allShouldAccept[i] = shouldAccept
Expand All @@ -136,7 +137,7 @@ func (r *OCR3Runner[RI]) RunRound(ctx context.Context) (result RoundResult[RI],
for i, n := range r.nodes {
shouldTransmit, err2 := n.ShouldTransmitAcceptedReport(ctx, seqNr, report)
if err2 != nil {
return RoundResult[RI]{}, fmt.Errorf("%s: %w", err2, ErrShouldTransmitAcceptedReport)
return RoundResult[RI]{}, fmt.Errorf("%w: %w", err2, ErrShouldTransmitAcceptedReport)
}

allShouldTransmit[i] = shouldTransmit
Expand Down

0 comments on commit 9909698

Please sign in to comment.