Skip to content

Commit e26b024

Browse files
committed
feat: Processor.PreprocessingGasCharge errors on unknown tx
1 parent 04c4cbc commit e26b024

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

core/vm/evm.libevm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ func (evm *EVM) spendPreprocessingGas(gas uint64) (uint64, error) {
8080
if evm.depth > 0 || !libevmHooks.Registered() {
8181
return gas, nil
8282
}
83-
c := libevmHooks.Get().PreprocessingGasCharge(evm.StateDB.TxHash())
83+
c, err := libevmHooks.Get().PreprocessingGasCharge(evm.StateDB.TxHash())
84+
if err != nil {
85+
return gas, err
86+
}
8487
if c > gas {
8588
return 0, ErrOutOfGas
8689
}

core/vm/evm.libevm_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func (o *evmArgOverrider) OverrideEVMResetArgs(r params.Rules, _ *EVMResetArgs)
4747
}
4848
}
4949

50-
func (o *evmArgOverrider) PreprocessingGasCharge(common.Hash) uint64 { return 0 }
50+
func (o *evmArgOverrider) PreprocessingGasCharge(common.Hash) (uint64, error) {
51+
return 0, nil
52+
}
5153

5254
func (o *evmArgOverrider) register(t *testing.T) {
5355
t.Helper()

core/vm/hooks.libevm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Hooks interface {
4848
// [EVMInterpreter] is invoked and reports its gas charge for spending at the
4949
// beginning of [EVM.Call] or [EVM.Create].
5050
type Preprocessor interface {
51-
PreprocessingGasCharge(tx common.Hash) uint64
51+
PreprocessingGasCharge(tx common.Hash) (uint64, error)
5252
}
5353

5454
// NewEVMArgs are the arguments received by [NewEVM], available for override

libevm/precompiles/parallel/parallel.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package parallel
2020

2121
import (
22+
"errors"
2223
"fmt"
2324
"sync"
2425

@@ -154,7 +155,8 @@ func (p *Processor[R]) FinishBlock(b *types.Block) {
154155
// Every result channel is guaranteed to have some value in its buffer
155156
// because [Processor.BeforeBlock] either sends a nil *R or it
156157
// dispatches a job that will send a non-nil *R.
157-
delete(p.txGas, (<-p.results[i]).tx)
158+
tx := (<-p.results[i]).tx
159+
delete(p.txGas, tx)
158160
}
159161
}
160162

@@ -171,7 +173,7 @@ func (p *Processor[R]) FinishBlock(b *types.Block) {
171173
// that if R is a pointer then modifications will persist between calls.
172174
func (p *Processor[R]) Result(i int) (R, bool) {
173175
ch := p.results[i]
174-
r := (<-ch)
176+
r := <-ch
175177
defer func() {
176178
ch <- r
177179
}()
@@ -185,13 +187,17 @@ func (p *Processor[R]) Result(i int) (R, bool) {
185187
return *r.val, true
186188
}
187189

188-
func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules) (ok bool, err error) {
190+
func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules) (process bool, err error) {
191+
// An explicit 0 is necessary to avoid [Processor.PreprocessingGasCharge]
192+
// returning [ErrTxUnknown].
193+
p.txGas[tx.Hash()] = 0
194+
189195
cost, ok := p.handler.Gas(tx)
190196
if !ok {
191197
return false, nil
192198
}
193199
defer func() {
194-
if ok && err == nil {
200+
if process && err == nil {
195201
p.txGas[tx.Hash()] = cost
196202
}
197203
}()
@@ -214,10 +220,19 @@ func (p *Processor[R]) shouldProcess(tx *types.Transaction, rules params.Rules)
214220
return left >= cost, nil
215221
}
216222

217-
var _ vm.Preprocessor = (*Processor[struct{}])(nil)
223+
// ErrTxUnknown is returned by [Processor.PreprocessingGasCharge] if it is
224+
// called with a transaction hash that wasn't in the last block passed to
225+
// [Processor.StartBlock].
226+
var ErrTxUnknown = errors.New("transaction unknown by parallel preprocessor")
218227

219228
// PreprocessingGasCharge implements the [vm.Preprocessor] interface and MUST be
220229
// registered via [vm.RegisterHooks] to ensure proper gas accounting.
221-
func (p *Processor[R]) PreprocessingGasCharge(tx common.Hash) uint64 {
222-
return p.txGas[tx]
230+
func (p *Processor[R]) PreprocessingGasCharge(tx common.Hash) (uint64, error) {
231+
g, ok := p.txGas[tx]
232+
if !ok {
233+
return 0, fmt.Errorf("%w: %v", ErrTxUnknown, tx)
234+
}
235+
return g, nil
223236
}
237+
238+
var _ vm.Preprocessor = (*Processor[struct{}])(nil)

0 commit comments

Comments
 (0)