Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter: to check tx.Rollback() by ruleguard #2383

Merged
merged 5 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ linters:
- structcheck
- unused
- varcheck
- gocritic

linters-settings:
gocritic:
enabled-checks:
- ruleguard
settings:
ruleguard:
rules: "rules.go"
govet:
disable:
- deepequalerrors
Expand Down
4 changes: 1 addition & 3 deletions cmd/integration/commands/refetence_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ func fToMdbx(ctx context.Context, to string) error {
if err1 != nil {
return err1
}
defer func() {
dstTx.Rollback()
}()
defer dstTx.Rollback()

commitEvery := time.NewTicker(5 * time.Second)
defer commitEvery.Stop()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/petar/GoLLRB v0.0.0-20190514000832-33fb24c13b99
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/tsdb v0.10.0
github.com/quasilyte/go-ruleguard/dsl v0.3.6
github.com/rs/cors v1.8.0
github.com/shirou/gopsutil/v3 v3.21.6
github.com/spf13/cobra v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,8 @@ github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150/go.mod h1:qhTCs0
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/quasilyte/go-ruleguard/dsl v0.3.6 h1:W2wnISJifyda0x/RXq15Qjrsu9iOhX2gy4+Ku+owylw=
github.com/quasilyte/go-ruleguard/dsl v0.3.6/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down Expand Up @@ -988,8 +990,6 @@ github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefld
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/torquem-ch/mdbx-go v0.15.0 h1:HUPA06n7tEAyLXv+rFuWvk01FKEm2MavLD9VVEQqMCs=
github.com/torquem-ch/mdbx-go v0.15.0/go.mod h1:T2fsoJDVppxfAPTLd1svUgH1kpPmeXdPESmroSHcL1E=
github.com/torquem-ch/mdbx-go v0.16.0 h1:x4dTXDvy1w9MxjlAX6J3/bDqIqu4XQDqTR/u2jMnTiA=
github.com/torquem-ch/mdbx-go v0.16.0/go.mod h1:T2fsoJDVppxfAPTLd1svUgH1kpPmeXdPESmroSHcL1E=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
Expand Down
62 changes: 62 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// +build gorules

package gorules

// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
import (
"github.com/quasilyte/go-ruleguard/dsl"
//quasilyterules "github.com/quasilyte/ruleguard-rules-test"
)

func init() {
//dsl.ImportRules("qrules", quasilyterules.Bundle)
}

func txDeferRollback(m dsl.Matcher) {
// Common pattern for long-living transactions:
// tx, err := db.Begin()
// if err != nil {
// return err
// }
// defer tx.Rollback()
//
// ... code which uses database in transaction
//
// err := tx.Commit()
// if err != nil {
// return err
// }

m.Match(
`$tx, $err := $db.BeginRw($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRw($ctx); $chk; $rollback`,
`$tx, $err := $db.Begin($ctx); $chk; $rollback`,
`$tx, $err = $db.Begin($ctx); $chk; $rollback`,
).
Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)).
//At(m["unlock"]).
Report(`add "defer $tx.Rollback()" right after transaction creation error check. Or consider use "$db.View" or "$db.Update" .`)

}

func mismatchingUnlock(m dsl.Matcher) {
// By default, an entire match position is used as a location.
// This can be changed by the At() method that binds the location
// to the provided named submatch.
//
// In the rules below text editor would get mismatching method
// name locations:
//
// defer mu.RUnlock()
// ^^^^^^^

m.Match(`$mu.Lock(); defer $mu.$unlock()`).
Where(m["unlock"].Text == "RUnlock").
At(m["unlock"]).
Report(`maybe $mu.Unlock() was intended?`)

m.Match(`$mu.RLock(); defer $mu.$unlock()`).
Where(m["unlock"].Text == "Unlock").
At(m["unlock"]).
Report(`maybe $mu.RUnlock() was intended?`)
}
8 changes: 5 additions & 3 deletions cmd/hack/binary-deps/main.go → tools.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// +build trick_go_mod_tidy
// +build tools

package tools

// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
//
// This module is just a hack for 'go mod tidy' command
//
// Problem is - 'go mod tidy' removes from go.mod file lines if you don't use them in source code
Expand All @@ -12,8 +16,6 @@

// build tag 'trick_go_mod_tidy' - is used to hide warnings of IDEA (because we can't import `main` packages in go)

package main

import (
_ "github.com/fjl/gencodec"
_ "github.com/kevinburke/go-bindata"
Expand Down
5 changes: 3 additions & 2 deletions turbo/snapshotsync/headers_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"errors"
"fmt"
"os"
"time"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
"github.com/ledgerwatch/erigon/log"
"os"
"time"
)

func CreateHeadersSnapshot(ctx context.Context, readTX ethdb.Tx, toBlock uint64, snapshotPath string) error {
Expand Down