|
|
|
@ -4,9 +4,9 @@ import ( |
|
|
|
|
"errors" |
|
|
|
|
"math/big" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/core" |
|
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/state" |
|
|
|
|
"github.com/ethereum/go-ethereum/vm" |
|
|
|
|
) |
|
|
|
@ -18,9 +18,9 @@ type Env struct { |
|
|
|
|
initial bool |
|
|
|
|
Gas *big.Int |
|
|
|
|
|
|
|
|
|
origin []byte |
|
|
|
|
parent []byte |
|
|
|
|
coinbase []byte |
|
|
|
|
origin common.Address |
|
|
|
|
//parent common.Hash
|
|
|
|
|
coinbase common.Address |
|
|
|
|
|
|
|
|
|
number *big.Int |
|
|
|
|
time int64 |
|
|
|
@ -41,9 +41,9 @@ func NewEnv(state *state.StateDB) *Env { |
|
|
|
|
func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env { |
|
|
|
|
env := NewEnv(state) |
|
|
|
|
|
|
|
|
|
env.origin = common.Hex2Bytes(exeValues["caller"]) |
|
|
|
|
env.parent = common.Hex2Bytes(envValues["previousHash"]) |
|
|
|
|
env.coinbase = common.Hex2Bytes(envValues["currentCoinbase"]) |
|
|
|
|
env.origin = common.HexToAddress(exeValues["caller"]) |
|
|
|
|
//env.parent = common.Hex2Bytes(envValues["previousHash"])
|
|
|
|
|
env.coinbase = common.HexToAddress(envValues["currentCoinbase"]) |
|
|
|
|
env.number = common.Big(envValues["currentNumber"]) |
|
|
|
|
env.time = common.Big(envValues["currentTimestamp"]).Int64() |
|
|
|
|
env.difficulty = common.Big(envValues["currentDifficulty"]) |
|
|
|
@ -53,17 +53,18 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues |
|
|
|
|
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) State() *state.StateDB { return self.state } |
|
|
|
|
func (self *Env) GasLimit() *big.Int { return self.gasLimit } |
|
|
|
|
func (self *Env) VmType() vm.Type { return vm.StdVmTy } |
|
|
|
|
func (self *Env) GetHash(n uint64) []byte { |
|
|
|
|
return crypto.Sha3([]byte(big.NewInt(int64(n)).String())) |
|
|
|
|
func (self *Env) Origin() common.Address { return self.origin } |
|
|
|
|
func (self *Env) BlockNumber() *big.Int { return self.number } |
|
|
|
|
|
|
|
|
|
//func (self *Env) PrevHash() []byte { return self.parent }
|
|
|
|
|
func (self *Env) Coinbase() common.Address { return self.coinbase } |
|
|
|
|
func (self *Env) Time() int64 { return self.time } |
|
|
|
|
func (self *Env) Difficulty() *big.Int { return self.difficulty } |
|
|
|
|
func (self *Env) State() *state.StateDB { return self.state } |
|
|
|
|
func (self *Env) GasLimit() *big.Int { return self.gasLimit } |
|
|
|
|
func (self *Env) VmType() vm.Type { return vm.StdVmTy } |
|
|
|
|
func (self *Env) GetHash(n uint64) common.Hash { |
|
|
|
|
return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String()))) |
|
|
|
|
} |
|
|
|
|
func (self *Env) AddLog(log state.Log) { |
|
|
|
|
self.logs = append(self.logs, log) |
|
|
|
@ -87,37 +88,39 @@ func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { |
|
|
|
|
return vm.Transfer(from, to, amount) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *Env) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution { |
|
|
|
|
func (self *Env) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution { |
|
|
|
|
exec := core.NewExecution(self, addr, data, gas, price, value) |
|
|
|
|
|
|
|
|
|
return exec |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *Env) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { |
|
|
|
|
func (self *Env) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { |
|
|
|
|
if self.vmTest && self.depth > 0 { |
|
|
|
|
caller.ReturnGas(gas, price) |
|
|
|
|
|
|
|
|
|
return nil, nil |
|
|
|
|
} |
|
|
|
|
exe := self.vm(addr, data, gas, price, value) |
|
|
|
|
exe := self.vm(&addr, data, gas, price, value) |
|
|
|
|
ret, err := exe.Call(addr, caller) |
|
|
|
|
self.Gas = exe.Gas |
|
|
|
|
|
|
|
|
|
return ret, err |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
func (self *Env) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { |
|
|
|
|
func (self *Env) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { |
|
|
|
|
if self.vmTest && self.depth > 0 { |
|
|
|
|
caller.ReturnGas(gas, price) |
|
|
|
|
|
|
|
|
|
return nil, nil |
|
|
|
|
} |
|
|
|
|
exe := self.vm(caller.Address(), data, gas, price, value) |
|
|
|
|
|
|
|
|
|
caddr := caller.Address() |
|
|
|
|
exe := self.vm(&caddr, data, gas, price, value) |
|
|
|
|
return exe.Call(addr, caller) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { |
|
|
|
|
exe := self.vm(addr, data, gas, price, value) |
|
|
|
|
func (self *Env) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { |
|
|
|
|
exe := self.vm(nil, data, gas, price, value) |
|
|
|
|
if self.vmTest { |
|
|
|
|
caller.ReturnGas(gas, price) |
|
|
|
|
|
|
|
|
@ -132,8 +135,8 @@ func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, val |
|
|
|
|
|
|
|
|
|
func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) { |
|
|
|
|
var ( |
|
|
|
|
to = FromHex(exec["address"]) |
|
|
|
|
from = FromHex(exec["caller"]) |
|
|
|
|
to = common.HexToAddress(exec["address"]) |
|
|
|
|
from = common.HexToAddress(exec["caller"]) |
|
|
|
|
data = FromHex(exec["data"]) |
|
|
|
|
gas = common.Big(exec["gas"]) |
|
|
|
|
price = common.Big(exec["gasPrice"]) |
|
|
|
@ -156,14 +159,18 @@ func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Log |
|
|
|
|
func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) { |
|
|
|
|
var ( |
|
|
|
|
keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"]))) |
|
|
|
|
to = FromHex(tx["to"]) |
|
|
|
|
data = FromHex(tx["data"]) |
|
|
|
|
gas = common.Big(tx["gasLimit"]) |
|
|
|
|
price = common.Big(tx["gasPrice"]) |
|
|
|
|
value = common.Big(tx["value"]) |
|
|
|
|
caddr = FromHex(env["currentCoinbase"]) |
|
|
|
|
caddr = common.HexToAddress(env["currentCoinbase"]) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var to *common.Address |
|
|
|
|
if len(tx["to"]) > 2 { |
|
|
|
|
t := common.HexToAddress(tx["to"]) |
|
|
|
|
to = &t |
|
|
|
|
} |
|
|
|
|
// Set pre compiled contracts
|
|
|
|
|
vm.Precompiled = vm.PrecompiledContracts() |
|
|
|
|
|
|
|
|
@ -171,9 +178,9 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state. |
|
|
|
|
coinbase := statedb.GetOrNewStateObject(caddr) |
|
|
|
|
coinbase.SetGasPool(common.Big(env["currentGasLimit"])) |
|
|
|
|
|
|
|
|
|
message := NewMessage(keyPair.Address(), to, data, value, gas, price) |
|
|
|
|
message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price) |
|
|
|
|
vmenv := NewEnvFromMap(statedb, env, tx) |
|
|
|
|
vmenv.origin = keyPair.Address() |
|
|
|
|
vmenv.origin = common.BytesToAddress(keyPair.Address()) |
|
|
|
|
ret, _, err := core.ApplyMessage(vmenv, message, coinbase) |
|
|
|
|
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) { |
|
|
|
|
statedb.Set(snapshot) |
|
|
|
@ -184,20 +191,21 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state. |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type Message struct { |
|
|
|
|
from, to []byte |
|
|
|
|
from common.Address |
|
|
|
|
to *common.Address |
|
|
|
|
value, gas, price *big.Int |
|
|
|
|
data []byte |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewMessage(from, to, data []byte, value, gas, price *big.Int) Message { |
|
|
|
|
func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int) Message { |
|
|
|
|
return Message{from, to, value, gas, price, data} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self Message) Hash() []byte { return nil } |
|
|
|
|
func (self Message) From() []byte { return self.from } |
|
|
|
|
func (self Message) To() []byte { return self.to } |
|
|
|
|
func (self Message) GasPrice() *big.Int { return self.price } |
|
|
|
|
func (self Message) Gas() *big.Int { return self.gas } |
|
|
|
|
func (self Message) Value() *big.Int { return self.value } |
|
|
|
|
func (self Message) Nonce() uint64 { return 0 } |
|
|
|
|
func (self Message) Data() []byte { return self.data } |
|
|
|
|
func (self Message) Hash() []byte { return nil } |
|
|
|
|
func (self Message) From() (common.Address, error) { return self.from, nil } |
|
|
|
|
func (self Message) To() *common.Address { return self.to } |
|
|
|
|
func (self Message) GasPrice() *big.Int { return self.price } |
|
|
|
|
func (self Message) Gas() *big.Int { return self.gas } |
|
|
|
|
func (self Message) Value() *big.Int { return self.value } |
|
|
|
|
func (self Message) Nonce() uint64 { return 0 } |
|
|
|
|
func (self Message) Data() []byte { return self.data } |
|
|
|
|