state_test_util.go 6.49 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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 18 19 20 21
package tests

import (
	"bytes"
	"fmt"
Taylor Gerring's avatar
Taylor Gerring committed
22
	"io"
23 24
	"math/big"
	"strconv"
25
	"strings"
26
	"testing"
27 28 29 30 31 32

	"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/ethdb"
Taylor Gerring's avatar
Taylor Gerring committed
33
	"github.com/ethereum/go-ethereum/logger/glog"
34
	"github.com/ethereum/go-ethereum/params"
35 36
)

37
func RunStateTestWithReader(chainConfig *params.ChainConfig, r io.Reader, skipTests []string) error {
Taylor Gerring's avatar
Taylor Gerring committed
38 39 40
	tests := make(map[string]VmTest)
	if err := readJson(r, &tests); err != nil {
		return err
41
	}
42

43
	if err := runStateTests(chainConfig, tests, skipTests); err != nil {
Taylor Gerring's avatar
Taylor Gerring committed
44 45 46 47 48 49
		return err
	}

	return nil
}

50
func RunStateTest(chainConfig *params.ChainConfig, p string, skipTests []string) error {
51
	tests := make(map[string]VmTest)
Taylor Gerring's avatar
Taylor Gerring committed
52 53 54 55
	if err := readJsonFile(p, &tests); err != nil {
		return err
	}

56
	if err := runStateTests(chainConfig, tests, skipTests); err != nil {
Taylor Gerring's avatar
Taylor Gerring committed
57 58 59 60 61 62 63
		return err
	}

	return nil

}

64
func BenchStateTest(chainConfig *params.ChainConfig, p string, conf bconf, b *testing.B) error {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	tests := make(map[string]VmTest)
	if err := readJsonFile(p, &tests); err != nil {
		return err
	}
	test, ok := tests[conf.name]
	if !ok {
		return fmt.Errorf("test not found: %s", conf.name)
	}

	// 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)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
89
		benchStateTest(chainConfig, test, env, b)
90 91 92 93 94
	}

	return nil
}

95
func benchStateTest(chainConfig *params.ChainConfig, test VmTest, env map[string]string, b *testing.B) {
96 97
	b.StopTimer()
	db, _ := ethdb.NewMemDatabase()
98
	statedb := makePreState(db, test.Pre)
99 100
	b.StartTimer()

101
	RunState(chainConfig, statedb, env, test.Exec)
102 103
}

104
func runStateTests(chainConfig *params.ChainConfig, tests map[string]VmTest, skipTests []string) error {
105 106
	skipTest := make(map[string]bool, len(skipTests))
	for _, name := range skipTests {
Taylor Gerring's avatar
Taylor Gerring committed
107 108
		skipTest[name] = true
	}
109 110

	for name, test := range tests {
111
		if skipTest[name] /*|| name != "EXP_Empty"*/ {
Taylor Gerring's avatar
Taylor Gerring committed
112
			glog.Infoln("Skipping state test", name)
113
			continue
114
		}
115

116
		//fmt.Println("StateTest:", name)
117
		if err := runStateTest(chainConfig, test); err != nil {
Taylor Gerring's avatar
Taylor Gerring committed
118
			return fmt.Errorf("%s: %s\n", name, err.Error())
119 120
		}

121
		//glog.Infoln("State test passed: ", name)
Taylor Gerring's avatar
Taylor Gerring committed
122 123 124
		//fmt.Println(string(statedb.Dump()))
	}
	return nil
125

Taylor Gerring's avatar
Taylor Gerring committed
126
}
127

128
func runStateTest(chainConfig *params.ChainConfig, test VmTest) error {
Taylor Gerring's avatar
Taylor Gerring committed
129
	db, _ := ethdb.NewMemDatabase()
130
	statedb := makePreState(db, test.Pre)
131

Taylor Gerring's avatar
Taylor Gerring committed
132 133 134 135 136 137 138 139 140 141 142 143
	// 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)
	}
144

Taylor Gerring's avatar
Taylor Gerring committed
145 146 147 148
	var (
		ret []byte
		// gas  *big.Int
		// err  error
149
		logs vm.Logs
Taylor Gerring's avatar
Taylor Gerring committed
150 151
	)

152
	ret, logs, _, _ = RunState(chainConfig, statedb, env, test.Transaction)
Taylor Gerring's avatar
Taylor Gerring committed
153

154
	// Compare expected and actual return
155 156 157 158 159 160 161
	var rexp []byte
	if strings.HasPrefix(test.Out, "#") {
		n, _ := strconv.Atoi(test.Out[1:])
		rexp = make([]byte, n)
	} else {
		rexp = common.FromHex(test.Out)
	}
Taylor Gerring's avatar
Taylor Gerring committed
162 163 164
	if bytes.Compare(rexp, ret) != 0 {
		return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret)
	}
165

Taylor Gerring's avatar
Taylor Gerring committed
166 167 168
	// check post state
	for addr, account := range test.Post {
		obj := statedb.GetStateObject(common.HexToAddress(addr))
169 170 171
		if obj == nil {
			return fmt.Errorf("did not find expected post-state account: %s", addr)
		}
172

Taylor Gerring's avatar
Taylor Gerring committed
173
		if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
174
			return fmt.Errorf("(%x) balance failed. Expected: %v have: %v\n", obj.Address().Bytes()[:4], common.String2Big(account.Balance), obj.Balance())
175 176
		}

Taylor Gerring's avatar
Taylor Gerring committed
177
		if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
178
			return fmt.Errorf("(%x) nonce failed. Expected: %v have: %v\n", obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
179 180
		}

Taylor Gerring's avatar
Taylor Gerring committed
181
		for addr, value := range account.Storage {
182
			v := statedb.GetState(obj.Address(), common.HexToHash(addr))
Taylor Gerring's avatar
Taylor Gerring committed
183
			vexp := common.HexToHash(value)
Taylor Gerring's avatar
Taylor Gerring committed
184

Taylor Gerring's avatar
Taylor Gerring committed
185
			if v != vexp {
186
				return fmt.Errorf("storage failed:\n%x: %s:\nexpected: %x\nhave:     %x\n(%v %v)\n", obj.Address().Bytes(), addr, vexp, v, vexp.Big(), v.Big())
Taylor Gerring's avatar
Taylor Gerring committed
187 188
			}
		}
Taylor Gerring's avatar
Taylor Gerring committed
189
	}
190

191
	root, _ := statedb.Commit(false)
192
	if common.HexToHash(test.PostStateRoot) != root {
193
		return fmt.Errorf("Post state root error. Expected: %s have: %x", test.PostStateRoot, root)
Taylor Gerring's avatar
Taylor Gerring committed
194
	}
Taylor Gerring's avatar
Taylor Gerring committed
195 196 197 198 199 200 201 202

	// check logs
	if len(test.Logs) > 0 {
		if err := checkLogs(test.Logs, logs); err != nil {
			return err
		}
	}

Taylor Gerring's avatar
Taylor Gerring committed
203
	return nil
204 205
}

206
func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, env, tx map[string]string) ([]byte, vm.Logs, *big.Int, error) {
207
	environment, msg := NewEVMEnvironment(false, chainConfig, statedb, env, tx)
208 209
	// Set pre compiled contracts
	vm.Precompiled = vm.PrecompiledContracts()
210
	gaspool := new(core.GasPool).AddGas(common.Big(env["currentGasLimit"]))
211

212 213 214 215 216
	root, _ := statedb.Commit(false)
	statedb.Reset(root)

	snapshot := statedb.Snapshot()

217
	ret, gasUsed, err := core.ApplyMessage(environment, msg, gaspool)
218
	if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || core.IsGasLimitErr(err) {
219
		statedb.RevertToSnapshot(snapshot)
220
	}
221
	statedb.Commit(chainConfig.IsEIP158(environment.Context.BlockNumber))
222

223
	return ret, statedb.Logs(), gasUsed, err
224
}