From a0682088ab5db14d332c4e07ac8f72946d62fd7c Mon Sep 17 00:00:00 2001 From: pwd123 <46750216+dlscjf151@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:40:11 +0900 Subject: [PATCH] Add readonly transaction rollback check rule (#8682) --- accounts/abi/bind/backends/simulated.go | 2 +- cmd/rpctest/rpctest/account_range_verify.go | 2 ++ core/state/temporal/kv_temporal.go | 2 +- erigon-lib/rules.go | 2 ++ rules.go | 2 ++ turbo/jsonrpc/erigon_cache_check.go | 1 + 6 files changed, 9 insertions(+), 2 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index ff2f1393b..e1b8a6390 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -177,7 +177,7 @@ func (b *SimulatedBackend) emptyPendingBlock() { if b.pendingReaderTx != nil { b.pendingReaderTx.Rollback() } - tx, err := b.m.DB.BeginRo(context.Background()) + tx, err := b.m.DB.BeginRo(context.Background()) //nolint:gocritic if err != nil { panic(err) } diff --git a/cmd/rpctest/rpctest/account_range_verify.go b/cmd/rpctest/rpctest/account_range_verify.go index dcb89c619..ed1bef30e 100644 --- a/cmd/rpctest/rpctest/account_range_verify.go +++ b/cmd/rpctest/rpctest/account_range_verify.go @@ -122,11 +122,13 @@ func CompareAccountRange(logger log.Logger, erigonURL, gethURL, tmpDataDir, geth log.Error(err.Error()) return } + defer tgTx.Rollback() gethTx, err := gethKV.BeginRo(context.Background()) if err != nil { log.Error(err.Error()) return } + defer gethTx.Rollback() tgCursor, err := tgTx.Cursor(kv.E2AccountsHistory) if err != nil { log.Error(err.Error()) diff --git a/core/state/temporal/kv_temporal.go b/core/state/temporal/kv_temporal.go index 3ebd94952..8be4940df 100644 --- a/core/state/temporal/kv_temporal.go +++ b/core/state/temporal/kv_temporal.go @@ -105,7 +105,7 @@ func (db *DB) Agg() *state.AggregatorV3 { return db.agg } func (db *DB) InternalDB() kv.RwDB { return db.RwDB } func (db *DB) BeginTemporalRo(ctx context.Context) (kv.TemporalTx, error) { - kvTx, err := db.RwDB.BeginRo(ctx) + kvTx, err := db.RwDB.BeginRo(ctx) //nolint:gocritic if err != nil { return nil, err } diff --git a/erigon-lib/rules.go b/erigon-lib/rules.go index 80485ab1e..dde688bbc 100644 --- a/erigon-lib/rules.go +++ b/erigon-lib/rules.go @@ -34,6 +34,8 @@ func txDeferRollback(m dsl.Matcher) { `$tx, $err = $db.BeginRw($ctx); $chk; $rollback`, `$tx, $err := $db.Begin($ctx); $chk; $rollback`, `$tx, $err = $db.Begin($ctx); $chk; $rollback`, + `$tx, $err := $db.BeginRo($ctx); $chk; $rollback`, + `$tx, $err = $db.BeginRo($ctx); $chk; $rollback`, ). Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)). //At(m["rollback"]). diff --git a/rules.go b/rules.go index f947bc0eb..889b03bf6 100644 --- a/rules.go +++ b/rules.go @@ -33,6 +33,8 @@ func txDeferRollback(m dsl.Matcher) { `$tx, $err = $db.BeginRw($ctx); $chk; $rollback`, `$tx, $err := $db.Begin($ctx); $chk; $rollback`, `$tx, $err = $db.Begin($ctx); $chk; $rollback`, + `$tx, $err := $db.BeginRo($ctx); $chk; $rollback`, + `$tx, $err = $db.BeginRo($ctx); $chk; $rollback`, ). Where(!m["rollback"].Text.Matches(`defer .*\.Rollback()`)). //At(m["rollback"]). diff --git a/turbo/jsonrpc/erigon_cache_check.go b/turbo/jsonrpc/erigon_cache_check.go index 903ca3c94..a487af36c 100644 --- a/turbo/jsonrpc/erigon_cache_check.go +++ b/turbo/jsonrpc/erigon_cache_check.go @@ -14,6 +14,7 @@ func (api *ErigonImpl) CacheCheck() (*kvcache.CacheValidationResult, error) { if err != nil { return nil, err } + defer tx.Rollback() result, err := cache.ValidateCurrentRoot(ctx, tx) if err != nil {