From 9a73c4a122cb8ab7df15e394faa9d98dca716d36 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 15 Oct 2024 16:28:28 +0800 Subject: [PATCH] Add early return in `ComputeTxEnv` for state sync transactions (#12315) `ComputeTxEnv` relied on `txIndex < len(block.Transactions())` which is violated by Polygon's state sync event transaction. We handle this case by adding an early return for this case, returning only ibs and blockContext which is used during state sync event tracing. Fixes #11701, #12007 --- turbo/transactions/tracing.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/turbo/transactions/tracing.go b/turbo/transactions/tracing.go index b532b118335..01ac27ac9cf 100644 --- a/turbo/transactions/tracing.go +++ b/turbo/transactions/tracing.go @@ -45,9 +45,6 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ // Create the parent state database statedb := state.New(reader) - if txIndex == 0 && len(block.Transactions()) == 0 { - return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, statedb, reader, nil - } getHeader := func(hash libcommon.Hash, n uint64) *types.Header { h, _ := headerReader.HeaderByNumber(ctx, dbtx, n) return h @@ -59,6 +56,11 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ // Recompute transactions up to the target index. signer := types.MakeSigner(cfg, block.NumberU64(), block.Time()) if historyV3 { + if txIndex == len(block.Transactions()) { + // tx is a state sync transaction + return nil, blockContext, evmtypes.TxContext{}, statedb, reader, nil + } + rules := cfg.Rules(blockContext.BlockNumber, blockContext.Time) txn := block.Transactions()[txIndex] statedb.SetTxContext(txn.Hash(), block.Hash(), txIndex) @@ -119,6 +121,12 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ return nil, blockContext, evmtypes.TxContext{}, statedb, reader, nil } } + + if txIndex == len(block.Transactions()) { + // tx is a state sync transaction + return nil, blockContext, evmtypes.TxContext{}, statedb, reader, nil + } + return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %x", txIndex, block.Hash()) }