Skip to content

Commit f76cc66

Browse files
committed
Changed context and ADDMOD, MULMOD
* Cleaned up VM execution. VM run now takes a context * ADDMOD/MULMOD - removed incorrect cast
1 parent 80592f2 commit f76cc66

6 files changed

Lines changed: 44 additions & 24 deletions

File tree

‎core/execution.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ContextRef) (ret
5959

6060
snapshot := env.State().Copy()
6161
start := time.Now()
62-
ret, err = evm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
62+
63+
context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
64+
context.SetCallCode(contextAddr, code)
65+
66+
ret, err = evm.Run(context, self.input) //self.value, self.Gas, self.price, self.input)
6367
chainlogger.Debugf("vm took %v\n", time.Since(start))
6468
if err != nil {
6569
env.State().Set(snapshot)

‎tests/vm/gh_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func RunVmTest(p string, t *testing.T) {
116116
} else {
117117
ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction)
118118
}
119-
statedb.Sync()
120119

121120
rexp := helper.FromHex(test.Out)
122121
if bytes.Compare(rexp, ret) != 0 {
@@ -157,6 +156,7 @@ func RunVmTest(p string, t *testing.T) {
157156
}
158157

159158
if !isVmTest {
159+
statedb.Sync()
160160
if !bytes.Equal(ethutil.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
161161
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
162162
}

‎vm/common.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vm
22

33
import (
4+
"math"
45
"math/big"
56

67
"github.com/ethereum/go-ethereum/ethutil"
@@ -117,3 +118,10 @@ func toValue(val *big.Int) interface{} {
117118

118119
return val
119120
}
121+
122+
func getCode(code []byte, start, size uint64) []byte {
123+
x := uint64(math.Min(float64(start), float64(len(code))))
124+
y := uint64(math.Min(float64(x+size), float64(len(code))))
125+
126+
return ethutil.RightPadBytes(code[x:y], int(size))
127+
}

‎vm/context.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@ type ContextRef interface {
1515

1616
type Context struct {
1717
caller ContextRef
18-
object ContextRef
19-
Code []byte
18+
self ContextRef
2019

21-
Gas, UsedGas, Price *big.Int
20+
Code []byte
21+
CodeAddr []byte
22+
23+
value, Gas, UsedGas, Price *big.Int
2224

2325
Args []byte
2426
}
2527

2628
// Create a new context for the given data items
27-
func NewContext(caller ContextRef, object ContextRef, code []byte, gas, price *big.Int) *Context {
28-
c := &Context{caller: caller, object: object, Code: code, Args: nil}
29+
func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int) *Context {
30+
c := &Context{caller: caller, self: object, Args: nil}
2931

3032
// Gas should be a pointer so it can safely be reduced through the run
3133
// This pointer will be off the state transition
3234
c.Gas = gas //new(big.Int).Set(gas)
35+
c.value = new(big.Int).Set(value)
3336
// In most cases price and value are pointers to transaction objects
3437
// and we don't want the transaction's values to change.
3538
c.Price = new(big.Int).Set(price)
@@ -62,10 +65,7 @@ func (c *Context) GetRangeValue(x, size uint64) []byte {
6265
}
6366

6467
func (c *Context) GetCode(x, size uint64) []byte {
65-
x = uint64(math.Min(float64(x), float64(len(c.Code))))
66-
y := uint64(math.Min(float64(x+size), float64(len(c.Code))))
67-
68-
return ethutil.RightPadBytes(c.Code[x:y], int(size))
68+
return getCode(c.Code, x, size)
6969
}
7070

7171
func (c *Context) Return(ret []byte) []byte {
@@ -101,9 +101,14 @@ func (c *Context) ReturnGas(gas, price *big.Int) {
101101
* Set / Get
102102
*/
103103
func (c *Context) Address() []byte {
104-
return c.object.Address()
104+
return c.self.Address()
105105
}
106106

107107
func (self *Context) SetCode(code []byte) {
108108
self.Code = code
109109
}
110+
111+
func (self *Context) SetCallCode(addr, code []byte) {
112+
self.Code = code
113+
self.CodeAddr = addr
114+
}

‎vm/virtual_machine.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package vm
22

3-
import "math/big"
4-
53
type VirtualMachine interface {
64
Env() Environment
7-
Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, data []byte) ([]byte, error)
5+
Run(context *Context, data []byte) ([]byte, error)
86
Printf(string, ...interface{}) VirtualMachine
97
Endl() VirtualMachine
108
}

‎vm/vm.go‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ func New(env Environment) *Vm {
3232
return &Vm{debug: Debug, env: env, logTy: lt, Recoverable: true}
3333
}
3434

35-
func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, callData []byte) (ret []byte, err error) {
35+
func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
36+
//func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, callData []byte) (ret []byte, err error) {
3637
self.env.SetDepth(self.env.Depth() + 1)
3738

38-
context := NewContext(caller, me, code, gas, price)
39+
//context := NewContext(caller, me, code, gas, price)
40+
var (
41+
caller = context.caller
42+
code = context.Code
43+
value = context.value
44+
price = context.Price
45+
)
3946

4047
self.Printf("(%d) (%x) %x (code=%d) gas: %v (d) %x", self.env.Depth(), caller.Address()[:4], context.Address(), len(code), context.Gas, callData).Endl()
4148

@@ -55,7 +62,7 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
5562
}()
5663
}
5764

58-
if p := Precompiled[string(me.Address())]; p != nil {
65+
if p := Precompiled[string(context.CodeAddr)]; p != nil {
5966
return self.RunPrecompiled(p, callData, context)
6067
}
6168

@@ -352,7 +359,7 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
352359
z := stack.pop()
353360

354361
if z.Cmp(Zero) > 0 {
355-
add := U256(new(big.Int).Add(x, y))
362+
add := new(big.Int).Add(x, y)
356363
base.Mod(add, z)
357364

358365
base = U256(base)
@@ -362,13 +369,12 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
362369

363370
stack.push(base)
364371
case MULMOD:
365-
366372
x := stack.pop()
367373
y := stack.pop()
368374
z := stack.pop()
369375

370-
mul := new(big.Int).Mul(x, y)
371-
if len(z.Bytes()) > 0 { // NOT 0x0
376+
if z.Cmp(Zero) > 0 {
377+
mul := new(big.Int).Mul(x, y)
372378
base.Mod(mul, z)
373379

374380
U256(base)
@@ -476,13 +482,12 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
476482
} else {
477483
code = context.Code
478484
}
479-
context := NewContext(nil, nil, code, ethutil.Big0, ethutil.Big0)
480485
var (
481486
mOff = stack.pop().Uint64()
482487
cOff = stack.pop().Uint64()
483488
l = stack.pop().Uint64()
484489
)
485-
codeCopy := context.GetCode(cOff, l)
490+
codeCopy := getCode(code, cOff, l)
486491

487492
mem.Set(mOff, l, codeCopy)
488493

0 commit comments

Comments
 (0)