2015-06-10 16:04:56 +00:00
|
|
|
package tests
|
2014-10-14 22:41:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-06-10 18:38:39 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-12-01 23:03:53 +00:00
|
|
|
"math/big"
|
|
|
|
"strconv"
|
2014-10-14 22:41:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-06-10 18:38:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-03-24 14:15:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2015-06-10 19:02:16 +00:00
|
|
|
// "github.com/ethereum/go-ethereum/core/types"
|
2015-06-10 18:38:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-03-17 11:56:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2015-06-10 18:38:39 +00:00
|
|
|
// "github.com/ethereum/go-ethereum/logger"
|
2014-10-14 22:41:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
Balance string
|
|
|
|
Code string
|
|
|
|
Nonce string
|
|
|
|
Storage map[string]string
|
|
|
|
}
|
|
|
|
|
2014-12-03 11:21:12 +00:00
|
|
|
type Log struct {
|
2014-12-10 10:27:10 +00:00
|
|
|
AddressF string `json:"address"`
|
|
|
|
DataF string `json:"data"`
|
|
|
|
TopicsF []string `json:"topics"`
|
|
|
|
BloomF string `json:"bloom"`
|
|
|
|
}
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
|
|
|
|
func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
|
2014-12-10 10:27:10 +00:00
|
|
|
func (self Log) RlpData() interface{} { return nil }
|
|
|
|
func (self Log) Topics() [][]byte {
|
|
|
|
t := make([][]byte, len(self.TopicsF))
|
|
|
|
for i, topic := range self.TopicsF {
|
2015-03-16 10:27:38 +00:00
|
|
|
t[i] = common.Hex2Bytes(topic)
|
2014-12-10 10:27:10 +00:00
|
|
|
}
|
|
|
|
return t
|
2014-12-03 11:21:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
|
2015-03-17 11:56:29 +00:00
|
|
|
obj := state.NewStateObject(common.HexToAddress(addr), db)
|
2015-03-16 10:27:38 +00:00
|
|
|
obj.SetBalance(common.Big(account.Balance))
|
2014-10-14 22:41:00 +00:00
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
if common.IsHex(account.Code) {
|
2014-10-14 22:41:00 +00:00
|
|
|
account.Code = account.Code[2:]
|
|
|
|
}
|
2015-03-16 10:27:38 +00:00
|
|
|
obj.SetCode(common.Hex2Bytes(account.Code))
|
|
|
|
obj.SetNonce(common.Big(account.Nonce).Uint64())
|
2014-10-14 22:41:00 +00:00
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:00:54 +00:00
|
|
|
type VmEnv struct {
|
2014-12-01 23:03:53 +00:00
|
|
|
CurrentCoinbase string
|
|
|
|
CurrentDifficulty string
|
|
|
|
CurrentGasLimit string
|
|
|
|
CurrentNumber string
|
|
|
|
CurrentTimestamp interface{}
|
|
|
|
PreviousHash string
|
|
|
|
}
|
|
|
|
|
2014-10-14 22:41:00 +00:00
|
|
|
type VmTest struct {
|
|
|
|
Callcreates interface{}
|
2014-12-01 23:03:53 +00:00
|
|
|
//Env map[string]string
|
2015-06-10 17:00:54 +00:00
|
|
|
Env VmEnv
|
2015-03-03 11:21:27 +00:00
|
|
|
Exec map[string]string
|
|
|
|
Transaction map[string]string
|
|
|
|
Logs []Log
|
|
|
|
Gas string
|
|
|
|
Out string
|
|
|
|
Post map[string]Account
|
|
|
|
Pre map[string]Account
|
|
|
|
PostStateRoot string
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-18 21:28:16 +00:00
|
|
|
func RunVmTest(p string, t *testing.T) {
|
2015-03-09 10:28:35 +00:00
|
|
|
|
2014-10-14 22:41:00 +00:00
|
|
|
tests := make(map[string]VmTest)
|
2015-06-10 17:00:54 +00:00
|
|
|
CreateFileTests(t, p, &tests)
|
2014-10-14 22:41:00 +00:00
|
|
|
|
|
|
|
for name, test := range tests {
|
2015-04-08 18:45:39 +00:00
|
|
|
/*
|
2015-06-10 16:04:56 +00:00
|
|
|
vm.Debug = true
|
|
|
|
glog.SetV(4)
|
|
|
|
glog.SetToStderr(true)
|
|
|
|
if name != "Call50000_sha256" {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-08 18:45:39 +00:00
|
|
|
*/
|
2015-01-07 12:17:48 +00:00
|
|
|
db, _ := ethdb.NewMemDatabase()
|
2015-03-17 11:56:29 +00:00
|
|
|
statedb := state.New(common.Hash{}, db)
|
2014-10-14 22:41:00 +00:00
|
|
|
for addr, account := range test.Pre {
|
2015-01-07 12:17:48 +00:00
|
|
|
obj := StateObjectFromAccount(db, addr, account)
|
2014-12-03 11:21:12 +00:00
|
|
|
statedb.SetStateObject(obj)
|
2014-12-18 20:58:26 +00:00
|
|
|
for a, v := range account.Storage {
|
2015-06-17 09:24:40 +00:00
|
|
|
obj.SetState(common.HexToHash(a), common.HexToHash(v))
|
2014-12-18 20:58:26 +00:00
|
|
|
}
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 23:03:53 +00:00
|
|
|
// XXX Yeah, yeah...
|
|
|
|
env := make(map[string]string)
|
|
|
|
env["currentCoinbase"] = test.Env.CurrentCoinbase
|
|
|
|
env["currentDifficulty"] = test.Env.CurrentDifficulty
|
|
|
|
env["currentGasLimit"] = test.Env.CurrentGasLimit
|
|
|
|
env["currentNumber"] = test.Env.CurrentNumber
|
|
|
|
env["previousHash"] = test.Env.PreviousHash
|
|
|
|
if n, ok := test.Env.CurrentTimestamp.(float64); ok {
|
|
|
|
env["currentTimestamp"] = strconv.Itoa(int(n))
|
|
|
|
} else {
|
|
|
|
env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-12-03 11:21:12 +00:00
|
|
|
ret []byte
|
|
|
|
gas *big.Int
|
|
|
|
err error
|
|
|
|
logs state.Logs
|
2014-12-01 23:03:53 +00:00
|
|
|
)
|
|
|
|
|
2015-06-10 18:38:39 +00:00
|
|
|
ret, logs, gas, err = RunVm(statedb, env, test.Exec)
|
2014-12-01 23:03:53 +00:00
|
|
|
|
2015-06-10 19:02:16 +00:00
|
|
|
// Compare expectedand actual return
|
2015-06-10 18:38:39 +00:00
|
|
|
rexp := common.FromHex(test.Out)
|
|
|
|
if bytes.Compare(rexp, ret) != 0 {
|
|
|
|
t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 19:02:16 +00:00
|
|
|
// Check gas usage
|
2015-06-10 18:38:39 +00:00
|
|
|
if len(test.Gas) == 0 && err == nil {
|
|
|
|
t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
|
|
|
|
} else {
|
|
|
|
gexp := common.Big(test.Gas)
|
|
|
|
if gexp.Cmp(gas) != 0 {
|
|
|
|
t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
|
2014-12-01 23:03:53 +00:00
|
|
|
}
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 19:02:16 +00:00
|
|
|
// check post state
|
2014-10-14 22:41:00 +00:00
|
|
|
for addr, account := range test.Post {
|
2015-03-17 11:56:29 +00:00
|
|
|
obj := statedb.GetStateObject(common.HexToAddress(addr))
|
2014-12-18 20:58:26 +00:00
|
|
|
if obj == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-14 22:41:00 +00:00
|
|
|
for addr, value := range account.Storage {
|
2015-06-17 09:24:40 +00:00
|
|
|
v := obj.GetState(common.HexToHash(addr))
|
|
|
|
vexp := common.HexToHash(value)
|
2014-10-14 22:41:00 +00:00
|
|
|
|
2015-06-17 09:24:40 +00:00
|
|
|
if v != vexp {
|
|
|
|
t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big())
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-03 11:21:12 +00:00
|
|
|
|
2015-06-10 19:02:16 +00:00
|
|
|
// check logs
|
2014-12-03 11:21:12 +00:00
|
|
|
if len(test.Logs) > 0 {
|
2015-06-10 19:02:16 +00:00
|
|
|
CheckLogs(test.Logs, logs, t)
|
2014-12-03 11:21:12 +00:00
|
|
|
}
|
2015-06-10 18:38:39 +00:00
|
|
|
|
|
|
|
fmt.Println("VM test passed: ", name)
|
|
|
|
|
2015-03-24 14:15:17 +00:00
|
|
|
//fmt.Println(string(statedb.Dump()))
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
2015-06-10 18:38:39 +00:00
|
|
|
// logger.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Env struct {
|
|
|
|
depth int
|
|
|
|
state *state.StateDB
|
|
|
|
skipTransfer bool
|
|
|
|
initial bool
|
|
|
|
Gas *big.Int
|
|
|
|
|
|
|
|
origin common.Address
|
|
|
|
//parent common.Hash
|
|
|
|
coinbase common.Address
|
|
|
|
|
|
|
|
number *big.Int
|
|
|
|
time int64
|
|
|
|
difficulty *big.Int
|
|
|
|
gasLimit *big.Int
|
|
|
|
|
|
|
|
logs state.Logs
|
|
|
|
|
|
|
|
vmTest bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEnv(state *state.StateDB) *Env {
|
|
|
|
return &Env{
|
|
|
|
state: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env {
|
|
|
|
env := NewEnv(state)
|
|
|
|
|
|
|
|
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"])
|
|
|
|
env.gasLimit = common.Big(envValues["currentGasLimit"])
|
|
|
|
env.Gas = new(big.Int)
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
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.state.AddLog(log)
|
|
|
|
}
|
|
|
|
func (self *Env) Depth() int { return self.depth }
|
|
|
|
func (self *Env) SetDepth(i int) { self.depth = i }
|
|
|
|
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
|
|
|
|
if self.skipTransfer {
|
|
|
|
// ugly hack
|
|
|
|
if self.initial {
|
|
|
|
self.initial = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if from.Balance().Cmp(amount) < 0 {
|
|
|
|
return errors.New("Insufficient balance in account")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vm.Transfer(from, to, amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
ret, err := exe.Call(addr, caller)
|
|
|
|
self.Gas = exe.Gas
|
|
|
|
|
|
|
|
return ret, err
|
|
|
|
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
caddr := caller.Address()
|
|
|
|
exe := self.vm(&caddr, data, gas, price, value)
|
|
|
|
return exe.Call(addr, caller)
|
2014-10-14 22:41:00 +00:00
|
|
|
}
|
2015-06-10 18:38:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
nonce := self.state.GetNonce(caller.Address())
|
|
|
|
obj := self.state.GetOrNewStateObject(crypto.CreateAddress(caller.Address(), nonce))
|
|
|
|
|
|
|
|
return nil, nil, obj
|
|
|
|
} else {
|
|
|
|
return exe.Create(caller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) {
|
|
|
|
var (
|
|
|
|
to = common.HexToAddress(exec["address"])
|
|
|
|
from = common.HexToAddress(exec["caller"])
|
|
|
|
data = common.FromHex(exec["data"])
|
|
|
|
gas = common.Big(exec["gas"])
|
|
|
|
price = common.Big(exec["gasPrice"])
|
|
|
|
value = common.Big(exec["value"])
|
|
|
|
)
|
|
|
|
// Reset the pre-compiled contracts for VM tests.
|
|
|
|
vm.Precompiled = make(map[string]*vm.PrecompiledAccount)
|
|
|
|
|
|
|
|
caller := state.GetOrNewStateObject(from)
|
|
|
|
|
|
|
|
vmenv := NewEnvFromMap(state, env, exec)
|
|
|
|
vmenv.vmTest = true
|
|
|
|
vmenv.skipTransfer = true
|
|
|
|
vmenv.initial = true
|
|
|
|
ret, err := vmenv.Call(caller, to, data, gas, price, value)
|
|
|
|
|
|
|
|
return ret, vmenv.state.Logs(), vmenv.Gas, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
from common.Address
|
|
|
|
to *common.Address
|
|
|
|
value, gas, price *big.Int
|
|
|
|
data []byte
|
|
|
|
nonce uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int, nonce uint64) Message {
|
|
|
|
return Message{from, to, value, gas, price, data, nonce}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 self.nonce }
|
|
|
|
func (self Message) Data() []byte { return self.data }
|