mirror of https://github.com/status-im/op-geth.git
core/vm: Improved error reporting for trace logging
This commit is contained in:
parent
02d629af8f
commit
287f990891
|
@ -45,6 +45,7 @@ type StructLog struct {
|
||||||
Memory []byte
|
Memory []byte
|
||||||
Stack []*big.Int
|
Stack []*big.Int
|
||||||
Storage map[common.Hash][]byte
|
Storage map[common.Hash][]byte
|
||||||
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Account interface {
|
type Account interface {
|
||||||
|
|
|
@ -2,20 +2,14 @@ package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type OutOfGasError struct {
|
type OutOfGasError struct{}
|
||||||
req, has *big.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
func OOG(req, has *big.Int) OutOfGasError {
|
|
||||||
return OutOfGasError{req, has}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self OutOfGasError) Error() string {
|
func (self OutOfGasError) Error() string {
|
||||||
return fmt.Sprintf("out of gas! require %v, have %v", self.req, self.has)
|
return "Out Of Gas"
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsOOGErr(err error) bool {
|
func IsOOGErr(err error) bool {
|
||||||
|
|
|
@ -9,9 +9,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func StdErrFormat(logs []StructLog) {
|
func StdErrFormat(logs []StructLog) {
|
||||||
fmt.Fprintf(os.Stderr, "VM Stats %d ops\n", len(logs))
|
fmt.Fprintf(os.Stderr, "VM STAT %d OPs\n", len(logs))
|
||||||
for _, log := range logs {
|
for _, log := range logs {
|
||||||
fmt.Fprintf(os.Stderr, "PC %08d: %s GAS: %v COST: %v\n", log.Pc, log.Op, log.Gas, log.GasCost)
|
fmt.Fprintf(os.Stderr, "PC %08d: %s GAS: %v COST: %v", log.Pc, log.Op, log.Gas, log.GasCost)
|
||||||
|
if log.Err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, " ERROR: %v", log.Err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
|
||||||
fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack))
|
fmt.Fprintln(os.Stderr, "STACK =", len(log.Stack))
|
||||||
|
|
||||||
for i := len(log.Stack) - 1; i >= 0; i-- {
|
for i := len(log.Stack) - 1; i >= 0; i-- {
|
||||||
|
|
|
@ -43,35 +43,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
|
||||||
code = context.Code
|
code = context.Code
|
||||||
value = context.value
|
value = context.value
|
||||||
price = context.Price
|
price = context.Price
|
||||||
)
|
|
||||||
|
|
||||||
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
|
|
||||||
defer func() {
|
|
||||||
if self.After != nil {
|
|
||||||
self.After(context, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
|
|
||||||
// In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
|
|
||||||
context.UseGas(context.Gas)
|
|
||||||
|
|
||||||
ret = context.Return(nil)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if context.CodeAddr != nil {
|
|
||||||
if p := Precompiled[context.CodeAddr.Str()]; p != nil {
|
|
||||||
return self.RunPrecompiled(p, input, context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't bother with the execution if there's no code.
|
|
||||||
if len(code) == 0 {
|
|
||||||
return context.Return(nil), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
op OpCode // current opcode
|
op OpCode // current opcode
|
||||||
codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching
|
codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching
|
||||||
mem = NewMemory() // bound memory
|
mem = NewMemory() // bound memory
|
||||||
|
@ -93,8 +65,38 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newMemSize *big.Int
|
||||||
|
cost *big.Int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
|
||||||
|
defer func() {
|
||||||
|
if self.After != nil {
|
||||||
|
self.After(context, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
self.log(pc, op, context.Gas, cost, mem, stack, context, err)
|
||||||
|
|
||||||
|
// In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
|
||||||
|
context.UseGas(context.Gas)
|
||||||
|
|
||||||
|
ret = context.Return(nil)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if context.CodeAddr != nil {
|
||||||
|
if p := Precompiled[context.CodeAddr.Str()]; p != nil {
|
||||||
|
return self.RunPrecompiled(p, input, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't bother with the execution if there's no code.
|
||||||
|
if len(code) == 0 {
|
||||||
|
return context.Return(nil), nil
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// The base for all big integer arithmetic
|
// The base for all big integer arithmetic
|
||||||
base := new(big.Int)
|
base := new(big.Int)
|
||||||
|
@ -103,24 +105,23 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
|
||||||
op = context.GetOp(pc)
|
op = context.GetOp(pc)
|
||||||
|
|
||||||
// calculate the new memory size and gas price for the current executing opcode
|
// calculate the new memory size and gas price for the current executing opcode
|
||||||
newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack)
|
newMemSize, cost, err = self.calculateGasAndSize(context, caller, op, statedb, mem, stack)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
self.log(pc, op, context.Gas, gas, mem, stack, context)
|
|
||||||
|
|
||||||
// Use the calculated gas. When insufficient gas is present, use all gas and return an
|
// Use the calculated gas. When insufficient gas is present, use all gas and return an
|
||||||
// Out Of Gas error
|
// Out Of Gas error
|
||||||
if !context.UseGas(gas) {
|
if !context.UseGas(cost) {
|
||||||
tmp := new(big.Int).Set(context.Gas)
|
|
||||||
|
|
||||||
context.UseGas(context.Gas)
|
context.UseGas(context.Gas)
|
||||||
|
|
||||||
return context.Return(nil), OOG(gas, tmp)
|
return context.Return(nil), OutOfGasError{}
|
||||||
}
|
}
|
||||||
// Resize the memory calculated previously
|
// Resize the memory calculated previously
|
||||||
mem.Resize(newMemSize.Uint64())
|
mem.Resize(newMemSize.Uint64())
|
||||||
|
// Add a log message
|
||||||
|
self.log(pc, op, context.Gas, cost, mem, stack, context, nil)
|
||||||
|
|
||||||
switch op {
|
switch op {
|
||||||
case ADD:
|
case ADD:
|
||||||
|
@ -783,15 +784,13 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, input []byte, context *Con
|
||||||
|
|
||||||
return context.Return(ret), nil
|
return context.Return(ret), nil
|
||||||
} else {
|
} else {
|
||||||
tmp := new(big.Int).Set(context.Gas)
|
return nil, OutOfGasError{}
|
||||||
|
|
||||||
return nil, OOG(gas, tmp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// log emits a log event to the environment for each opcode encountered. This is not to be confused with the
|
// log emits a log event to the environment for each opcode encountered. This is not to be confused with the
|
||||||
// LOG* opcode.
|
// LOG* opcode.
|
||||||
func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, context *Context) {
|
func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, stack *stack, context *Context, err error) {
|
||||||
if Debug {
|
if Debug {
|
||||||
mem := make([]byte, len(memory.Data()))
|
mem := make([]byte, len(memory.Data()))
|
||||||
copy(mem, memory.Data())
|
copy(mem, memory.Data())
|
||||||
|
@ -804,7 +803,7 @@ func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, st
|
||||||
storage[common.BytesToHash(k)] = v
|
storage[common.BytesToHash(k)] = v
|
||||||
})
|
})
|
||||||
|
|
||||||
self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage})
|
self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, err})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue