Skip to content

Commit

Permalink
build: updated linting to use golangci-lint CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM authored and MonikaCat committed Jan 19, 2024
1 parent d12b2a7 commit b1cb8fd
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 112 deletions.
22 changes: 13 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 6
steps:
- name: Checkout
- uses: actions/checkout@v3
- name: Compute Diff
- uses: technote-space/get-diff-action@v4
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Compute diff 📜
uses: technote-space/[email protected]
with:
SUFFIX_FILTER: |
.go
.mod
.sum
- name: Run lint
uses: golangci/golangci-lint-action@v3
- name: Setup Go 🧰
if: "env.GIT_DIFF != ''"
uses: actions/setup-go@v3
with:
version: v1.28
args: --timeout 10m
github-token: ${{ secrets.GITHUB_TOKEN }}
go-version: 1.18

- name: Run lint ✅
if: "env.GIT_DIFF != ''"
run: make lint
78 changes: 64 additions & 14 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
run:
tests: false
# # timeout for analysis, e.g. 30s, 5m, default is 1m
# timeout: 5m

linters:
disable-all: true
enable:
- errcheck
- bodyclose
- deadcode
- depguard
- dogsled
- gocritic
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- maligned
- misspell
- nakedret
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- unparam
- misspell
- govet
- gocyclo
linters-settings:
gocyclo:
min-complexity: 11
errcheck:
ignore: fmt:.*,io/ioutil:^Read.*,github.com/spf13/viper:BindPFlag
golint:
min-confidence: 1.1
- nolintlint

issues:
exclude:
- composite
run:
tests: false
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec
- text: "comment on exported var"
linters:
- golint
- text: "don't use an underscore in package name"
linters:
- golint
- text: "ST1003:"
linters:
- stylecheck
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
# https://github.com/dominikh/go-tools/issues/389
- text: "ST1016:"
linters:
- stylecheck

- text: "singleCaseSwitch: should rewrite switch statement to if statement"
linters:
- gocritic

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

linters-settings:
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
43 changes: 33 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ install: go.sum
@go install -mod=readonly $(BUILD_FLAGS) ./cmd/juno
.PHONY: install

###############################################################################
### Tools & Dependencies ###
###############################################################################

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download

go.sum: go.mod
@echo "--> Ensure dependencies have not been modified"
@go mod verify
@go mod tidy

clean:
rm -rf $(BUILDDIR)/

.PHONY: go-mod-cache go.sum clean

###############################################################################
### Tests & Simulation ###
###############################################################################
Expand All @@ -65,20 +83,25 @@ test-unit: start-docker-test
@go test -mod=readonly -v -coverprofile coverage.txt ./...
.PHONY: test-unit

###############################################################################
### Linting ###
###############################################################################
golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint

lint:
golangci-lint run --out-format=tab
.PHONY: lint
@echo "--> Running linter"
@go run $(golangci_lint_cmd) run --timeout=10m

lint-fix:
golangci-lint run --fix --out-format=tab --issues-exit-code=0
.PHONY: lint-fix
@echo "--> Running linter"
@go run $(golangci_lint_cmd) run --fix --out-format=tab --issues-exit-code=0

.PHONY: lint lint-fix

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' | xargs goimports -w -local github.com/forbole/juno
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs gofmt -w -s
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs misspell -w
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -path "./venv" | xargs goimports -w -local github.com/forbole/juno
.PHONY: format

