github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/cmd/evm/internal/t8ntool/execution.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package t8ntool
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/common/math"
    27  	"github.com/ethereum/go-ethereum/consensus/ethash"
    28  	"github.com/ethereum/go-ethereum/consensus/misc"
    29  	"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
    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/tracing"
    34  	"github.com/ethereum/go-ethereum/core/types"
    35  	"github.com/ethereum/go-ethereum/core/vm"
    36  	"github.com/ethereum/go-ethereum/crypto"
    37  	"github.com/ethereum/go-ethereum/eth/tracers"
    38  	"github.com/ethereum/go-ethereum/ethdb"
    39  	"github.com/ethereum/go-ethereum/log"
    40  	"github.com/ethereum/go-ethereum/params"
    41  	"github.com/ethereum/go-ethereum/rlp"
    42  	"github.com/ethereum/go-ethereum/trie"
    43  	"github.com/ethereum/go-ethereum/triedb"
    44  	"github.com/holiman/uint256"
    45  	"golang.org/x/crypto/sha3"
    46  )
    47  
    48  type Prestate struct {
    49  	Env stEnv              `json:"env"`
    50  	Pre types.GenesisAlloc `json:"pre"`
    51  }
    52  
    53  // ExecutionResult contains the execution status after running a state test, any
    54  // error that might have occurred and a dump of the final state if requested.
    55  type ExecutionResult struct {
    56  	StateRoot            common.Hash           `json:"stateRoot"`
    57  	TxRoot               common.Hash           `json:"txRoot"`
    58  	ReceiptRoot          common.Hash           `json:"receiptsRoot"`
    59  	LogsHash             common.Hash           `json:"logsHash"`
    60  	Bloom                types.Bloom           `json:"logsBloom"        gencodec:"required"`
    61  	Receipts             types.Receipts        `json:"receipts"`
    62  	Rejected             []*rejectedTx         `json:"rejected,omitempty"`
    63  	Difficulty           *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
    64  	GasUsed              math.HexOrDecimal64   `json:"gasUsed"`
    65  	BaseFee              *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
    66  	WithdrawalsRoot      *common.Hash          `json:"withdrawalsRoot,omitempty"`
    67  	CurrentExcessBlobGas *math.HexOrDecimal64  `json:"currentExcessBlobGas,omitempty"`
    68  	CurrentBlobGasUsed   *math.HexOrDecimal64  `json:"blobGasUsed,omitempty"`
    69  }
    70  
    71  type ommer struct {
    72  	Delta   uint64         `json:"delta"`
    73  	Address common.Address `json:"address"`
    74  }
    75  
    76  //go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
    77  type stEnv struct {
    78  	Coinbase              common.Address                      `json:"currentCoinbase"   gencodec:"required"`
    79  	Difficulty            *big.Int                            `json:"currentDifficulty"`
    80  	Random                *big.Int                            `json:"currentRandom"`
    81  	ParentDifficulty      *big.Int                            `json:"parentDifficulty"`
    82  	ParentBaseFee         *big.Int                            `json:"parentBaseFee,omitempty"`
    83  	ParentGasUsed         uint64                              `json:"parentGasUsed,omitempty"`
    84  	ParentGasLimit        uint64                              `json:"parentGasLimit,omitempty"`
    85  	GasLimit              uint64                              `json:"currentGasLimit"   gencodec:"required"`
    86  	Number                uint64                              `json:"currentNumber"     gencodec:"required"`
    87  	Timestamp             uint64                              `json:"currentTimestamp"  gencodec:"required"`
    88  	ParentTimestamp       uint64                              `json:"parentTimestamp,omitempty"`
    89  	BlockHashes           map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
    90  	Ommers                []ommer                             `json:"ommers,omitempty"`
    91  	Withdrawals           []*types.Withdrawal                 `json:"withdrawals,omitempty"`
    92  	BaseFee               *big.Int                            `json:"currentBaseFee,omitempty"`
    93  	ParentUncleHash       common.Hash                         `json:"parentUncleHash"`
    94  	ExcessBlobGas         *uint64                             `json:"currentExcessBlobGas,omitempty"`
    95  	ParentExcessBlobGas   *uint64                             `json:"parentExcessBlobGas,omitempty"`
    96  	ParentBlobGasUsed     *uint64                             `json:"parentBlobGasUsed,omitempty"`
    97  	ParentBeaconBlockRoot *common.Hash                        `json:"parentBeaconBlockRoot"`
    98  }
    99  
   100  type stEnvMarshaling struct {
   101  	Coinbase            common.UnprefixedAddress
   102  	Difficulty          *math.HexOrDecimal256
   103  	Random              *math.HexOrDecimal256
   104  	ParentDifficulty    *math.HexOrDecimal256
   105  	ParentBaseFee       *math.HexOrDecimal256
   106  	ParentGasUsed       math.HexOrDecimal64
   107  	ParentGasLimit      math.HexOrDecimal64
   108  	GasLimit            math.HexOrDecimal64
   109  	Number              math.HexOrDecimal64
   110  	Timestamp           math.HexOrDecimal64
   111  	ParentTimestamp     math.HexOrDecimal64
   112  	BaseFee             *math.HexOrDecimal256
   113  	ExcessBlobGas       *math.HexOrDecimal64
   114  	ParentExcessBlobGas *math.HexOrDecimal64
   115  	ParentBlobGasUsed   *math.HexOrDecimal64
   116  }
   117  
   118  type rejectedTx struct {
   119  	Index int    `json:"index"`
   120  	Err   string `json:"error"`
   121  }
   122  
   123  // Apply applies a set of transactions to a pre-state
   124  func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
   125  	txIt txIterator, miningReward int64,
   126  	getTracerFn func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error)) (*state.StateDB, *ExecutionResult, []byte, error) {
   127  	// Capture errors for BLOCKHASH operation, if we haven't been supplied the
   128  	// required blockhashes
   129  	var hashError error
   130  	getHash := func(num uint64) common.Hash {
   131  		if pre.Env.BlockHashes == nil {
   132  			hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num)
   133  			return common.Hash{}
   134  		}
   135  		h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)]
   136  		if !ok {
   137  			hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num)
   138  		}
   139  		return h
   140  	}
   141  	var (
   142  		statedb     = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre)
   143  		signer      = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp)
   144  		gaspool     = new(core.GasPool)
   145  		blockHash   = common.Hash{0x13, 0x37}
   146  		rejectedTxs []*rejectedTx
   147  		includedTxs types.Transactions
   148  		gasUsed     = uint64(0)
   149  		blobGasUsed = uint64(0)
   150  		receipts    = make(types.Receipts, 0)
   151  		txIndex     = 0
   152  	)
   153  	gaspool.AddGas(pre.Env.GasLimit)
   154  	vmContext := vm.BlockContext{
   155  		CanTransfer: core.CanTransfer,
   156  		Transfer:    core.Transfer,
   157  		Coinbase:    pre.Env.Coinbase,
   158  		BlockNumber: new(big.Int).SetUint64(pre.Env.Number),
   159  		Time:        pre.Env.Timestamp,
   160  		Difficulty:  pre.Env.Difficulty,
   161  		GasLimit:    pre.Env.GasLimit,
   162  		GetHash:     getHash,
   163  	}
   164  	// If currentBaseFee is defined, add it to the vmContext.
   165  	if pre.Env.BaseFee != nil {
   166  		vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee)
   167  	}
   168  	// If random is defined, add it to the vmContext.
   169  	if pre.Env.Random != nil {
   170  		rnd := common.BigToHash(pre.Env.Random)
   171  		vmContext.Random = &rnd
   172  	}
   173  	// Calculate the BlobBaseFee
   174  	var excessBlobGas uint64
   175  	if pre.Env.ExcessBlobGas != nil {
   176  		excessBlobGas = *pre.Env.ExcessBlobGas
   177  		vmContext.BlobBaseFee = eip4844.CalcBlobFee(excessBlobGas)
   178  	} else {
   179  		// If it is not explicitly defined, but we have the parent values, we try
   180  		// to calculate it ourselves.
   181  		parentExcessBlobGas := pre.Env.ParentExcessBlobGas
   182  		parentBlobGasUsed := pre.Env.ParentBlobGasUsed
   183  		if parentExcessBlobGas != nil && parentBlobGasUsed != nil {
   184  			excessBlobGas = eip4844.CalcExcessBlobGas(*parentExcessBlobGas, *parentBlobGasUsed)
   185  			vmContext.BlobBaseFee = eip4844.CalcBlobFee(excessBlobGas)
   186  		}
   187  	}
   188  	// If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
   189  	// done in StateProcessor.Process(block, ...), right before transactions are applied.
   190  	if chainConfig.DAOForkSupport &&
   191  		chainConfig.DAOForkBlock != nil &&
   192  		chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
   193  		misc.ApplyDAOHardFork(statedb)
   194  	}
   195  	if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
   196  		evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
   197  		core.ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
   198  	}
   199  
   200  	for i := 0; txIt.Next(); i++ {
   201  		tx, err := txIt.Tx()
   202  		if err != nil {
   203  			log.Warn("rejected tx", "index", i, "error", err)
   204  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   205  			continue
   206  		}
   207  		if tx.Type() == types.BlobTxType && vmContext.BlobBaseFee == nil {
   208  			errMsg := "blob tx used but field env.ExcessBlobGas missing"
   209  			log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", errMsg)
   210  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, errMsg})
   211  			continue
   212  		}
   213  		msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee)
   214  		if err != nil {
   215  			log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
   216  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   217  			continue
   218  		}
   219  		txBlobGas := uint64(0)
   220  		if tx.Type() == types.BlobTxType {
   221  			txBlobGas = uint64(params.BlobTxBlobGasPerBlob * len(tx.BlobHashes()))
   222  			if used, max := blobGasUsed+txBlobGas, uint64(params.MaxBlobGasPerBlock); used > max {
   223  				err := fmt.Errorf("blob gas (%d) would exceed maximum allowance %d", used, max)
   224  				log.Warn("rejected tx", "index", i, "err", err)
   225  				rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   226  				continue
   227  			}
   228  		}
   229  		tracer, traceOutput, err := getTracerFn(txIndex, tx.Hash())
   230  		if err != nil {
   231  			return nil, nil, nil, err
   232  		}
   233  		if tracer != nil {
   234  			vmConfig.Tracer = tracer.Hooks
   235  		}
   236  		statedb.SetTxContext(tx.Hash(), txIndex)
   237  
   238  		var (
   239  			txContext = core.NewEVMTxContext(msg)
   240  			snapshot  = statedb.Snapshot()
   241  			prevGas   = gaspool.Gas()
   242  		)
   243  		evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
   244  
   245  		if tracer != nil && tracer.OnTxStart != nil {
   246  			tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
   247  		}
   248  		// (ret []byte, usedGas uint64, failed bool, err error)
   249  		msgResult, err := core.ApplyMessage(evm, msg, gaspool)
   250  		if err != nil {
   251  			statedb.RevertToSnapshot(snapshot)
   252  			log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
   253  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   254  			gaspool.SetGas(prevGas)
   255  			if tracer != nil {
   256  				if tracer.OnTxEnd != nil {
   257  					tracer.OnTxEnd(nil, err)
   258  				}
   259  				if err := writeTraceResult(tracer, traceOutput); err != nil {
   260  					log.Warn("Error writing tracer output", "err", err)
   261  				}
   262  			}
   263  			continue
   264  		}
   265  		includedTxs = append(includedTxs, tx)
   266  		if hashError != nil {
   267  			return nil, nil, nil, NewError(ErrorMissingBlockhash, hashError)
   268  		}
   269  		blobGasUsed += txBlobGas
   270  		gasUsed += msgResult.UsedGas
   271  
   272  		// Receipt:
   273  		{
   274  			var root []byte
   275  			if chainConfig.IsByzantium(vmContext.BlockNumber) {
   276  				statedb.Finalise(true)
   277  			} else {
   278  				root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
   279  			}
   280  
   281  			// Create a new receipt for the transaction, storing the intermediate root and
   282  			// gas used by the tx.
   283  			receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed}
   284  			if msgResult.Failed() {
   285  				receipt.Status = types.ReceiptStatusFailed
   286  			} else {
   287  				receipt.Status = types.ReceiptStatusSuccessful
   288  			}
   289  			receipt.TxHash = tx.Hash()
   290  			receipt.GasUsed = msgResult.UsedGas
   291  
   292  			// If the transaction created a contract, store the creation address in the receipt.
   293  			if msg.To == nil {
   294  				receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
   295  			}
   296  
   297  			// Set the receipt logs and create the bloom filter.
   298  			receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash)
   299  			receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
   300  			// These three are non-consensus fields:
   301  			//receipt.BlockHash
   302  			//receipt.BlockNumber
   303  			receipt.TransactionIndex = uint(txIndex)
   304  			receipts = append(receipts, receipt)
   305  			if tracer != nil {
   306  				if tracer.Hooks.OnTxEnd != nil {
   307  					tracer.Hooks.OnTxEnd(receipt, nil)
   308  				}
   309  				writeTraceResult(tracer, traceOutput)
   310  			}
   311  		}
   312  
   313  		txIndex++
   314  	}
   315  	statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
   316  	// Add mining reward? (-1 means rewards are disabled)
   317  	if miningReward >= 0 {
   318  		// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
   319  		// where
   320  		// - the coinbase self-destructed, or
   321  		// - there are only 'bad' transactions, which aren't executed. In those cases,
   322  		//   the coinbase gets no txfee, so isn't created, and thus needs to be touched
   323  		var (
   324  			blockReward = big.NewInt(miningReward)
   325  			minerReward = new(big.Int).Set(blockReward)
   326  			perOmmer    = new(big.Int).Div(blockReward, big.NewInt(32))
   327  		)
   328  		for _, ommer := range pre.Env.Ommers {
   329  			// Add 1/32th for each ommer included
   330  			minerReward.Add(minerReward, perOmmer)
   331  			// Add (8-delta)/8
   332  			reward := big.NewInt(8)
   333  			reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
   334  			reward.Mul(reward, blockReward)
   335  			reward.Div(reward, big.NewInt(8))
   336  			statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
   337  		}
   338  		statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
   339  	}
   340  	// Apply withdrawals
   341  	for _, w := range pre.Env.Withdrawals {
   342  		// Amount is in gwei, turn into wei
   343  		amount := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), big.NewInt(params.GWei))
   344  		statedb.AddBalance(w.Address, uint256.MustFromBig(amount), tracing.BalanceIncreaseWithdrawal)
   345  	}
   346  	// Commit block
   347  	root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber))
   348  	if err != nil {
   349  		return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
   350  	}
   351  	execRs := &ExecutionResult{
   352  		StateRoot:   root,
   353  		TxRoot:      types.DeriveSha(includedTxs, trie.NewStackTrie(nil)),
   354  		ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)),
   355  		Bloom:       types.CreateBloom(receipts),
   356  		LogsHash:    rlpHash(statedb.Logs()),
   357  		Receipts:    receipts,
   358  		Rejected:    rejectedTxs,
   359  		Difficulty:  (*math.HexOrDecimal256)(vmContext.Difficulty),
   360  		GasUsed:     (math.HexOrDecimal64)(gasUsed),
   361  		BaseFee:     (*math.HexOrDecimal256)(vmContext.BaseFee),
   362  	}
   363  	if pre.Env.Withdrawals != nil {
   364  		h := types.DeriveSha(types.Withdrawals(pre.Env.Withdrawals), trie.NewStackTrie(nil))
   365  		execRs.WithdrawalsRoot = &h
   366  	}
   367  	if vmContext.BlobBaseFee != nil {
   368  		execRs.CurrentExcessBlobGas = (*math.HexOrDecimal64)(&excessBlobGas)
   369  		execRs.CurrentBlobGasUsed = (*math.HexOrDecimal64)(&blobGasUsed)
   370  	}
   371  	// Re-create statedb instance with new root upon the updated database
   372  	// for accessing latest states.
   373  	statedb, err = state.New(root, statedb.Database(), nil)
   374  	if err != nil {
   375  		return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
   376  	}
   377  	body, _ := rlp.EncodeToBytes(includedTxs)
   378  	return statedb, execRs, body, nil
   379  }
   380  
   381  func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB {
   382  	sdb := state.NewDatabaseWithConfig(db, &triedb.Config{Preimages: true})
   383  	statedb, _ := state.New(types.EmptyRootHash, sdb, nil)
   384  	for addr, a := range accounts {
   385  		statedb.SetCode(addr, a.Code)
   386  		statedb.SetNonce(addr, a.Nonce)
   387  		statedb.SetBalance(addr, uint256.MustFromBig(a.Balance), tracing.BalanceIncreaseGenesisBalance)
   388  		for k, v := range a.Storage {
   389  			statedb.SetState(addr, k, v)
   390  		}
   391  	}
   392  	// Commit and re-open to start with a clean state.
   393  	root, _ := statedb.Commit(0, false)
   394  	statedb, _ = state.New(root, sdb, nil)
   395  	return statedb
   396  }
   397  
   398  func rlpHash(x interface{}) (h common.Hash) {
   399  	hw := sha3.NewLegacyKeccak256()
   400  	rlp.Encode(hw, x)
   401  	hw.Sum(h[:0])
   402  	return h
   403  }
   404  
   405  // calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
   406  // the caller does not provide an explicit difficulty, but instead provides only
   407  // parent timestamp + difficulty.
   408  // Note: this method only works for ethash engine.
   409  func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64,
   410  	parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
   411  	uncleHash := parentUncleHash
   412  	if uncleHash == (common.Hash{}) {
   413  		uncleHash = types.EmptyUncleHash
   414  	}
   415  	parent := &types.Header{
   416  		ParentHash: common.Hash{},
   417  		UncleHash:  uncleHash,
   418  		Difficulty: parentDifficulty,
   419  		Number:     new(big.Int).SetUint64(number - 1),
   420  		Time:       parentTime,
   421  	}
   422  	return ethash.CalcDifficulty(config, currentTime, parent)
   423  }
   424  
   425  func writeTraceResult(tracer *tracers.Tracer, f io.WriteCloser) error {
   426  	defer f.Close()
   427  	result, err := tracer.GetResult()
   428  	if err != nil || result == nil {
   429  		return err
   430  	}
   431  	err = json.NewEncoder(f).Encode(result)
   432  	if err != nil {
   433  		return err
   434  	}
   435  	return nil
   436  }