aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/execution.go27
-rw-r--r--core/vm/analysis.go24
-rw-r--r--core/vm/common.go11
-rw-r--r--core/vm/context.go34
-rw-r--r--core/vm/gas.go164
-rw-r--r--core/vm/memory.go22
-rw-r--r--core/vm/stack.go10
-rw-r--r--core/vm/vm.go132
-rw-r--r--core/vm_env.go10
9 files changed, 235 insertions, 199 deletions
diff --git a/core/execution.go b/core/execution.go
index 72eb22bd5..24e085e6d 100644
--- a/core/execution.go
+++ b/core/execution.go
@@ -11,14 +11,18 @@ import (
)
type Execution struct {
- env vm.Environment
- address *common.Address
- input []byte
+ env vm.Environment
+ address *common.Address
+ input []byte
+ evm vm.VirtualMachine
+
Gas, price, value *big.Int
}
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
- return &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
+ exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
+ exe.evm = vm.NewVm(env)
+ return exe
}
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
@@ -28,11 +32,17 @@ func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]by
return self.exec(&codeAddr, code, caller)
}
+func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
+ ret, err = self.exec(nil, self.input, caller)
+ account = self.env.State().GetStateObject(*self.address)
+ return
+}
+
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
start := time.Now()
env := self.env
- evm := vm.NewVm(env)
+ evm := self.evm
if env.Depth() == vm.MaxCallDepth {
caller.ReturnGas(self.Gas, self.price)
@@ -70,10 +80,3 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.
return
}
-
-func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
- ret, err = self.exec(nil, self.input, caller)
- account = self.env.State().GetStateObject(*self.address)
-
- return
-}
diff --git a/core/vm/analysis.go b/core/vm/analysis.go
index 411df5686..264d55cb9 100644
--- a/core/vm/analysis.go
+++ b/core/vm/analysis.go
@@ -1,9 +1,25 @@
package vm
-import "gopkg.in/fatih/set.v0"
+import (
+ "math/big"
-func analyseJumpDests(code []byte) (dests *set.Set) {
- dests = set.New()
+ "gopkg.in/fatih/set.v0"
+)
+
+type destinations struct {
+ set *set.Set
+}
+
+func (d *destinations) Has(dest *big.Int) bool {
+ return d.set.Has(string(dest.Bytes()))
+}
+
+func (d *destinations) Add(dest *big.Int) {
+ d.set.Add(string(dest.Bytes()))
+}
+
+func analyseJumpDests(code []byte) (dests *destinations) {
+ dests = &destinations{set.New()}
for pc := uint64(0); pc < uint64(len(code)); pc++ {
var op OpCode = OpCode(code[pc])
@@ -13,7 +29,7 @@ func analyseJumpDests(code []byte) (dests *set.Set) {
pc += a
case JUMPDEST:
- dests.Add(pc)
+ dests.Add(big.NewInt(int64(pc)))
}
}
return
diff --git a/core/vm/common.go b/core/vm/common.go
index 8d8f4253f..5ff4e05f2 100644
--- a/core/vm/common.go
+++ b/core/vm/common.go
@@ -33,6 +33,7 @@ var (
S256 = common.S256
Zero = common.Big0
+ One = common.Big1
max = big.NewInt(math.MaxInt64)
)
@@ -80,3 +81,13 @@ func getData(data []byte, start, size *big.Int) []byte {
e := common.BigMin(new(big.Int).Add(s, size), dlen)
return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
}
+
+func UseGas(gas, amount *big.Int) bool {
+ if gas.Cmp(amount) < 0 {
+ return false
+ }
+
+ // Sub the amount of gas from the remaining
+ gas.Sub(gas, amount)
+ return true
+}
diff --git a/core/vm/context.go b/core/vm/context.go
index e73199b77..29bb9f74e 100644
--- a/core/vm/context.go
+++ b/core/vm/context.go
@@ -1,7 +1,6 @@
package vm
import (
- "math"
"math/big"
"github.com/ethereum/go-ethereum/common"
@@ -41,29 +40,18 @@ func NewContext(caller ContextRef, object ContextRef, value, gas, price *big.Int
return c
}
-func (c *Context) GetOp(n uint64) OpCode {
+func (c *Context) GetOp(n *big.Int) OpCode {
return OpCode(c.GetByte(n))
}
-func (c *Context) GetByte(n uint64) byte {
- if n < uint64(len(c.Code)) {
- return c.Code[n]
+func (c *Context) GetByte(n *big.Int) byte {
+ if n.Cmp(big.NewInt(int64(len(c.Code)))) < 0 {
+ return c.Code[n.Int64()]
}
return 0
}
-func (c *Context) GetBytes(x, y int) []byte {
- return c.GetRangeValue(uint64(x), uint64(y))
-}
-
-func (c *Context) GetRangeValue(x, size uint64) []byte {
- x = uint64(math.Min(float64(x), float64(len(c.Code))))
- y := uint64(math.Min(float64(x+size), float64(len(c.Code))))
-
- return common.RightPadBytes(c.Code[x:y], int(size))
-}
-
func (c *Context) Return(ret []byte) []byte {
// Return the remaining gas to the caller
c.caller.ReturnGas(c.Gas, c.Price)
@@ -74,16 +62,12 @@ func (c *Context) Return(ret []byte) []byte {
/*
* Gas functions
*/
-func (c *Context) UseGas(gas *big.Int) bool {
- if c.Gas.Cmp(gas) < 0 {
- return false
+func (c *Context) UseGas(gas *big.Int) (ok bool) {
+ ok = UseGas(c.Gas, gas)
+ if ok {
+ c.UsedGas.Add(c.UsedGas, gas)
}
-
- // Sub the amount of gas from the remaining
- c.Gas.Sub(c.Gas, gas)
- c.UsedGas.Add(c.UsedGas, gas)
-
- return true
+ return
}
// Implement the caller interface
diff --git a/core/vm/gas.go b/core/vm/gas.go
index c4d5e4c4e..2d5d7ae18 100644
--- a/core/vm/gas.go
+++ b/core/vm/gas.go
@@ -1,11 +1,9 @@
package vm
-import "math/big"
-
-type req struct {
- stack int
- gas *big.Int
-}
+import (
+ "fmt"
+ "math/big"
+)
var (
GasQuickStep = big.NewInt(2)
@@ -56,75 +54,30 @@ var (
GasCopyWord = big.NewInt(3)
)
-var _baseCheck = map[OpCode]req{
- // Req stack Gas price
- ADD: {2, GasFastestStep},
- LT: {2, GasFastestStep},
- GT: {2, GasFastestStep},
- SLT: {2, GasFastestStep},
- SGT: {2, GasFastestStep},
- EQ: {2, GasFastestStep},
- ISZERO: {1, GasFastestStep},
- SUB: {2, GasFastestStep},
- AND: {2, GasFastestStep},
- OR: {2, GasFastestStep},
- XOR: {2, GasFastestStep},
- NOT: {1, GasFastestStep},
- BYTE: {2, GasFastestStep},
- CALLDATALOAD: {1, GasFastestStep},
- CALLDATACOPY: {3, GasFastestStep},
- MLOAD: {1, GasFastestStep},
- MSTORE: {2, GasFastestStep},
- MSTORE8: {2, GasFastestStep},
- CODECOPY: {3, GasFastestStep},
- MUL: {2, GasFastStep},
- DIV: {2, GasFastStep},
- SDIV: {2, GasFastStep},
- MOD: {2, GasFastStep},
- SMOD: {2, GasFastStep},
- SIGNEXTEND: {2, GasFastStep},
- ADDMOD: {3, GasMidStep},
- MULMOD: {3, GasMidStep},
- JUMP: {1, GasMidStep},
- JUMPI: {2, GasSlowStep},
- EXP: {2, GasSlowStep},
- ADDRESS: {0, GasQuickStep},
- ORIGIN: {0, GasQuickStep},
- CALLER: {0, GasQuickStep},
- CALLVALUE: {0, GasQuickStep},
- CODESIZE: {0, GasQuickStep},
- GASPRICE: {0, GasQuickStep},
- COINBASE: {0, GasQuickStep},
- TIMESTAMP: {0, GasQuickStep},
- NUMBER: {0, GasQuickStep},
- CALLDATASIZE: {0, GasQuickStep},
- DIFFICULTY: {0, GasQuickStep},
- GASLIMIT: {0, GasQuickStep},
- POP: {0, GasQuickStep},
- PC: {0, GasQuickStep},
- MSIZE: {0, GasQuickStep},
- GAS: {0, GasQuickStep},
- BLOCKHASH: {1, GasExtStep},
- BALANCE: {0, GasExtStep},
- EXTCODESIZE: {1, GasExtStep},
- EXTCODECOPY: {4, GasExtStep},
- SLOAD: {1, GasStorageGet},
- SSTORE: {2, Zero},
- SHA3: {1, GasSha3Base},
- CREATE: {3, GasCreate},
- CALL: {7, GasCall},
- CALLCODE: {7, GasCall},
- JUMPDEST: {0, GasJumpDest},
- SUICIDE: {1, Zero},
- RETURN: {2, Zero},
-}
+func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
+ // PUSH and DUP are a bit special. They all cost the same but we do want to have checking on stack push limit
+ // PUSH is also allowed to calculate the same price for all PUSHes
+ // DUP requirements are handled elsewhere (except for the stack limit check)
+ if op >= PUSH1 && op <= PUSH32 {
+ op = PUSH1
+ }
+ if op >= DUP1 && op <= DUP16 {
+ op = DUP1
+ }
-func baseCheck(op OpCode, stack *stack, gas *big.Int) {
if r, ok := _baseCheck[op]; ok {
- stack.require(r.stack)
+ err := stack.require(r.stackPop)
+ if err != nil {
+ return err
+ }
+
+ if r.stackPush && len(stack.data)-r.stackPop+1 > 1024 {
+ return fmt.Errorf("stack limit reached (%d)", maxStack)
+ }
gas.Add(gas, r.gas)
}
+ return nil
}
func toWordSize(size *big.Int) *big.Int {
@@ -133,3 +86,74 @@ func toWordSize(size *big.Int) *big.Int {
tmp.Div(tmp, u256(32))
return tmp
}
+
+type req struct {
+ stackPop int
+ gas *big.Int
+ stackPush bool
+}
+
+var _baseCheck = map[OpCode]req{
+ // opcode | stack pop | gas price | stack push
+ ADD: {2, GasFastestStep, true},
+ LT: {2, GasFastestStep, true},
+ GT: {2, GasFastestStep, true},
+ SLT: {2, GasFastestStep, true},
+ SGT: {2, GasFastestStep, true},
+ EQ: {2, GasFastestStep, true},
+ ISZERO: {1, GasFastestStep, true},
+ SUB: {2, GasFastestStep, true},
+ AND: {2, GasFastestStep, true},
+ OR: {2, GasFastestStep, true},
+ XOR: {2, GasFastestStep, true},
+ NOT: {1, GasFastestStep, true},
+ BYTE: {2, GasFastestStep, true},
+ CALLDATALOAD: {1, GasFastestStep, true},
+ CALLDATACOPY: {3, GasFastestStep, true},
+ MLOAD: {1, GasFastestStep, true},
+ MSTORE: {2, GasFastestStep, false},
+ MSTORE8: {2, GasFastestStep, false},
+ CODECOPY: {3, GasFastestStep, false},
+ MUL: {2, GasFastStep, true},
+ DIV: {2, GasFastStep, true},
+ SDIV: {2, GasFastStep, true},
+ MOD: {2, GasFastStep, true},
+ SMOD: {2, GasFastStep, true},
+ SIGNEXTEND: {2, GasFastStep, true},
+ ADDMOD: {3, GasMidStep, true},
+ MULMOD: {3, GasMidStep, true},
+ JUMP: {1, GasMidStep, false},
+ JUMPI: {2, GasSlowStep, false},
+ EXP: {2, GasSlowStep, true},
+ ADDRESS: {0, GasQuickStep, true},
+ ORIGIN: {0, GasQuickStep, true},
+ CALLER: {0, GasQuickStep, true},
+ CALLVALUE: {0, GasQuickStep, true},
+ CODESIZE: {0, GasQuickStep, true},
+ GASPRICE: {0, GasQuickStep, true},
+ COINBASE: {0, GasQuickStep, true},
+ TIMESTAMP: {0, GasQuickStep, true},
+ NUMBER: {0, GasQuickStep, true},
+ CALLDATASIZE: {0, GasQuickStep, true},
+ DIFFICULTY: {0, GasQuickStep, true},
+ GASLIMIT: {0, GasQuickStep, true},
+ POP: {1, GasQuickStep, false},
+ PC: {0, GasQuickStep, true},
+ MSIZE: {0, GasQuickStep, true},
+ GAS: {0, GasQuickStep, true},
+ BLOCKHASH: {1, GasExtStep, true},
+ BALANCE: {0, GasExtStep, true},
+ EXTCODESIZE: {1, GasExtStep, true},
+ EXTCODECOPY: {4, GasExtStep, false},
+ SLOAD: {1, GasStorageGet, true},
+ SSTORE: {2, Zero, false},
+ SHA3: {1, GasSha3Base, true},
+ CREATE: {3, GasCreate, true},
+ CALL: {7, GasCall, true},
+ CALLCODE: {7, GasCall, true},
+ JUMPDEST: {0, GasJumpDest, false},
+ SUICIDE: {1, Zero, false},
+ RETURN: {2, Zero, false},
+ PUSH1: {0, GasFastestStep, true},
+ DUP1: {0, Zero, true},
+}
diff --git a/core/vm/memory.go b/core/vm/memory.go
index dd47fa1b5..b77d486eb 100644
--- a/core/vm/memory.go
+++ b/core/vm/memory.go
@@ -15,21 +15,17 @@ func NewMemory() *Memory {
}
func (m *Memory) Set(offset, size uint64, value []byte) {
- value = common.RightPadBytes(value, int(size))
+ // length of store may never be less than offset + size.
+ // The store should be resized PRIOR to setting the memory
+ if size > uint64(len(m.store)) {
+ panic("INVALID memory: store empty")
+ }
- totSize := offset + size
- lenSize := uint64(len(m.store) - 1)
- if totSize > lenSize {
- // Calculate the diff between the sizes
- diff := totSize - lenSize
- if diff > 0 {
- // Create a new empty slice and append it
- newSlice := make([]byte, diff-1)
- // Resize slice
- m.store = append(m.store, newSlice...)
- }
+ // It's possible the offset is greater than 0 and size equals 0. This is because
+ // the calcMemSize (common.go) could potentially return 0 when size is zero (NO-OP)
+ if size > 0 {
+ copy(m.store[offset:offset+size], common.RightPadBytes(value, int(size)))
}
- copy(m.store[offset:offset+size], value)
}
func (m *Memory) Resize(size uint64) {
diff --git a/core/vm/stack.go b/core/vm/stack.go
index 1e093476b..168637708 100644
--- a/core/vm/stack.go
+++ b/core/vm/stack.go
@@ -17,10 +17,7 @@ type stack struct {
}
func (st *stack) push(d *big.Int) {
- if len(st.data) == maxStack {
- panic(fmt.Sprintf("stack limit reached (%d)", maxStack))
- }
-
+ // NOTE push limit (1024) is checked in baseCheck
stackItem := new(big.Int).Set(d)
if len(st.data) > st.ptr {
st.data[st.ptr] = stackItem
@@ -52,10 +49,11 @@ func (st *stack) peek() *big.Int {
return st.data[st.len()-1]
}
-func (st *stack) require(n int) {
+func (st *stack) require(n int) error {
if st.len() < n {
- panic(fmt.Sprintf("stack underflow (%d <=> %d)", len(st.data), n))
+ return fmt.Errorf("stack underflow (%d <=> %d)", len(st.data), n)
}
+ return nil
}
func (st *stack) Print() {
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 562689dca..6c3dd240a 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -24,6 +24,9 @@ type Vm struct {
Fn string
Recoverable bool
+
+ // Will be called before the vm returns
+ After func(*Context, error)
}
func New(env Environment) *Vm {
@@ -45,20 +48,20 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
self.Printf("(%d) (%x) %x (code=%d) gas: %v (d) %x", self.env.Depth(), caller.Address().Bytes()[:4], context.Address(), len(code), context.Gas, callData).Endl()
- if self.Recoverable {
- // Recover from any require exception
- defer func() {
- if r := recover(); r != nil {
- self.Printf(" %v", r).Endl()
-
- context.UseGas(context.Gas)
+ // 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)
+ }
- ret = context.Return(nil)
+ if err != nil {
+ self.Printf(" %v", err).Endl()
+ // In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
+ context.UseGas(context.Gas)
- err = fmt.Errorf("%v", r)
- }
- }()
- }
+ ret = context.Return(nil)
+ }
+ }()
if context.CodeAddr != nil {
if p := Precompiled[context.CodeAddr.Str()]; p != nil {
@@ -69,25 +72,24 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
var (
op OpCode
- destinations = analyseJumpDests(context.Code)
- mem = NewMemory()
- stack = newStack()
- pc uint64 = 0
- step = 0
- statedb = self.env.State()
+ destinations = analyseJumpDests(context.Code)
+ mem = NewMemory()
+ stack = newStack()
+ pc = new(big.Int)
+ statedb = self.env.State()
- jump = func(from uint64, to *big.Int) {
- p := to.Uint64()
-
- nop := context.GetOp(p)
- if !destinations.Has(p) {
- panic(fmt.Sprintf("invalid jump destination (%v) %v", nop, p))
+ jump = func(from *big.Int, to *big.Int) error {
+ nop := context.GetOp(to)
+ if !destinations.Has(to) {
+ return fmt.Errorf("invalid jump destination (%v) %v", nop, to)
}
self.Printf(" ~> %v", to)
- pc = to.Uint64()
+ pc = to
self.Endl()
+
+ return nil
}
)
@@ -100,12 +102,14 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
// The base for all big integer arithmetic
base := new(big.Int)
- step++
// Get the memory location of pc
op = context.GetOp(pc)
self.Printf("(pc) %-3d -o- %-14s (m) %-4d (s) %-4d ", pc, op.String(), mem.Len(), stack.len())
- newMemSize, gas := self.calculateGasAndSize(context, caller, op, statedb, mem, stack)
+ newMemSize, gas, err := self.calculateGasAndSize(context, caller, op, statedb, mem, stack)
+ if err != nil {
+ return nil, err
+ }
self.Printf("(g) %-3v (%v)", gas, context.Gas)
@@ -420,22 +424,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
self.Printf(" => %v", value)
case CALLDATALOAD:
- var (
- offset = stack.pop()
- data = make([]byte, 32)
- lenData = big.NewInt(int64(len(callData)))
- )
-
- if lenData.Cmp(offset) >= 0 {
- length := new(big.Int).Add(offset, common.Big32)
- length = common.BigMin(length, lenData)
-
- copy(data, callData[offset.Int64():length.Int64()])
- }
+ data := getData(callData, stack.pop(), common.Big32)
self.Printf(" => 0x%x", data)
- stack.push(common.BigD(data))
+ stack.push(common.Bytes2Big(data))
case CALLDATASIZE:
l := int64(len(callData))
stack.push(big.NewInt(l))
@@ -534,13 +527,11 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
// 0x50 range
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
- a := uint64(op - PUSH1 + 1)
- byts := context.GetRangeValue(pc+1, a)
+ a := big.NewInt(int64(op - PUSH1 + 1))
+ byts := getData(code, new(big.Int).Add(pc, big.NewInt(1)), a)
// push value to stack
- stack.push(common.BigD(byts))
- pc += a
-
- step += int(op) - int(PUSH1) + 1
+ stack.push(common.Bytes2Big(byts))
+ pc.Add(pc, a)
self.Printf(" => 0x%x", byts)
case POP:
@@ -600,14 +591,18 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
self.Printf(" {0x%x : 0x%x}", loc, val.Bytes())
case JUMP:
- jump(pc, stack.pop())
+ if err := jump(pc, stack.pop()); err != nil {
+ return nil, err
+ }
continue
case JUMPI:
pos, cond := stack.pop(), stack.pop()
if cond.Cmp(common.BigTrue) >= 0 {
- jump(pc, pos)
+ if err := jump(pc, pos); err != nil {
+ return nil, err
+ }
continue
}
@@ -616,7 +611,8 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
case JUMPDEST:
case PC:
- stack.push(big.NewInt(int64(pc)))
+ //stack.push(big.NewInt(int64(pc)))
+ stack.push(pc)
case MSIZE:
stack.push(big.NewInt(int64(mem.Len())))
case GAS:
@@ -642,7 +638,6 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
self.Printf(" (*) 0x0 %v", suberr)
} else {
-
// gas < len(ret) * CreateDataGas == NO_CODE
dataGas := big.NewInt(int64(len(ret)))
dataGas.Mul(dataGas, GasCreateByte)
@@ -720,37 +715,47 @@ func (self *Vm) Run(context *Context, callData []byte) (ret []byte, err error) {
default:
self.Printf("(pc) %-3v Invalid opcode %x\n", pc, op).Endl()
- panic(fmt.Errorf("Invalid opcode %x", op))
+ return nil, fmt.Errorf("Invalid opcode %x", op)
}
- pc++
+ pc.Add(pc, One)
self.Endl()
}
}
-func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int) {
+func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
var (
gas = new(big.Int)
newMemSize *big.Int = new(big.Int)
)
- baseCheck(op, stack, gas)
+ err := baseCheck(op, stack, gas)
+ if err != nil {
+ return nil, nil, err
+ }
// stack Check, memory resize & gas phase
switch op {
- case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
- gas.Set(GasFastestStep)
case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
n := int(op - SWAP1 + 2)
- stack.require(n)
+ err := stack.require(n)
+ if err != nil {
+ return nil, nil, err
+ }
gas.Set(GasFastestStep)
case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
n := int(op - DUP1 + 1)
- stack.require(n)
+ err := stack.require(n)
+ if err != nil {
+ return nil, nil, err
+ }
gas.Set(GasFastestStep)
case LOG0, LOG1, LOG2, LOG3, LOG4:
n := int(op - LOG0)
- stack.require(n + 2)
+ err := stack.require(n + 2)
+ if err != nil {
+ return nil, nil, err
+ }
mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1]
@@ -762,7 +767,10 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
case EXP:
gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(len(stack.data[stack.len()-2].Bytes()))), GasExpByte))
case SSTORE:
- stack.require(2)
+ err := stack.require(2)
+ if err != nil {
+ return nil, nil, err
+ }
var g *big.Int
y, x := stack.data[stack.len()-2], stack.data[stack.len()-1]
@@ -853,7 +861,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
}
}
- return newMemSize, gas
+ return newMemSize, gas, nil
}
func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context *Context) (ret []byte, err error) {
@@ -869,7 +877,7 @@ func (self *Vm) RunPrecompiled(p *PrecompiledAccount, callData []byte, context *
tmp := new(big.Int).Set(context.Gas)
- panic(OOG(gas, tmp).Error())
+ return nil, OOG(gas, tmp)
}
}
diff --git a/core/vm_env.go b/core/vm_env.go
index 52e8b20a9..6a604fccd 100644
--- a/core/vm_env.go
+++ b/core/vm_env.go
@@ -54,21 +54,17 @@ func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount)
}
-func (self *VMEnv) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *Execution {
- return NewExecution(self, addr, data, gas, price, value)
-}
-
func (self *VMEnv) Call(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
- exe := self.vm(&addr, data, gas, price, value)
+ exe := NewExecution(self, &addr, data, gas, price, value)
return exe.Call(addr, me)
}
func (self *VMEnv) CallCode(me vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
maddr := me.Address()
- exe := self.vm(&maddr, data, gas, price, value)
+ exe := NewExecution(self, &maddr, data, gas, price, value)
return exe.Call(addr, me)
}
func (self *VMEnv) Create(me vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) {
- exe := self.vm(nil, data, gas, price, value)
+ exe := NewExecution(self, nil, data, gas, price, value)
return exe.Create(me)
}