vm_test_util.go 5.62 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
package tests
obscuren's avatar
obscuren committed
18 19 20

import (
	"bytes"
21
	"encoding/json"
22
	"fmt"
obscuren's avatar
obscuren committed
23
	"math/big"
obscuren's avatar
obscuren committed
24

obscuren's avatar
obscuren committed
25
	"github.com/ethereum/go-ethereum/common"
26
	"github.com/ethereum/go-ethereum/common/hexutil"
27
	"github.com/ethereum/go-ethereum/common/math"
28
	"github.com/ethereum/go-ethereum/core"
29
	"github.com/ethereum/go-ethereum/core/rawdb"
30
	"github.com/ethereum/go-ethereum/core/state"
31
	"github.com/ethereum/go-ethereum/core/vm"
32
	"github.com/ethereum/go-ethereum/crypto"
33
	"github.com/ethereum/go-ethereum/params"
obscuren's avatar
obscuren committed
34 35
)

36 37 38 39
// VMTest checks EVM execution without block or transaction context.
// See https://github.com/ethereum/tests/wiki/VM-Tests for the test format specification.
type VMTest struct {
	json vmJSON
Taylor Gerring's avatar
Taylor Gerring committed
40 41
}

42 43
func (t *VMTest) UnmarshalJSON(data []byte) error {
	return json.Unmarshal(data, &t.json)
44 45
}

46
type vmJSON struct {
47 48 49 50 51 52 53 54
	Env           stEnv                 `json:"env"`
	Exec          vmExec                `json:"exec"`
	Logs          common.UnprefixedHash `json:"logs"`
	GasRemaining  *math.HexOrDecimal64  `json:"gas"`
	Out           hexutil.Bytes         `json:"out"`
	Pre           core.GenesisAlloc     `json:"pre"`
	Post          core.GenesisAlloc     `json:"post"`
	PostStateRoot common.Hash           `json:"postStateRoot"`
55 56
}

57 58 59 60 61 62 63 64 65 66 67
//go:generate gencodec -type vmExec -field-override vmExecMarshaling -out gen_vmexec.go

type vmExec struct {
	Address  common.Address `json:"address"  gencodec:"required"`
	Caller   common.Address `json:"caller"   gencodec:"required"`
	Origin   common.Address `json:"origin"   gencodec:"required"`
	Code     []byte         `json:"code"     gencodec:"required"`
	Data     []byte         `json:"data"     gencodec:"required"`
	Value    *big.Int       `json:"value"    gencodec:"required"`
	GasLimit uint64         `json:"gas"      gencodec:"required"`
	GasPrice *big.Int       `json:"gasPrice" gencodec:"required"`
68 69
}

70 71 72 73 74 75 76 77 78
type vmExecMarshaling struct {
	Address  common.UnprefixedAddress
	Caller   common.UnprefixedAddress
	Origin   common.UnprefixedAddress
	Code     hexutil.Bytes
	Data     hexutil.Bytes
	Value    *math.HexOrDecimal256
	GasLimit math.HexOrDecimal64
	GasPrice *math.HexOrDecimal256
Taylor Gerring's avatar
Taylor Gerring committed
79 80
}

81
func (t *VMTest) Run(vmconfig vm.Config, snapshotter bool) error {
82 83 84 85 86 87 88 89 90
	snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
	if snapshotter {
		preRoot := statedb.IntermediateRoot(false)
		defer func() {
			if _, err := snaps.Journal(preRoot); err != nil {
				panic(err)
			}
		}()
	}
91
	ret, gasRemaining, err := t.exec(statedb, vmconfig)
Taylor Gerring's avatar
Taylor Gerring committed
92

93 94 95
	if t.json.GasRemaining == nil {
		if err == nil {
			return fmt.Errorf("gas unspecified (indicating an error), but VM returned no error")
96
		}
97 98
		if gasRemaining > 0 {
			return fmt.Errorf("gas unspecified (indicating an error), but VM returned gas remaining > 0")
obscuren's avatar
obscuren committed
99
		}
100
		return nil
Taylor Gerring's avatar
Taylor Gerring committed
101
	}
102 103 104
	// Test declares gas, expecting outputs to match.
	if !bytes.Equal(ret, t.json.Out) {
		return fmt.Errorf("return data mismatch: got %x, want %x", ret, t.json.Out)
Taylor Gerring's avatar
Taylor Gerring committed
105
	}
106 107
	if gasRemaining != uint64(*t.json.GasRemaining) {
		return fmt.Errorf("remaining gas %v, want %v", gasRemaining, *t.json.GasRemaining)
Taylor Gerring's avatar
Taylor Gerring committed
108
	}
109 110 111 112
	for addr, account := range t.json.Post {
		for k, wantV := range account.Storage {
			if haveV := statedb.GetState(addr, k); haveV != wantV {
				return fmt.Errorf("wrong storage value at %x:\n  got  %x\n  want %x", k, haveV, wantV)
obscuren's avatar
obscuren committed
113 114
			}
		}
Taylor Gerring's avatar
Taylor Gerring committed
115
	}
116 117 118
	// if root := statedb.IntermediateRoot(false); root != t.json.PostStateRoot {
	// 	return fmt.Errorf("post state root mismatch, got %x, want %x", root, t.json.PostStateRoot)
	// }
119 120 121 122
	if logs := rlpHash(statedb.Logs()); logs != common.Hash(t.json.Logs) {
		return fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, t.json.Logs)
	}
	return nil
123
}
124

125 126 127 128
func (t *VMTest) exec(statedb *state.StateDB, vmconfig vm.Config) ([]byte, uint64, error) {
	evm := t.newEVM(statedb, vmconfig)
	e := t.json.Exec
	return evm.Call(vm.AccountRef(e.Caller), e.Address, e.Data, e.GasLimit, e.Value)
129 130
}

131 132 133 134 135 136 137 138 139 140
func (t *VMTest) newEVM(statedb *state.StateDB, vmconfig vm.Config) *vm.EVM {
	initialCall := true
	canTransfer := func(db vm.StateDB, address common.Address, amount *big.Int) bool {
		if initialCall {
			initialCall = false
			return true
		}
		return core.CanTransfer(db, address, amount)
	}
	transfer := func(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {}
141 142 143 144 145
	txContext := vm.TxContext{
		Origin:   t.json.Exec.Origin,
		GasPrice: t.json.Exec.GasPrice,
	}
	context := vm.BlockContext{
146 147 148 149 150 151 152 153 154 155
		CanTransfer: canTransfer,
		Transfer:    transfer,
		GetHash:     vmTestBlockHash,
		Coinbase:    t.json.Env.Coinbase,
		BlockNumber: new(big.Int).SetUint64(t.json.Env.Number),
		Time:        new(big.Int).SetUint64(t.json.Env.Timestamp),
		GasLimit:    t.json.Env.GasLimit,
		Difficulty:  t.json.Env.Difficulty,
	}
	vmconfig.NoRecursion = true
156
	return vm.NewEVM(context, txContext, statedb, params.MainnetChainConfig, vmconfig)
157
}
158

159 160
func vmTestBlockHash(n uint64) common.Hash {
	return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
161
}