diff options
author | obscuren <geffobscura@gmail.com> | 2014-10-15 23:12:26 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-10-15 23:12:26 +0800 |
commit | 311c6f8a3fed5ac03ee4b442fd0f420072bc41b4 (patch) | |
tree | de823080f3704d7690fd7172c9742962b66baf3a /ethvm | |
parent | 266d21209478bdb8c89e1ffb95d7f0de34635968 (diff) | |
download | go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar.gz go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar.bz2 go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar.lz go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar.xz go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.tar.zst go-tangerine-311c6f8a3fed5ac03ee4b442fd0f420072bc41b4.zip |
Fixed remote Arithmetic tests
Diffstat (limited to 'ethvm')
-rw-r--r-- | ethvm/common.go | 3 | ||||
-rw-r--r-- | ethvm/vm.go | 65 | ||||
-rw-r--r-- | ethvm/vm_debug.go | 117 |
3 files changed, 120 insertions, 65 deletions
diff --git a/ethvm/common.go b/ethvm/common.go index 6854a5219..617a8279f 100644 --- a/ethvm/common.go +++ b/ethvm/common.go @@ -35,7 +35,8 @@ var ( LogTyPretty byte = 0x1 LogTyDiff byte = 0x2 - To256 = ethutil.To256 + U256 = ethutil.U256 + S256 = ethutil.S256 ) const MaxCallDepth = 1024 diff --git a/ethvm/vm.go b/ethvm/vm.go index dad031e01..4df98328e 100644 --- a/ethvm/vm.go +++ b/ethvm/vm.go @@ -8,6 +8,9 @@ import ( "github.com/ethereum/eth-go/ethutil" ) +// BIG FAT WARNING. THIS VM IS NOT YET IS USE! +// I want to get all VM tests pass first before updating this VM + type Vm struct { env Environment err error @@ -170,7 +173,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Add(y, x) - To256(base) + U256(base) // Pop result back on the stack stack.Push(base) @@ -180,7 +183,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Sub(y, x) - To256(base) + U256(base) // Pop result back on the stack stack.Push(base) @@ -190,7 +193,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Mul(y, x) - To256(base) + U256(base) // Pop result back on the stack stack.Push(base) @@ -202,21 +205,29 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Div(y, x) } - To256(base) + U256(base) // Pop result back on the stack stack.Push(base) case SDIV: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) - if x.Cmp(ethutil.Big0) != 0 { - base.Div(y, x) - } + if x.Cmp(ethutil.Big0) == 0 { + base.Set(ethutil.Big0) + } else { + n := new(big.Int) + if new(big.Int).Mul(y, x).Cmp(ethutil.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } - To256(base) + base.Div(y.Abs(y), x.Mul(x.Abs(x), n)) + + U256(base) + } - // Pop result back on the stack stack.Push(base) case MOD: require(2) @@ -224,16 +235,27 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Mod(y, x) - To256(base) + U256(base) stack.Push(base) case SMOD: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) - base.Mod(y, x) + if x.Cmp(ethutil.Big0) == 0 { + base.Set(ethutil.Big0) + } else { + n := new(big.Int) + if y.Cmp(ethutil.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } - To256(base) + base.Mod(y.Abs(y), x.Mul(x.Abs(x), n)) + + U256(base) + } stack.Push(base) @@ -243,12 +265,15 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Exp(y, x, Pow256) - To256(base) + U256(base) stack.Push(base) case NEG: require(1) base.Sub(Pow256, stack.Pop()) + + base = U256(base) + stack.Push(base) case LT: require(2) @@ -272,16 +297,16 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { case SLT: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) // x < y - if y.Cmp(x) < 0 { + if y.Cmp(S256(x)) < 0 { stack.Push(ethutil.BigTrue) } else { stack.Push(ethutil.BigFalse) } case SGT: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) // x > y if y.Cmp(x) > 0 { @@ -345,7 +370,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Add(x, y) base.Mod(base, z) - To256(base) + U256(base) stack.Push(base) case MULMOD: @@ -358,7 +383,7 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { base.Mul(x, y) base.Mod(base, z) - To256(base) + U256(base) stack.Push(base) diff --git a/ethvm/vm_debug.go b/ethvm/vm_debug.go index df8cbb10c..d3d3d7696 100644 --- a/ethvm/vm_debug.go +++ b/ethvm/vm_debug.go @@ -40,10 +40,27 @@ func NewDebugVm(env Environment) *DebugVm { func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { self.depth++ + var ( + op OpCode + + mem = &Memory{} + stack = NewStack() + pc = big.NewInt(0) + step = 0 + prevStep = 0 + require = func(m int) { + if stack.Len() < m { + panic(fmt.Sprintf("%04v (%v) stack err size = %d, required = %d", pc, op, stack.Len(), m)) + } + } + ) + if self.Recoverable { // Recover from any require exception defer func() { if r := recover(); r != nil { + self.Endl() + ret = closure.Return(nil) err = fmt.Errorf("%v", r) } @@ -62,21 +79,6 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { vmlogger.Debugf("(%s) %x gas: %v (d) %x\n", self.Fn, closure.Address(), closure.Gas, closure.Args) - var ( - op OpCode - - mem = &Memory{} - stack = NewStack() - pc = big.NewInt(0) - step = 0 - prevStep = 0 - require = func(m int) { - if stack.Len() < m { - panic(fmt.Sprintf("%04v (%v) stack err size = %d, required = %d", pc, op, stack.Len(), m)) - } - } - ) - for { prevStep = step // The base for all big integer arithmetic @@ -223,7 +225,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Add(y, x) - To256(base) + U256(base) self.Printf(" = %v", base) // Pop result back on the stack @@ -235,7 +237,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Sub(y, x) - To256(base) + U256(base) self.Printf(" = %v", base) // Pop result back on the stack @@ -247,60 +249,84 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Mul(y, x) - To256(base) + U256(base) self.Printf(" = %v", base) // Pop result back on the stack stack.Push(base) case DIV: require(2) - x, y := stack.Popn() - self.Printf(" %v / %v", y, x) + x, y := stack.Pop(), stack.Pop() + self.Printf(" %v / %v", x, y) - if x.Cmp(ethutil.Big0) != 0 { - base.Div(y, x) + if y.Cmp(ethutil.Big0) != 0 { + base.Div(x, y) } - To256(base) + U256(base) self.Printf(" = %v", base) // Pop result back on the stack stack.Push(base) case SDIV: require(2) - x, y := stack.Popn() - self.Printf(" %v / %v", y, x) + x, y := S256(stack.Pop()), S256(stack.Pop()) - if x.Cmp(ethutil.Big0) != 0 { - base.Div(y, x) - } + self.Printf(" %v / %v", x, y) - To256(base) + if y.Cmp(ethutil.Big0) == 0 { + base.Set(ethutil.Big0) + } else { + n := new(big.Int) + if new(big.Int).Mul(x, y).Cmp(ethutil.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } + + base.Div(x.Abs(x), y.Abs(y)).Mul(base, n) + + U256(base) + } self.Printf(" = %v", base) - // Pop result back on the stack stack.Push(base) case MOD: require(2) - x, y := stack.Popn() + x, y := stack.Pop(), stack.Pop() - self.Printf(" %v %% %v", y, x) + self.Printf(" %v %% %v", x, y) - base.Mod(y, x) + if y.Cmp(ethutil.Big0) == 0 { + base.Set(ethutil.Big0) + } else { + base.Mod(x, y) + } - To256(base) + U256(base) self.Printf(" = %v", base) stack.Push(base) case SMOD: require(2) - x, y := stack.Popn() + x, y := S256(stack.Pop()), S256(stack.Pop()) - self.Printf(" %v %% %v", y, x) + self.Printf(" %v %% %v", x, y) - base.Mod(y, x) + if y.Cmp(ethutil.Big0) == 0 { + base.Set(ethutil.Big0) + } else { + n := new(big.Int) + if x.Cmp(ethutil.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } - To256(base) + base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n) + + U256(base) + } self.Printf(" = %v", base) stack.Push(base) @@ -313,7 +339,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Exp(y, x, Pow256) - To256(base) + U256(base) self.Printf(" = %v", base) @@ -321,6 +347,9 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { case NEG: require(1) base.Sub(Pow256, stack.Pop()) + + base = U256(base) + stack.Push(base) case LT: require(2) @@ -346,17 +375,17 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { case SLT: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) self.Printf(" %v < %v", y, x) // x < y - if y.Cmp(x) < 0 { + if y.Cmp(S256(x)) < 0 { stack.Push(ethutil.BigTrue) } else { stack.Push(ethutil.BigFalse) } case SGT: require(2) - x, y := stack.Popn() + y, x := S256(stack.Pop()), S256(stack.Pop()) self.Printf(" %v > %v", y, x) // x > y @@ -426,7 +455,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Add(x, y) base.Mod(base, z) - To256(base) + U256(base) self.Printf(" = %v", base) @@ -441,7 +470,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { base.Mul(x, y) base.Mod(base, z) - To256(base) + U256(base) self.Printf(" = %v", base) |