From a67a15528aa5da902a17d49f5dad19db3975032a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 12:04:56 -0400 Subject: Split tests from helper code --- tests/vm_test_util.go | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 tests/vm_test_util.go (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go new file mode 100644 index 000000000..f91070736 --- /dev/null +++ b/tests/vm_test_util.go @@ -0,0 +1,219 @@ +package tests + +import ( + "bytes" + "math/big" + "strconv" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/tests/helper" +) + +type Account struct { + Balance string + Code string + Nonce string + Storage map[string]string +} + +type Log struct { + AddressF string `json:"address"` + DataF string `json:"data"` + TopicsF []string `json:"topics"` + BloomF string `json:"bloom"` +} + +func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) } +func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) } +func (self Log) RlpData() interface{} { return nil } +func (self Log) Topics() [][]byte { + t := make([][]byte, len(self.TopicsF)) + for i, topic := range self.TopicsF { + t[i] = common.Hex2Bytes(topic) + } + return t +} + +func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject { + obj := state.NewStateObject(common.HexToAddress(addr), db) + obj.SetBalance(common.Big(account.Balance)) + + if common.IsHex(account.Code) { + account.Code = account.Code[2:] + } + obj.SetCode(common.Hex2Bytes(account.Code)) + obj.SetNonce(common.Big(account.Nonce).Uint64()) + + return obj +} + +type Env struct { + CurrentCoinbase string + CurrentDifficulty string + CurrentGasLimit string + CurrentNumber string + CurrentTimestamp interface{} + PreviousHash string +} + +type VmTest struct { + Callcreates interface{} + //Env map[string]string + Env Env + Exec map[string]string + Transaction map[string]string + Logs []Log + Gas string + Out string + Post map[string]Account + Pre map[string]Account + PostStateRoot string +} + +func RunVmTest(p string, t *testing.T) { + + tests := make(map[string]VmTest) + helper.CreateFileTests(t, p, &tests) + + for name, test := range tests { + /* + vm.Debug = true + glog.SetV(4) + glog.SetToStderr(true) + if name != "Call50000_sha256" { + continue + } + */ + db, _ := ethdb.NewMemDatabase() + statedb := state.New(common.Hash{}, db) + for addr, account := range test.Pre { + obj := StateObjectFromAccount(db, addr, account) + statedb.SetStateObject(obj) + for a, v := range account.Storage { + obj.SetState(common.HexToHash(a), common.HexToHash(v)) + } + } + + // XXX Yeah, yeah... + env := make(map[string]string) + env["currentCoinbase"] = test.Env.CurrentCoinbase + env["currentDifficulty"] = test.Env.CurrentDifficulty + env["currentGasLimit"] = test.Env.CurrentGasLimit + env["currentNumber"] = test.Env.CurrentNumber + env["previousHash"] = test.Env.PreviousHash + if n, ok := test.Env.CurrentTimestamp.(float64); ok { + env["currentTimestamp"] = strconv.Itoa(int(n)) + } else { + env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) + } + + var ( + ret []byte + gas *big.Int + err error + logs state.Logs + ) + + isVmTest := len(test.Exec) > 0 + if isVmTest { + ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec) + } else { + ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction) + } + + switch name { + // the memory required for these tests (4294967297 bytes) would take too much time. + // on 19 May 2015 decided to skip these tests their output. + case "mload32bitBound_return", "mload32bitBound_return2": + default: + rexp := helper.FromHex(test.Out) + if bytes.Compare(rexp, ret) != 0 { + t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) + } + } + + if isVmTest { + if len(test.Gas) == 0 && err == nil { + t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) + } else { + gexp := common.Big(test.Gas) + if gexp.Cmp(gas) != 0 { + t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) + } + } + } + + for addr, account := range test.Post { + obj := statedb.GetStateObject(common.HexToAddress(addr)) + if obj == nil { + continue + } + + if len(test.Exec) == 0 { + if obj.Balance().Cmp(common.Big(account.Balance)) != 0 { + t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) + } + + if obj.Nonce() != common.String2Big(account.Nonce).Uint64() { + t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce()) + } + + } + + for addr, value := range account.Storage { + v := obj.GetState(common.HexToHash(addr)) + vexp := common.HexToHash(value) + + if v != vexp { + t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) + } + } + } + + if !isVmTest { + statedb.Sync() + //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { + if common.HexToHash(test.PostStateRoot) != statedb.Root() { + t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) + } + } + + if len(test.Logs) > 0 { + if len(test.Logs) != len(logs) { + t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) + } else { + for i, log := range test.Logs { + if common.HexToAddress(log.AddressF) != logs[i].Address { + t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address) + } + + if !bytes.Equal(logs[i].Data, helper.FromHex(log.DataF)) { + t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data) + } + + if len(log.TopicsF) != len(logs[i].Topics) { + t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics) + } else { + for j, topic := range log.TopicsF { + if common.HexToHash(topic) != logs[i].Topics[j] { + t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j]) + } + } + } + genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) + + if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { + t.Errorf("'%s' bloom mismatch", name) + } + } + } + } + //fmt.Println(string(statedb.Dump())) + } + logger.Flush() +} -- cgit v1.2.3 From 1b26d4f220689dac18d560a4c1ecb3b29d99deb0 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 13:00:54 -0400 Subject: Flatten helper directory --- tests/vm_test_util.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index f91070736..cf95db80f 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/tests/helper" ) type Account struct { @@ -52,7 +51,7 @@ func StateObjectFromAccount(db common.Database, addr string, account Account) *s return obj } -type Env struct { +type VmEnv struct { CurrentCoinbase string CurrentDifficulty string CurrentGasLimit string @@ -64,7 +63,7 @@ type Env struct { type VmTest struct { Callcreates interface{} //Env map[string]string - Env Env + Env VmEnv Exec map[string]string Transaction map[string]string Logs []Log @@ -78,7 +77,7 @@ type VmTest struct { func RunVmTest(p string, t *testing.T) { tests := make(map[string]VmTest) - helper.CreateFileTests(t, p, &tests) + CreateFileTests(t, p, &tests) for name, test := range tests { /* @@ -121,9 +120,9 @@ func RunVmTest(p string, t *testing.T) { isVmTest := len(test.Exec) > 0 if isVmTest { - ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec) + ret, logs, gas, err = RunVm(statedb, env, test.Exec) } else { - ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction) + ret, logs, gas, err = RunState(statedb, env, test.Transaction) } switch name { @@ -131,7 +130,7 @@ func RunVmTest(p string, t *testing.T) { // on 19 May 2015 decided to skip these tests their output. case "mload32bitBound_return", "mload32bitBound_return2": default: - rexp := helper.FromHex(test.Out) + rexp := common.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } @@ -192,7 +191,7 @@ func RunVmTest(p string, t *testing.T) { t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address) } - if !bytes.Equal(logs[i].Data, helper.FromHex(log.DataF)) { + if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) { t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data) } -- cgit v1.2.3 From 7c6ef0ddac91564f31ef852fd4ef1e821db17c2e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 14:38:39 -0400 Subject: Separate and identify tests runners --- tests/vm_test_util.go | 230 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 186 insertions(+), 44 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index cf95db80f..3852ba4bf 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -2,15 +2,20 @@ package tests import ( "bytes" + "errors" + "fmt" "math/big" "strconv" "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/logger" + // "github.com/ethereum/go-ethereum/logger" ) type Account struct { @@ -118,32 +123,19 @@ func RunVmTest(p string, t *testing.T) { logs state.Logs ) - isVmTest := len(test.Exec) > 0 - if isVmTest { - ret, logs, gas, err = RunVm(statedb, env, test.Exec) - } else { - ret, logs, gas, err = RunState(statedb, env, test.Transaction) - } + ret, logs, gas, err = RunVm(statedb, env, test.Exec) - switch name { - // the memory required for these tests (4294967297 bytes) would take too much time. - // on 19 May 2015 decided to skip these tests their output. - case "mload32bitBound_return", "mload32bitBound_return2": - default: - rexp := common.FromHex(test.Out) - if bytes.Compare(rexp, ret) != 0 { - t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) - } + rexp := common.FromHex(test.Out) + if bytes.Compare(rexp, ret) != 0 { + t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } - if isVmTest { - if len(test.Gas) == 0 && err == nil { - t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) - } else { - gexp := common.Big(test.Gas) - if gexp.Cmp(gas) != 0 { - t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) - } + if len(test.Gas) == 0 && err == nil { + t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) + } else { + gexp := common.Big(test.Gas) + if gexp.Cmp(gas) != 0 { + t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) } } @@ -153,17 +145,6 @@ func RunVmTest(p string, t *testing.T) { continue } - if len(test.Exec) == 0 { - if obj.Balance().Cmp(common.Big(account.Balance)) != 0 { - t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) - } - - if obj.Nonce() != common.String2Big(account.Nonce).Uint64() { - t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce()) - } - - } - for addr, value := range account.Storage { v := obj.GetState(common.HexToHash(addr)) vexp := common.HexToHash(value) @@ -174,14 +155,6 @@ func RunVmTest(p string, t *testing.T) { } } - if !isVmTest { - statedb.Sync() - //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { - if common.HexToHash(test.PostStateRoot) != statedb.Root() { - t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) - } - } - if len(test.Logs) > 0 { if len(test.Logs) != len(logs) { t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) @@ -212,7 +185,176 @@ func RunVmTest(p string, t *testing.T) { } } } + + fmt.Println("VM test passed: ", name) + //fmt.Println(string(statedb.Dump())) } - logger.Flush() + // logger.Flush() +} + +type Env struct { + depth int + state *state.StateDB + skipTransfer bool + initial bool + Gas *big.Int + + origin common.Address + //parent common.Hash + coinbase common.Address + + number *big.Int + time int64 + difficulty *big.Int + gasLimit *big.Int + + logs state.Logs + + vmTest bool +} + +func NewEnv(state *state.StateDB) *Env { + return &Env{ + state: state, + } +} + +func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env { + env := NewEnv(state) + + env.origin = common.HexToAddress(exeValues["caller"]) + //env.parent = common.Hex2Bytes(envValues["previousHash"]) + env.coinbase = common.HexToAddress(envValues["currentCoinbase"]) + env.number = common.Big(envValues["currentNumber"]) + env.time = common.Big(envValues["currentTimestamp"]).Int64() + env.difficulty = common.Big(envValues["currentDifficulty"]) + env.gasLimit = common.Big(envValues["currentGasLimit"]) + env.Gas = new(big.Int) + + return env +} + +func (self *Env) Origin() common.Address { return self.origin } +func (self *Env) BlockNumber() *big.Int { return self.number } + +//func (self *Env) PrevHash() []byte { return self.parent } +func (self *Env) Coinbase() common.Address { return self.coinbase } +func (self *Env) Time() int64 { return self.time } +func (self *Env) Difficulty() *big.Int { return self.difficulty } +func (self *Env) State() *state.StateDB { return self.state } +func (self *Env) GasLimit() *big.Int { return self.gasLimit } +func (self *Env) VmType() vm.Type { return vm.StdVmTy } +func (self *Env) GetHash(n uint64) common.Hash { + return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String()))) +} +func (self *Env) AddLog(log *state.Log) { + self.state.AddLog(log) +} +func (self *Env) Depth() int { return self.depth } +func (self *Env) SetDepth(i int) { self.depth = i } +func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { + if self.skipTransfer { + // ugly hack + if self.initial { + self.initial = false + return nil + } + + if from.Balance().Cmp(amount) < 0 { + return errors.New("Insufficient balance in account") + } + + return nil + } + return vm.Transfer(from, to, amount) +} + +func (self *Env) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution { + exec := core.NewExecution(self, addr, data, gas, price, value) + + return exec +} + +func (self *Env) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + if self.vmTest && self.depth > 0 { + caller.ReturnGas(gas, price) + + return nil, nil + } + exe := self.vm(&addr, data, gas, price, value) + ret, err := exe.Call(addr, caller) + self.Gas = exe.Gas + + return ret, err + +} +func (self *Env) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { + if self.vmTest && self.depth > 0 { + caller.ReturnGas(gas, price) + + return nil, nil + } + + caddr := caller.Address() + exe := self.vm(&caddr, data, gas, price, value) + return exe.Call(addr, caller) } + +func (self *Env) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { + exe := self.vm(nil, data, gas, price, value) + if self.vmTest { + caller.ReturnGas(gas, price) + + nonce := self.state.GetNonce(caller.Address()) + obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce)) + + return nil, nil, obj + } else { + return exe.Create(caller) + } +} + +func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) { + var ( + to = common.HexToAddress(exec["address"]) + from = common.HexToAddress(exec["caller"]) + data = common.FromHex(exec["data"]) + gas = common.Big(exec["gas"]) + price = common.Big(exec["gasPrice"]) + value = common.Big(exec["value"]) + ) + // Reset the pre-compiled contracts for VM tests. + vm.Precompiled = make(map[string]*vm.PrecompiledAccount) + + caller := state.GetOrNewStateObject(from) + + vmenv := NewEnvFromMap(state, env, exec) + vmenv.vmTest = true + vmenv.skipTransfer = true + vmenv.initial = true + ret, err := vmenv.Call(caller, to, data, gas, price, value) + + return ret, vmenv.state.Logs(), vmenv.Gas, err +} + +type Message struct { + from common.Address + to *common.Address + value, gas, price *big.Int + data []byte + nonce uint64 +} + +func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message { + return Message{from, to, value, gas, price, data, nonce} +} + +func (self Message) Hash() []byte { return nil } +func (self Message) From() (common.Address, error) { return self.from, nil } +func (self Message) To() *common.Address { return self.to } +func (self Message) GasPrice() *big.Int { return self.price } +func (self Message) Gas() *big.Int { return self.gas } +func (self Message) Value() *big.Int { return self.value } +func (self Message) Nonce() uint64 { return self.nonce } +func (self Message) Data() []byte { return self.data } -- cgit v1.2.3 From 24554629b162d20a1f945386a45e3221c58adc2b Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 15:02:16 -0400 Subject: DRY log check --- tests/vm_test_util.go | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 3852ba4bf..6743a0872 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -11,7 +11,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" - "github.com/ethereum/go-ethereum/core/types" + // "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" @@ -125,11 +125,13 @@ func RunVmTest(p string, t *testing.T) { ret, logs, gas, err = RunVm(statedb, env, test.Exec) + // Compare expectedand actual return rexp := common.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } + // Check gas usage if len(test.Gas) == 0 && err == nil { t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) } else { @@ -139,6 +141,7 @@ func RunVmTest(p string, t *testing.T) { } } + // check post state for addr, account := range test.Post { obj := statedb.GetStateObject(common.HexToAddress(addr)) if obj == nil { @@ -155,35 +158,9 @@ func RunVmTest(p string, t *testing.T) { } } + // check logs if len(test.Logs) > 0 { - if len(test.Logs) != len(logs) { - t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) - } else { - for i, log := range test.Logs { - if common.HexToAddress(log.AddressF) != logs[i].Address { - t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address) - } - - if !bytes.Equal(logs[i].Data, common.FromHex(log.DataF)) { - t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data) - } - - if len(log.TopicsF) != len(logs[i].Topics) { - t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics) - } else { - for j, topic := range log.TopicsF { - if common.HexToHash(topic) != logs[i].Topics[j] { - t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j]) - } - } - } - genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) - - if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { - t.Errorf("'%s' bloom mismatch", name) - } - } - } + CheckLogs(test.Logs, logs, t) } fmt.Println("VM test passed: ", name) -- cgit v1.2.3 From c5d6fcbaba545d1078f5411dc67208d5d388222e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 16:10:33 -0400 Subject: Return error up stack instead of passing testing var down --- tests/vm_test_util.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 6743a0872..5d9635afd 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -6,7 +6,7 @@ import ( "fmt" "math/big" "strconv" - "testing" + // "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" @@ -79,10 +79,13 @@ type VmTest struct { PostStateRoot string } -func RunVmTest(p string, t *testing.T) { +func RunVmTest(p string) error { tests := make(map[string]VmTest) - CreateFileTests(t, p, &tests) + err := CreateFileTests(p, &tests) + if err != nil { + return err + } for name, test := range tests { /* @@ -128,16 +131,16 @@ func RunVmTest(p string, t *testing.T) { // Compare expectedand actual return rexp := common.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { - t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) + return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } // Check gas usage if len(test.Gas) == 0 && err == nil { - t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) + return fmt.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) } else { gexp := common.Big(test.Gas) if gexp.Cmp(gas) != 0 { - t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) + return fmt.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) } } @@ -153,7 +156,7 @@ func RunVmTest(p string, t *testing.T) { vexp := common.HexToHash(value) if v != vexp { - t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) + return t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) } } } @@ -168,6 +171,7 @@ func RunVmTest(p string, t *testing.T) { //fmt.Println(string(statedb.Dump())) } // logger.Flush() + return nil } type Env struct { -- cgit v1.2.3 From b6d40a931286b4c998f58ad074db0a692aeace6e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 16:29:42 -0400 Subject: Cleanup/reorg --- tests/vm_test_util.go | 216 +------------------------------------------------- 1 file changed, 4 insertions(+), 212 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 5d9635afd..066217620 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -2,83 +2,16 @@ package tests import ( "bytes" - "errors" "fmt" "math/big" "strconv" - // "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/state" - // "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - // "github.com/ethereum/go-ethereum/logger" ) -type Account struct { - Balance string - Code string - Nonce string - Storage map[string]string -} - -type Log struct { - AddressF string `json:"address"` - DataF string `json:"data"` - TopicsF []string `json:"topics"` - BloomF string `json:"bloom"` -} - -func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) } -func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) } -func (self Log) RlpData() interface{} { return nil } -func (self Log) Topics() [][]byte { - t := make([][]byte, len(self.TopicsF)) - for i, topic := range self.TopicsF { - t[i] = common.Hex2Bytes(topic) - } - return t -} - -func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject { - obj := state.NewStateObject(common.HexToAddress(addr), db) - obj.SetBalance(common.Big(account.Balance)) - - if common.IsHex(account.Code) { - account.Code = account.Code[2:] - } - obj.SetCode(common.Hex2Bytes(account.Code)) - obj.SetNonce(common.Big(account.Nonce).Uint64()) - - return obj -} - -type VmEnv struct { - CurrentCoinbase string - CurrentDifficulty string - CurrentGasLimit string - CurrentNumber string - CurrentTimestamp interface{} - PreviousHash string -} - -type VmTest struct { - Callcreates interface{} - //Env map[string]string - Env VmEnv - Exec map[string]string - Transaction map[string]string - Logs []Log - Gas string - Out string - Post map[string]Account - Pre map[string]Account - PostStateRoot string -} - func RunVmTest(p string) error { tests := make(map[string]VmTest) @@ -163,139 +96,19 @@ func RunVmTest(p string) error { // check logs if len(test.Logs) > 0 { - CheckLogs(test.Logs, logs, t) + lerr := checkLogs(test.Logs, logs) + if lerr != nil { + return fmt.Errorf("'%s' ", name, lerr.Error()) + } } fmt.Println("VM test passed: ", name) //fmt.Println(string(statedb.Dump())) } - // logger.Flush() return nil } -type Env struct { - depth int - state *state.StateDB - skipTransfer bool - initial bool - Gas *big.Int - - origin common.Address - //parent common.Hash - coinbase common.Address - - number *big.Int - time int64 - difficulty *big.Int - gasLimit *big.Int - - logs state.Logs - - vmTest bool -} - -func NewEnv(state *state.StateDB) *Env { - return &Env{ - state: state, - } -} - -func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env { - env := NewEnv(state) - - env.origin = common.HexToAddress(exeValues["caller"]) - //env.parent = common.Hex2Bytes(envValues["previousHash"]) - env.coinbase = common.HexToAddress(envValues["currentCoinbase"]) - env.number = common.Big(envValues["currentNumber"]) - env.time = common.Big(envValues["currentTimestamp"]).Int64() - env.difficulty = common.Big(envValues["currentDifficulty"]) - env.gasLimit = common.Big(envValues["currentGasLimit"]) - env.Gas = new(big.Int) - - return env -} - -func (self *Env) Origin() common.Address { return self.origin } -func (self *Env) BlockNumber() *big.Int { return self.number } - -//func (self *Env) PrevHash() []byte { return self.parent } -func (self *Env) Coinbase() common.Address { return self.coinbase } -func (self *Env) Time() int64 { return self.time } -func (self *Env) Difficulty() *big.Int { return self.difficulty } -func (self *Env) State() *state.StateDB { return self.state } -func (self *Env) GasLimit() *big.Int { return self.gasLimit } -func (self *Env) VmType() vm.Type { return vm.StdVmTy } -func (self *Env) GetHash(n uint64) common.Hash { - return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String()))) -} -func (self *Env) AddLog(log *state.Log) { - self.state.AddLog(log) -} -func (self *Env) Depth() int { return self.depth } -func (self *Env) SetDepth(i int) { self.depth = i } -func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { - if self.skipTransfer { - // ugly hack - if self.initial { - self.initial = false - return nil - } - - if from.Balance().Cmp(amount) < 0 { - return errors.New("Insufficient balance in account") - } - - return nil - } - return vm.Transfer(from, to, amount) -} - -func (self *Env) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution { - exec := core.NewExecution(self, addr, data, gas, price, value) - - return exec -} - -func (self *Env) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { - if self.vmTest && self.depth > 0 { - caller.ReturnGas(gas, price) - - return nil, nil - } - exe := self.vm(&addr, data, gas, price, value) - ret, err := exe.Call(addr, caller) - self.Gas = exe.Gas - - return ret, err - -} -func (self *Env) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { - if self.vmTest && self.depth > 0 { - caller.ReturnGas(gas, price) - - return nil, nil - } - - caddr := caller.Address() - exe := self.vm(&caddr, data, gas, price, value) - return exe.Call(addr, caller) -} - -func (self *Env) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { - exe := self.vm(nil, data, gas, price, value) - if self.vmTest { - caller.ReturnGas(gas, price) - - nonce := self.state.GetNonce(caller.Address()) - obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce)) - - return nil, nil, obj - } else { - return exe.Create(caller) - } -} - func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) { var ( to = common.HexToAddress(exec["address"]) @@ -318,24 +131,3 @@ func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Log return ret, vmenv.state.Logs(), vmenv.Gas, err } - -type Message struct { - from common.Address - to *common.Address - value, gas, price *big.Int - data []byte - nonce uint64 -} - -func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message { - return Message{from, to, value, gas, price, data, nonce} -} - -func (self Message) Hash() []byte { return nil } -func (self Message) From() (common.Address, error) { return self.from, nil } -func (self Message) To() *common.Address { return self.to } -func (self Message) GasPrice() *big.Int { return self.price } -func (self Message) Gas() *big.Int { return self.gas } -func (self Message) Value() *big.Int { return self.value } -func (self Message) Nonce() uint64 { return self.nonce } -func (self Message) Data() []byte { return self.data } -- cgit v1.2.3 From ac0637c41332de1f49fb0955f4fbe0fb908a77d5 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 17:04:06 -0400 Subject: More consistent test interfaces + test skipping --- tests/vm_test_util.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 066217620..55036ed82 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -13,6 +13,10 @@ import ( ) func RunVmTest(p string) error { + skipTest := make(map[string]bool, len(vmSkipTests)) + for _, name := range vmSkipTests { + skipTest[name] = true + } tests := make(map[string]VmTest) err := CreateFileTests(p, &tests) @@ -21,14 +25,10 @@ func RunVmTest(p string) error { } for name, test := range tests { - /* - vm.Debug = true - glog.SetV(4) - glog.SetToStderr(true) - if name != "Call50000_sha256" { - continue - } - */ + if skipTest[name] { + fmt.Println("Skipping state test", name) + return nil + } db, _ := ethdb.NewMemDatabase() statedb := state.New(common.Hash{}, db) for addr, account := range test.Pre { -- cgit v1.2.3 From 6ff956394a26fe13c774797284220b8231ebf809 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 10 Jun 2015 18:11:30 -0400 Subject: DRY file loading --- tests/vm_test_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 55036ed82..28e0c3f40 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -19,7 +19,7 @@ func RunVmTest(p string) error { } tests := make(map[string]VmTest) - err := CreateFileTests(p, &tests) + err := readTestFile(p, &tests) if err != nil { return err } -- cgit v1.2.3 From c941a39b756783d95526a74374dd2aa4283474fa Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 11 Jun 2015 13:06:56 -0400 Subject: Cleanup logging --- tests/vm_test_util.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 28e0c3f40..4145d1ebf 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/logger/glog" ) func RunVmTest(p string) error { @@ -26,7 +27,7 @@ func RunVmTest(p string) error { for name, test := range tests { if skipTest[name] { - fmt.Println("Skipping state test", name) + glog.Infoln("Skipping VM test", name) return nil } db, _ := ethdb.NewMemDatabase() @@ -102,8 +103,7 @@ func RunVmTest(p string) error { } } - fmt.Println("VM test passed: ", name) - + glog.Infoln("VM test passed: ", name) //fmt.Println(string(statedb.Dump())) } return nil -- cgit v1.2.3 From 01ec4dbb1251751b8bbf62ddb3b3a02dc50d29fc Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sun, 14 Jun 2015 17:55:03 -0400 Subject: Add stdin option --- tests/vm_test_util.go | 167 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 65 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 4145d1ebf..f7f1198ec 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -3,6 +3,7 @@ package tests import ( "bytes" "fmt" + "io" "math/big" "strconv" @@ -13,99 +14,135 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" ) -func RunVmTest(p string) error { - skipTest := make(map[string]bool, len(vmSkipTests)) - for _, name := range vmSkipTests { - skipTest[name] = true +func RunVmTestWithReader(r io.Reader) error { + tests := make(map[string]VmTest) + err := readJson(r, &tests) + if err != nil { + return err } + if err != nil { + return err + } + + if err := runVmTests(tests); err != nil { + return err + } + + return nil +} + +func RunVmTest(p string) error { + tests := make(map[string]VmTest) - err := readTestFile(p, &tests) + err := readJsonFile(p, &tests) if err != nil { return err } + if err := runVmTests(tests); err != nil { + return err + } + + return nil +} + +func runVmTests(tests map[string]VmTest) error { + skipTest := make(map[string]bool, len(VmSkipTests)) + for _, name := range VmSkipTests { + skipTest[name] = true + } + for name, test := range tests { if skipTest[name] { glog.Infoln("Skipping VM test", name) return nil } - db, _ := ethdb.NewMemDatabase() - statedb := state.New(common.Hash{}, db) - for addr, account := range test.Pre { - obj := StateObjectFromAccount(db, addr, account) - statedb.SetStateObject(obj) - for a, v := range account.Storage { - obj.SetState(common.HexToHash(a), common.HexToHash(v)) - } + + if err := runVmTest(test); err != nil { + return fmt.Errorf("%s %s", name, err.Error()) } - // XXX Yeah, yeah... - env := make(map[string]string) - env["currentCoinbase"] = test.Env.CurrentCoinbase - env["currentDifficulty"] = test.Env.CurrentDifficulty - env["currentGasLimit"] = test.Env.CurrentGasLimit - env["currentNumber"] = test.Env.CurrentNumber - env["previousHash"] = test.Env.PreviousHash - if n, ok := test.Env.CurrentTimestamp.(float64); ok { - env["currentTimestamp"] = strconv.Itoa(int(n)) - } else { - env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) + glog.Infoln("VM test passed: ", name) + //fmt.Println(string(statedb.Dump())) + } + return nil +} + +func runVmTest(test VmTest) error { + db, _ := ethdb.NewMemDatabase() + statedb := state.New(common.Hash{}, db) + for addr, account := range test.Pre { + obj := StateObjectFromAccount(db, addr, account) + statedb.SetStateObject(obj) + for a, v := range account.Storage { + obj.SetState(common.HexToHash(a), common.HexToHash(v)) } + } - var ( - ret []byte - gas *big.Int - err error - logs state.Logs - ) + // XXX Yeah, yeah... + env := make(map[string]string) + env["currentCoinbase"] = test.Env.CurrentCoinbase + env["currentDifficulty"] = test.Env.CurrentDifficulty + env["currentGasLimit"] = test.Env.CurrentGasLimit + env["currentNumber"] = test.Env.CurrentNumber + env["previousHash"] = test.Env.PreviousHash + if n, ok := test.Env.CurrentTimestamp.(float64); ok { + env["currentTimestamp"] = strconv.Itoa(int(n)) + } else { + env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) + } - ret, logs, gas, err = RunVm(statedb, env, test.Exec) + var ( + ret []byte + gas *big.Int + err error + logs state.Logs + ) - // Compare expectedand actual return - rexp := common.FromHex(test.Out) - if bytes.Compare(rexp, ret) != 0 { - return fmt.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) - } + ret, logs, gas, err = RunVm(statedb, env, test.Exec) - // Check gas usage - if len(test.Gas) == 0 && err == nil { - return fmt.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) - } else { - gexp := common.Big(test.Gas) - if gexp.Cmp(gas) != 0 { - return fmt.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) - } + // Compare expectedand actual return + rexp := common.FromHex(test.Out) + if bytes.Compare(rexp, ret) != 0 { + return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) + } + + // Check gas usage + if len(test.Gas) == 0 && err == nil { + return fmt.Errorf("gas unspecified, indicating an error. VM returned (incorrectly) successfull") + } else { + gexp := common.Big(test.Gas) + if gexp.Cmp(gas) != 0 { + return fmt.Errorf("gas failed. Expected %v, got %v\n", gexp, gas) } + } - // check post state - for addr, account := range test.Post { - obj := statedb.GetStateObject(common.HexToAddress(addr)) - if obj == nil { - continue - } + // check post state + for addr, account := range test.Post { + obj := statedb.GetStateObject(common.HexToAddress(addr)) + if obj == nil { + continue + } - for addr, value := range account.Storage { - v := obj.GetState(common.HexToHash(addr)) - vexp := common.HexToHash(value) + for addr, value := range account.Storage { + v := obj.GetState(common.HexToHash(addr)) + vexp := common.HexToHash(value) - if v != vexp { - return t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) - } + if v != vexp { + return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.BigD(vexp), v.Big(v)) } } + } - // check logs - if len(test.Logs) > 0 { - lerr := checkLogs(test.Logs, logs) - if lerr != nil { - return fmt.Errorf("'%s' ", name, lerr.Error()) - } + // check logs + if len(test.Logs) > 0 { + lerr := checkLogs(test.Logs, logs) + if lerr != nil { + return lerr } - - glog.Infoln("VM test passed: ", name) - //fmt.Println(string(statedb.Dump())) } + return nil } -- cgit v1.2.3 From baea8e87e5dfdcfb7b2fdcef48fa6038d60a6f9c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 18 Jun 2015 22:27:44 +0200 Subject: Rebase cleanup --- tests/vm_test_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index f7f1198ec..9fccafd8e 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -130,7 +130,7 @@ func runVmTest(test VmTest) error { vexp := common.HexToHash(value) if v != vexp { - return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.BigD(vexp), v.Big(v)) + return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) } } } -- cgit v1.2.3 From 8d3faf69d00420b80d4d737e618b2c7791c10ae9 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Thu, 18 Jun 2015 22:38:17 +0200 Subject: Build error fixes --- tests/vm_test_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index 9fccafd8e..afeedda2a 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -102,7 +102,7 @@ func runVmTest(test VmTest) error { ret, logs, gas, err = RunVm(statedb, env, test.Exec) - // Compare expectedand actual return + // Compare expected and actual return rexp := common.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) -- cgit v1.2.3 From 0743243dce05c38c1f4949e44467d20a22a1f743 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Fri, 19 Jun 2015 11:38:23 +0200 Subject: Add --skip option to CLI Disassociates hardcoded tests to skip when running via CLI. Tests still skipped when running `go test` --- tests/vm_test_util.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'tests/vm_test_util.go') diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go index afeedda2a..286991764 100644 --- a/tests/vm_test_util.go +++ b/tests/vm_test_util.go @@ -14,7 +14,7 @@ import ( "github.com/ethereum/go-ethereum/logger/glog" ) -func RunVmTestWithReader(r io.Reader) error { +func RunVmTestWithReader(r io.Reader, skipTests []string) error { tests := make(map[string]VmTest) err := readJson(r, &tests) if err != nil { @@ -25,14 +25,14 @@ func RunVmTestWithReader(r io.Reader) error { return err } - if err := runVmTests(tests); err != nil { + if err := runVmTests(tests, skipTests); err != nil { return err } return nil } -func RunVmTest(p string) error { +func RunVmTest(p string, skipTests []string) error { tests := make(map[string]VmTest) err := readJsonFile(p, &tests) @@ -40,16 +40,16 @@ func RunVmTest(p string) error { return err } - if err := runVmTests(tests); err != nil { + if err := runVmTests(tests, skipTests); err != nil { return err } return nil } -func runVmTests(tests map[string]VmTest) error { - skipTest := make(map[string]bool, len(VmSkipTests)) - for _, name := range VmSkipTests { +func runVmTests(tests map[string]VmTest, skipTests []string) error { + skipTest := make(map[string]bool, len(skipTests)) + for _, name := range skipTests { skipTest[name] = true } -- cgit v1.2.3