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/helper/common.go | 11 --- tests/helper/init.go | 16 ---- tests/helper/readers.go | 42 --------- tests/helper/trie.go | 31 ------- tests/helper/vm.go | 220 ------------------------------------------------ tests/init.go | 53 ++++++++++++ tests/vm.go | 220 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/vm_test_util.go | 15 ++-- 8 files changed, 280 insertions(+), 328 deletions(-) delete mode 100644 tests/helper/common.go delete mode 100644 tests/helper/init.go delete mode 100644 tests/helper/readers.go delete mode 100644 tests/helper/trie.go delete mode 100644 tests/helper/vm.go create mode 100644 tests/init.go create mode 100644 tests/vm.go diff --git a/tests/helper/common.go b/tests/helper/common.go deleted file mode 100644 index 21ea2261f..000000000 --- a/tests/helper/common.go +++ /dev/null @@ -1,11 +0,0 @@ -package helper - -import "github.com/ethereum/go-ethereum/common" - -func FromHex(h string) []byte { - if common.IsHex(h) { - h = h[2:] - } - - return common.Hex2Bytes(h) -} diff --git a/tests/helper/init.go b/tests/helper/init.go deleted file mode 100644 index 73d563e02..000000000 --- a/tests/helper/init.go +++ /dev/null @@ -1,16 +0,0 @@ -package helper - -import ( - "log" - "os" - - logpkg "github.com/ethereum/go-ethereum/logger" -) - -var Logger *logpkg.StdLogSystem -var Log = logpkg.NewLogger("TEST") - -func init() { - Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel) - logpkg.AddLogSystem(Logger) -} diff --git a/tests/helper/readers.go b/tests/helper/readers.go deleted file mode 100644 index 03313aeda..000000000 --- a/tests/helper/readers.go +++ /dev/null @@ -1,42 +0,0 @@ -package helper - -import ( - "encoding/json" - "io" - "io/ioutil" - "net/http" - "os" - "testing" -) - -func readJSON(t *testing.T, reader io.Reader, value interface{}) { - data, err := ioutil.ReadAll(reader) - err = json.Unmarshal(data, &value) - if err != nil { - t.Error(err) - } -} - -func CreateHttpTests(t *testing.T, uri string, value interface{}) { - resp, err := http.Get(uri) - if err != nil { - t.Error(err) - - return - } - defer resp.Body.Close() - - readJSON(t, resp.Body, value) -} - -func CreateFileTests(t *testing.T, fn string, value interface{}) { - file, err := os.Open(fn) - if err != nil { - t.Error(err) - - return - } - defer file.Close() - - readJSON(t, file, value) -} diff --git a/tests/helper/trie.go b/tests/helper/trie.go deleted file mode 100644 index 9e666d333..000000000 --- a/tests/helper/trie.go +++ /dev/null @@ -1,31 +0,0 @@ -package helper - -import "github.com/ethereum/go-ethereum/trie" - -type MemDatabase struct { - db map[string][]byte -} - -func NewMemDatabase() (*MemDatabase, error) { - db := &MemDatabase{db: make(map[string][]byte)} - return db, nil -} -func (db *MemDatabase) Put(key []byte, value []byte) { - db.db[string(key)] = value -} -func (db *MemDatabase) Get(key []byte) ([]byte, error) { - return db.db[string(key)], nil -} -func (db *MemDatabase) Delete(key []byte) error { - delete(db.db, string(key)) - return nil -} -func (db *MemDatabase) Print() {} -func (db *MemDatabase) Close() {} -func (db *MemDatabase) LastKnownTD() []byte { return nil } - -func NewTrie() *trie.Trie { - db, _ := NewMemDatabase() - - return trie.New(nil, db) -} diff --git a/tests/helper/vm.go b/tests/helper/vm.go deleted file mode 100644 index e29a2d8ee..000000000 --- a/tests/helper/vm.go +++ /dev/null @@ -1,220 +0,0 @@ -package helper - -import ( - "errors" - "math/big" - - "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/vm" - "github.com/ethereum/go-ethereum/crypto" -) - -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 - - vmTest bool - logs []vm.StructLog -} - -func NewEnv(state *state.StateDB) *Env { - return &Env{ - state: state, - } -} - -func (self *Env) StructLogs() []vm.StructLog { - return self.logs -} - -func (self *Env) AddStructLog(log vm.StructLog) { - self.logs = append(self.logs, log) -} - -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 = 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 -} - -func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) { - var ( - keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"]))) - data = FromHex(tx["data"]) - gas = common.Big(tx["gasLimit"]) - price = common.Big(tx["gasPrice"]) - value = common.Big(tx["value"]) - nonce = common.Big(tx["nonce"]).Uint64() - caddr = common.HexToAddress(env["currentCoinbase"]) - ) - - var to *common.Address - if len(tx["to"]) > 2 { - t := common.HexToAddress(tx["to"]) - to = &t - } - // Set pre compiled contracts - vm.Precompiled = vm.PrecompiledContracts() - - snapshot := statedb.Copy() - coinbase := statedb.GetOrNewStateObject(caddr) - coinbase.SetGasPool(common.Big(env["currentGasLimit"])) - - message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce) - vmenv := NewEnvFromMap(statedb, env, tx) - vmenv.origin = common.BytesToAddress(keyPair.Address()) - ret, _, err := core.ApplyMessage(vmenv, message, coinbase) - if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) { - statedb.Set(snapshot) - } - statedb.Update() - - 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 } diff --git a/tests/init.go b/tests/init.go new file mode 100644 index 000000000..b487f81c3 --- /dev/null +++ b/tests/init.go @@ -0,0 +1,53 @@ +package tests + +import ( + "encoding/json" + "io" + "io/ioutil" + // "log" + "net/http" + "os" + "testing" + + // logpkg "github.com/ethereum/go-ethereum/logger" +) + +// var Logger *logpkg.StdLogSystem +// var Log = logpkg.NewLogger("TEST") + +// func init() { +// Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel) +// logpkg.AddLogSystem(Logger) +// } + +func readJSON(t *testing.T, reader io.Reader, value interface{}) { + data, err := ioutil.ReadAll(reader) + err = json.Unmarshal(data, &value) + if err != nil { + t.Error(err) + } +} + +func CreateHttpTests(t *testing.T, uri string, value interface{}) { + resp, err := http.Get(uri) + if err != nil { + t.Error(err) + + return + } + defer resp.Body.Close() + + readJSON(t, resp.Body, value) +} + +func CreateFileTests(t *testing.T, fn string, value interface{}) { + file, err := os.Open(fn) + if err != nil { + t.Error(err) + + return + } + defer file.Close() + + readJSON(t, file, value) +} diff --git a/tests/vm.go b/tests/vm.go new file mode 100644 index 000000000..52e498ccc --- /dev/null +++ b/tests/vm.go @@ -0,0 +1,220 @@ +package tests + +import ( + "errors" + "math/big" + + "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/vm" + "github.com/ethereum/go-ethereum/crypto" +) + +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 + + vmTest bool + logs []vm.StructLog +} + +func NewEnv(state *state.StateDB) *Env { + return &Env{ + state: state, + } +} + +func (self *Env) StructLogs() []vm.StructLog { + return self.logs +} + +func (self *Env) AddStructLog(log vm.StructLog) { + self.logs = append(self.logs, log) +} + +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 +} + +func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) { + var ( + keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"]))) + data = common.FromHex(tx["data"]) + gas = common.Big(tx["gasLimit"]) + price = common.Big(tx["gasPrice"]) + value = common.Big(tx["value"]) + nonce = common.Big(tx["nonce"]).Uint64() + caddr = common.HexToAddress(env["currentCoinbase"]) + ) + + var to *common.Address + if len(tx["to"]) > 2 { + t := common.HexToAddress(tx["to"]) + to = &t + } + // Set pre compiled contracts + vm.Precompiled = vm.PrecompiledContracts() + + snapshot := statedb.Copy() + coinbase := statedb.GetOrNewStateObject(caddr) + coinbase.SetGasPool(common.Big(env["currentGasLimit"])) + + message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce) + vmenv := NewEnvFromMap(statedb, env, tx) + vmenv.origin = common.BytesToAddress(keyPair.Address()) + ret, _, err := core.ApplyMessage(vmenv, message, coinbase) + if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) { + statedb.Set(snapshot) + } + statedb.Update() + + 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 } 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