updated rnd vm test => state test

This commit is contained in:
obscuren 2015-03-12 01:12:28 +01:00
parent 96496888ed
commit 31a95151c9
2 changed files with 92 additions and 31 deletions

View File

@ -24,10 +24,13 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"os"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/ethdb"
@ -37,6 +40,24 @@ import (
"github.com/ethereum/go-ethereum/tests/helper"
)
type Log struct {
AddressF string `json:"address"`
DataF string `json:"data"`
TopicsF []string `json:"topics"`
BloomF string `json:"bloom"`
}
func (self Log) Address() []byte { return ethutil.Hex2Bytes(self.AddressF) }
func (self Log) Data() []byte { return ethutil.Hex2Bytes(self.DataF) }
func (self Log) RlpData() interface{} { return nil }
func (self Log) Topics() [][]byte {
t := make([][]byte, len(self.TopicsF))
for i, topic := range self.TopicsF {
t[i] = ethutil.Hex2Bytes(topic)
}
return t
}
type Account struct {
Balance string
Code string
@ -59,12 +80,25 @@ func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *
type VmTest struct {
Callcreates interface{}
Env map[string]string
//Env map[string]string
Env Env
Exec map[string]string
Transaction map[string]string
Logs []Log
Gas string
Out string
Post map[string]Account
Pre map[string]Account
PostStateRoot string
}
type Env struct {
CurrentCoinbase string
CurrentDifficulty string
CurrentGasLimit string
CurrentNumber string
CurrentTimestamp interface{}
PreviousHash string
}
func RunVmTest(r io.Reader) (failed int) {
@ -78,49 +112,75 @@ func RunVmTest(r io.Reader) (failed int) {
for name, test := range tests {
db, _ := ethdb.NewMemDatabase()
state := state.New(nil, db)
statedb := state.New(nil, db)
for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account)
state.SetStateObject(obj)
statedb.SetStateObject(obj)
}
ret, _, gas, err := helper.RunVm(state, test.Env, test.Exec)
// When an error is returned it doesn't always mean the tests fails.
// Have to come up with some conditional failing mechanism.
if err != nil {
log.Println(err)
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)
}
ret, logs, _, _ := helper.RunState(statedb, env, test.Transaction)
statedb.Sync()
rexp := helper.FromHex(test.Out)
if bytes.Compare(rexp, ret) != 0 {
log.Printf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
failed = 1
}
if len(test.Gas) == 0 && err == nil {
log.Printf("0 gas indicates error but no error given by VM")
failed = 1
} else {
gexp := ethutil.Big(test.Gas)
if gexp.Cmp(gas) != 0 {
log.Printf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
failed = 1
}
fmt.Printf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
}
for addr, account := range test.Post {
obj := state.GetStateObject(helper.FromHex(addr))
obj := statedb.GetStateObject(helper.FromHex(addr))
if obj == nil {
continue
}
if len(test.Exec) == 0 {
if obj.Balance().Cmp(ethutil.Big(account.Balance)) != 0 {
fmt.Printf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(ethutil.Big(account.Balance), obj.Balance()))
}
}
for addr, value := range account.Storage {
v := obj.GetState(helper.FromHex(addr)).Bytes()
vexp := helper.FromHex(value)
if bytes.Compare(v, vexp) != 0 {
log.Printf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, ethutil.BigD(vexp), ethutil.BigD(v))
failed = 1
fmt.Printf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, ethutil.BigD(vexp), ethutil.BigD(v))
}
}
}
if !bytes.Equal(ethutil.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
fmt.Printf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
}
if len(test.Logs) > 0 {
if len(test.Logs) != len(logs) {
fmt.Printf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
} else {
/*
fmt.Println("A", test.Logs)
fmt.Println("B", logs)
for i, log := range test.Logs {
genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) {
t.Errorf("bloom mismatch")
}
}
*/
}
}
logger.Flush()
}

View File

@ -15,10 +15,11 @@ type stack struct {
}
func (st *stack) push(d *big.Int) {
stackItem := new(big.Int).Set(d)
if len(st.data) > st.ptr {
st.data[st.ptr] = d
st.data[st.ptr] = stackItem
} else {
st.data = append(st.data, d)
st.data = append(st.data, stackItem)
}
st.ptr++
}