github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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/tirogen/go-ethereum/common"
    28  	"github.com/tirogen/go-ethereum/common/hexutil"
    29  	"github.com/tirogen/go-ethereum/common/math"
    30  	"github.com/tirogen/go-ethereum/core"
    31  	"github.com/tirogen/go-ethereum/core/rawdb"
    32  	"github.com/tirogen/go-ethereum/core/state"
    33  	"github.com/tirogen/go-ethereum/core/state/snapshot"
    34  	"github.com/tirogen/go-ethereum/core/types"
    35  	"github.com/tirogen/go-ethereum/core/vm"
    36  	"github.com/tirogen/go-ethereum/crypto"
    37  	"github.com/tirogen/go-ethereum/ethdb"
    38  	"github.com/tirogen/go-ethereum/params"
    39  	"github.com/tirogen/go-ethereum/rlp"
    40  	"golang.org/x/crypto/sha3"
    41  )
    42  
    43  // StateTest checks transaction processing without block context.
    44  // See https://github.com/ethereum/EIPs/issues/176 for the test format specification.
    45  type StateTest struct {
    46  	json stJSON
    47  }
    48  
    49  // StateSubtest selects a specific configuration of a General State Test.
    50  type StateSubtest struct {
    51  	Fork  string
    52  	Index int
    53  }
    54  
    55  func (t *StateTest) UnmarshalJSON(in []byte) error {
    56  	return json.Unmarshal(in, &t.json)
    57  }
    58  
    59  type stJSON struct {
    60  	Env  stEnv                    `json:"env"`
    61  	Pre  core.GenesisAlloc        `json:"pre"`
    62  	Tx   stTransaction            `json:"transaction"`
    63  	Out  hexutil.Bytes            `json:"out"`
    64  	Post map[string][]stPostState `json:"post"`
    65  }
    66  
    67  type stPostState struct {
    68  	Root            common.UnprefixedHash `json:"hash"`
    69  	Logs            common.UnprefixedHash `json:"logs"`
    70  	TxBytes         hexutil.Bytes         `json:"txbytes"`
    71  	ExpectException string                `json:"expectException"`
    72  	Indexes         struct {
    73  		Data  int `json:"data"`
    74  		Gas   int `json:"gas"`
    75  		Value int `json:"value"`
    76  	}
    77  }
    78  
    79  //go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
    80  
    81  type stEnv struct {
    82  	Coinbase   common.Address `json:"currentCoinbase"   gencodec:"required"`
    83  	Difficulty *big.Int       `json:"currentDifficulty" gencodec:"optional"`
    84  	Random     *big.Int       `json:"currentRandom"     gencodec:"optional"`
    85  	GasLimit   uint64         `json:"currentGasLimit"   gencodec:"required"`
    86  	Number     uint64         `json:"currentNumber"     gencodec:"required"`
    87  	Timestamp  uint64         `json:"currentTimestamp"  gencodec:"required"`
    88  	BaseFee    *big.Int       `json:"currentBaseFee"    gencodec:"optional"`
    89  }
    90  
    91  type stEnvMarshaling struct {
    92  	Coinbase   common.UnprefixedAddress
    93  	Difficulty *math.HexOrDecimal256
    94  	Random     *math.HexOrDecimal256
    95  	GasLimit   math.HexOrDecimal64
    96  	Number     math.HexOrDecimal64
    97  	Timestamp  math.HexOrDecimal64
    98  	BaseFee    *math.HexOrDecimal256
    99  }
   100  
   101  //go:generate go run github.com/fjl/gencodec -type stTransaction -field-override stTransactionMarshaling -out gen_sttransaction.go
   102  
   103  type stTransaction struct {
   104  	GasPrice             *big.Int            `json:"gasPrice"`
   105  	MaxFeePerGas         *big.Int            `json:"maxFeePerGas"`
   106  	MaxPriorityFeePerGas *big.Int            `json:"maxPriorityFeePerGas"`
   107  	Nonce                uint64              `json:"nonce"`
   108  	To                   string              `json:"to"`
   109  	Data                 []string            `json:"data"`
   110  	AccessLists          []*types.AccessList `json:"accessLists,omitempty"`
   111  	GasLimit             []uint64            `json:"gasLimit"`
   112  	Value                []string            `json:"value"`
   113  	PrivateKey           []byte              `json:"secretKey"`
   114  }
   115  
   116  type stTransactionMarshaling struct {
   117  	GasPrice             *math.HexOrDecimal256
   118  	MaxFeePerGas         *math.HexOrDecimal256
   119  	MaxPriorityFeePerGas *math.HexOrDecimal256
   120  	Nonce                math.HexOrDecimal64
   121  	GasLimit             []math.HexOrDecimal64
   122  	PrivateKey           hexutil.Bytes
   123  }
   124  
   125  // GetChainConfig takes a fork definition and returns a chain config.
   126  // The fork definition can be
   127  // - a plain forkname, e.g. `Byzantium`,
   128  // - a fork basename, and a list of EIPs to enable; e.g. `Byzantium+1884+1283`.
   129  func GetChainConfig(forkString string) (baseConfig *params.ChainConfig, eips []int, err error) {
   130  	var (
   131  		splitForks            = strings.Split(forkString, "+")
   132  		ok                    bool
   133  		baseName, eipsStrings = splitForks[0], splitForks[1:]
   134  	)
   135  	if baseConfig, ok = Forks[baseName]; !ok {
   136  		return nil, nil, UnsupportedForkError{baseName}
   137  	}
   138  	for _, eip := range eipsStrings {
   139  		if eipNum, err := strconv.Atoi(eip); err != nil {
   140  			return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
   141  		} else {
   142  			if !vm.ValidEip(eipNum) {
   143  				return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum)
   144  			}
   145  			eips = append(eips, eipNum)
   146  		}
   147  	}
   148  	return baseConfig, eips, nil
   149  }
   150  
   151  // Subtests returns all valid subtests of the test.
   152  func (t *StateTest) Subtests() []StateSubtest {
   153  	var sub []StateSubtest
   154  	for fork, pss := range t.json.Post {
   155  		for i := range pss {
   156  			sub = append(sub, StateSubtest{fork, i})
   157  		}
   158  	}
   159  	return sub
   160  }
   161  
   162  // checkError checks if the error returned by the state transition matches any expected error.
   163  // A failing expectation returns a wrapped version of the original error, if any,
   164  // or a new error detailing the failing expectation.
   165  // This function does not return or modify the original error, it only evaluates and returns expectations for the error.
   166  func (t *StateTest) checkError(subtest StateSubtest, err error) error {
   167  	expectedError := t.json.Post[subtest.Fork][subtest.Index].ExpectException
   168  	if err == nil && expectedError == "" {
   169  		return nil
   170  	}
   171  	if err == nil && expectedError != "" {
   172  		return fmt.Errorf("expected error %q, got no error", expectedError)
   173  	}
   174  	if err != nil && expectedError == "" {
   175  		return fmt.Errorf("unexpected error: %w", err)
   176  	}
   177  	if err != nil && expectedError != "" {
   178  		// Ignore expected errors (TODO MariusVanDerWijden check error string)
   179  		return nil
   180  	}
   181  	return nil
   182  }
   183  
   184  // Run executes a specific subtest and verifies the post-state and logs
   185  func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, error) {
   186  	snaps, statedb, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter)
   187  	if checkedErr := t.checkError(subtest, err); checkedErr != nil {
   188  		return snaps, statedb, checkedErr
   189  	}
   190  	// The error has been checked; if it was unexpected, it's already returned.
   191  	if err != nil {
   192  		// Here, an error exists but it was expected.
   193  		// We do not check the post state or logs.
   194  		return snaps, statedb, nil
   195  	}
   196  	post := t.json.Post[subtest.Fork][subtest.Index]
   197  	// N.B: We need to do this in a two-step process, because the first Commit takes care
   198  	// of suicides, and we need to touch the coinbase _after_ it has potentially suicided.
   199  	if root != common.Hash(post.Root) {
   200  		return snaps, statedb, fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
   201  	}
   202  	if logs := rlpHash(statedb.Logs()); logs != common.Hash(post.Logs) {
   203  		return snaps, statedb, fmt.Errorf("post state logs hash mismatch: got %x, want %x", logs, post.Logs)
   204  	}
   205  	return snaps, statedb, nil
   206  }
   207  
   208  // RunNoVerify runs a specific subtest and returns the statedb and post-state root
   209  func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool) (*snapshot.Tree, *state.StateDB, common.Hash, error) {
   210  	config, eips, err := GetChainConfig(subtest.Fork)
   211  	if err != nil {
   212  		return nil, nil, common.Hash{}, UnsupportedForkError{subtest.Fork}
   213  	}
   214  	vmconfig.ExtraEips = eips
   215  	block := t.genesis(config).ToBlock()
   216  	snaps, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, snapshotter)
   217  
   218  	var baseFee *big.Int
   219  	if config.IsLondon(new(big.Int)) {
   220  		baseFee = t.json.Env.BaseFee
   221  		if baseFee == nil {
   222  			// Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to
   223  			// parent - 2 : 0xa as the basefee for 'this' context.
   224  			baseFee = big.NewInt(0x0a)
   225  		}
   226  	}
   227  	post := t.json.Post[subtest.Fork][subtest.Index]
   228  	msg, err := t.json.Tx.toMessage(post, baseFee)
   229  	if err != nil {
   230  		return nil, nil, common.Hash{}, err
   231  	}
   232  
   233  	// Try to recover tx with current signer
   234  	if len(post.TxBytes) != 0 {
   235  		var ttx types.Transaction
   236  		err := ttx.UnmarshalBinary(post.TxBytes)
   237  		if err != nil {
   238  			return nil, nil, common.Hash{}, err
   239  		}
   240  
   241  		if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil {
   242  			return nil, nil, common.Hash{}, err
   243  		}
   244  	}
   245  
   246  	// Prepare the EVM.
   247  	txContext := core.NewEVMTxContext(msg)
   248  	context := core.NewEVMBlockContext(block.Header(), nil, &t.json.Env.Coinbase)
   249  	context.GetHash = vmTestBlockHash
   250  	context.BaseFee = baseFee
   251  	context.Random = nil
   252  	if config.IsLondon(new(big.Int)) {
   253  		if t.json.Env.Random != nil {
   254  			rnd := common.BigToHash(t.json.Env.Random)
   255  			context.Random = &rnd
   256  		}
   257  		context.Difficulty = big.NewInt(0)
   258  	} else {
   259  		if t.json.Env.Difficulty != nil {
   260  			context.Difficulty = new(big.Int).Set(t.json.Env.Difficulty)
   261  		}
   262  	}
   263  	evm := vm.NewEVM(context, txContext, statedb, config, vmconfig)
   264  	// Execute the message.
   265  	snapshot := statedb.Snapshot()
   266  	gaspool := new(core.GasPool)
   267  	gaspool.AddGas(block.GasLimit())
   268  	_, err = core.ApplyMessage(evm, msg, gaspool)
   269  	if err != nil {
   270  		statedb.RevertToSnapshot(snapshot)
   271  	}
   272  	// Add 0-value mining reward. This only makes a difference in the cases
   273  	// where
   274  	// - the coinbase suicided, or
   275  	// - there are only 'bad' transactions, which aren't executed. In those cases,
   276  	//   the coinbase gets no txfee, so isn't created, and thus needs to be touched
   277  	statedb.AddBalance(block.Coinbase(), new(big.Int))
   278  	// Commit block
   279  	statedb.Commit(config.IsEIP158(block.Number()))
   280  	// And _now_ get the state root
   281  	root := statedb.IntermediateRoot(config.IsEIP158(block.Number()))
   282  	return snaps, statedb, root, err
   283  }
   284  
   285  func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {
   286  	return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas]
   287  }
   288  
   289  func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter bool) (*snapshot.Tree, *state.StateDB) {
   290  	sdb := state.NewDatabase(db)
   291  	statedb, _ := state.New(common.Hash{}, sdb, nil)
   292  	for addr, a := range accounts {
   293  		statedb.SetCode(addr, a.Code)
   294  		statedb.SetNonce(addr, a.Nonce)
   295  		statedb.SetBalance(addr, a.Balance)
   296  		for k, v := range a.Storage {
   297  			statedb.SetState(addr, k, v)
   298  		}
   299  	}
   300  	// Commit and re-open to start with a clean state.
   301  	root, _ := statedb.Commit(false)
   302  
   303  	var snaps *snapshot.Tree
   304  	if snapshotter {
   305  		snapconfig := snapshot.Config{
   306  			CacheSize:  1,
   307  			Recovery:   false,
   308  			NoBuild:    false,
   309  			AsyncBuild: false,
   310  		}
   311  		snaps, _ = snapshot.New(snapconfig, db, sdb.TrieDB(), root)
   312  	}
   313  	statedb, _ = state.New(root, sdb, snaps)
   314  	return snaps, statedb
   315  }
   316  
   317  func (t *StateTest) genesis(config *params.ChainConfig) *core.Genesis {
   318  	genesis := &core.Genesis{
   319  		Config:     config,
   320  		Coinbase:   t.json.Env.Coinbase,
   321  		Difficulty: t.json.Env.Difficulty,
   322  		GasLimit:   t.json.Env.GasLimit,
   323  		Number:     t.json.Env.Number,
   324  		Timestamp:  t.json.Env.Timestamp,
   325  		Alloc:      t.json.Pre,
   326  	}
   327  	if t.json.Env.Random != nil {
   328  		// Post-Merge
   329  		genesis.Mixhash = common.BigToHash(t.json.Env.Random)
   330  		genesis.Difficulty = big.NewInt(0)
   331  	}
   332  	return genesis
   333  }
   334  
   335  func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (core.Message, error) {
   336  	// Derive sender from private key if present.
   337  	var from common.Address
   338  	if len(tx.PrivateKey) > 0 {
   339  		key, err := crypto.ToECDSA(tx.PrivateKey)
   340  		if err != nil {
   341  			return nil, fmt.Errorf("invalid private key: %v", err)
   342  		}
   343  		from = crypto.PubkeyToAddress(key.PublicKey)
   344  	}
   345  	// Parse recipient if present.
   346  	var to *common.Address
   347  	if tx.To != "" {
   348  		to = new(common.Address)
   349  		if err := to.UnmarshalText([]byte(tx.To)); err != nil {
   350  			return nil, fmt.Errorf("invalid to address: %v", err)
   351  		}
   352  	}
   353  
   354  	// Get values specific to this post state.
   355  	if ps.Indexes.Data > len(tx.Data) {
   356  		return nil, fmt.Errorf("tx data index %d out of bounds", ps.Indexes.Data)
   357  	}
   358  	if ps.Indexes.Value > len(tx.Value) {
   359  		return nil, fmt.Errorf("tx value index %d out of bounds", ps.Indexes.Value)
   360  	}
   361  	if ps.Indexes.Gas > len(tx.GasLimit) {
   362  		return nil, fmt.Errorf("tx gas limit index %d out of bounds", ps.Indexes.Gas)
   363  	}
   364  	dataHex := tx.Data[ps.Indexes.Data]
   365  	valueHex := tx.Value[ps.Indexes.Value]
   366  	gasLimit := tx.GasLimit[ps.Indexes.Gas]
   367  	// Value, Data hex encoding is messy: https://github.com/ethereum/tests/issues/203
   368  	value := new(big.Int)
   369  	if valueHex != "0x" {
   370  		v, ok := math.ParseBig256(valueHex)
   371  		if !ok {
   372  			return nil, fmt.Errorf("invalid tx value %q", valueHex)
   373  		}
   374  		value = v
   375  	}
   376  	data, err := hex.DecodeString(strings.TrimPrefix(dataHex, "0x"))
   377  	if err != nil {
   378  		return nil, fmt.Errorf("invalid tx data %q", dataHex)
   379  	}
   380  	var accessList types.AccessList
   381  	if tx.AccessLists != nil && tx.AccessLists[ps.Indexes.Data] != nil {
   382  		accessList = *tx.AccessLists[ps.Indexes.Data]
   383  	}
   384  	// If baseFee provided, set gasPrice to effectiveGasPrice.
   385  	gasPrice := tx.GasPrice
   386  	if baseFee != nil {
   387  		if tx.MaxFeePerGas == nil {
   388  			tx.MaxFeePerGas = gasPrice
   389  		}
   390  		if tx.MaxFeePerGas == nil {
   391  			tx.MaxFeePerGas = new(big.Int)
   392  		}
   393  		if tx.MaxPriorityFeePerGas == nil {
   394  			tx.MaxPriorityFeePerGas = tx.MaxFeePerGas
   395  		}
   396  		gasPrice = math.BigMin(new(big.Int).Add(tx.MaxPriorityFeePerGas, baseFee),
   397  			tx.MaxFeePerGas)
   398  	}
   399  	if gasPrice == nil {
   400  		return nil, fmt.Errorf("no gas price provided")
   401  	}
   402  
   403  	msg := types.NewMessage(from, to, tx.Nonce, value, gasLimit, gasPrice,
   404  		tx.MaxFeePerGas, tx.MaxPriorityFeePerGas, data, accessList, false)
   405  	return msg, nil
   406  }
   407  
   408  func rlpHash(x interface{}) (h common.Hash) {
   409  	hw := sha3.NewLegacyKeccak256()
   410  	rlp.Encode(hw, x)
   411  	hw.Sum(h[:0])
   412  	return h
   413  }
   414  
   415  func vmTestBlockHash(n uint64) common.Hash {
   416  	return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
   417  }