diff options
author | Felix Lange <fjl@twurst.com> | 2014-10-23 21:01:27 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2014-10-23 21:01:27 +0800 |
commit | 69baa465ea69ae60eed802445cf0132b9eb69934 (patch) | |
tree | b09da7582b5c4850d4db13aee808f2fef2f97de0 /tests/helper | |
parent | 50fd46924900869e7210217c6a07979b544991c8 (diff) | |
parent | feef194829b07570e91873ed5d1e8cc51e8fa430 (diff) | |
download | go-tangerine-69baa465ea69ae60eed802445cf0132b9eb69934.tar.gz go-tangerine-69baa465ea69ae60eed802445cf0132b9eb69934.tar.zst go-tangerine-69baa465ea69ae60eed802445cf0132b9eb69934.zip |
Merge eth-go repository into go-ethereum
mist, etheruem have been moved to cmd/
Diffstat (limited to 'tests/helper')
-rw-r--r-- | tests/helper/common.go | 11 | ||||
-rw-r--r-- | tests/helper/init.go | 19 | ||||
-rw-r--r-- | tests/helper/readers.go | 42 | ||||
-rw-r--r-- | tests/helper/trie.go | 31 | ||||
-rw-r--r-- | tests/helper/vm.go | 67 |
5 files changed, 170 insertions, 0 deletions
diff --git a/tests/helper/common.go b/tests/helper/common.go new file mode 100644 index 000000000..6a071968d --- /dev/null +++ b/tests/helper/common.go @@ -0,0 +1,11 @@ +package helper + +import "github.com/ethereum/go-ethereum/ethutil" + +func FromHex(h string) []byte { + if ethutil.IsHex(h) { + h = h[2:] + } + + return ethutil.Hex2Bytes(h) +} diff --git a/tests/helper/init.go b/tests/helper/init.go new file mode 100644 index 000000000..896ed5fe0 --- /dev/null +++ b/tests/helper/init.go @@ -0,0 +1,19 @@ +package helper + +import ( + "log" + "os" + + "github.com/ethereum/go-ethereum/ethlog" + "github.com/ethereum/go-ethereum/ethutil" +) + +var Logger ethlog.LogSystem +var Log = ethlog.NewLogger("TEST") + +func init() { + Logger = ethlog.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlog.InfoLevel) + ethlog.AddLogSystem(Logger) + + ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "") +} diff --git a/tests/helper/readers.go b/tests/helper/readers.go new file mode 100644 index 000000000..03313aeda --- /dev/null +++ b/tests/helper/readers.go @@ -0,0 +1,42 @@ +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 new file mode 100644 index 000000000..119bac713 --- /dev/null +++ b/tests/helper/trie.go @@ -0,0 +1,31 @@ +package helper + +import "github.com/ethereum/go-ethereum/ethtrie" + +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() *ethtrie.Trie { + db, _ := NewMemDatabase() + + return ethtrie.New(db, "") +} diff --git a/tests/helper/vm.go b/tests/helper/vm.go new file mode 100644 index 000000000..5660caf64 --- /dev/null +++ b/tests/helper/vm.go @@ -0,0 +1,67 @@ +package helper + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/ethstate" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/vm" +) + +type Env struct { + state *ethstate.State + + origin []byte + parent []byte + coinbase []byte + + number *big.Int + time int64 + difficulty *big.Int + gasLimit *big.Int +} + +func NewEnv(state *ethstate.State) *Env { + return &Env{ + state: state, + } +} + +func NewEnvFromMap(state *ethstate.State, envValues map[string]string, exeValues map[string]string) *Env { + env := NewEnv(state) + + env.origin = ethutil.Hex2Bytes(exeValues["caller"]) + env.parent = ethutil.Hex2Bytes(envValues["previousHash"]) + env.coinbase = ethutil.Hex2Bytes(envValues["currentCoinbase"]) + env.number = ethutil.Big(envValues["currentNumber"]) + env.time = ethutil.Big(envValues["currentTimestamp"]).Int64() + env.difficulty = ethutil.Big(envValues["currentDifficulty"]) + env.gasLimit = ethutil.Big(envValues["currentGasLimit"]) + + return env +} + +func (self *Env) Origin() []byte { return self.origin } +func (self *Env) BlockNumber() *big.Int { return self.number } +func (self *Env) PrevHash() []byte { return self.parent } +func (self *Env) Coinbase() []byte { return self.coinbase } +func (self *Env) Time() int64 { return self.time } +func (self *Env) Difficulty() *big.Int { return self.difficulty } +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 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"])) + + 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 +} |