clean:
rm -f tools-stamp ./build/**
.PHONY: clean
.PHONY: lint lint-fix format
4 changes: 2 additions & 2 deletions cmd/migrate/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ type LoggingConfig struct {
}

type ParsingConfig struct {
GenesisFilePath string `toml:"genesis_file_path"`
Workers int64 `toml:"workers"`
StartHeight int64 `toml:"start_height"`
ParseNewBlocks bool `toml:"listen_new_blocks"`
ParseOldBlocks bool `toml:"parse_old_blocks"`
GenesisFilePath string `toml:"genesis_file_path"`
ParseGenesis bool `toml:"parse_genesis"`
StartHeight int64 `toml:"start_height"`
FastSync bool `toml:"fast_sync"`
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/v2/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func RunMigration(_ *parsecmdtypes.Config) error {
}

v2File := config.GetConfigFilePath()
return ioutil.WriteFile(v2File, bz, 0666)
return ioutil.WriteFile(v2File, bz, 0600)
}

func migrateConfig() (Config, error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/migrate/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type Config struct {
}

type ParserConfig struct {
GenesisFilePath string `yaml:"genesis_file_path,omitempty"`
Workers int64 `yaml:"workers"`
StartHeight int64 `yaml:"start_height"`
ParseNewBlocks bool `yaml:"listen_new_blocks"`
ParseOldBlocks bool `yaml:"parse_old_blocks"`
GenesisFilePath string `yaml:"genesis_file_path,omitempty"`
ParseGenesis bool `yaml:"parse_genesis"`
StartHeight int64 `yaml:"start_height"`
FastSync bool `yaml:"fast_sync,omitempty"`

// Following there are the new fields that have been added into v3. We use pointers and the "omitempty" clause
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func RunMigration(parseConfig *parsecmdtypes.Config) error {
return fmt.Errorf("error while serializing config: %s", err)
}

err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0666)
err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0600)
if err != nil {
return fmt.Errorf("error while writing v3 config: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/start/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func NewStartCmd(cmdCfg *parsecmdtypes.Config) *cobra.Command {
}
}

return StartParsing(context)
return startParsing(context)
},
}
}

// StartParsing represents the function that should be called when the parse command is executed
func StartParsing(ctx *parser.Context) error {
// startParsing represents the function that should be called when the parse command is executed
func startParsing(ctx *parser.Context) error {
// Get the config
cfg := config.Cfg.Parser
logging.StartHeight.Add(float64(cfg.StartHeight))
Expand All @@ -76,7 +76,7 @@ func StartParsing(ctx *parser.Context) error {
exportQueue := types.NewQueue(25)

// Create workers
workers := make([]parser.Worker, cfg.Workers, cfg.Workers)
workers := make([]parser.Worker, cfg.Workers)
for i := range workers {
workers[i] = parser.NewWorker(ctx, exportQueue, i)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func mustGetLatestHeight(ctx *parser.Context) int64 {
// trapSignal will listen for any OS signal and invoke Done on the main
// WaitGroup allowing the main process to gracefully exit.
func trapSignal(ctx *parser.Context) {
var sigCh = make(chan os.Signal)
var sigCh = make(chan os.Signal, 1)

signal.Notify(sigCh, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGINT)
Expand Down
20 changes: 11 additions & 9 deletions database/legacy/v3/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (db *Migrator) getOldTransactions(batchSize int64, offset int64) ([]types.T
stmt := fmt.Sprintf("SELECT * FROM transaction_old ORDER BY height LIMIT %v OFFSET %v", batchSize, offset)

var rows []types.TransactionRow
err := db.Sql.Select(&rows, stmt)
err := db.SQL.Select(&rows, stmt)
if err != nil {
return nil, err
}
Expand All @@ -75,7 +75,7 @@ func (db *Migrator) createPartitionTable(table string, partitionID int64) error
partitionTable := fmt.Sprintf("%s_%v", table, partitionID)

stmt := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s PARTITION OF %s FOR VALUES IN (%v)`, partitionTable, table, partitionID)
_, err := db.Sql.Exec(stmt)
_, err := db.SQL.Exec(stmt)
return err
}

Expand Down Expand Up @@ -106,7 +106,7 @@ func (db *Migrator) migrateTransactions(rows []types.TransactionRow, partitionSi
stmt = stmt[:len(stmt)-1] // remove trailing ,
stmt += " ON CONFLICT DO NOTHING"

_, err := db.Sql.Exec(stmt, params...)
_, err := db.SQL.Exec(stmt, params...)
if err != nil {
return fmt.Errorf("error while inserting transaction: %s", err)
}
Expand Down Expand Up @@ -147,13 +147,15 @@ func (db *Migrator) insertTransactionMessages(tx types.TransactionRow, partition
return fmt.Errorf("error while unmarshaling messages: %s", err)
}

for i, m := range msgs {
for i, msg := range msgs {
msg := msg

// Append params
msgType := m["@type"].(string)[1:] // remove head "/"
involvedAddresses := types.MessageParser(m)
delete(m, "@type")
msgType := msg["@type"].(string)[1:] // remove head "/"
involvedAddresses := types.MessageParser(msg)
delete(msg, "@type")

mBz, err := json.Marshal(&m)
mBz, err := json.Marshal(&msg)
if err != nil {
return fmt.Errorf("error while marshaling msg value to json: %s", err)
}
Expand All @@ -168,6 +170,6 @@ func (db *Migrator) insertTransactionMessages(tx types.TransactionRow, partition
stmt = stmt[:len(stmt)-1] // remove trailing ","
stmt += " ON CONFLICT DO NOTHING"

_, err = db.Sql.Exec(stmt, params...)
_, err = db.SQL.Exec(stmt, params...)
return err
}
4 changes: 2 additions & 2 deletions database/legacy/v3/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var _ database.Migrator = &Migrator{}

// Migrator represents the database migrator that should be used to migrate from v2 of the database to v3
type Migrator struct {
Sql *sqlx.DB
SQL *sqlx.DB
}

func NewMigrator(db *postgresql.Database) *Migrator {
return &Migrator{
Sql: sqlx.NewDb(db.Sql, "postgres"),
SQL: sqlx.NewDb(db.SQL, "postgres"),
}
}
12 changes: 6 additions & 6 deletions database/legacy/v3/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ALTER INDEX IF EXISTS transaction_hash_index RENAME TO transaction_old_hash_inde
ALTER INDEX IF EXISTS transaction_height_index RENAME TO transaction_old_height_index;
ALTER TABLE IF EXISTS transaction_old RENAME CONSTRAINT transaction_height_fkey TO transaction_old_height_fkey;`

_, err := db.Sql.Exec(stmt)
_, err := db.SQL.Exec(stmt)
return err
}

Expand All @@ -54,7 +54,7 @@ ALTER INDEX IF EXISTS message_transaction_hash_index RENAME TO message_old_trans
ALTER INDEX IF EXISTS message_type_index RENAME TO message_old_type_index;
ALTER TABLE IF EXISTS message_old RENAME CONSTRAINT message_transaction_hash_fkey TO message_old_transaction_hash_fkey;`

_, err := db.Sql.Exec(stmt)
_, err := db.SQL.Exec(stmt)
return err
}

Expand Down Expand Up @@ -93,7 +93,7 @@ GRANT ALL PRIVILEGES ON transaction TO "%s";
`,
config.Cfg.Database.User)

_, err := db.Sql.Exec(stmt)
_, err := db.SQL.Exec(stmt)
return err
}

Expand All @@ -120,7 +120,7 @@ CREATE INDEX message_involved_accounts_index ON message USING GIN(involved_accou
GRANT ALL PRIVILEGES ON message TO "%s";
`, config.Cfg.Database.User)

_, err := db.Sql.Exec(stmt)
_, err := db.SQL.Exec(stmt)
return err
}

Expand All @@ -130,7 +130,7 @@ GRANT ALL PRIVILEGES ON message TO "%s";
// the message table itself (as we've added this field with the new schema).
func (db *Migrator) migrateMessagesByAddressFunction() error {
// Delete the old function
_, err := db.Sql.Exec("DROP FUNCTION IF EXISTS messages_by_address(text[],text[],bigint,bigint);")
_, err := db.SQL.Exec("DROP FUNCTION IF EXISTS messages_by_address(text[],text[],bigint,bigint);")
if err != nil {
return err
}
Expand All @@ -151,6 +151,6 @@ ORDER BY height DESC LIMIT "limit" OFFSET "offset"
$$ LANGUAGE sql STABLE;
`

_, err = db.Sql.Exec(stmt)
_, err = db.SQL.Exec(stmt)
return err
}
4 changes: 3 additions & 1 deletion database/migrate/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var DefaultAccountParser = []string{
}

func MessageParser(msg map[string]interface{}) (addresses string) {
accountParser := append(DefaultAccountParser, CustomAccountParser...)
var accountParser []string
accountParser = append(accountParser, DefaultAccountParser...)
accountParser = append(accountParser, CustomAccountParser...)

addresses += "{"
for _, role := range accountParser {
Expand Down
Loading

0 comments on commit b1cb8fd

Please sign in to comment.