Skip to content

Commit

Permalink
Gas used assert (#12596)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Nov 4, 2024
1 parent 143e3ec commit 379b21f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
3 changes: 3 additions & 0 deletions erigon-lib/common/dbg/dbg_assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dbg

var AssertEnabled = EnvBool("ERIGON_ASSERT", true)
14 changes: 10 additions & 4 deletions polygon/tracer/trace_bor_state_sync_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func TraceBorStateSyncTxnDebugAPI(
stream *jsoniter.Stream,
callTimeout time.Duration,
msgs []*types.Message,
) error {
) (usedGas uint64, err error) {
txCtx := initStateSyncTxContext(blockNum, blockHash)
tracer, streaming, cancel, err := transactions.AssembleTracer(ctx, traceConfig, txCtx.TxHash, stream, callTimeout)
if err != nil {
stream.WriteNil()
return err
return usedGas, err
}

defer cancel()
Expand All @@ -62,10 +62,16 @@ func TraceBorStateSyncTxnDebugAPI(
rules := chainConfig.Rules(blockNum, blockTime)
stateWriter := state.NewNoopWriter()
execCb := func(evm *vm.EVM, refunds bool) (*evmtypes.ExecutionResult, error) {
return traceBorStateSyncTxn(ctx, ibs, stateWriter, msgs, evm, rules, txCtx, refunds)
res, err := traceBorStateSyncTxn(ctx, ibs, stateWriter, msgs, evm, rules, txCtx, refunds)
if err != nil {
return res, err
}
usedGas = res.UsedGas
return res, nil
}

return transactions.ExecuteTraceTx(blockCtx, txCtx, ibs, traceConfig, chainConfig, stream, tracer, streaming, execCb)
err = transactions.ExecuteTraceTx(blockCtx, txCtx, ibs, traceConfig, chainConfig, stream, tracer, streaming, execCb)
return usedGas, err
}

func TraceBorStateSyncTxnTraceAPI(
Expand Down
28 changes: 22 additions & 6 deletions turbo/jsonrpc/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"time"

"github.com/erigontech/erigon-lib/common/dbg"
"github.com/holiman/uint256"
jsoniter "github.com/json-iterator/go"

Expand Down Expand Up @@ -113,6 +114,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
stream.WriteArrayStart()

txns := block.Transactions()

var borStateSyncTxn types.Transaction
if *config.BorTraceEnabled {
borStateSyncTxHash := bortypes.ComputeBorTxHash(block.NumberU64(), block.Hash())
Expand All @@ -133,6 +135,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}
}

var usedGas uint64
for idx, txn := range txns {
isBorStateSyncTxn := borStateSyncTxn == txn
var txnHash common.Hash
Expand Down Expand Up @@ -179,7 +182,8 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
return err
}

err = polygontracer.TraceBorStateSyncTxnDebugAPI(
var _usedGas uint64
_usedGas, err = polygontracer.TraceBorStateSyncTxnDebugAPI(
ctx,
chainConfig,
config,
Expand All @@ -192,8 +196,11 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
api.evmCallTimeout,
stateSyncEvents,
)
usedGas += _usedGas
} else {
err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
var _usedGas uint64
_usedGas, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
usedGas += _usedGas
}
if err == nil {
err = ibs.FinalizeTx(rules, state.NewNoopWriter())
Expand All @@ -215,6 +222,12 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}
}

if dbg.AssertEnabled {
if block.GasUsed() != usedGas {
panic(fmt.Errorf("assert: block.GasUsed() %d != usedGas %d. blockNum=%d", block.GasUsed(), usedGas, blockNumber))
}
}

stream.WriteArrayEnd()
if err := stream.Flush(); err != nil {
return err
Expand Down Expand Up @@ -321,7 +334,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo
return err
}

return polygontracer.TraceBorStateSyncTxnDebugAPI(
_, err = polygontracer.TraceBorStateSyncTxnDebugAPI(
ctx,
chainConfig,
config,
Expand All @@ -334,6 +347,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo
api.evmCallTimeout,
stateSyncEvents,
)
return err
}

msg, txCtx, err := transactions.ComputeTxContext(ibs, engine, rules, signer, block, chainConfig, txnIndex)
Expand All @@ -343,7 +357,8 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo
}

// Trace the transaction and return
return transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
_, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
return err
}

// TraceCall implements debug_traceCall. Returns Geth style call traces.
Expand Down Expand Up @@ -411,7 +426,8 @@ func (api *PrivateDebugAPIImpl) TraceCall(ctx context.Context, args ethapi.CallA
blockCtx := transactions.NewEVMBlockContext(engine, header, blockNrOrHash.RequireCanonical, dbtx, api._blockReader, chainConfig)
txCtx := core.NewEVMTxContext(msg)
// Trace the transaction and return
return transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
_, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout)
return err
}

func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bundle, simulateContext StateContext, config *tracersConfig.TraceConfig, stream *jsoniter.Stream) error {
Expand Down Expand Up @@ -574,7 +590,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun
}
txCtx = core.NewEVMTxContext(msg)
ibs.SetTxContext(txnIndex)
err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout)
_, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout)
if err != nil {
stream.WriteArrayEnd()
stream.WriteArrayEnd()
Expand Down
14 changes: 10 additions & 4 deletions turbo/transactions/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,27 @@ func TraceTx(
chainConfig *chain.Config,
stream *jsoniter.Stream,
callTimeout time.Duration,
) error {
) (usedGas uint64, err error) {
tracer, streaming, cancel, err := AssembleTracer(ctx, config, txCtx.TxHash, stream, callTimeout)
if err != nil {
stream.WriteNil()
return err
return 0, err
}

defer cancel()

execCb := func(evm *vm.EVM, refunds bool) (*evmtypes.ExecutionResult, error) {
gp := new(core.GasPool).AddGas(message.Gas()).AddBlobGas(message.BlobGas())
return core.ApplyMessage(evm, message, gp, refunds, false /* gasBailout */)
res, err := core.ApplyMessage(evm, message, gp, refunds, false /* gasBailout */)
if err != nil {
return res, err
}
usedGas = res.UsedGas
return res, nil
}

return ExecuteTraceTx(blockCtx, txCtx, ibs, config, chainConfig, stream, tracer, streaming, execCb)
err = ExecuteTraceTx(blockCtx, txCtx, ibs, config, chainConfig, stream, tracer, streaming, execCb)
return usedGas, err
}

func AssembleTracer(
Expand Down

0 comments on commit 379b21f

Please sign in to comment.