44 "errors"
55 "math/big"
66
7+ "github.com/ethereum/go-ethereum/common"
78 "github.com/ethereum/go-ethereum/core"
89 "github.com/ethereum/go-ethereum/crypto"
9- "github.com/ethereum/go-ethereum/common"
1010 "github.com/ethereum/go-ethereum/state"
1111 "github.com/ethereum/go-ethereum/vm"
1212)
@@ -18,9 +18,9 @@ type Env struct {
1818 initial bool
1919 Gas * big.Int
2020
21- origin [] byte
22- parent [] byte
23- coinbase [] byte
21+ origin common. Address
22+ // parent common.Hash
23+ coinbase common. Address
2424
2525 number * big.Int
2626 time int64
@@ -41,9 +41,9 @@ func NewEnv(state *state.StateDB) *Env {
4141func NewEnvFromMap (state * state.StateDB , envValues map [string ]string , exeValues map [string ]string ) * Env {
4242 env := NewEnv (state )
4343
44- env .origin = common .Hex2Bytes (exeValues ["caller" ])
45- env .parent = common .Hex2Bytes (envValues ["previousHash" ])
46- env .coinbase = common .Hex2Bytes (envValues ["currentCoinbase" ])
44+ env .origin = common .HexToAddress (exeValues ["caller" ])
45+ // env.parent = common.Hex2Bytes(envValues["previousHash"])
46+ env .coinbase = common .HexToAddress (envValues ["currentCoinbase" ])
4747 env .number = common .Big (envValues ["currentNumber" ])
4848 env .time = common .Big (envValues ["currentTimestamp" ]).Int64 ()
4949 env .difficulty = common .Big (envValues ["currentDifficulty" ])
@@ -53,17 +53,18 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues
5353 return env
5454}
5555
56- func (self * Env ) Origin () []byte { return self .origin }
57- func (self * Env ) BlockNumber () * big.Int { return self .number }
58- func (self * Env ) PrevHash () []byte { return self .parent }
59- func (self * Env ) Coinbase () []byte { return self .coinbase }
60- func (self * Env ) Time () int64 { return self .time }
61- func (self * Env ) Difficulty () * big.Int { return self .difficulty }
62- func (self * Env ) State () * state.StateDB { return self .state }
63- func (self * Env ) GasLimit () * big.Int { return self .gasLimit }
64- func (self * Env ) VmType () vm.Type { return vm .StdVmTy }
65- func (self * Env ) GetHash (n uint64 ) []byte {
66- return crypto .Sha3 ([]byte (big .NewInt (int64 (n )).String ()))
56+ func (self * Env ) Origin () common.Address { return self .origin }
57+ func (self * Env ) BlockNumber () * big.Int { return self .number }
58+
59+ //func (self *Env) PrevHash() []byte { return self.parent }
60+ func (self * Env ) Coinbase () common.Address { return self .coinbase }
61+ func (self * Env ) Time () int64 { return self .time }
62+ func (self * Env ) Difficulty () * big.Int { return self .difficulty }
63+ func (self * Env ) State () * state.StateDB { return self .state }
64+ func (self * Env ) GasLimit () * big.Int { return self .gasLimit }
65+ func (self * Env ) VmType () vm.Type { return vm .StdVmTy }
66+ func (self * Env ) GetHash (n uint64 ) common.Hash {
67+ return common .BytesToHash (crypto .Sha3 ([]byte (big .NewInt (int64 (n )).String ())))
6768}
6869func (self * Env ) AddLog (log state.Log ) {
6970 self .logs = append (self .logs , log )
@@ -87,37 +88,39 @@ func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
8788 return vm .Transfer (from , to , amount )
8889}
8990
90- func (self * Env ) vm (addr , data []byte , gas , price , value * big.Int ) * core.Execution {
91+ func (self * Env ) vm (addr * common. Address , data []byte , gas , price , value * big.Int ) * core.Execution {
9192 exec := core .NewExecution (self , addr , data , gas , price , value )
9293
9394 return exec
9495}
9596
96- func (self * Env ) Call (caller vm.ContextRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
97+ func (self * Env ) Call (caller vm.ContextRef , addr common. Address , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
9798 if self .vmTest && self .depth > 0 {
9899 caller .ReturnGas (gas , price )
99100
100101 return nil , nil
101102 }
102- exe := self .vm (addr , data , gas , price , value )
103+ exe := self .vm (& addr , data , gas , price , value )
103104 ret , err := exe .Call (addr , caller )
104105 self .Gas = exe .Gas
105106
106107 return ret , err
107108
108109}
109- func (self * Env ) CallCode (caller vm.ContextRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
110+ func (self * Env ) CallCode (caller vm.ContextRef , addr common. Address , data []byte , gas , price , value * big.Int ) ([]byte , error ) {
110111 if self .vmTest && self .depth > 0 {
111112 caller .ReturnGas (gas , price )
112113
113114 return nil , nil
114115 }
115- exe := self .vm (caller .Address (), data , gas , price , value )
116+
117+ caddr := caller .Address ()
118+ exe := self .vm (& caddr , data , gas , price , value )
116119 return exe .Call (addr , caller )
117120}
118121
119- func (self * Env ) Create (caller vm.ContextRef , addr , data []byte , gas , price , value * big.Int ) ([]byte , error , vm.ContextRef ) {
120- exe := self .vm (addr , data , gas , price , value )
122+ func (self * Env ) Create (caller vm.ContextRef , data []byte , gas , price , value * big.Int ) ([]byte , error , vm.ContextRef ) {
123+ exe := self .vm (nil , data , gas , price , value )
121124 if self .vmTest {
122125 caller .ReturnGas (gas , price )
123126
@@ -132,8 +135,8 @@ func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, val
132135
133136func RunVm (state * state.StateDB , env , exec map [string ]string ) ([]byte , state.Logs , * big.Int , error ) {
134137 var (
135- to = FromHex (exec ["address" ])
136- from = FromHex (exec ["caller" ])
138+ to = common . HexToAddress (exec ["address" ])
139+ from = common . HexToAddress (exec ["caller" ])
137140 data = FromHex (exec ["data" ])
138141 gas = common .Big (exec ["gas" ])
139142 price = common .Big (exec ["gasPrice" ])
@@ -156,24 +159,28 @@ func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Log
156159func RunState (statedb * state.StateDB , env , tx map [string ]string ) ([]byte , state.Logs , * big.Int , error ) {
157160 var (
158161 keyPair , _ = crypto .NewKeyPairFromSec ([]byte (common .Hex2Bytes (tx ["secretKey" ])))
159- to = FromHex (tx ["to" ])
160162 data = FromHex (tx ["data" ])
161163 gas = common .Big (tx ["gasLimit" ])
162164 price = common .Big (tx ["gasPrice" ])
163165 value = common .Big (tx ["value" ])
164- caddr = FromHex (env ["currentCoinbase" ])
166+ caddr = common . HexToAddress (env ["currentCoinbase" ])
165167 )
166168
169+ var to * common.Address
170+ if len (tx ["to" ]) > 2 {
171+ t := common .HexToAddress (tx ["to" ])
172+ to = & t
173+ }
167174 // Set pre compiled contracts
168175 vm .Precompiled = vm .PrecompiledContracts ()
169176
170177 snapshot := statedb .Copy ()
171178 coinbase := statedb .GetOrNewStateObject (caddr )
172179 coinbase .SetGasPool (common .Big (env ["currentGasLimit" ]))
173180
174- message := NewMessage (keyPair .Address (), to , data , value , gas , price )
181+ message := NewMessage (common . BytesToAddress ( keyPair .Address () ), to , data , value , gas , price )
175182 vmenv := NewEnvFromMap (statedb , env , tx )
176- vmenv .origin = keyPair .Address ()
183+ vmenv .origin = common . BytesToAddress ( keyPair .Address () )
177184 ret , _ , err := core .ApplyMessage (vmenv , message , coinbase )
178185 if core .IsNonceErr (err ) || core .IsInvalidTxErr (err ) {
179186 statedb .Set (snapshot )
@@ -184,20 +191,21 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.
184191}
185192
186193type Message struct {
187- from , to []byte
194+ from common.Address
195+ to * common.Address
188196 value , gas , price * big.Int
189197 data []byte
190198}
191199
192- func NewMessage (from , to , data []byte , value , gas , price * big.Int ) Message {
200+ func NewMessage (from common. Address , to * common. Address , data []byte , value , gas , price * big.Int ) Message {
193201 return Message {from , to , value , gas , price , data }
194202}
195203
196- func (self Message ) Hash () []byte { return nil }
197- func (self Message ) From () [] byte { return self .from }
198- func (self Message ) To () [] byte { return self .to }
199- func (self Message ) GasPrice () * big.Int { return self .price }
200- func (self Message ) Gas () * big.Int { return self .gas }
201- func (self Message ) Value () * big.Int { return self .value }
202- func (self Message ) Nonce () uint64 { return 0 }
203- func (self Message ) Data () []byte { return self .data }
204+ func (self Message ) Hash () []byte { return nil }
205+ func (self Message ) From () (common. Address , error ) { return self .from , nil }
206+ func (self Message ) To () * common. Address { return self .to }
207+ func (self Message ) GasPrice () * big.Int { return self .price }
208+ func (self Message ) Gas () * big.Int { return self .gas }
209+ func (self Message ) Value () * big.Int { return self .value }
210+ func (self Message ) Nonce () uint64 { return 0 }
211+ func (self Message ) Data () []byte { return self .data }
0 commit comments