rm canceler

This commit is contained in:
Sina Mahmoodi 2024-03-07 13:08:27 +01:00
parent 5bdbf07837
commit eeb0a46429
3 changed files with 22 additions and 23 deletions

View file

@ -46,12 +46,6 @@ type StateDB interface {
GetRefund() uint64 GetRefund() uint64
} }
// Canceler is an interface that wraps the Cancel method.
// It allows loggers to cancel EVM processing.
type Canceler interface {
Cancel()
}
// VMContext provides the context for the EVM execution. // VMContext provides the context for the EVM execution.
type VMContext struct { type VMContext struct {
Coinbase common.Address Coinbase common.Address
@ -62,7 +56,6 @@ type VMContext struct {
GasPrice *big.Int GasPrice *big.Int
ChainConfig *params.ChainConfig ChainConfig *params.ChainConfig
StateDB StateDB StateDB StateDB
VM Canceler
} }
// BlockEvent is emitted upon tracing an incoming block. // BlockEvent is emitted upon tracing an incoming block.

View file

@ -554,6 +554,5 @@ func (evm *EVM) GetVMContext() *tracing.VMContext {
GasPrice: evm.TxContext.GasPrice, GasPrice: evm.TxContext.GasPrice,
ChainConfig: evm.ChainConfig(), ChainConfig: evm.ChainConfig(),
StateDB: evm.StateDB, StateDB: evm.StateDB,
VM: evm,
} }
} }

View file

