diff options
Diffstat (limited to 'cmd/evm/main.go')
-rw-r--r-- | cmd/evm/main.go | 90 |
1 files changed, 68 insertions, 22 deletions
diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 2b4d47684..fb1be3caa 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -32,6 +32,8 @@ import ( "runtime" "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/ethutil" "github.com/ethereum/go-ethereum/logger" @@ -45,6 +47,7 @@ var ( loglevel = flag.Int("log", 4, "log level") gas = flag.String("gas", "1000000", "gas amount") price = flag.String("price", "0", "gas price") + value = flag.String("value", "0", "tx value") dump = flag.Bool("dump", false, "dump state after run") data = flag.String("data", "", "data") ) @@ -61,13 +64,17 @@ func main() { ethutil.ReadConfig("/tm/evmtest", "/tmp/evm", "") - stateObject := state.NewStateObject([]byte("evmuser")) - closure := vm.NewClosure(nil, stateObject, stateObject, ethutil.Hex2Bytes(*code), ethutil.Big(*gas), ethutil.Big(*price)) + db, _ := ethdb.NewMemDatabase() + 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() - env := NewVmEnv() - ret, _, e := closure.Call(vm.New(env, vm.DebugVmTy), ethutil.Hex2Bytes(*data)) + ret, e := vmenv.Call(sender, receiver.Address(), ethutil.Hex2Bytes(*data), ethutil.Big(*gas), ethutil.Big(*price), ethutil.Big(*value)) logger.Flush() if e != nil { @@ -75,7 +82,7 @@ func main() { } if *dump { - fmt.Println(string(env.state.Dump())) + fmt.Println(string(statedb.Dump())) } var mem runtime.MemStats @@ -92,26 +99,65 @@ num gc: %d fmt.Printf("%x\n", ret) } -type VmEnv struct { +type VMEnv struct { state *state.State + block *types.Block + + transactor []byte + value *big.Int + + depth int + Gas *big.Int + time int64 } -func NewVmEnv() *VmEnv { - db, _ := ethdb.NewMemDatabase() - return &VmEnv{state.New(trie.New(db, ""))} +func NewEnv(state *state.State, transactor []byte, value *big.Int) *VMEnv { + return &VMEnv{ + state: state, + transactor: transactor, + value: value, + time: time.Now().Unix(), + } +} + +func (self *VMEnv) State() *state.State { return self.state } +func (self *VMEnv) Origin() []byte { return self.transactor } +func (self *VMEnv) BlockNumber() *big.Int { return ethutil.Big0 } +func (self *VMEnv) PrevHash() []byte { return make([]byte, 32) } +func (self *VMEnv) Coinbase() []byte { return self.transactor } +func (self *VMEnv) Time() int64 { return self.time } +func (self *VMEnv) Difficulty() *big.Int { return ethutil.Big1 } +func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) } +func (self *VMEnv) Value() *big.Int { return self.value } +func (self *VMEnv) GasLimit() *big.Int { return big.NewInt(1000000000) } +func (self *VMEnv) Depth() int { return self.depth } +func (self *VMEnv) SetDepth(i int) { self.depth = i } +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 (VmEnv) Origin() []byte { return nil } -func (VmEnv) BlockNumber() *big.Int { return nil } -func (VmEnv) BlockHash() []byte { return nil } -func (VmEnv) PrevHash() []byte { return nil } -func (VmEnv) Coinbase() []byte { return nil } -func (VmEnv) Time() int64 { return 0 } -func (VmEnv) GasLimit() *big.Int { return nil } -func (VmEnv) Difficulty() *big.Int { return nil } -func (VmEnv) Value() *big.Int { return nil } -func (self *VmEnv) State() *state.State { return self.state } -func (VmEnv) AddLog(*state.Log) {} -func (VmEnv) Transfer(from, to vm.Account, amount *big.Int) error { - return nil +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) } |