Skip to content

Commit 1e247a3

Browse files
authored
add ci (cosmos#1502)
1 parent e44e7b8 commit 1e247a3

File tree

13 files changed

+292
-44
lines changed

13 files changed

+292
-44
lines changed

.github.com/workflows/lint.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
- release/**
7+
pull_request:
8+
merge_group:
9+
permissions:
10+
contents: read
11+
jobs:
12+
golangci:
13+
name: golangci-lint
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: DeterminateSystems/nix-installer-action@main
18+
- uses: DeterminateSystems/magic-nix-cache-action@main
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version: "1.21"
22+
check-latest: true
23+
- name: run linting (long)
24+
if: env.GIT_DIFF
25+
id: lint_long
26+
run: |
27+
make lint

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go.work
2+
go.work.sum

.golangci.yml

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
run:
2+
tests: true
3+
timeout: 15m
4+
allow-parallel-runners: true
5+
skip-files:
6+
- ".*\\.pb\\.go$"
7+
- ".*\\.pb\\.gw\\.go$"
8+
- ".*\\.pulsar\\.go$"
9+
10+
linters:
11+
disable-all: true
12+
enable:
13+
- dogsled
14+
- errcheck
15+
- exportloopref
16+
- gci
17+
- goconst
18+
- gocritic
19+
- gofumpt
20+
- gosec
21+
- gosimple
22+
- govet
23+
- ineffassign
24+
- misspell
25+
- nakedret
26+
- nolintlint
27+
- revive
28+
- staticcheck
29+
- stylecheck
30+
- thelper
31+
- typecheck
32+
- unconvert
33+
- unused
34+
35+
issues:
36+
exclude-rules:
37+
- text: "Use of weak random number generator"
38+
linters:
39+
- gosec
40+
- text: "ST1003:"
41+
linters:
42+
- stylecheck
43+
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
44+
# https://github.com/dominikh/go-tools/issues/389
45+
- text: "ST1016:"
46+
linters:
47+
- stylecheck
48+
- text: "leading space"
49+
linters:
50+
- nolintlint
51+
max-issues-per-linter: 10000
52+
max-same-issues: 10000
53+
54+
linters-settings:
55+
gci:
56+
custom-order: true
57+
sections:
58+
- standard # Standard section: captures all standard packages.
59+
- default # Default section: contains all imports that could not be matched to another section type.
60+
- prefix(cosmossdk.io github.com/cosmos/cosmos-sdk)
61+
- prefix(github.com/cosmos/sdk-tutorials)
62+
revive:
63+
rules:
64+
- name: redefines-builtin-id
65+
disabled: true
66+
67+
gosec:
68+
# To select a subset of rules to run.
69+
# Available rules: https://github.com/securego/gosec#available-rules
70+
# Default: [] - means include all rules
71+
includes:
72+
# - G101 # Look for hard coded credentials
73+
- G102 # Bind to all interfaces
74+
- G103 # Audit the use of unsafe block
75+
- G104 # Audit errors not checked
76+
- G106 # Audit the use of ssh.InsecureIgnoreHostKey
77+
- G107 # Url provided to HTTP request as taint input
78+
- G108 # Profiling endpoint automatically exposed on /debug/pprof
79+
- G109 # Potential Integer overflow made by strconv.Atoi result conversion to int16/32
80+
- G110 # Potential DoS vulnerability via decompression bomb
81+
- G111 # Potential directory traversal
82+
- G112 # Potential slowloris attack
83+
- G113 # Usage of Rat.SetString in math/big with an overflow (CVE-2022-23772)
84+
- G114 # Use of net/http serve function that has no support for setting timeouts
85+
- G201 # SQL query construction using format string
86+
- G202 # SQL query construction using string concatenation
87+
- G203 # Use of unescaped data in HTML templates
88+
- G204 # Audit use of command execution
89+
- G301 # Poor file permissions used when creating a directory
90+
- G302 # Poor file permissions used with chmod
91+
- G303 # Creating tempfile using a predictable path
92+
- G304 # File path provided as taint input
93+
- G305 # File traversal when extracting zip/tar archive
94+
- G306 # Poor file permissions used when writing to a new file
95+
- G307 # Deferring a method which returns an error
96+
- G401 # Detect the usage of DES, RC4, MD5 or SHA1
97+
- G402 # Look for bad TLS connection settings
98+
- G403 # Ensure minimum RSA key length of 2048 bits
99+
- G404 # Insecure random number source (rand)
100+
- G501 # Import blocklist: crypto/md5
101+
- G502 # Import blocklist: crypto/des
102+
- G503 # Import blocklist: crypto/rc4
103+
- G504 # Import blocklist: net/http/cgi
104+
- G505 # Import blocklist: crypto/sha1
105+
- G601 # Implicit memory aliasing of items from a range statement
106+
misspell:
107+
locale: US
108+
gofumpt:
109+
extra-rules: true
110+
dogsled:
111+
max-blank-identifiers: 6
112+
maligned:
113+
suggest-new: true
114+
nolintlint:
115+
allow-unused: false
116+
require-explanation: true
117+
require-specific: false
118+
gosimple:
119+
checks: ["all"]
120+
gocritic:
121+
disabled-checks:
122+
- regexpMust
123+
- appendAssign
124+
- ifElseChain

Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###############################################################################
2+
### Linting ###
3+
###############################################################################
4+
5+
golangci_version=v1.55.0
6+
7+
lint-install:
8+
@echo "--> Installing golangci-lint $(golangci_version)"
9+
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
10+
11+
lint:
12+
@echo "--> Running linter"
13+
$(MAKE) lint-install
14+
@./scripts/go-lint-all.bash --timeout=15m
15+
16+
lint-fix:
17+
@echo "--> Running linter"
18+
$(MAKE) lint-install
19+
@./scripts/go-lint-all.bash --fix
20+
21+
.PHONY: lint lint-fix

go.work.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
go 1.21
2+
3+
toolchain go1.21
4+
5+
use (
6+
./tutorials/base
7+
)

scripts/go-lint-all.bash

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -o pipefail
4+
5+
REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )"
6+
export REPO_ROOT
7+
8+
LINT_TAGS="e2e,ledger,test_ledger_mock"
9+
if [[ ! -z "${NIX:-}" ]]; then
10+
LINT_TAGS+=",rocksdb"
11+
fi
12+
export LINT_TAGS
13+
14+
lint_module() {
15+
local root="$1"
16+
shift
17+
if [ -f $root ]; then
18+
cd "$(dirname "$root")"
19+
else
20+
cd "$REPO_ROOT/$root"
21+
fi
22+
echo "linting $(grep "^module" go.mod) [$(date -Iseconds -u)]"
23+
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --build-tags=${LINT_TAGS}
24+
25+
# always lint simapp with app_v1 build tag, otherwise it never gets linted
26+
if [[ "$(grep "^module" go.mod)" == "module cosmossdk.io/simapp" ]]; then
27+
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --build-tags=app_v1
28+
fi
29+
}
30+
export -f lint_module
31+
32+
# if LINT_DIFF env is set, only lint the files in the current commit otherwise lint all files
33+
if [[ -z "${LINT_DIFF:-}" ]]; then
34+
find "${REPO_ROOT}" -type f -name go.mod -print0 | xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@"
35+
else
36+
if [[ -z $GIT_DIFF ]]; then
37+
GIT_DIFF=$(git diff --name-only) || true
38+
fi
39+
40+
if [[ -z "$GIT_DIFF" ]]; then
41+
echo "no files to lint"
42+
exit 0
43+
fi
44+
45+
GIT_DIFF=$(echo $GIT_DIFF | tr -d "'" | tr ' ' '\n' | grep '\.go$' | grep -v '\.pb\.go$' | grep -Eo '^[^/]+\/[^/]+' | uniq)
46+
47+
lint_sdk=false
48+
for dir in ${GIT_DIFF[@]}; do
49+
if [[ ! -f "$REPO_ROOT/$dir/go.mod" ]]; then
50+
lint_sdk=true
51+
else
52+
lint_module $dir "$@"
53+
fi
54+
done
55+
56+
if [[ $lint_sdk ]]; then
57+
cd "$REPO_ROOT"
58+
echo "linting github.com/cosmos/cosmos-sdk [$(date -Iseconds -u)]"
59+
golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" --build-tags=${LINT_TAGS}
60+
fi
61+
fi

tutorials/base/app/app.go

+9-12
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"os"
77
"path/filepath"
88

9-
dbm "github.com/cosmos/cosmos-db"
10-
9+
_ "cosmossdk.io/api/cosmos/tx/config/v1" // import for side-effects
1110
"cosmossdk.io/core/appconfig"
1211
"cosmossdk.io/depinject"
1312
"cosmossdk.io/log"
1413
storetypes "cosmossdk.io/store/types"
15-
14+
dbm "github.com/cosmos/cosmos-db"
1615
"github.com/cosmos/cosmos-sdk/baseapp"
1716
"github.com/cosmos/cosmos-sdk/client"
1817
"github.com/cosmos/cosmos-sdk/codec"
@@ -23,22 +22,20 @@ import (
2322
"github.com/cosmos/cosmos-sdk/server/config"
2423
servertypes "github.com/cosmos/cosmos-sdk/server/types"
2524
"github.com/cosmos/cosmos-sdk/types/module"
25+
_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side-effects
2626
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
27+
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import for side-effects
28+
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side-effects
2729
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
30+
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side-effects
2831
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
32+
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
2933
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
3034
"github.com/cosmos/cosmos-sdk/x/genutil"
3135
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
36+
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
37+
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side-effects
3238
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
33-
34-
_ "cosmossdk.io/api/cosmos/tx/config/v1" // import for side-effects
35-
_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side-effects
36-
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import for side-effects
37-
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side-effects
38-
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side-effects
39-
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
40-
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
41-
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side-effects
4239
)
4340

4441
// DefaultNodeHome default home directories for the application daemon

tutorials/base/app/export.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (app *TutorialApp) ExportAppStateAndValidators(
5151
}
5252

5353
// prepare for fresh start at zero height
54-
// NOTE zero height genesis is a temporary feature, which will be deprecated in favour of export at a block height
54+
// NOTE zero height genesis is a temporary feature, which will be deprecated in favor of export at a block height
5555
func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
5656
applyAllowedAddrs := false
5757

@@ -73,15 +73,17 @@ func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAdd
7373
/* Handle fee distribution state. */
7474

7575
// withdraw all validator commission
76-
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
76+
if err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
7777
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
7878
if err != nil {
7979
panic(err)
8080
}
8181

8282
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, valBz)
8383
return false
84-
})
84+
}); err != nil {
85+
panic(err)
86+
}
8587

