Updated the VM & VM tests

* Stack Error shouldn't revert to previous state
* Updated VM Test tool
* Added Transfer method to VM Env
This commit is contained in:
obscuren 2014-10-23 01:01:26 +02:00
parent 51ecab6967
commit 29b8a0bc5f
15 changed files with 30 additions and 44 deletions

View File

@ -162,7 +162,7 @@ func AddTestNetFunds(block *Block) {
} { } {
codedAddr := ethutil.Hex2Bytes(addr) codedAddr := ethutil.Hex2Bytes(addr)
account := block.state.GetAccount(codedAddr) 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) block.state.UpdateStateObject(account)
} }
} }

View File

@ -89,8 +89,8 @@ func (self *StateTransition) BuyGas() error {
var err error var err error
sender := self.Sender() sender := self.Sender()
if sender.Balance.Cmp(self.tx.GasValue()) < 0 { 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) return fmt.Errorf("Insufficient funds to pre-pay gas. Req %v, has %v", self.tx.GasValue(), sender.Balance())
} }
coinbase := self.Coinbase() coinbase := self.Coinbase()
@ -171,7 +171,7 @@ func (self *StateTransition) TransitionState() (err error) {
return 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) 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 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) { func (self *StateTransition) Eval(msg *ethstate.Message, script []byte, context *ethstate.StateObject) (ret []byte, err error) {
var ( var (
transactor = self.Sender() 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.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 return
} }

View File

@ -117,7 +117,7 @@ func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
totAmount := new(big.Int).Set(tx.Value) totAmount := new(big.Int).Set(tx.Value)
// Make sure there's enough in the sender's account. Having insufficient // Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it. // 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()) return fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
} }

View File

@ -4,6 +4,7 @@ import (
"math/big" "math/big"
"github.com/ethereum/eth-go/ethstate" "github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/vm"
) )
type VMEnv struct { 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) Value() *big.Int { return self.tx.Value }
func (self *VMEnv) State() *ethstate.State { return self.state } func (self *VMEnv) State() *ethstate.State { return self.state }
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } 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)
}

View File

@ -98,7 +98,7 @@ func (self *JSPipe) StorageAt(addr, storageAddr string) string {
} }
func (self *JSPipe) BalanceAt(addr 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 { func (self *JSPipe) TxCountAt(address string) int {

View File

@ -5,6 +5,7 @@ import (
"github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethstate" "github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/vm"
) )
type VMEnv struct { 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) Value() *big.Int { return self.value }
func (self *VMEnv) State() *ethstate.State { return self.state } func (self *VMEnv) State() *ethstate.State { return self.state }
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit } 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)
}

View File

@ -296,7 +296,7 @@ func (p *EthereumApi) GetBalanceAt(args *GetBalanceArgs, reply *string) error {
return err return err
} }
state := p.pipe.World().SafeGet(ethutil.Hex2Bytes(args.Address)) 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 return nil
} }

View File

