diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-06-11 01:00:54 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-06-19 04:15:07 +0800 |
commit | 1b26d4f220689dac18d560a4c1ecb3b29d99deb0 (patch) | |
tree | 66bed6c2091770fc58476299691204bb30efb7a5 /tests/helper | |
parent | e82100367f794856a8807b5fcfe9f0043902a294 (diff) | |
download | go-tangerine-1b26d4f220689dac18d560a4c1ecb3b29d99deb0.tar.gz go-tangerine-1b26d4f220689dac18d560a4c1ecb3b29d99deb0.tar.zst go-tangerine-1b26d4f220689dac18d560a4c1ecb3b29d99deb0.zip |
Flatten helper directory
Diffstat (limited to 'tests/helper')
-rw-r--r-- | tests/helper/common.go | 11 | ||||
-rw-r--r-- | tests/helper/init.go | 16 | ||||
-rw-r--r-- | tests/helper/readers.go | 42 | ||||
-rw-r--r-- | tests/helper/trie.go | 31 | ||||
-rw-r--r-- | tests/helper/vm.go | 220 |
5 files changed, 0 insertions, 320 deletions
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 } |