From 64c7e1e54510e84298589a0631bd3eb805ecdb27 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 17 Jul 2015 23:09:36 +0200 Subject: [PATCH] core/vm: changed stack behaviour The vm stack no longer keeps track of a stack ptr but instead just let go decide on reuse --- core/chain_manager_test.go | 1 - core/vm/gas.go | 4 ++-- core/vm/stack.go | 37 ++++++++----------------------------- core/vm/vm.go | 12 ++++++------ 4 files changed, 16 insertions(+), 38 deletions(-) diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index 5f8cfd4f4f..a2870e9e88 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -392,7 +392,6 @@ func chm(genesis *types.Block, db common.Database) *ChainManager { bc.futureBlocks, _ = lru.New(100) bc.processor = bproc{} bc.ResetWithGenesisBlock(genesis) - bc.txState = state.ManageState(bc.State()) return bc } diff --git a/core/vm/gas.go b/core/vm/gas.go index 1710ef0c95..996de1cfff 100644 --- a/core/vm/gas.go +++ b/core/vm/gas.go @@ -54,8 +54,8 @@ func baseCheck(op OpCode, stack *stack, gas *big.Int) error { return err } - if r.stackPush > 0 && len(stack.data)-r.stackPop+r.stackPush > int(params.StackLimit.Int64())+1 { - return fmt.Errorf("stack limit reached %d (%d)", len(stack.data), params.StackLimit.Int64()) + if r.stackPush > 0 && stack.len()-r.stackPop+r.stackPush > int(params.StackLimit.Int64()) { + return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64()) } gas.Add(gas, r.gas) diff --git a/core/vm/stack.go b/core/vm/stack.go index 31541f38f0..c6544d4855 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -1,19 +1,3 @@ -// Copyright 2014 The go-ethereum Authors -// This file is part of go-ethereum. -// -// go-ethereum is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// go-ethereum is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with go-ethereum. If not, see . - package vm import ( @@ -27,32 +11,27 @@ func newstack() *stack { type stack struct { data []*big.Int - ptr int } func (st *stack) Data() []*big.Int { - return st.data[:st.ptr] + return st.data } func (st *stack) push(d *big.Int) { // 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 - } else { - st.data = append(st.data, stackItem) - } - st.ptr++ + //stackItem := new(big.Int).Set(d) + //st.data = append(st.data, stackItem) + st.data = append(st.data, d) } func (st *stack) pop() (ret *big.Int) { - st.ptr-- - ret = st.data[st.ptr] + ret = st.data[len(st.data)-1] + st.data = st.data[:len(st.data)-1] return } func (st *stack) len() int { - return st.ptr + return len(st.data) } func (st *stack) swap(n int) { @@ -60,7 +39,7 @@ func (st *stack) swap(n int) { } func (st *stack) dup(n int) { - st.push(st.data[st.len()-n]) + st.push(new(big.Int).Set(st.data[st.len()-n])) } func (st *stack) peek() *big.Int { diff --git a/core/vm/vm.go b/core/vm/vm.go index 9b3fd0009b..20051f6cd0 100644 --- a/core/vm/vm.go +++ b/core/vm/vm.go @@ -376,7 +376,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { addr := common.BigToAddress(stack.pop()) balance := statedb.GetBalance(addr) - stack.push(balance) + stack.push(new(big.Int).Set(balance)) case ORIGIN: origin := self.env.Origin() @@ -388,7 +388,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { stack.push(common.Bytes2Big(caller.Bytes())) case CALLVALUE: - stack.push(value) + stack.push(new(big.Int).Set(value)) case CALLDATALOAD: data := getData(input, stack.pop(), common.Big32) @@ -441,7 +441,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { mem.Set(mOff.Uint64(), l.Uint64(), codeCopy) case GASPRICE: - stack.push(context.Price) + stack.push(new(big.Int).Set(context.Price)) case BLOCKHASH: num := stack.pop() @@ -471,11 +471,11 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { case DIFFICULTY: difficulty := self.env.Difficulty() - stack.push(difficulty) + stack.push(new(big.Int).Set(difficulty)) case GASLIMIT: - stack.push(self.env.GasLimit()) + stack.push(new(big.Int).Set(self.env.GasLimit())) 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: size := uint64(op - PUSH1 + 1) @@ -555,7 +555,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) { case MSIZE: stack.push(big.NewInt(int64(mem.Len()))) case GAS: - stack.push(context.Gas) + stack.push(new(big.Int).Set(context.Gas)) case CREATE: