github.com/bamzi/go-ethereum@v1.6.7-0.20170704111104-138f26c93af1/tests/state_test_util.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tests 18 19 import ( 20 "bytes" 21 "fmt" 22 "io" 23 "strconv" 24 "strings" 25 "testing" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/common/math" 29 "github.com/ethereum/go-ethereum/core" 30 "github.com/ethereum/go-ethereum/core/state" 31 "github.com/ethereum/go-ethereum/core/types" 32 "github.com/ethereum/go-ethereum/ethdb" 33 "github.com/ethereum/go-ethereum/log" 34 "github.com/ethereum/go-ethereum/params" 35 ) 36 37 func RunStateTestWithReader(chainConfig *params.ChainConfig, r io.Reader, skipTests []string) error { 38 tests := make(map[string]VmTest) 39 if err := readJson(r, &tests); err != nil { 40 return err 41 } 42 43 if err := runStateTests(chainConfig, tests, skipTests); err != nil { 44 return err 45 } 46 47 return nil 48 } 49 50 func RunStateTest(chainConfig *params.ChainConfig, p string, skipTests []string) error { 51 tests := make(map[string]VmTest) 52 if err := readJsonFile(p, &tests); err != nil { 53 return err 54 } 55 56 if err := runStateTests(chainConfig, tests, skipTests); err != nil { 57 return err 58 } 59 60 return nil 61 62 } 63 64 func BenchStateTest(chainConfig *params.ChainConfig, p string, conf bconf, b *testing.B) error { 65 tests := make(map[string]VmTest) 66 if err := readJsonFile(p, &tests); err != nil { 67 return err 68 } 69 test, ok := tests[conf.name] 70 if !ok { 71 return fmt.Errorf("test not found: %s", conf.name) 72 } 73 74 // XXX Yeah, yeah... 75 env := make(map[string]string) 76 env["currentCoinbase"] = test.Env.CurrentCoinbase 77 env["currentDifficulty"] = test.Env.CurrentDifficulty 78 env["currentGasLimit"] = test.Env.CurrentGasLimit 79 env["currentNumber"] = test.Env.CurrentNumber 80 env["previousHash"] = test.Env.PreviousHash 81 if n, ok := test.Env.CurrentTimestamp.(float64); ok { 82 env["currentTimestamp"] = strconv.Itoa(int(n)) 83 } else { 84 env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) 85 } 86 87 b.ResetTimer() 88 for i := 0; i < b.N; i++ { 89 benchStateTest(chainConfig, test, env, b) 90 } 91 92 return nil 93 } 94 95 func benchStateTest(chainConfig *params.ChainConfig, test VmTest, env map[string]string, b *testing.B) { 96 b.StopTimer() 97 db, _ := ethdb.NewMemDatabase() 98 statedb := makePreState(db, test.Pre) 99 b.StartTimer() 100 101 RunState(chainConfig, statedb, db, env, test.Exec) 102 } 103 104 func runStateTests(chainConfig *params.ChainConfig, tests map[string]VmTest, skipTests []string) error { 105 skipTest := make(map[string]bool, len(skipTests)) 106 for _, name := range skipTests { 107 skipTest[name] = true 108 } 109 110 for name, test := range tests { 111 if skipTest[name] /*|| name != "JUMPDEST_Attack"*/ { 112 log.Info(fmt.Sprint("Skipping state test", name)) 113 continue 114 } 115 116 //fmt.Println("StateTest:", name) 117 if err := runStateTest(chainConfig, test); err != nil { 118 return fmt.Errorf("%s: %s\n", name, err.Error()) 119 } 120 121 //log.Info(fmt.Sprint("State test passed: ", name)) 122 //fmt.Println(string(statedb.Dump())) 123 } 124 return nil 125 126 } 127 128 func runStateTest(chainConfig *params.ChainConfig, test VmTest) error { 129 db, _ := ethdb.NewMemDatabase() 130 statedb := makePreState(db, test.Pre) 131 132 // XXX Yeah, yeah... 133 env := make(map[string]string) 134 env["currentCoinbase"] = test.Env.CurrentCoinbase 135 env["currentDifficulty"] = test.Env.CurrentDifficulty 136 env["currentGasLimit"] = test.Env.CurrentGasLimit 137 env["currentNumber"] = test.Env.CurrentNumber 138 env["previousHash"] = test.Env.PreviousHash 139 if n, ok := test.Env.CurrentTimestamp.(float64); ok { 140 env["currentTimestamp"] = strconv.Itoa(int(n)) 141 } else { 142 env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) 143 } 144 145 ret, logs, root, _ := RunState(chainConfig, statedb, db, env, test.Transaction) 146 147 // Return value: 148 var rexp []byte 149 if strings.HasPrefix(test.Out, "#") { 150 n, _ := strconv.Atoi(test.Out[1:]) 151 rexp = make([]byte, n) 152 } else { 153 rexp = common.FromHex(test.Out) 154 } 155 if !bytes.Equal(rexp, ret) { 156 return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) 157 } 158 // Post state content: 159 for addr, account := range test.Post { 160 address := common.HexToAddress(addr) 161 if !statedb.Exist(address) { 162 return fmt.Errorf("did not find expected post-state account: %s", addr) 163 } 164 if balance := statedb.GetBalance(address); balance.Cmp(math.MustParseBig256(account.Balance)) != 0 { 165 return fmt.Errorf("(%x) balance failed. Expected: %v have: %v\n", address[:4], math.MustParseBig256(account.Balance), balance) 166 } 167 if nonce := statedb.GetNonce(address); nonce != math.MustParseUint64(account.Nonce) { 168 return fmt.Errorf("(%x) nonce failed. Expected: %v have: %v\n", address[:4], account.Nonce, nonce) 169 } 170 for addr, value := range account.Storage { 171 v := statedb.GetState(address, common.HexToHash(addr)) 172 vexp := common.HexToHash(value) 173 if v != vexp { 174 return fmt.Errorf("storage failed:\n%x: %s:\nexpected: %x\nhave: %x\n(%v %v)\n", address[:4], addr, vexp, v, vexp.Big(), v.Big()) 175 } 176 } 177 } 178 // Root: 179 if common.HexToHash(test.PostStateRoot) != root { 180 return fmt.Errorf("Post state root error. Expected: %s have: %x", test.PostStateRoot, root) 181 } 182 // Logs: 183 return checkLogs(test.Logs, logs) 184 } 185 186 func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, db ethdb.Database, env, tx map[string]string) ([]byte, []*types.Log, common.Hash, error) { 187 environment, msg := NewEVMEnvironment(false, chainConfig, statedb, env, tx) 188 gaspool := new(core.GasPool).AddGas(math.MustParseBig256(env["currentGasLimit"])) 189 190 snapshot := statedb.Snapshot() 191 ret, _, err := core.ApplyMessage(environment, msg, gaspool) 192 if err != nil { 193 statedb.RevertToSnapshot(snapshot) 194 } 195 root, _ := statedb.CommitTo(db, chainConfig.IsEIP158(environment.Context.BlockNumber)) 196 return ret, statedb.Logs(), root, err 197 }