Skip to content

Commit

Permalink
rpcdaemon: Only allow large txIndex for state sync transactions (#12342)
Browse files Browse the repository at this point in the history
Refinement from #12315 as this condition should only be allowed under
certain cases.
  • Loading branch information
shohamc1 authored Oct 18, 2024
1 parent 0d1e381 commit add87c8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/devnet/services/polygon/proofgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (rg *requestGenerator) GetTransactionReceipt(ctx context.Context, hash libc
}
defer tx.Rollback()

_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, reader, tx, 0, false)
_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, reader, tx, 0, false, false)

if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions turbo/jsonrpc/debug_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ledgerwatch/erigon-lib/common/hexutil"

jsoniter "github.com/json-iterator/go"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/kv"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (api *PrivateDebugAPIImpl) StorageRangeAt(ctx context.Context, blockHash co
return StorageRangeResult{}, nil
}

_, _, _, _, stateReader, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx))
_, _, _, _, stateReader, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx), false)
if err != nil {
return StorageRangeResult{}, err
}
Expand Down Expand Up @@ -345,7 +346,7 @@ func (api *PrivateDebugAPIImpl) AccountAt(ctx context.Context, blockHash common.
if block == nil {
return nil, nil
}
_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx))
_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx), false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (api *BaseAPI) getReceipts(ctx context.Context, tx kv.Tx, block *types.Bloc
return nil, err
}

_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, 0, api.historyV3(tx))
_, _, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, 0, api.historyV3(tx), false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/otterscan_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (api *OtterscanAPIImpl) runTracer(ctx context.Context, tx kv.Tx, hash commo
}
engine := api.engine()

msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx))
msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, int(txIndex), api.historyV3(tx), false)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions turbo/jsonrpc/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp
}
engine := api.engine()

_, blockCtx, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, 0, api.historyV3(tx))
_, blockCtx, _, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, 0, api.historyV3(tx), false)
if err != nil {
stream.WriteNil()
return err
Expand Down Expand Up @@ -275,7 +275,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo
}
engine := api.engine()

msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, txnIndex, api.historyV3(tx))
msg, blockCtx, txCtx, ibs, _, err := transactions.ComputeTxEnv(ctx, engine, block, chainConfig, api._blockReader, tx, txnIndex, api.historyV3(tx), isBorStateSyncTxn)
if err != nil {
stream.WriteNil()
return err
Expand Down
10 changes: 7 additions & 3 deletions turbo/transactions/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type BlockGetter interface {
}

// ComputeTxEnv returns the execution environment of a certain transaction.
func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *types.Block, cfg *chain.Config, headerReader services.HeaderReader, dbtx kv.Tx, txIndex int, historyV3 bool) (core.Message, evmtypes.BlockContext, evmtypes.TxContext, *state.IntraBlockState, state.StateReader, error) {
func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *types.Block, cfg *chain.Config, headerReader services.HeaderReader, dbtx kv.Tx, txIndex int, historyV3, isBorStateSyncTxn bool) (core.Message, evmtypes.BlockContext, evmtypes.TxContext, *state.IntraBlockState, state.StateReader, error) {
reader, err := rpchelper.CreateHistoryStateReader(dbtx, block.NumberU64(), txIndex, historyV3, cfg.ChainName)
if err != nil {
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, err
Expand All @@ -45,6 +45,10 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ
// Create the parent state database
statedb := state.New(reader)

if !isBorStateSyncTxn && 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
Expand All @@ -56,7 +60,7 @@ 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()) {
if isBorStateSyncTxn && txIndex == len(block.Transactions()) {
// tx is a state sync transaction
return nil, blockContext, evmtypes.TxContext{}, statedb, reader, nil
}
Expand Down Expand Up @@ -122,7 +126,7 @@ func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *typ
}
}

if txIndex == len(block.Transactions()) {
if isBorStateSyncTxn && txIndex == len(block.Transactions()) {
// tx is a state sync transaction
return nil, blockContext, evmtypes.TxContext{}, statedb, reader, nil
}
Expand Down

0 comments on commit add87c8

Please sign in to comment.