From 29b8a0bc5ffa7a674a06a211e1c8bdd1b6ed07b1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 23 Oct 2014 01:01:26 +0200 Subject: Updated the VM & VM tests * Stack Error shouldn't revert to previous state * Updated VM Test tool * Added Transfer method to VM Env --- ethchain/chain_manager.go | 2 +- ethchain/state_transition.go | 23 +++++------------------ ethchain/transaction_pool.go | 2 +- ethchain/vm_env.go | 4 ++++ ethpipe/js_pipe.go | 2 +- ethpipe/vm_env.go | 4 ++++ rpc/packages.go | 2 +- tests/ethtest/main.go | 2 +- tests/helper/vm.go | 5 ++--- tests/vm/gh_test.go | 1 + vm/common.go | 2 +- vm/execution.go | 14 +++++++------- vm/types.go | 2 -- vm/vm.go | 4 ---- vm/vm_debug.go | 5 +---- 15 files changed, 30 insertions(+), 44 deletions(-) diff --git a/ethchain/chain_manager.go b/ethchain/chain_manager.go index 227b02c0a..9f82eae41 100644 --- a/ethchain/chain_manager.go +++ b/ethchain/chain_manager.go @@ -162,7 +162,7 @@ func AddTestNetFunds(block *Block) { } { codedAddr := ethutil.Hex2Bytes(addr) account := block.state.GetAccount(codedAddr) - account.Balance = ethutil.Big("1606938044258990275541962092341162602522202993782792835301376") //ethutil.BigPow(2, 200) + account.SetBalance(ethutil.Big("1606938044258990275541962092341162602522202993782792835301376")) //ethutil.BigPow(2, 200) block.state.UpdateStateObject(account) } } diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index 79321eaac..1e6834729 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -89,8 +89,8 @@ func (self *StateTransition) BuyGas() error { var err error sender := self.Sender() - if sender.Balance.Cmp(self.tx.GasValue()) < 0 { - return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance) + if sender.Balance().Cmp(self.tx.GasValue()) < 0 { + return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance()) } coinbase := self.Coinbase() @@ -171,7 +171,7 @@ func (self *StateTransition) TransitionState() (err error) { return } - if sender.Balance.Cmp(self.value) < 0 { + if sender.Balance().Cmp(self.value) < 0 { return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance) } @@ -243,19 +243,6 @@ func (self *StateTransition) TransitionState() (err error) { return } -func (self *StateTransition) transferValue(sender, receiver *ethstate.StateObject) error { - if sender.Balance.Cmp(self.value) < 0 { - return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Balance) - } - - // Subtract the amount from the senders account - sender.SubAmount(self.value) - // Add the amount to receivers account which should conclude this transaction - receiver.AddAmount(self.value) - - return nil -} - func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context *ethstate.StateObject) (ret []byte, err error) { var ( transactor = self.Sender() @@ -265,9 +252,9 @@ func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context ) //vm := vm.New(env, vm.Type(ethutil.Config.VmType)) - vm := vm.New(env, vm.DebugVmTy) + evm := vm.New(env, vm.DebugVmTy) - ret, _, err = callerClosure.Call(vm, self.tx.Data) + ret, _, err = callerClosure.Call(evm, self.tx.Data) return } diff --git a/ethchain/transaction_pool.go b/ethchain/transaction_pool.go index ff3184582..0ddc4e435 100644 --- a/ethchain/transaction_pool.go +++ b/ethchain/transaction_pool.go @@ -117,7 +117,7 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error { totAmount := new(big.Int).Set(tx.Value) // Make sure there's enough in the sender's account. Having insufficient // funds won't invalidate this transaction but simple ignores it. - if sender.Balance.Cmp(totAmount) < 0 { + if sender.Balance().Cmp(totAmount) < 0 { return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender()) } diff --git a/ethchain/vm_env.go b/ethchain/vm_env.go index 4600878d1..36c9d6002 100644 --- a/ethchain/vm_env.go +++ b/ethchain/vm_env.go @@ -4,6 +4,7 @@ import ( "math/big" "github.com/ethereum/eth-go/ethstate" + "github.com/ethereum/eth-go/vm" ) type VMEnv struct { @@ -30,3 +31,6 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) Value() *big.Int { return self.tx.Value } func (self *VMEnv) State() *ethstate.State { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } +func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { + return vm.Transfer(from, to, amount) +} diff --git a/ethpipe/js_pipe.go b/ethpipe/js_pipe.go index 873373b75..7eb33b4ea 100644 --- a/ethpipe/js_pipe.go +++ b/ethpipe/js_pipe.go @@ -98,7 +98,7 @@ func (self *JSPipe) StorageAt(addr, storageAddr string) string { } func (self *JSPipe) BalanceAt(addr string) string { - return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance.String() + return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance().String() } func (self *JSPipe) TxCountAt(address string) int { diff --git a/ethpipe/vm_env.go b/ethpipe/vm_env.go index 10ce0e561..7ef335800 100644 --- a/ethpipe/vm_env.go +++ b/ethpipe/vm_env.go @@ -5,6 +5,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethstate" + "github.com/ethereum/eth-go/vm" ) type VMEnv struct { @@ -33,3 +34,6 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() } func (self *VMEnv) Value() *big.Int { return self.value } func (self *VMEnv) State() *ethstate.State { return self.state } func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } +func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { + return vm.Transfer(from, to, amount) +} diff --git a/rpc/packages.go b/rpc/packages.go index d8dae003f..3fba7ae4f 100644 --- a/rpc/packages.go +++ b/rpc/packages.go @@ -296,7 +296,7 @@ func (p *EthereumApi) GetBalanceAt(args *GetBalanceArgs, reply *string) error { return err } state := p.pipe.World().SafeGet(ethutil.Hex2Bytes(args.Address)) - *reply = NewSuccessRes(BalanceRes{Balance: state.Balance.String(), Address: args.Address}) + *reply = NewSuccessRes(BalanceRes{Balance: state.Balance().String(), Address: args.Address}) return nil } diff --git a/tests/ethtest/main.go b/tests/ethtest/main.go index 3e85891e4..e19892557 100644 --- a/tests/ethtest/main.go +++ b/tests/ethtest/main.go @@ -22,7 +22,7 @@ type Account struct { func StateObjectFromAccount(addr string, account Account) *ethstate.StateObject { obj := ethstate.NewStateObject(ethutil.Hex2Bytes(addr)) - obj.Balance = ethutil.Big(account.Balance) + obj.SetBalance(ethutil.Big(account.Balance)) if ethutil.IsHex(account.Code) { account.Code = account.Code[2:] diff --git a/tests/helper/vm.go b/tests/helper/vm.go index 06c3d4eca..f70ce48a6 100644 --- a/tests/helper/vm.go +++ b/tests/helper/vm.go @@ -51,17 +51,16 @@ func (self *Env) BlockHash() []byte { return nil } func (self *Env) State() *ethstate.State { return self.state } func (self *Env) GasLimit() *big.Int { return self.gasLimit } func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { - return nil + return vm.Transfer(from, to, amount) } func RunVm(state *ethstate.State, env, exec map[string]string) ([]byte, *big.Int, error) { address := FromHex(exec["address"]) caller := state.GetOrNewStateObject(FromHex(exec["caller"])) - caller.SetBalance(ethutil.Big(exec["value"])) evm := vm.New(NewEnvFromMap(state, env, exec), vm.DebugVmTy) - execution := vm.NewExecution(evm, address, FromHex(exec["data"]), ethutil.Big(exec["gas"]), ethutil.Big(exec["gasPrice"]), ethutil.Big(exec["value"])) + execution.SkipTransfer = true ret, err := execution.Exec(address, caller) return ret, execution.Gas, err diff --git a/tests/vm/gh_test.go b/tests/vm/gh_test.go index 64f279d8d..a86831d9e 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -89,6 +89,7 @@ func TestVMArithmetic(t *testing.T) { } func TestVMSystemOperation(t *testing.T) { + //helper.Logger.SetLogLevel(5) const fn = "../files/vmtests/vmSystemOperationsTest.json" RunVmTest(fn, t) } diff --git a/vm/common.go b/vm/common.go index 6921b38ff..3d9f57290 100644 --- a/vm/common.go +++ b/vm/common.go @@ -39,7 +39,7 @@ var ( S256 = ethutil.S256 ) -const MaxCallDepth = 1024 +const MaxCallDepth = 1025 func calcMemSize(off, l *big.Int) *big.Int { if l.Cmp(ethutil.Big0) == 0 { diff --git a/vm/execution.go b/vm/execution.go index 4c4bd1e3c..bd174d64e 100644 --- a/vm/execution.go +++ b/vm/execution.go @@ -13,6 +13,7 @@ type Execution struct { address, input []byte Gas, price, value *big.Int object *ethstate.StateObject + SkipTransfer bool } func NewExecution(vm VirtualMachine, address, input []byte, gas, gasPrice, value *big.Int) *Execution { @@ -49,17 +50,17 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte, }) from, to := caller.Object(), env.State().GetOrNewStateObject(self.address) - err = env.Transfer(from, to, self.value) + // Skipping transfer is used on testing for the initial call + if !self.SkipTransfer { + err = env.Transfer(from, to, self.value) + } + if err != nil { caller.ReturnGas(self.Gas, self.price) err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance) } else { self.object = to - - //caller.Object().SubAmount(self.value) - //stateObject.AddAmount(self.value) - // Pre-compiled contracts (address.go) 1, 2 & 3. naddr := ethutil.BigD(caddr).Uint64() if p := Precompiled[naddr]; p != nil { @@ -73,14 +74,13 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte, c.exe = self if self.vm.Depth() == MaxCallDepth { - c.UseGas(c.Gas) + c.UseGas(self.Gas) return c.Return(nil), fmt.Errorf("Max call depth exceeded (%d)", MaxCallDepth) } // Executer the closure and get the return value (if any) ret, _, err = c.Call(self.vm, self.input) - msg.Output = ret } } diff --git a/vm/types.go b/vm/types.go index 5fd92052b..6fb9a5c95 100644 --- a/vm/types.go +++ b/vm/types.go @@ -151,7 +151,6 @@ const ( CALLCODE = 0xf3 // 0x70 range - other - LOG = 0xfe // XXX Unofficial SUICIDE = 0xff ) @@ -300,7 +299,6 @@ var opCodeToString = map[OpCode]string{ CALLCODE: "CALLCODE", // 0x70 range - other - LOG: "LOG", SUICIDE: "SUICIDE", } diff --git a/vm/vm.go b/vm/vm.go index b5c7c0e21..1a7a40a36 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -660,8 +660,6 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { // Get the arguments from the memory args := mem.Get(inOffset.Int64(), inSize.Int64()) - //snapshot := self.env.State().Copy() - var executeAddr []byte if op == CALLCODE { executeAddr = closure.Address() @@ -673,8 +671,6 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) { ret, err := msg.Exec(addr.Bytes(), closure) if err != nil { stack.Push(ethutil.BigFalse) - - //self.env.State().Set(snapshot) } else { stack.Push(ethutil.BigTrue) diff --git a/vm/vm_debug.go b/vm/vm_debug.go index acdeb4be9..ba1781109 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -237,10 +237,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) { mem.Resize(newMemSize.Uint64()) switch op { - case LOG: - stack.Print() - mem.Print() - // 0x20 range + // 0x20 range case ADD: require(2) x, y := stack.Popn() -- cgit v1.2.3