@@ -32,6 +32,8 @@ import (
3232 "runtime"
3333 "time"
3434
35+ "github.com/ethereum/go-ethereum/core"
36+ "github.com/ethereum/go-ethereum/core/types"
3537 "github.com/ethereum/go-ethereum/ethdb"
3638 "github.com/ethereum/go-ethereum/ethutil"
3739 "github.com/ethereum/go-ethereum/logger"
4547 loglevel = flag .Int ("log" , 4 , "log level" )
4648 gas = flag .String ("gas" , "1000000" , "gas amount" )
4749 price = flag .String ("price" , "0" , "gas price" )
50+ value = flag .String ("value" , "0" , "tx value" )
4851 dump = flag .Bool ("dump" , false , "dump state after run" )
4952 data = flag .String ("data" , "" , "data" )
5053)
@@ -61,21 +64,25 @@ func main() {
6164
6265 ethutil .ReadConfig ("/tm/evmtest" , "/tmp/evm" , "" )
6366
64- stateObject := state .NewStateObject ([]byte ("evmuser" ))
65- closure := vm .NewClosure (nil , stateObject , stateObject , ethutil .Hex2Bytes (* code ), ethutil .Big (* gas ), ethutil .Big (* price ))
67+ db , _ := ethdb .NewMemDatabase ()
68+ statedb := state .New (trie .New (db , "" ))
69+ sender := statedb .NewStateObject ([]byte ("sender" ))
70+ receiver := statedb .NewStateObject ([]byte ("receiver" ))
71+ receiver .SetCode ([]byte (* code ))
72+
73+ vmenv := NewEnv (statedb , []byte ("evmuser" ), ethutil .Big (* value ))
6674
6775 tstart := time .Now ()
6876
69- env := NewVmEnv ()
70- ret , _ , e := closure .Call (vm .New (env , vm .DebugVmTy ), ethutil .Hex2Bytes (* data ))
77+ ret , e := vmenv .Call (sender , receiver .Address (), ethutil .Hex2Bytes (* data ), ethutil .Big (* gas ), ethutil .Big (* price ), ethutil .Big (* value ))
7178
7279 logger .Flush ()
7380 if e != nil {
7481 perr (e )
7582 }
7683
7784 if * dump {
78- fmt .Println (string (env . state .Dump ()))
85+ fmt .Println (string (statedb .Dump ()))
7986 }
8087
8188 var mem runtime.MemStats
@@ -92,26 +99,65 @@ num gc: %d
9299 fmt .Printf ("%x\n " , ret )
93100}
94101
95- type VmEnv struct {
102+ type VMEnv struct {
96103 state * state.State
104+ block * types.Block
105+
106+ transactor []byte
107+ value * big.Int
108+
109+ depth int
110+ Gas * big.Int
111+ time int64
97112}
98113
99- func NewVmEnv () * VmEnv {
100- db , _ := ethdb .NewMemDatabase ()
101- return & VmEnv {state .New (trie .New (db , "" ))}
114+ func NewEnv (state * state.State , transactor []byte , value * big.Int ) * VMEnv {
115+ return & VMEnv {
116+ state : state ,
117+ transactor : transactor ,
118+ value : value ,
119+ time : time .Now ().Unix (),
120+ }
121+ }
122+
123+ func (self * VMEnv ) State () * state.State { return self .state }
124+ func (self * VMEnv ) Origin () []byte { return self .transactor }
125+ func (self * VMEnv ) BlockNumber () * big.Int { return ethutil .Big0 }
126+ func (self * VMEnv ) PrevHash () []byte { return make ([]byte , 32 ) }
127+ func (self * VMEnv ) Coinbase () []byte { return self .transactor }
128+ func (self * VMEnv ) Time () int64 { return self .time }
129+ func (self * VMEnv ) Difficulty () * big.Int { return ethutil .Big1 }
130+ func (self * VMEnv ) BlockHash () []byte { return make ([]byte , 32 ) }
131+ func (self * VMEnv ) Value () * big.Int { return self .value }
132+ func (self * VMEnv ) GasLimit () * big.Int { return big .NewInt (1000000000 ) }
133+ func (self * VMEnv ) Depth () int { return self .depth }
134+ func (self * VMEnv ) SetDepth (i int ) { self .depth = i }
135+ func (self * VMEnv ) AddLog (log * state.Log ) {
136+ self .state .AddLog (log )
137+ }
138+ func (self * VMEnv ) Transfer (from , to vm.Account , amount * big.Int ) error {
139+ return vm .Transfer (from , to , amount )
140+ }
141+
142+ func (self * VMEnv ) vm (addr , data []byte , gas , price , value * big.Int ) * core.Execution {
143+ evm := vm .New (self , vm .DebugVmTy )
144+
145+ return core .NewExecution (evm , addr , data , gas , price , value )
146+ }
147+
148+ func (self * VMEnv ) Call (caller vm.ClosureRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
149+ exe := self .vm (addr , data , gas , price , value )
150+ ret , err := exe .Call (addr , caller )
151+ self .Gas = exe .Gas
152+
153+ return ret , err
154+ }
155+ func (self * VMEnv ) CallCode (caller vm.ClosureRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
156+ exe := self .vm (caller .Address (), data , gas , price , value )
157+ return exe .Call (addr , caller )
102158}
103159
104- func (VmEnv ) Origin () []byte { return nil }
105- func (VmEnv ) BlockNumber () * big.Int { return nil }
106- func (VmEnv ) BlockHash () []byte { return nil }
107- func (VmEnv ) PrevHash () []byte { return nil }
108- func (VmEnv ) Coinbase () []byte { return nil }
109- func (VmEnv ) Time () int64 { return 0 }
110- func (VmEnv ) GasLimit () * big.Int { return nil }
111- func (VmEnv ) Difficulty () * big.Int { return nil }
112- func (VmEnv ) Value () * big.Int { return nil }
113- func (self * VmEnv ) State () * state.State { return self .state }
114- func (VmEnv ) AddLog (* state.Log ) {}
115- func (VmEnv ) Transfer (from , to vm.Account , amount * big.Int ) error {
116- return nil
160+ func (self * VMEnv ) Create (caller vm.ClosureRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error , vm.ClosureRef ) {
161+ exe := self .vm (addr , data , gas , price , value )
162+ return exe .Create (caller )
117163}
0 commit comments