|
| 1 | +// Copyright 2026 The Erigon Authors |
| 2 | +// This file is part of Erigon. |
| 3 | +// |
| 4 | +// Erigon is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Erigon is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with Erigon. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// Package receipts provides shared receipt derivation by replaying transactions. |
| 18 | +// Used by both the RPC layer (rpc/jsonrpc/receipts) and the execution pipeline |
| 19 | +// (execution/stagedsync) to avoid duplicating transaction replay logic. |
| 20 | +package receipts |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "fmt" |
| 25 | + |
| 26 | + "github.com/erigontech/erigon/common" |
| 27 | + "github.com/erigontech/erigon/execution/chain" |
| 28 | + "github.com/erigontech/erigon/execution/protocol" |
| 29 | + "github.com/erigontech/erigon/execution/protocol/rules" |
| 30 | + "github.com/erigontech/erigon/execution/state" |
| 31 | + "github.com/erigontech/erigon/execution/types" |
| 32 | + "github.com/erigontech/erigon/execution/types/accounts" |
| 33 | + "github.com/erigontech/erigon/execution/vm" |
| 34 | +) |
| 35 | + |
| 36 | +// GetHeaderFunc returns a header by hash+number. Used for BLOCKHASH opcode. |
| 37 | +type GetHeaderFunc = func(hash common.Hash, number uint64) (*types.Header, error) |
| 38 | + |
| 39 | +// DeriveForRange replays transactions fromIdx..toIdx-1 (0-based within the block) |
| 40 | +// against the provided IntraBlockState and returns receipts for each. |
| 41 | +// |
| 42 | +// The caller is responsible for: |
| 43 | +// - Creating the IntraBlockState with the correct state reader (history or live) |
| 44 | +// - Providing a GasPool with the block's gas limit |
| 45 | +// - Providing the GetHeader function for BLOCKHASH resolution |
| 46 | +// |
| 47 | +// No caching — callers wrap this with their own caching layer. |
| 48 | +func DeriveForRange( |
| 49 | + ctx context.Context, |
| 50 | + cfg *chain.Config, |
| 51 | + engine rules.EngineReader, |
| 52 | + header *types.Header, |
| 53 | + txns types.Transactions, |
| 54 | + fromIdx int, |
| 55 | + toIdx int, |
| 56 | + ibs *state.IntraBlockState, |
| 57 | + gp *protocol.GasPool, |
| 58 | + getHeader GetHeaderFunc, |
| 59 | +) (types.Receipts, error) { |
| 60 | + if fromIdx < 0 { |
| 61 | + fromIdx = 0 |
| 62 | + } |
| 63 | + if toIdx > len(txns) { |
| 64 | + toIdx = len(txns) |
| 65 | + } |
| 66 | + if fromIdx >= toIdx { |
| 67 | + return nil, nil |
| 68 | + } |
| 69 | + |
| 70 | + blockNum := header.Number.Uint64() |
| 71 | + gasUsed := new(protocol.GasUsed) |
| 72 | + noopWriter := state.NewNoopWriter() |
| 73 | + hashFn := protocol.GetHashFn(header, getHeader) |
| 74 | + vmCfg := vm.Config{} |
| 75 | + |
| 76 | + // If starting mid-block, we need to replay 0..fromIdx-1 first to get |
| 77 | + // cumulative gas and state to the right point. We discard those receipts. |
| 78 | + for i := 0; i < fromIdx; i++ { |
| 79 | + select { |
| 80 | + case <-ctx.Done(): |
| 81 | + return nil, ctx.Err() |
| 82 | + default: |
| 83 | + } |
| 84 | + ibs.SetTxContext(blockNum, i) |
| 85 | + evm := protocol.CreateEVM(cfg, hashFn, engine, accounts.NilAddress, ibs, header, vmCfg) |
| 86 | + _, err := protocol.ApplyTransactionWithEVM(cfg, engine, gp, ibs, noopWriter, header, txns[i], gasUsed, vmCfg, evm) |
| 87 | + if err != nil { |
| 88 | + return nil, fmt.Errorf("receipts.DeriveForRange: replay tx %d (warmup): %w", i, err) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Now execute the target range and collect receipts. |
| 93 | + receipts := make(types.Receipts, 0, toIdx-fromIdx) |
| 94 | + for i := fromIdx; i < toIdx; i++ { |
| 95 | + select { |
| 96 | + case <-ctx.Done(): |
| 97 | + return nil, ctx.Err() |
| 98 | + default: |
| 99 | + } |
| 100 | + ibs.SetTxContext(blockNum, i) |
| 101 | + evm := protocol.CreateEVM(cfg, hashFn, engine, accounts.NilAddress, ibs, header, vmCfg) |
| 102 | + receipt, err := protocol.ApplyTransactionWithEVM(cfg, engine, gp, ibs, noopWriter, header, txns[i], gasUsed, vmCfg, evm) |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("receipts.DeriveForRange: replay tx %d: %w", i, err) |
| 105 | + } |
| 106 | + receipts = append(receipts, receipt) |
| 107 | + } |
| 108 | + |
| 109 | + return receipts, nil |
| 110 | +} |
| 111 | + |
| 112 | +// DeriveBlockReceipts replays all transactions in a block and returns their receipts. |
| 113 | +// Convenience wrapper around DeriveForRange(ctx, cfg, engine, header, txns, 0, len(txns), ...). |
| 114 | +func DeriveBlockReceipts( |
| 115 | + ctx context.Context, |
| 116 | + cfg *chain.Config, |
| 117 | + engine rules.EngineReader, |
| 118 | + header *types.Header, |
| 119 | + txns types.Transactions, |
| 120 | + ibs *state.IntraBlockState, |
| 121 | + gp *protocol.GasPool, |
| 122 | + getHeader GetHeaderFunc, |
| 123 | +) (types.Receipts, error) { |
| 124 | + return DeriveForRange(ctx, cfg, engine, header, txns, 0, len(txns), ibs, gp, getHeader) |
| 125 | +} |
| 126 | + |
| 127 | +// DerivePriorReceipts replays transactions 0..startTxIndex-1 and returns their |
| 128 | +// receipts. Used when execution resumes mid-block from a snapshot boundary and |
| 129 | +// Finalize needs the full receipt set for requests hash computation. |
| 130 | +func DerivePriorReceipts( |
| 131 | + ctx context.Context, |
| 132 | + cfg *chain.Config, |
| 133 | + engine rules.EngineReader, |
| 134 | + header *types.Header, |
| 135 | + txns types.Transactions, |
| 136 | + startTxIndex int, |
| 137 | + ibs *state.IntraBlockState, |
| 138 | + gp *protocol.GasPool, |
| 139 | + getHeader GetHeaderFunc, |
| 140 | +) (types.Receipts, error) { |
| 141 | + return DeriveForRange(ctx, cfg, engine, header, txns, 0, startTxIndex, ibs, gp, getHeader) |
| 142 | +} |
0 commit comments