@ -22,7 +22,7 @@ type Account struct {
func StateObjectFromAccount(addr string, account Account) *ethstate.StateObject { func StateObjectFromAccount(addr string, account Account) *ethstate.StateObject {
obj := ethstate.NewStateObject(ethutil.Hex2Bytes(addr)) obj := ethstate.NewStateObject(ethutil.Hex2Bytes(addr))
obj.Balance = ethutil.Big(account.Balance) obj.SetBalance(ethutil.Big(account.Balance))
if ethutil.IsHex(account.Code) { if ethutil.IsHex(account.Code) {
account.Code = account.Code[2:] account.Code = account.Code[2:]

View File

@ -51,17 +51,16 @@ func (self *Env) BlockHash() []byte { return nil }
func (self *Env) State() *ethstate.State { return self.state } func (self *Env) State() *ethstate.State { return self.state }
func (self *Env) GasLimit() *big.Int { return self.gasLimit } func (self *Env) GasLimit() *big.Int { return self.gasLimit }
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { 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) { func RunVm(state *ethstate.State, env, exec map[string]string) ([]byte, *big.Int, error) {
address := FromHex(exec["address"]) address := FromHex(exec["address"])
caller := state.GetOrNewStateObject(FromHex(exec["caller"])) caller := state.GetOrNewStateObject(FromHex(exec["caller"]))
caller.SetBalance(ethutil.Big(exec["value"]))
evm := vm.New(NewEnvFromMap(state, env, exec), vm.DebugVmTy) 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 := 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) ret, err := execution.Exec(address, caller)
return ret, execution.Gas, err return ret, execution.Gas, err

View File

@ -89,6 +89,7 @@ func TestVMArithmetic(t *testing.T) {
} }
func TestVMSystemOperation(t *testing.T) { func TestVMSystemOperation(t *testing.T) {
//helper.Logger.SetLogLevel(5)
const fn = "../files/vmtests/vmSystemOperationsTest.json" const fn = "../files/vmtests/vmSystemOperationsTest.json"
RunVmTest(fn, t) RunVmTest(fn, t)
} }

View File

@ -39,7 +39,7 @@ var (
S256 = ethutil.S256 S256 = ethutil.S256
) )
const MaxCallDepth = 1024 const MaxCallDepth = 1025
func calcMemSize(off, l *big.Int) *big.Int { func calcMemSize(off, l *big.Int) *big.Int {
if l.Cmp(ethutil.Big0) == 0 { if l.Cmp(ethutil.Big0) == 0 {

View File

@ -13,6 +13,7 @@ type Execution struct {
address, input []byte address, input []byte
Gas, price, value *big.Int Gas, price, value *big.Int
object *ethstate.StateObject object *ethstate.StateObject
SkipTransfer bool
} }
func NewExecution(vm VirtualMachine, address, input []byte, gas, gasPrice, value *big.Int) *Execution { 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) 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 { if err != nil {
caller.ReturnGas(self.Gas, self.price) caller.ReturnGas(self.Gas, self.price)
err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance) err = fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance)
} else { } else {
self.object = to self.object = to
//caller.Object().SubAmount(self.value)
//stateObject.AddAmount(self.value)
// Pre-compiled contracts (address.go) 1, 2 & 3. // Pre-compiled contracts (address.go) 1, 2 & 3.
naddr := ethutil.BigD(caddr).Uint64() naddr := ethutil.BigD(caddr).Uint64()
if p := Precompiled[naddr]; p != nil { if p := Precompiled[naddr]; p != nil {
@ -73,14 +74,13 @@ func (self *Execution) exec(code, caddr []byte, caller ClosureRef) (ret []byte,
c.exe = self c.exe = self
if self.vm.Depth() == MaxCallDepth { 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) return c.Return(nil), fmt.Errorf("Max call depth exceeded (%d)", MaxCallDepth)
} }
// Executer the closure and get the return value (if any) // Executer the closure and get the return value (if any)
ret, _, err = c.Call(self.vm, self.input) ret, _, err = c.Call(self.vm, self.input)
msg.Output = ret msg.Output = ret
} }
} }

View File

@ -151,7 +151,6 @@ const (
CALLCODE = 0xf3 CALLCODE = 0xf3
// 0x70 range - other // 0x70 range - other
LOG = 0xfe // XXX Unofficial
SUICIDE = 0xff SUICIDE = 0xff
) )
@ -300,7 +299,6 @@ var opCodeToString = map[OpCode]string{
CALLCODE: "CALLCODE", CALLCODE: "CALLCODE",
// 0x70 range - other // 0x70 range - other
LOG: "LOG",
SUICIDE: "SUICIDE", SUICIDE: "SUICIDE",
} }

View File

@ -660,8 +660,6 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
// Get the arguments from the memory // Get the arguments from the memory
args := mem.Get(inOffset.Int64(), inSize.Int64()) args := mem.Get(inOffset.Int64(), inSize.Int64())
//snapshot := self.env.State().Copy()
var executeAddr []byte var executeAddr []byte
if op == CALLCODE { if op == CALLCODE {
executeAddr = closure.Address() executeAddr = closure.Address()
@ -673,8 +671,6 @@ func (self *Vm) RunClosure(closure *Closure) (ret []byte, err error) {
ret, err := msg.Exec(addr.Bytes(), closure) ret, err := msg.Exec(addr.Bytes(), closure)
if err != nil { if err != nil {
stack.Push(ethutil.BigFalse) stack.Push(ethutil.BigFalse)
//self.env.State().Set(snapshot)
} else { } else {
stack.Push(ethutil.BigTrue) stack.Push(ethutil.BigTrue)

View File

@ -237,10 +237,7 @@ func (self *DebugVm) RunClosure(closure *Closure) (ret []byte, err error) {
mem.Resize(newMemSize.Uint64()) mem.Resize(newMemSize.Uint64())
switch op { switch op {
case LOG: // 0x20 range
stack.Print()
mem.Print()
// 0x20 range
case ADD: case ADD:
require(2) require(2)
x, y := stack.Popn() x, y := stack.Popn()