diff options
Diffstat (limited to 'tests/vm')
-rw-r--r-- | tests/vm/gh_test.go | 172 |
1 files changed, 142 insertions, 30 deletions
diff --git a/tests/vm/gh_test.go b/tests/vm/gh_test.go index da9de6db5..41ebba957 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -2,9 +2,14 @@ package vm import ( "bytes" + "math/big" + "strconv" "testing" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/tests/helper" ) @@ -16,8 +21,26 @@ type Account struct { Storage map[string]string } -func StateObjectFromAccount(addr string, account Account) *state.StateObject { - obj := state.NewStateObject(ethutil.Hex2Bytes(addr)) +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 +} + +func StateObjectFromAccount(db ethutil.Database, addr string, account Account) *state.StateObject { + obj := state.NewStateObject(ethutil.Hex2Bytes(addr), db) obj.SetBalance(ethutil.Big(account.Balance)) if ethutil.IsHex(account.Code) { @@ -29,10 +52,22 @@ func StateObjectFromAccount(addr string, account Account) *state.StateObject { 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 map[string]string + Env Env Exec map[string]string + Transaction map[string]string + Logs []Log Gas string Out string Post map[string]Account @@ -44,17 +79,41 @@ func RunVmTest(p string, t *testing.T) { helper.CreateFileTests(t, p, &tests) for name, test := range tests { - state := state.New(helper.NewTrie()) + db, _ := ethdb.NewMemDatabase() + statedb := state.New(nil, db) for addr, account := range test.Pre { - obj := StateObjectFromAccount(addr, account) - state.SetStateObject(obj) + obj := StateObjectFromAccount(db, addr, account) + statedb.SetStateObject(obj) + for a, v := range account.Storage { + obj.SetState(helper.FromHex(a), ethutil.NewValue(helper.FromHex(v))) + } } - 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 { - helper.Log.Infoln(err) + // 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) } rexp := helper.FromHex(test.Out) @@ -62,13 +121,33 @@ func RunVmTest(p string, t *testing.T) { t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret) } - gexp := ethutil.Big(test.Gas) - if gexp.Cmp(gas) != 0 { - t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) + if isVmTest { + if len(test.Gas) == 0 && err == nil { + // Log VM err + helper.Log.Infof("%s's: %v\n", name, err) + t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name) + } else { + gexp := ethutil.Big(test.Gas) + if gexp.Cmp(gas) != 0 { + // Log VM err + helper.Log.Infof("%s's: %v\n", name, err) + t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas) + } + } } 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 { + 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(ethutil.Big(account.Balance), obj.Balance())) + } + } + for addr, value := range account.Storage { v := obj.GetState(helper.FromHex(addr)).Bytes() vexp := helper.FromHex(value) @@ -78,53 +157,86 @@ func RunVmTest(p string, t *testing.T) { } } } + + if len(test.Logs) > 0 { + for i, log := range test.Logs { + genBloom := ethutil.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 64) + if !bytes.Equal(genBloom, ethutil.Hex2Bytes(log.BloomF)) { + t.Errorf("bloom mismatch") + } + } + } } + 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) { - //helper.Logger.SetLogLevel(5) - const fn = "../files/vmtests/vmArithmeticTest.json" - RunVmTest(fn, t) -} - -func TestVMSystemOperation(t *testing.T) { - //helper.Logger.SetLogLevel(5) - const fn = "../files/vmtests/vmSystemOperationsTest.json" + const fn = "../files/VMTests/vmArithmeticTest.json" RunVmTest(fn, t) } func TestBitwiseLogicOperation(t *testing.T) { - const fn = "../files/vmtests/vmBitwiseLogicOperationTest.json" + const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json" RunVmTest(fn, t) } func TestBlockInfo(t *testing.T) { - const fn = "../files/vmtests/vmBlockInfoTest.json" + const fn = "../files/VMTests/vmBlockInfoTest.json" RunVmTest(fn, t) } func TestEnvironmentalInfo(t *testing.T) { - const fn = "../files/vmtests/vmEnvironmentalInfoTest.json" + const fn = "../files/VMTests/vmEnvironmentalInfoTest.json" RunVmTest(fn, t) } func TestFlowOperation(t *testing.T) { - const fn = "../files/vmtests/vmIOandFlowOperationsTest.json" + const fn = "../files/VMTests/vmIOandFlowOperationsTest.json" RunVmTest(fn, t) } func TestPushDupSwap(t *testing.T) { - const fn = "../files/vmtests/vmPushDupSwapTest.json" + const fn = "../files/VMTests/vmPushDupSwapTest.json" RunVmTest(fn, t) } func TestVMSha3(t *testing.T) { - const fn = "../files/vmtests/vmSha3Test.json" + const fn = "../files/VMTests/vmSha3Test.json" RunVmTest(fn, t) } func TestVm(t *testing.T) { - const fn = "../files/vmtests/vmtests.json" + const fn = "../files/VMTests/vmtests.json" + RunVmTest(fn, t) +} + +func TestVmLog(t *testing.T) { + const fn = "../files/VMTests/vmLogTest.json" + RunVmTest(fn, t) +} + +func TestStateSystemOperations(t *testing.T) { + const fn = "../files/StateTests/stSystemOperationsTest.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) } |