Skip to content

Commit

Permalink
BeginTemporalRo/Rw defer rules (#13136)
Browse files Browse the repository at this point in the history
example run:
```
// BalanceAt returns the wei balance of a certain account in the blockchain.
func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract libcommon.Address, blockNumber *big.Int) (*uint256.Int, error) {
	b.mu.Lock()
	defer b.mu.Unlock()
	// tx, err := b.m.DB.BeginTemporalRo(context.Background())
	// if err != nil {
	// 	return nil, err
	// }
	// defer tx.Rollback()
	// stateDB := b.stateByBlockNumber(tx, blockNumber)

	tx2, err := b.m.DB.BeginTemporalRo(context.Background())
	if err != nil {
		return nil, err
	}

	stateDb := b.stateByBlockNumber(tx2, blockNumber)

	return stateDb.GetBalance(contract)
}
```

linter run:
```
>> make lint
	            ^
accounts/abi/bind/backends/simulated.go:224:2: ruleguard: Add "defer tx2.Rollback()" right after transaction creation error check.
			If you are in the loop - consider use "b.m.DB.View" or "b.m.DB.Update" or extract whole transaction to function.
```
  • Loading branch information
sudeepdino008 authored Dec 18, 2024
1 parent 6a9a7be commit 635193c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions erigon-lib/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func txDeferRollback(m dsl.Matcher) {
`$tx, $err = $db.Begin($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginRwNosync($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRwNosync($ctx); $chk; $rollback`,
).
Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)).
//At(m["rollback"]).
Expand Down
6 changes: 6 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func txDeferRollback(m dsl.Matcher) {
`$tx, $err = $db.Begin($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRo($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRw($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginTemporalRo($ctx); $chk; $rollback`,
`$tx, $err := $db.BeginRwNosync($ctx); $chk; $rollback`,
`$tx, $err = $db.BeginRwNosync($ctx); $chk; $rollback`,
).
Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)).
//At(m["rollback"]).
Expand Down

0 comments on commit 635193c

Please sign in to comment.