mirror of https://github.com/ethereum/go-ethereum
commit
9c69c051ba
@ -1,11 +0,0 @@ |
||||
package helper |
||||
|
||||
import "github.com/ethereum/go-ethereum/common" |
||||
|
||||
func FromHex(h string) []byte { |
||||
if common.IsHex(h) { |
||||
h = h[2:] |
||||
} |
||||
|
||||
return common.Hex2Bytes(h) |
||||
} |
@ -1,16 +0,0 @@ |
||||
package helper |
||||
|
||||
import ( |
||||
"log" |
||||
"os" |
||||
|
||||
logpkg "github.com/ethereum/go-ethereum/logger" |
||||
) |
||||
|
||||
var Logger *logpkg.StdLogSystem |
||||
var Log = logpkg.NewLogger("TEST") |
||||
|
||||
func init() { |
||||
Logger = logpkg.NewStdLogSystem(os.Stdout, log.LstdFlags, logpkg.InfoLevel) |
||||
logpkg.AddLogSystem(Logger) |
||||
} |
@ -1,42 +0,0 @@ |
||||
package helper |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"os" |
||||
"testing" |
||||
) |
||||
|
||||
func readJSON(t *testing.T, reader io.Reader, value interface{}) { |
||||
data, err := ioutil.ReadAll(reader) |
||||
err = json.Unmarshal(data, &value) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func CreateHttpTests(t *testing.T, uri string, value interface{}) { |
||||
resp, err := http.Get(uri) |
||||
if err != nil { |
||||
t.Error(err) |
||||
|
||||
return |
||||
} |
||||
defer resp.Body.Close() |
||||
|
||||
readJSON(t, resp.Body, value) |
||||
} |
||||
|
||||
func CreateFileTests(t *testing.T, fn string, value interface{}) { |
||||
file, err := os.Open(fn) |
||||
if err != nil { |
||||
t.Error(err) |
||||
|
||||
return |
||||
} |
||||
defer file.Close() |
||||
|
||||
readJSON(t, file, value) |
||||
} |
@ -1,31 +0,0 @@ |
||||
package helper |
||||
|
||||
import "github.com/ethereum/go-ethereum/trie" |
||||
|
||||
type MemDatabase struct { |
||||
db map[string][]byte |
||||
} |
||||
|
||||
func NewMemDatabase() (*MemDatabase, error) { |
||||
db := &MemDatabase{db: make(map[string][]byte)} |
||||
return db, nil |
||||
} |
||||
func (db *MemDatabase) Put(key []byte, value []byte) { |
||||
db.db[string(key)] = value |
||||
} |
||||
func (db *MemDatabase) Get(key []byte) ([]byte, error) { |
||||
return db.db[string(key)], nil |
||||
} |
||||
func (db *MemDatabase) Delete(key []byte) error { |
||||
delete(db.db, string(key)) |
||||
return nil |
||||
} |
||||
func (db *MemDatabase) Print() {} |
||||
func (db *MemDatabase) Close() {} |
||||
func (db *MemDatabase) LastKnownTD() []byte { return nil } |
||||
|
||||
func NewTrie() *trie.Trie { |
||||
db, _ := NewMemDatabase() |
||||
|
||||
return trie.New(nil, db) |
||||
} |
@ -0,0 +1,82 @@ |
||||
package tests |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
"io" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"os" |
||||
"path/filepath" |
||||
) |
||||
|
||||
var ( |
||||
baseDir = filepath.Join(".", "files") |
||||
blockTestDir = filepath.Join(baseDir, "BlockTests") |
||||
stateTestDir = filepath.Join(baseDir, "StateTests") |
||||
transactionTestDir = filepath.Join(baseDir, "TransactionTests") |
||||
vmTestDir = filepath.Join(baseDir, "VMTests") |
||||
|
||||
BlockSkipTests = []string{"SimpleTx3"} |
||||
TransSkipTests = []string{"TransactionWithHihghNonce256"} |
||||
StateSkipTests = []string{"mload32bitBound_return", "mload32bitBound_return2"} |
||||
VmSkipTests = []string{} |
||||
) |
||||
|
||||
func readJson(reader io.Reader, value interface{}) error { |
||||
data, err := ioutil.ReadAll(reader) |
||||
if err != nil { |
||||
return fmt.Errorf("Error reading JSON file", err.Error()) |
||||
} |
||||
|
||||
if err = json.Unmarshal(data, &value); err != nil { |
||||
if syntaxerr, ok := err.(*json.SyntaxError); ok { |
||||
line := findLine(data, syntaxerr.Offset) |
||||
return fmt.Errorf("JSON syntax error at line %v: %v", line, err) |
||||
} |
||||
return fmt.Errorf("JSON unmarshal error: %v", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func readJsonHttp(uri string, value interface{}) error { |
||||
resp, err := http.Get(uri) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer resp.Body.Close() |
||||
|
||||
err = readJson(resp.Body, value) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func readJsonFile(fn string, value interface{}) error { |
||||
file, err := os.Open(fn) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer file.Close() |
||||
|
||||
err = readJson(file, value) |
||||
if err != nil { |
||||
return fmt.Errorf("%s in file %s", err.Error(), fn) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// findLine returns the line number for the given offset into data.
|
||||
func findLine(data []byte, offset int64) (line int) { |
||||
line = 1 |
||||
for i, r := range string(data) { |
||||
if int64(i) >= offset { |
||||
return |
||||
} |
||||
if r == '\n' { |
||||
line++ |
||||
} |
||||
} |
||||
return |
||||
} |
@ -0,0 +1,134 @@ |
||||
package tests |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
) |
||||
|
||||
func TestStateSystemOperations(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stSystemOperationsTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateExample(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stExample.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStatePreCompiledContracts(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stPreCompiledContracts.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateRecursiveCreate(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stRecursiveCreate.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateSpecial(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stSpecialTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateRefund(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stRefundTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateBlockHash(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stBlockHashTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateInitCode(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stInitCodeTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateLog(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stLogTests.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateTransaction(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stTransactionTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestCallCreateCallCode(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stCallCreateCallCodeTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestMemory(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stMemoryTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestMemoryStress(t *testing.T) { |
||||
if os.Getenv("TEST_VM_COMPLEX") == "" { |
||||
t.Skip() |
||||
} |
||||
fn := filepath.Join(stateTestDir, "stMemoryStressTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestQuadraticComplexity(t *testing.T) { |
||||
if os.Getenv("TEST_VM_COMPLEX") == "" { |
||||
t.Skip() |
||||
} |
||||
fn := filepath.Join(stateTestDir, "stQuadraticComplexityTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestSolidity(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stSolidityTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestWallet(t *testing.T) { |
||||
fn := filepath.Join(stateTestDir, "stWalletTest.json") |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestStateTestsRandom(t *testing.T) { |
||||
fns, _ := filepath.Glob("./files/StateTests/RandomTests/*") |
||||
for _, fn := range fns { |
||||
if err := RunStateTest(fn, StateSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,181 @@ |
||||
package tests |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"io" |
||||
"math/big" |
||||
"strconv" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/core/state" |
||||
"github.com/ethereum/go-ethereum/core/vm" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/logger/glog" |
||||
) |
||||
|
||||
func RunStateTestWithReader(r io.Reader, skipTests []string) error { |
||||
tests := make(map[string]VmTest) |
||||
if err := readJson(r, &tests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := runStateTests(tests, skipTests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func RunStateTest(p string, skipTests []string) error { |
||||
tests := make(map[string]VmTest) |
||||
if err := readJsonFile(p, &tests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := runStateTests(tests, skipTests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
|
||||
} |
||||
|
||||
func runStateTests(tests map[string]VmTest, skipTests []string) error { |
||||
skipTest := make(map[string]bool, len(skipTests)) |
||||
for _, name := range skipTests { |
||||
skipTest[name] = true |
||||
} |
||||
|
||||
for name, test := range tests { |
||||
if skipTest[name] { |
||||
glog.Infoln("Skipping state test", name) |
||||
return nil |
||||
} |
||||
|
||||
if err := runStateTest(test); err != nil { |
||||
return fmt.Errorf("%s: %s\n", name, err.Error()) |
||||
} |
||||
|
||||
glog.Infoln("State test passed: ", name) |
||||
//fmt.Println(string(statedb.Dump()))
|
||||
} |
||||
return nil |
||||
|
||||
} |
||||
|
||||
func runStateTest(test VmTest) error { |
||||
db, _ := ethdb.NewMemDatabase() |
||||
statedb := state.New(common.Hash{}, db) |
||||
for addr, account := range test.Pre { |
||||
obj := StateObjectFromAccount(db, addr, account) |
||||
statedb.SetStateObject(obj) |
||||
for a, v := range account.Storage { |
||||
obj.SetState(common.HexToHash(a), common.HexToHash(v)) |
||||
} |
||||
} |
||||
|
||||
// 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 ( |
||||
ret []byte |
||||
// gas *big.Int
|
||||
// err error
|
||||
logs state.Logs |
||||
) |
||||
|
||||
ret, logs, _, _ = RunState(statedb, env, test.Transaction) |
||||
|
||||
// // Compare expected and actual return
|
||||
rexp := common.FromHex(test.Out) |
||||
if bytes.Compare(rexp, ret) != 0 { |
||||
return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) |
||||
} |
||||
|
||||
// check post state
|
||||
for addr, account := range test.Post { |
||||
obj := statedb.GetStateObject(common.HexToAddress(addr)) |
||||
if obj == nil { |
||||
continue |
||||
} |
||||
|
||||
if obj.Balance().Cmp(common.Big(account.Balance)) != 0 { |
||||
return fmt.Errorf("(%x) balance failed. Expected %v, got %v => %v\n", obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) |
||||
} |
||||
|
||||
if obj.Nonce() != common.String2Big(account.Nonce).Uint64() { |
||||
return fmt.Errorf("(%x) nonce failed. Expected %v, got %v\n", obj.Address().Bytes()[:4], account.Nonce, obj.Nonce()) |
||||
} |
||||
|
||||
for addr, value := range account.Storage { |
||||
v := obj.GetState(common.HexToHash(addr)) |
||||
vexp := common.HexToHash(value) |
||||
|
||||
if v != vexp { |
||||
return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) |
||||
} |
||||
} |
||||
} |
||||
|
||||
statedb.Sync() |
||||
if common.HexToHash(test.PostStateRoot) != statedb.Root() { |
||||
return fmt.Errorf("Post state root error. Expected %s, got %x", test.PostStateRoot, statedb.Root()) |
||||
} |
||||
|
||||
// check logs
|
||||
if len(test.Logs) > 0 { |
||||
if err := checkLogs(test.Logs, logs); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
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"]))) |
||||
data = common.FromHex(tx["data"]) |
||||
gas = common.Big(tx["gasLimit"]) |
||||
price = common.Big(tx["gasPrice"]) |
||||
value = common.Big(tx["value"]) |
||||
nonce = common.Big(tx["nonce"]).Uint64() |
||||
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() |
||||
|
||||
snapshot := statedb.Copy() |
||||
coinbase := statedb.GetOrNewStateObject(caddr) |
||||
coinbase.SetGasPool(common.Big(env["currentGasLimit"])) |
||||
|
||||
message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price, nonce) |
||||
vmenv := NewEnvFromMap(statedb, env, tx) |
||||
vmenv.origin = common.BytesToAddress(keyPair.Address()) |
||||
ret, _, err := core.ApplyMessage(vmenv, message, coinbase) |
||||
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || state.IsGasLimitErr(err) { |
||||
statedb.Set(snapshot) |
||||
} |
||||
statedb.Update() |
||||
|
||||
return ret, vmenv.state.Logs(), vmenv.Gas, err |
||||
} |
@ -1,388 +0,0 @@ |
||||
package vm |
||||
|
||||
import ( |
||||
"bytes" |
||||
"math/big" |
||||
"os" |
||||
"path/filepath" |
||||
"strconv" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/state" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/tests/helper" |
||||
) |
||||
|
||||
type Account struct { |
||||
Balance string |
||||
Code string |
||||
Nonce string |
||||
Storage map[string]string |
||||
} |
||||
|
||||
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 common.Hex2Bytes(self.AddressF) } |
||||
func (self Log) Data() []byte { return common.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] = common.Hex2Bytes(topic) |
||||
} |
||||
return t |
||||
} |
||||
|
||||
func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject { |
||||
obj := state.NewStateObject(common.HexToAddress(addr), db) |
||||
obj.SetBalance(common.Big(account.Balance)) |
||||
|
||||
if common.IsHex(account.Code) { |
||||
account.Code = account.Code[2:] |
||||
} |
||||
obj.SetCode(common.Hex2Bytes(account.Code)) |
||||
obj.SetNonce(common.Big(account.Nonce).Uint64()) |
||||
|
||||
return obj |
||||
} |
||||
|
||||
type Env struct { |
||||
CurrentCoinbase string |
||||
CurrentDifficulty string |
||||
CurrentGasLimit string |
||||
CurrentNumber string |
||||
CurrentTimestamp interface{} |
||||
PreviousHash string |
||||
} |
||||
|
||||
type VmTest struct { |
||||
Callcreates interface{} |
||||
//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 |
||||
} |
||||
|
||||
func RunVmTest(p string, t *testing.T) { |
||||
|
||||
tests := make(map[string]VmTest) |
||||
helper.CreateFileTests(t, p, &tests) |
||||
|
||||
for name, test := range tests { |
||||
/* |
||||
vm.Debug = true |
||||
glog.SetV(4) |
||||
glog.SetToStderr(true) |
||||
if name != "Call50000_sha256" { |
||||
continue |
||||
} |
||||
*/ |
||||
db, _ := ethdb.NewMemDatabase() |
||||
statedb := state.New(common.Hash{}, db) |
||||
for addr, account := range test.Pre { |
||||
obj := StateObjectFromAccount(db, addr, account) |
||||
statedb.SetStateObject(obj) |
||||
for a, v := range account.Storage { |
||||
obj.SetState(common.HexToHash(a), common.HexToHash(v)) |
||||
} |
||||
} |
||||
|
||||
// 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 ( |
||||
ret []byte |
||||
gas *big.Int |
||||
err error |
||||
logs state.Logs |
||||
) |
||||
|
||||
isVmTest := len(test.Exec) > 0 |
||||
if isVmTest { |
||||
ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec) |
||||
} else { |
||||
ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction) |
||||
} |
||||
|
||||
switch name { |
||||
// the memory required for these tests (4294967297 bytes) would take too much time.
|
||||
// on 19 May 2015 decided to skip these tests their output.
|
||||
case "mload32bitBound_return", "mload32bitBound_return2": |
||||
default: |
||||
rexp := helper.FromHex(test.Out) |
||||
if bytes.Compare(rexp, ret) != 0 { |
||||
t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) |
||||
} |
||||
} |
||||
|
||||
if isVmTest { |
||||
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) |
||||
} |
||||
} |
||||
} |
||||
|
||||
for addr, account := range test.Post { |
||||
obj := statedb.GetStateObject(common.HexToAddress(addr)) |
||||
if obj == nil { |
||||
continue |
||||
} |
||||
|
||||
if len(test.Exec) == 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().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) |
||||
} |
||||
|
||||
if obj.Nonce() != common.String2Big(account.Nonce).Uint64() { |
||||
t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce()) |
||||
} |
||||
|
||||
} |
||||
|
||||
for addr, value := range account.Storage { |
||||
v := obj.GetState(common.HexToHash(addr)) |
||||
vexp := common.HexToHash(value) |
||||
|
||||
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()) |
||||
} |
||||
} |
||||
} |
||||
|
||||
if !isVmTest { |
||||
statedb.Sync() |
||||
//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()) |
||||
} |
||||
} |
||||
|
||||
if len(test.Logs) > 0 { |
||||
if len(test.Logs) != len(logs) { |
||||
t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) |
||||
} else { |
||||
for i, log := range test.Logs { |
||||
if common.HexToAddress(log.AddressF) != logs[i].Address { |
||||
t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address) |
||||
} |
||||
|
||||
if !bytes.Equal(logs[i].Data, helper.FromHex(log.DataF)) { |
||||
t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data) |
||||
} |
||||
|
||||
if len(log.TopicsF) != len(logs[i].Topics) { |
||||
t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics) |
||||
} else { |
||||
for j, topic := range log.TopicsF { |
||||
if common.HexToHash(topic) != logs[i].Topics[j] { |
||||
t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j]) |
||||
} |
||||
} |
||||
} |
||||
genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) |
||||
|
||||
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { |
||||
t.Errorf("'%s' bloom mismatch", name) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
//fmt.Println(string(statedb.Dump()))
|
||||
} |
||||
logger.Flush() |
||||
} |
||||
|
||||
// I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
|
||||
func TestVMArithmetic(t *testing.T) { |
||||
const fn = "../files/VMTests/vmArithmeticTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestBitwiseLogicOperation(t *testing.T) { |
||||
const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestBlockInfo(t *testing.T) { |
||||
const fn = "../files/VMTests/vmBlockInfoTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestEnvironmentalInfo(t *testing.T) { |
||||
const fn = "../files/VMTests/vmEnvironmentalInfoTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestFlowOperation(t *testing.T) { |
||||
const fn = "../files/VMTests/vmIOandFlowOperationsTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestLogTest(t *testing.T) { |
||||
const fn = "../files/VMTests/vmLogTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestPerformance(t *testing.T) { |
||||
const fn = "../files/VMTests/vmPerformanceTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestPushDupSwap(t *testing.T) { |
||||
const fn = "../files/VMTests/vmPushDupSwapTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestVMSha3(t *testing.T) { |
||||
const fn = "../files/VMTests/vmSha3Test.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestVm(t *testing.T) { |
||||
const fn = "../files/VMTests/vmtests.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestVmLog(t *testing.T) { |
||||
const fn = "../files/VMTests/vmLogTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestInputLimits(t *testing.T) { |
||||
const fn = "../files/VMTests/vmInputLimits.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestInputLimitsLight(t *testing.T) { |
||||
const fn = "../files/VMTests/vmInputLimitsLight.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateSystemOperations(t *testing.T) { |
||||
const fn = "../files/StateTests/stSystemOperationsTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateExample(t *testing.T) { |
||||
const fn = "../files/StateTests/stExample.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStatePreCompiledContracts(t *testing.T) { |
||||
const fn = "../files/StateTests/stPreCompiledContracts.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateRecursiveCreate(t *testing.T) { |
||||
const fn = "../files/StateTests/stRecursiveCreate.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateSpecial(t *testing.T) { |
||||
const fn = "../files/StateTests/stSpecialTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateRefund(t *testing.T) { |
||||
const fn = "../files/StateTests/stRefundTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateBlockHash(t *testing.T) { |
||||
const fn = "../files/StateTests/stBlockHashTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateInitCode(t *testing.T) { |
||||
const fn = "../files/StateTests/stInitCodeTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateLog(t *testing.T) { |
||||
const fn = "../files/StateTests/stLogTests.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateTransaction(t *testing.T) { |
||||
const fn = "../files/StateTests/stTransactionTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestCallCreateCallCode(t *testing.T) { |
||||
const fn = "../files/StateTests/stCallCreateCallCodeTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestMemory(t *testing.T) { |
||||
const fn = "../files/StateTests/stMemoryTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestMemoryStress(t *testing.T) { |
||||
if os.Getenv("TEST_VM_COMPLEX") == "" { |
||||
t.Skip() |
||||
} |
||||
const fn = "../files/StateTests/stMemoryStressTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestQuadraticComplexity(t *testing.T) { |
||||
if os.Getenv("TEST_VM_COMPLEX") == "" { |
||||
t.Skip() |
||||
} |
||||
const fn = "../files/StateTests/stQuadraticComplexityTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestSolidity(t *testing.T) { |
||||
const fn = "../files/StateTests/stSolidityTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestWallet(t *testing.T) { |
||||
const fn = "../files/StateTests/stWalletTest.json" |
||||
RunVmTest(fn, t) |
||||
} |
||||
|
||||
func TestStateTestsRandom(t *testing.T) { |
||||
fns, _ := filepath.Glob("../files/StateTests/RandomTests/*") |
||||
for _, fn := range fns { |
||||
RunVmTest(fn, t) |
||||
} |
||||
} |
||||
|
||||
func TestVMRandom(t *testing.T) { |
||||
t.Skip() // fucked as of 2015-06-09. unskip once unfucked /Gustav
|
||||
fns, _ := filepath.Glob("../files/VMTests/RandomTests/*") |
||||
for _, fn := range fns { |
||||
RunVmTest(fn, t) |
||||
} |
||||
} |
@ -1,3 +0,0 @@ |
||||
// This silences the warning given by 'go install ./...'.
|
||||
|
||||
package vm |
@ -0,0 +1,107 @@ |
||||
package tests |
||||
|
||||
import ( |
||||
"path/filepath" |
||||
"testing" |
||||
) |
||||
|
||||
// I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
|
||||
func TestVMArithmetic(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmArithmeticTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestBitwiseLogicOperation(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmBitwiseLogicOperationTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestBlockInfo(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmBlockInfoTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestEnvironmentalInfo(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmEnvironmentalInfoTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestFlowOperation(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmIOandFlowOperationsTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestLogTest(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmLogTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestPerformance(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmPerformanceTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestPushDupSwap(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmPushDupSwapTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestVMSha3(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmSha3Test.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestVm(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmtests.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestVmLog(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmLogTest.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestInputLimits(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmInputLimits.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestInputLimitsLight(t *testing.T) { |
||||
fn := filepath.Join(vmTestDir, "vmInputLimitsLight.json") |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestVMRandom(t *testing.T) { |
||||
fns, _ := filepath.Glob(filepath.Join(baseDir, "RandomTests", "*")) |
||||
for _, fn := range fns { |
||||
if err := RunVmTest(fn, VmSkipTests); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,170 @@ |
||||
package tests |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"io" |
||||
"math/big" |
||||
"strconv" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/state" |
||||
"github.com/ethereum/go-ethereum/core/vm" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/logger/glog" |
||||
) |
||||
|
||||
func RunVmTestWithReader(r io.Reader, skipTests []string) error { |
||||
tests := make(map[string]VmTest) |
||||
err := readJson(r, &tests) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := runVmTests(tests, skipTests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func RunVmTest(p string, skipTests []string) error { |
||||
|
||||
tests := make(map[string]VmTest) |
||||
err := readJsonFile(p, &tests) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if err := runVmTests(tests, skipTests); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func runVmTests(tests map[string]VmTest, skipTests []string) error { |
||||
skipTest := make(map[string]bool, len(skipTests)) |
||||
for _, name := range skipTests { |
||||
skipTest[name] = true |
||||
} |
||||
|
||||
for name, test := range tests { |
||||
if skipTest[name] { |
||||
glog.Infoln("Skipping VM test", name) |
||||
return nil |
||||
} |
||||
|
||||
if err := runVmTest(test); err != nil { |
||||
return fmt.Errorf("%s %s", name, err.Error()) |
||||
} |
||||
|
||||
glog.Infoln("VM test passed: ", name) |
||||
//fmt.Println(string(statedb.Dump()))
|
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func runVmTest(test VmTest) error { |
||||
db, _ := ethdb.NewMemDatabase() |
||||
statedb := state.New(common.Hash{}, db) |
||||
for addr, account := range test.Pre { |
||||
obj := StateObjectFromAccount(db, addr, account) |
||||
statedb.SetStateObject(obj) |
||||
for a, v := range account.Storage { |
||||
obj.SetState(common.HexToHash(a), common.HexToHash(v)) |
||||
} |
||||
} |
||||
|
||||
// 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 ( |
||||
ret []byte |
||||
gas *big.Int |
||||
err error |
||||
logs state.Logs |
||||
) |
||||
|
||||
ret, logs, gas, err = RunVm(statedb, env, test.Exec) |
||||
|
||||
// Compare expected and actual return
|
||||
rexp := common.FromHex(test.Out) |
||||
if bytes.Compare(rexp, ret) != 0 { |
||||
return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) |
||||
} |
||||
|
||||
// Check gas usage
|
||||
if len(test.Gas) == 0 && err == nil { |
||||
return fmt.Errorf("gas unspecified, indicating an error. VM returned (incorrectly) successfull") |
||||
} else { |
||||
gexp := common.Big(test.Gas) |
||||
if gexp.Cmp(gas) != 0 { |
||||
return fmt.Errorf("gas failed. Expected %v, got %v\n", gexp, gas) |
||||
} |
||||
} |
||||
|
||||
// check post state
|
||||
for addr, account := range test.Post { |
||||
obj := statedb.GetStateObject(common.HexToAddress(addr)) |
||||
if obj == nil { |
||||
continue |
||||
} |
||||
|
||||
for addr, value := range account.Storage { |
||||
v := obj.GetState(common.HexToHash(addr)) |
||||
vexp := common.HexToHash(value) |
||||
|
||||
if v != vexp { |
||||
return fmt.Errorf("(%x: %s) storage failed. Expected %x, got %x (%v %v)\n", obj.Address().Bytes()[0:4], addr, vexp, v, vexp.Big(), v.Big()) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// check logs
|
||||
if len(test.Logs) > 0 { |
||||
lerr := checkLogs(test.Logs, logs) |
||||
if lerr != nil { |
||||
return lerr |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
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 |
||||
} |
Loading…
Reference in new issue