@ -249,7 +249,6 @@ func (t *jsTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from
gasPriceBig, err := t.toBig(t.vm, env.GasPrice.String()) gasPriceBig, err := t.toBig(t.vm, env.GasPrice.String())
if err != nil { if err != nil {
t.err = err t.err = err
t.env.VM.Cancel()
return return
} }
t.ctx["gasPrice"] = gasPriceBig t.ctx["gasPrice"] = gasPriceBig
@ -258,6 +257,9 @@ func (t *jsTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from
// OnTxEnd implements the Tracer interface and is invoked at the end of // OnTxEnd implements the Tracer interface and is invoked at the end of
// transaction processing. // transaction processing.
func (t *jsTracer) OnTxEnd(receipt *types.Receipt, err error) { func (t *jsTracer) OnTxEnd(receipt *types.Receipt, err error) {
if t.err != nil {
return
}
if err != nil { if err != nil {
// Don't override vm error // Don't override vm error
if _, ok := t.ctx["error"]; !ok { if _, ok := t.ctx["error"]; !ok {
@ -270,9 +272,8 @@ func (t *jsTracer) OnTxEnd(receipt *types.Receipt, err error) {
// onStart implements the Tracer interface to initialize the tracing operation. // onStart implements the Tracer interface to initialize the tracing operation.
func (t *jsTracer) onStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { func (t *jsTracer) onStart(from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
cancel := func(err error) { if t.err != nil {
t.err = err return
t.env.VM.Cancel()
} }
if create { if create {
t.ctx["type"] = t.vm.ToValue("CREATE") t.ctx["type"] = t.vm.ToValue("CREATE")
@ -281,25 +282,25 @@ func (t *jsTracer) onStart(from common.Address, to common.Address, create bool,
} }
fromVal, err := t.toBuf(t.vm, from.Bytes()) fromVal, err := t.toBuf(t.vm, from.Bytes())
if err != nil { if err != nil {
cancel(err) t.err = err
return return
} }
t.ctx["from"] = fromVal t.ctx["from"] = fromVal
toVal, err := t.toBuf(t.vm, to.Bytes()) toVal, err := t.toBuf(t.vm, to.Bytes())
if err != nil { if err != nil {
cancel(err) t.err = err
return return
} }
t.ctx["to"] = toVal t.ctx["to"] = toVal
inputVal, err := t.toBuf(t.vm, input) inputVal, err := t.toBuf(t.vm, input)
if err != nil { if err != nil {
cancel(err) t.err = err
return return
} }
t.ctx["input"] = inputVal t.ctx["input"] = inputVal
valueBig, err := t.toBig(t.vm, value.String()) valueBig, err := t.toBig(t.vm, value.String())
if err != nil { if err != nil {
cancel(err) t.err = err
return return
} }
t.ctx["value"] = valueBig t.ctx["value"] = valueBig
@ -344,6 +345,9 @@ func (t *jsTracer) OnFault(pc uint64, op byte, gas, cost uint64, scope tracing.O
// onEnd is called after the call finishes to finalize the tracing. // onEnd is called after the call finishes to finalize the tracing.
func (t *jsTracer) onEnd(output []byte, gasUsed uint64, err error, reverted bool) { func (t *jsTracer) onEnd(output []byte, gasUsed uint64, err error, reverted bool) {
if t.err != nil {
return
}
if err != nil { if err != nil {
t.ctx["error"] = t.vm.ToValue(err.Error()) t.ctx["error"] = t.vm.ToValue(err.Error())
} }
@ -357,6 +361,9 @@ func (t *jsTracer) onEnd(output []byte, gasUsed uint64, err error, reverted bool
// OnEnter is called when EVM enters a new scope (via call, create or selfdestruct). // OnEnter is called when EVM enters a new scope (via call, create or selfdestruct).
func (t *jsTracer) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { func (t *jsTracer) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
if t.err != nil {
return
}
if depth == 0 { if depth == 0 {
t.onStart(from, to, vm.OpCode(typ) == vm.CREATE, input, gas, value) t.onStart(from, to, vm.OpCode(typ) == vm.CREATE, input, gas, value)
return return
@ -364,9 +371,6 @@ func (t *jsTracer) OnEnter(depth int, typ byte, from common.Address, to common.A
if !t.traceFrame { if !t.traceFrame {
return return
} }
if t.err != nil {
return
}
t.frame.typ = vm.OpCode(typ).String() t.frame.typ = vm.OpCode(typ).String()
t.frame.from = from t.frame.from = from
@ -386,6 +390,9 @@ func (t *jsTracer) OnEnter(depth int, typ byte, from common.Address, to common.A
// OnExit is called when EVM exits a scope, even if the scope didn't // OnExit is called when EVM exits a scope, even if the scope didn't
// execute any code. // execute any code.
func (t *jsTracer) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) { func (t *jsTracer) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
if t.err != nil {
return
}
if depth == 0 { if depth == 0 {
t.onEnd(output, gasUsed, err, reverted) t.onEnd(output, gasUsed, err, reverted)
return return
@ -405,6 +412,9 @@ func (t *jsTracer) OnExit(depth int, output []byte, gasUsed uint64, err error, r
// GetResult calls the Javascript 'result' function and returns its value, or any accumulated error // GetResult calls the Javascript 'result' function and returns its value, or any accumulated error
func (t *jsTracer) GetResult() (json.RawMessage, error) { func (t *jsTracer) GetResult() (json.RawMessage, error) {
if t.err != nil {
return nil, t.err
}
ctx := t.vm.ToValue(t.ctx) ctx := t.vm.ToValue(t.ctx)
res, err := t.result(t.obj, ctx, t.dbValue) res, err := t.result(t.obj, ctx, t.dbValue)
if err != nil { if err != nil {
@ -414,7 +424,7 @@ func (t *jsTracer) GetResult() (json.RawMessage, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return json.RawMessage(encoded), t.err return encoded, t.err
} }
// Stop terminates execution of the tracer at the first opportune moment. // Stop terminates execution of the tracer at the first opportune moment.
@ -427,9 +437,6 @@ func (t *jsTracer) Stop(err error) {
// execution. // execution.
func (t *jsTracer) onError(context string, err error) { func (t *jsTracer) onError(context string, err error) {
t.err = wrapError(context, err) t.err = wrapError(context, err)
// `env` is set on OnStart which comes before any JS execution.
// So it should be non-nil.
t.env.VM.Cancel()
} }
func wrapError(context string, err error) error { func wrapError(context string, err error) error {