Chnged to use GetOp instead & added error + checking

This commit is contained in:
obscuren 2014-10-23 14:04:00 +02:00
parent 91c876831a
commit feef194829
4 changed files with 58 additions and 6 deletions

View File

@ -63,7 +63,7 @@ func RunVmTest(js string) (failed int) {
// When an error is returned it doesn't always mean the tests fails. // When an error is returned it doesn't always mean the tests fails.
// Have to come up with some conditional failing mechanism. // Have to come up with some conditional failing mechanism.
if err != nil { if err != nil {
helper.Log.Infoln(err) log.Println(err)
} }
rexp := helper.FromHex(test.Out) rexp := helper.FromHex(test.Out)
@ -96,6 +96,7 @@ func RunVmTest(js string) (failed int) {
} }
func main() { func main() {
helper.Logger.SetLogLevel(5)
if len(os.Args) == 1 { if len(os.Args) == 1 {
log.Fatalln("no json supplied") log.Fatalln("no json supplied")
} }

51
vm/errors.go Normal file
View File

@ -0,0 +1,51 @@
package vm
import (
"fmt"
"math/big"
)
type OutOfGasError struct {
req, has *big.Int
}
func OOG(req, has *big.Int) OutOfGasError {
return OutOfGasError{req, has}
}
func (self OutOfGasError) Error() string {
return fmt.Sprintf("out of gas! require %v, have %v", self.req, self.has)
}
func IsOOGErr(err error) bool {
_, ok := err.(OutOfGasError)
return ok
}
type StackError struct {
req, has int
}
func StackErr(req, has int) StackError {
return StackError{req, has}
}
func (self StackError) Error() string {
return fmt.Sprintf("stack error! require %v, have %v", self.req, self.has)
}
func IsStack(err error) bool {
_, ok := err.(StackError)
return ok
}
type DepthError struct{}
func (self DepthError) Error() string {
return fmt.Sprintf("Max call depth exceeded (%d)", MaxCallDepth)
}
func IsDepthErr(err error) bool {
_, ok := err.(DepthError)
return ok
}

View File

@ -36,7 +36,7 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte,
snapshot := env.State().Copy() snapshot := env.State().Copy()
defer func() { defer func() {
if err != nil { if IsDepthErr(err) || IsOOGErr(err) {
env.State().Set(snapshot) env.State().Set(snapshot)
} }
}() }()
@ -76,7 +76,7 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte,
if self.vm.Depth() == MaxCallDepth { if self.vm.Depth() == MaxCallDepth {
c.UseGas(self.Gas) c.UseGas(self.Gas)
return c.Return(nil), fmt.Errorf("Max call depth exceeded (%d)", MaxCallDepth) return c.Return(nil), DepthError{}
} }
// Executer the closure and get the return value (if any) // Executer the closure and get the return value (if any)

View File

@ -107,7 +107,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
step++ step++
// Get the memory location of pc // Get the memory location of pc
op = OpCode(closure.Get(pc).Uint()) op = closure.GetOp(int(pc.Uint64()))
// XXX Leave this Println intact. Don't change this to the log system. // XXX Leave this Println intact. Don't change this to the log system.
// Used for creating diffs between implementations // Used for creating diffs between implementations
@ -246,11 +246,11 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
if !closure.UseGas(gas) { if !closure.UseGas(gas) {
self.Endl() self.Endl()
err := fmt.Errorf("Insufficient gas for %v. req %v has %v", op, gas, closure.Gas) tmp := new(big.Int).Set(closure.Gas)
closure.UseGas(closure.Gas) closure.UseGas(closure.Gas)
return closure.Return(nil), err return closure.Return(nil), OOG(gas, tmp)
} }
mem.Resize(newMemSize.Uint64()) mem.Resize(newMemSize.Uint64())