github.com/agwab/go-ethereum@v1.9.7/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  	"encoding/hex"
    21  	"encoding/json"
    22  	"fmt"
    23  	"math/big"
    24  	"strconv"
    25  	"strings"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/common/hexutil"
    29  	"github.com/ethereum/go-ethereum/common/math"
    30  	"github.com/ethereum/go-ethereum/core"
    31  	"github.com/ethereum/go-ethereum/core/rawdb"
    32  	"github.com/ethereum/go-ethereum/core/state"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/core/vm"
    35  	"github.com/ethereum/go-ethereum/crypto"
    36  	"github.com/ethereum/go-ethereum/ethdb"
    37  	"github.com/ethereum/go-ethereum/params"
    38  	"github.com/ethereum/go-ethereum/rlp"
    39  	"golang.org/x/crypto/sha3"
    40  )
    41  
    42  // StateTest checks transaction processing without block context.
    43  // See https://github.com/ethereum/EIPs/issues/176 for the test format specification.
    44  type StateTest struct {
    45  	json stJSON
    46  }
    47  
    48  // StateSubtest selects a specific configuration of a General State Test.
    49  type StateSubtest struct {
    50  	Fork  string
    51  	Index int
    52  }
    53  
    54  func (t *StateTest) UnmarshalJSON(in []byte) error {
    55  	return json.Unmarshal(in, &t.json)
    56  }
    57  
    58  type stJSON struct {
    59  	Env  stEnv                    `json:"env"`
    60  	Pre  core.GenesisAlloc        `json:"pre"`
    61  	Tx   stTransaction            `json:"transaction"`
    62  	Out  hexutil.Bytes            `json:"out"`
    63  	Post map[string][]stPostState `json:"post"`
    64  }
    65  
    66  type stPostState struct {
    67  	Root    common.UnprefixedHash `json:"hash"`
    68  	Logs    common.UnprefixedHash `json:"logs"`
    69  	Indexes struct {
    70  		Data  int `json:"data"`
    71  		Gas   int `json:"gas"`
    72  		Value int `json:"value"`
    73  	}
    74  }
    75  
    76  //go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
    77  
    78  type stEnv struct {
    79  	Coinbase   common.Address `json:"currentCoinbase"   gencodec:"required"`
    80  	Difficulty *big.Int       `json:"currentDifficulty" gencodec:"required"`
    81  	GasLimit   uint64         `json:"currentGasLimit"   gencodec:"required"`
    82  	Number     uint64         `json:"currentNumber"     gencodec:"required"`
    83  	Timestamp  uint64         `json:"currentTimestamp"  gencodec:"required"`
    84  }
    85  
    86  type stEnvMarshaling struct {
    87  	Coinbase   common.UnprefixedAddress
    88  	Difficulty *math.HexOrDecimal256
    89  	GasLimit   math.HexOrDecimal64
    90  	Number     math.HexOrDecimal64
    91  	Timestamp  math.HexOrDecimal64
    92  }
    93  
    94  //go:generate gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go
    95  
    96  type stTransaction struct {
    97  	GasPrice   *big.Int `json:"gasPrice"`
    98  	Nonce      uint64   `json:"nonce"`
    99  	To         string   `json:"to"`
   100  	Data       []string `json:"data"`
   101  	GasLimit   []uint64 `json:"gasLimit"`
   102  	Value      []string `json:"value"`
   103  	PrivateKey []byte   `json:"secretKey"`
   104  }
   105  
   106  type stTransactionMarshaling struct {
   107  	GasPrice   *math.HexOrDecimal256
   108  	Nonce      math.HexOrDecimal64
   109  	GasLimit   []math.HexOrDecimal64
   110  	PrivateKey hexutil.Bytes
   111  }
   112  
   113  // getVMConfig takes a fork definition and returns a chain config.
   114  // The fork definition can be
   115  // - a plain forkname, e.g. `Byzantium`,
   116  // - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`.
   117  func getVMConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) {
   118  	var (
   119  		splitForks            = strings.Split(forkString, "+")
   120  		ok                    bool
   121  		baseName, eipsStrings = splitForks[0], splitForks[1:]
   122  	)
   123  	if baseConfig, ok = Forks[baseName]; !ok {
   124  		return nil, nil, UnsupportedForkError{baseName}
   125  	}
   126  	for _, eip := range eipsStrings {
   127  		if eipNum, err := strconv.Atoi(eip); err != nil {
   128  			return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
   129  		} else {
   130  			eips = append(eips, eipNum)
   131  		}
   132  	}
   133  	return baseConfig, eips, nil
   134  }
   135  
   136  // Subtests returns all valid subtests of the test.
   137  func (t *StateTest) Subtests() []StateSubtest {
   138  	var sub []StateSubtest
   139  	for fork, pss := range t.json.Post {
   140  		for i := range pss {
   141  			sub = append(sub, StateSubtest{fork, i})
   142  		}
   143  	}
   144  	return sub
   145  }
   146  
   147  // Run executes a specific subtest and verifies the post-state and logs
   148  func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config) (*state.StateDB, error) {
   149  	statedb, root, err := t.RunNoVerify(subtest, vmconfig)
   150  	if err != nil {
   151  		return statedb, err
   152  	}
   153  	post := t.json.Post[subtest.Fork][subtest.Index]
   154  	// N.B: We need to do this in a two-step process, because the first Commit takes care
   155  	// of suicides, and we need to touch the coinbase _after_ it has potentially suicided.
   156  	if root != common.Hash(post.Root) {
   157  		return statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
   158  	}
   159  	if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
   160  		return statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
   161  	}
   162  	return statedb, nil
   163  }
   164  
   165  // RunNoVerify runs a specific subtest and returns the statedb and post-state root
   166  func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config) (*state.StateDB, common.Hash, error) {
   167  	config, eips, err := getVMConfig(subtest.Fork)
   168  	if err != nil {
   169  		return nil, common.Hash{}, UnsupportedForkError{subtest.Fork}
   170  	}
   171  	vmconfig.ExtraEips = eips
   172  	block := t.genesis(config).ToBlock(nil)
   173  	statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre)
   174  
   175  	post := t.json.Post[subtest.Fork][subtest.Index]
   176  	msg, err := t.json.Tx.toMessage(post)
   177  	if err != nil {
   178  		return nil, common.Hash{}, err
   179  	}
   180  	context := core.NewEVMContext(msg, block.Header(), nil, &t.json.Env.Coinbase)
   181  	context.GetHash = vmTestBlockHash
   182  	evm := vm.NewEVM(context, statedb, config, vmconfig)
   183  
   184  	gaspool := new(core.GasPool)
   185  	gaspool.AddGas(block.GasLimit())
   186  	snapshot := statedb.Snapshot()
   187  	if _, _, _, err := core.ApplyMessage(evm, msg, gaspool); err != nil {
   188  		statedb.RevertToSnapshot(snapshot)
   189  	}
   190  	// Commit block
   191  	statedb.Commit(config.IsEIP158(block.Number()))
   192  	// Add 0-value mining reward. This only makes a difference in the cases
   193  	// where
   194  	// - the coinbase suicided, or
   195  	// - there are only 'bad' transactions, which aren't executed. In those cases,
   196  	//   the coinbase gets no txfee, so isn't created, and thus needs to be touched
   197  	statedb.AddBalance(block.Coinbase(), new(big.Int))
   198  	// And _now_ get the state root
   199  	root := statedb.IntermediateRoot(config.IsEIP158(block.Number()))
   200  	return statedb, root, nil
   201  }
   202  
   203  func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {
   204  	return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas]
   205  }
   206  
   207  func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
   208  	sdb := state.NewDatabase(db)
   209  	statedb, _ := state.New(common.Hash{}, sdb)
   210  	for addr, a := range accounts {
   211  		statedb.SetCode(addr, a.Code)
   212  		statedb.SetNonce(addr, a.Nonce)
   213  		statedb.SetBalance(addr, a.Balance)
   214  		for k, v := range a.Storage {
   215  			statedb.SetState(addr, k, v)
   216  		}
   217  	}
   218  	// Commit and re-open to start with a clean state.
   219  	root, _ := statedb.Commit(false)
   220  	statedb, _ = state.New(root, sdb)
   221  	return statedb
   222  }
   223  
   224  func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis {
   225  	return &core.Genesis{
   226  		Config:     config,
   227  		Coinbase:   t.json.Env.Coinbase,
   228  		Difficulty: t.json.Env.Difficulty,
   229  		GasLimit:   t.json.Env.GasLimit,
   230  		Number:     t.json.Env.Number,
   231  		Timestamp:  t.json.Env.Timestamp,
   232  		Alloc:      t.json.Pre,
   233  	}
   234  }
   235  
   236  func (tx *stTransaction) toMessage(ps stPostState) (core.Message, error) {
   237  	// Derive sender from private key if present.
   238  	var from common.Address
   239  	if len(tx.PrivateKey) > 0 {
   240  		key, err := crypto.ToECDSA(tx.PrivateKey)
   241  		if err != nil {
   242  			return nil, fmt.Errorf("invalid private key: %v", err)
   243  		}
   244  		from = crypto.PubkeyToAddress(key.PublicKey)
   245  	}
   246  	// Parse recipient if present.
   247  	var to *common.Address
   248  	if tx.To != "" {
   249  		to = new(common.Address)
   250  		if err := to.UnmarshalText([]byte(tx.To)); err != nil {
   251  			return nil, fmt.Errorf("invalid to address: %v", err)
   252  		}
   253  	}
   254  
   255  	// Get values specific to this post state.
   256  	if ps.Indexes.Data > len(tx.Data) {
   257  		return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data)
   258  	}
   259  	if ps.Indexes.Value > len(tx.Value) {
   260  		return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value)
   261  	}
   262  	if ps.Indexes.Gas > len(tx.GasLimit) {
   263  		return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas)
   264  	}
   265  	dataHex := tx.Data[ps.Indexes.Data]
   266  	valueHex := tx.Value[ps.Indexes.Value]
   267  	gasLimit := tx.GasLimit[ps.Indexes.Gas]
   268  	// Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203
   269  	value := new(big.Int)
   270  	if valueHex != "0x" {
   271  		v, ok := math.ParseBig256(valueHex)
   272  		if !ok {
   273  			return nil, fmt.Errorf("invalid tx value %q", valueHex)
   274  		}
   275  		value = v
   276  	}
   277  	data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
   278  	if err != nil {
   279  		return nil, fmt.Errorf("invalid tx data %q", dataHex)
   280  	}
   281  
   282  	msg := types.NewMessage(from, to, tx.Nonce, value, gasLimit, tx.GasPrice, data, true)
   283  	return msg, nil
   284  }
   285  
   286  func rlpHash(x interface{}) (h common.Hash) {
   287  	hw := sha3.NewLegacyKeccak256()
   288  	rlp.Encode(hw, x)
   289  	hw.Sum(h[:0])
   290  	return h
   291  }