This commit is contained in:
obscuren 2015-03-17 12:56:29 +01:00
parent 17c5ba2b6b
commit b0ebccb31e
2 changed files with 65 additions and 50 deletions

View File

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

View File

@ -6,11 +6,12 @@ import (
"strconv" "strconv"
"testing" "testing"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/tests/helper" "github.com/ethereum/go-ethereum/tests/helper"
"github.com/ethereum/go-ethereum/vm"
) )
type Account struct { type Account struct {
@ -39,7 +40,7 @@ func (self Log) Topics() [][]byte {
} }
func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject { func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
obj := state.NewStateObject(common.Hex2Bytes(addr), db) obj := state.NewStateObject(common.HexToAddress(addr), db)
obj.SetBalance(common.Big(account.Balance)) obj.SetBalance(common.Big(account.Balance))
if common.IsHex(account.Code) { if common.IsHex(account.Code) {
@ -80,13 +81,18 @@ func RunVmTest(p string, t *testing.T) {
helper.CreateFileTests(t, p, &tests) helper.CreateFileTests(t, p, &tests)
for name, test := range tests { for name, test := range tests {
helper.Logger.SetLogLevel(5)
vm.Debug = true
if name != "TransactionCreateSuicideContract" {
continue
}
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
statedb := state.New(nil, db) statedb := state.New(common.Hash{}, db)
for addr, account := range test.Pre { for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account) obj := StateObjectFromAccount(db, addr, account)
statedb.SetStateObject(obj) statedb.SetStateObject(obj)
for a, v := range account.Storage { for a, v := range account.Storage {
obj.SetState(helper.FromHex(a), common.NewValue(helper.FromHex(v))) obj.SetState(common.HexToHash(a), common.NewValue(helper.FromHex(v)))
} }
} }
@ -134,30 +140,31 @@ func RunVmTest(p string, t *testing.T) {
} }
for addr, account := range test.Post { for addr, account := range test.Post {
obj := statedb.GetStateObject(helper.FromHex(addr)) obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil { if obj == nil {
continue continue
} }
if len(test.Exec) == 0 { if len(test.Exec) == 0 {
if obj.Balance().Cmp(common.Big(account.Balance)) != 0 { if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
} }
} }
for addr, value := range account.Storage { for addr, value := range account.Storage {
v := obj.GetState(helper.FromHex(addr)).Bytes() v := obj.GetState(common.HexToHash(addr)).Bytes()
vexp := helper.FromHex(value) vexp := helper.FromHex(value)
if bytes.Compare(v, vexp) != 0 { if bytes.Compare(v, vexp) != 0 {
t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v)) t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
} }
} }
} }
if !isVmTest { if !isVmTest {
statedb.Sync() statedb.Sync()
if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
if common.HexToHash(test.PostStateRoot) != statedb.Root() {
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
} }
} }