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