8688
// withdraw all delegator rewards
8789
dels, err := app.StakingKeeper.GetAllDelegations(ctx)
@@ -114,7 +116,7 @@ func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAdd
114116
ctx = ctx.WithBlockHeight(0)
115117

116118
// reinitialize all validators
117-
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
119+
if err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
118120
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
119121
if err != nil {
120122
panic(err)
@@ -140,7 +142,9 @@ func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAdd
140142
panic(err)
141143
}
142144
return false
143-
})
145+
}); err != nil {
146+
panic(err)
147+
}
144148

145149
// reinitialize all delegations
146150
for _, del := range dels {
@@ -171,22 +175,30 @@ func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAdd
171175
/* Handle staking state. */
172176

173177
// iterate through redelegations, reset creation height
174-
app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
178+
if err := app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
175179
for i := range red.Entries {
176180
red.Entries[i].CreationHeight = 0
177181
}
178-
app.StakingKeeper.SetRedelegation(ctx, red)
182+
if err := app.StakingKeeper.SetRedelegation(ctx, red); err != nil {
183+
panic(err)
184+
}
179185
return false
180-
})
186+
}); err != nil {
187+
panic(err)
188+
}
181189

182190
// iterate through unbonding delegations, reset creation height
183-
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
191+
if err := app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
184192
for i := range ubd.Entries {
185193
ubd.Entries[i].CreationHeight = 0
186194
}
187-
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
195+
if err := app.StakingKeeper.SetUnbondingDelegation(ctx, ubd); err != nil {
196+
panic(err)
197+
}
188198
return false
189-
})
199+
}); err != nil {
200+
panic(err)
201+
}
190202

191203
// Iterate through validators by power descending, reset bond heights, and
192204
// update bond intra-tx counters.
@@ -208,7 +220,9 @@ func (app *TutorialApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAdd
208220
validator.Jailed = true
209221
}
210222

211-
app.StakingKeeper.SetValidator(ctx, validator)
223+
if err := app.StakingKeeper.SetValidator(ctx, validator); err != nil {
224+
panic(err)
225+
}
212226
counter++
213227
}
214228

tutorials/base/app/params/config.go

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package params
33
import (
44
"cosmossdk.io/errors"
55
"cosmossdk.io/math"
6-
76
sdk "github.com/cosmos/cosmos-sdk/types"
87
"github.com/cosmos/cosmos-sdk/types/address"
98
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

0 commit comments

Comments
 (0)