Updated testing tools

This commit is contained in:
obscuren 2014-12-04 11:30:41 +01:00
parent 491edc16e7
commit 3664cd58e3
2 changed files with 69 additions and 23 deletions

View File

@ -82,7 +82,7 @@ func RunVmTest(js string) (failed int) {
state.SetStateObject(obj) state.SetStateObject(obj)
} }
ret, gas, err := helper.RunVm(state, test.Env, test.Exec) ret, _, gas, err := helper.RunVm(state, test.Env, test.Exec)
// When an error is returned it doesn't always mean the tests fails. // When an error is returned it doesn't always mean the tests fails.
// Have to come up with some conditional failing mechanism. // Have to come up with some conditional failing mechanism.
if err != nil { if err != nil {

View File

@ -32,6 +32,8 @@ import (
"runtime" "runtime"
"time" "time"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
@ -45,6 +47,7 @@ var (
loglevel = flag.Int("log", 4, "log level") loglevel = flag.Int("log", 4, "log level")
gas = flag.String("gas", "1000000", "gas amount") gas = flag.String("gas", "1000000", "gas amount")
price = flag.String("price", "0", "gas price") price = flag.String("price", "0", "gas price")
value = flag.String("value", "0", "tx value")
dump = flag.Bool("dump", false, "dump state after run") dump = flag.Bool("dump", false, "dump state after run")
data = flag.String("data", "", "data") data = flag.String("data", "", "data")
) )
@ -61,13 +64,17 @@ func main() {
ethutil.ReadConfig("/tm/evmtest", "/tmp/evm", "") ethutil.ReadConfig("/tm/evmtest", "/tmp/evm", "")
stateObject := state.NewStateObject([]byte("evmuser")) db, _ := ethdb.NewMemDatabase()
closure := vm.NewClosure(nil, stateObject, stateObject, ethutil.Hex2Bytes(*code), ethutil.Big(*gas), ethutil.Big(*price)) statedb := state.New(trie.New(db, ""))
sender := statedb.NewStateObject([]byte("sender"))
receiver := statedb.NewStateObject([]byte("receiver"))
receiver.SetCode([]byte(*code))
vmenv := NewEnv(statedb, []byte("evmuser"), ethutil.Big(*value))
tstart := time.Now() tstart := time.Now()
env := NewVmEnv() ret, e := vmenv.Call(sender, receiver.Address(), ethutil.Hex2Bytes(*data), ethutil.Big(*gas), ethutil.Big(*price), ethutil.Big(*value))
ret, _, e := closure.Call(vm.New(env, vm.DebugVmTy), ethutil.Hex2Bytes(*data))
logger.Flush() logger.Flush()
if e != nil { if e != nil {
@ -75,7 +82,7 @@ func main() {
} }
if *dump { if *dump {
fmt.Println(string(env.state.Dump())) fmt.Println(string(statedb.Dump()))
} }
var mem runtime.MemStats var mem runtime.MemStats
@ -92,26 +99,65 @@ num gc: %d
fmt.Printf("%x\n", ret) fmt.Printf("%x\n", ret)
} }
type VmEnv struct { type VMEnv struct {
state *state.State state *state.State
block *types.Block
transactor []byte
value *big.Int
depth int
Gas *big.Int
time int64
} }
func NewVmEnv() *VmEnv { func NewEnv(state *state.State, transactor []byte, value *big.Int) *VMEnv {
db, _ := ethdb.NewMemDatabase() return &VMEnv{
return &VmEnv{state.New(trie.New(db, ""))} state: state,
transactor: transactor,
value: value,
time: time.Now().Unix(),
}
} }
func (VmEnv) Origin() []byte { return nil } func (self *VMEnv) State() *state.State { return self.state }
func (VmEnv) BlockNumber() *big.Int { return nil } func (self *VMEnv) Origin() []byte { return self.transactor }
func (VmEnv) BlockHash() []byte { return nil } func (self *VMEnv) BlockNumber() *big.Int { return ethutil.Big0 }
func (VmEnv) PrevHash() []byte { return nil } func (self *VMEnv) PrevHash() []byte { return make([]byte, 32) }
func (VmEnv) Coinbase() []byte { return nil } func (self *VMEnv) Coinbase() []byte { return self.transactor }
func (VmEnv) Time() int64 { return 0 } func (self *VMEnv) Time() int64 { return self.time }
func (VmEnv) GasLimit() *big.Int { return nil } func (self *VMEnv) Difficulty() *big.Int { return ethutil.Big1 }
func (VmEnv) Difficulty() *big.Int { return nil } func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
func (VmEnv) Value() *big.Int { return nil } func (self *VMEnv) Value() *big.Int { return self.value }
func (self *VmEnv) State() *state.State { return self.state } func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) }
func (VmEnv) AddLog(*state.Log) {} func (self *VMEnv) Depth() int { return self.depth }
func (VmEnv) Transfer(from, to vm.Account, amount *big.Int) error { func (self *VMEnv) SetDepth(i int) { self.depth = i }
return nil func (self *VMEnv) AddLog(log *state.Log) {
self.state.AddLog(log)
}
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount)
}
func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution {
evm := vm.New(self, vm.DebugVmTy)
return core.NewExecution(evm, addr, data, gas, price, value)
}
func (self *VMEnv) Call(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(addr, data, gas, price, value)
ret, err := exe.Call(addr, caller)
self.Gas = exe.Gas
return ret, err
}
func (self *VMEnv) CallCode(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) {
exe := self.vm(caller.Address(), data, gas, price, value)
return exe.Call(addr, caller)
}
func (self *VMEnv) Create(caller vm.ClosureRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ClosureRef) {
exe := self.vm(addr, data, gas, price, value)
return exe.Create(caller)
} }