github.com/jimmyx0x/go-ethereum@v1.10.28/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  	"fmt"
    21  	"math/big"
    22  	"os"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/math"
    26  	"github.com/ethereum/go-ethereum/consensus/ethash"
    27  	"github.com/ethereum/go-ethereum/consensus/misc"
    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/types"
    32  	"github.com/ethereum/go-ethereum/core/vm"
    33  	"github.com/ethereum/go-ethereum/crypto"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"github.com/ethereum/go-ethereum/params"
    37  	"github.com/ethereum/go-ethereum/rlp"
    38  	"github.com/ethereum/go-ethereum/trie"
    39  	"golang.org/x/crypto/sha3"
    40  )
    41  
    42  type Prestate struct {
    43  	Env stEnv             `json:"env"`
    44  	Pre core.GenesisAlloc `json:"pre"`
    45  }
    46  
    47  // ExecutionResult contains the execution status after running a state test, any
    48  // error that might have occurred and a dump of the final state if requested.
    49  type ExecutionResult struct {
    50  	StateRoot   common.Hash           `json:"stateRoot"`
    51  	TxRoot      common.Hash           `json:"txRoot"`
    52  	ReceiptRoot common.Hash           `json:"receiptsRoot"`
    53  	LogsHash    common.Hash           `json:"logsHash"`
    54  	Bloom       types.Bloom           `json:"logsBloom"        gencodec:"required"`
    55  	Receipts    types.Receipts        `json:"receipts"`
    56  	Rejected    []*rejectedTx         `json:"rejected,omitempty"`
    57  	Difficulty  *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
    58  	GasUsed     math.HexOrDecimal64   `json:"gasUsed"`
    59  	BaseFee     *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
    60  }
    61  
    62  type ommer struct {
    63  	Delta   uint64         `json:"delta"`
    64  	Address common.Address `json:"address"`
    65  }
    66  
    67  //go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
    68  type stEnv struct {
    69  	Coinbase         common.Address                      `json:"currentCoinbase"   gencodec:"required"`
    70  	Difficulty       *big.Int                            `json:"currentDifficulty"`
    71  	Random           *big.Int                            `json:"currentRandom"`
    72  	ParentDifficulty *big.Int                            `json:"parentDifficulty"`
    73  	ParentBaseFee    *big.Int                            `json:"parentBaseFee,omitempty"`
    74  	ParentGasUsed    uint64                              `json:"parentGasUsed,omitempty"`
    75  	ParentGasLimit   uint64                              `json:"parentGasLimit,omitempty"`
    76  	GasLimit         uint64                              `json:"currentGasLimit"   gencodec:"required"`
    77  	Number           uint64                              `json:"currentNumber"     gencodec:"required"`
    78  	Timestamp        uint64                              `json:"currentTimestamp"  gencodec:"required"`
    79  	ParentTimestamp  uint64                              `json:"parentTimestamp,omitempty"`
    80  	BlockHashes      map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
    81  	Ommers           []ommer                             `json:"ommers,omitempty"`
    82  	BaseFee          *big.Int                            `json:"currentBaseFee,omitempty"`
    83  	ParentUncleHash  common.Hash                         `json:"parentUncleHash"`
    84  }
    85  
    86  type stEnvMarshaling struct {
    87  	Coinbase         common.UnprefixedAddress
    88  	Difficulty       *math.HexOrDecimal256
    89  	Random           *math.HexOrDecimal256
    90  	ParentDifficulty *math.HexOrDecimal256
    91  	ParentBaseFee    *math.HexOrDecimal256
    92  	ParentGasUsed    math.HexOrDecimal64
    93  	ParentGasLimit   math.HexOrDecimal64
    94  	GasLimit         math.HexOrDecimal64
    95  	Number           math.HexOrDecimal64
    96  	Timestamp        math.HexOrDecimal64
    97  	ParentTimestamp  math.HexOrDecimal64
    98  	BaseFee          *math.HexOrDecimal256
    99  }
   100  
   101  type rejectedTx struct {
   102  	Index int    `json:"index"`
   103  	Err   string `json:"error"`
   104  }
   105  
   106  // Apply applies a set of transactions to a pre-state
   107  func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
   108  	txs types.Transactions, miningReward int64,
   109  	getTracerFn func(txIndex int, txHash common.Hash) (tracer vm.EVMLogger, err error)) (*state.StateDB, *ExecutionResult, error) {
   110  	// Capture errors for BLOCKHASH operation, if we haven't been supplied the
   111  	// required blockhashes
   112  	var hashError error
   113  	getHash := func(num uint64) common.Hash {
   114  		if pre.Env.BlockHashes == nil {
   115  			hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num)
   116  			return common.Hash{}
   117  		}
   118  		h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)]
   119  		if !ok {
   120  			hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num)
   121  		}
   122  		return h
   123  	}
   124  	var (
   125  		statedb     = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre)
   126  		signer      = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number))
   127  		gaspool     = new(core.GasPool)
   128  		blockHash   = common.Hash{0x13, 0x37}
   129  		rejectedTxs []*rejectedTx
   130  		includedTxs types.Transactions
   131  		gasUsed     = uint64(0)
   132  		receipts    = make(types.Receipts, 0)
   133  		txIndex     = 0
   134  	)
   135  	gaspool.AddGas(pre.Env.GasLimit)
   136  	vmContext := vm.BlockContext{
   137  		CanTransfer: core.CanTransfer,
   138  		Transfer:    core.Transfer,
   139  		Coinbase:    pre.Env.Coinbase,
   140  		BlockNumber: new(big.Int).SetUint64(pre.Env.Number),
   141  		Time:        new(big.Int).SetUint64(pre.Env.Timestamp),
   142  		Difficulty:  pre.Env.Difficulty,
   143  		GasLimit:    pre.Env.GasLimit,
   144  		GetHash:     getHash,
   145  	}
   146  	// If currentBaseFee is defined, add it to the vmContext.
   147  	if pre.Env.BaseFee != nil {
   148  		vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee)
   149  	}
   150  	// If random is defined, add it to the vmContext.
   151  	if pre.Env.Random != nil {
   152  		rnd := common.BigToHash(pre.Env.Random)
   153  		vmContext.Random = &rnd
   154  	}
   155  	// If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
   156  	// done in StateProcessor.Process(block, ...), right before transactions are applied.
   157  	if chainConfig.DAOForkSupport &&
   158  		chainConfig.DAOForkBlock != nil &&
   159  		chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
   160  		misc.ApplyDAOHardFork(statedb)
   161  	}
   162  
   163  	for i, tx := range txs {
   164  		msg, err := tx.AsMessage(signer, pre.Env.BaseFee)
   165  		if err != nil {
   166  			log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
   167  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   168  			continue
   169  		}
   170  		tracer, err := getTracerFn(txIndex, tx.Hash())
   171  		if err != nil {
   172  			return nil, nil, err
   173  		}
   174  		vmConfig.Tracer = tracer
   175  		vmConfig.Debug = (tracer != nil)
   176  		statedb.SetTxContext(tx.Hash(), txIndex)
   177  		txContext := core.NewEVMTxContext(msg)
   178  		snapshot := statedb.Snapshot()
   179  		evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
   180  
   181  		// (ret []byte, usedGas uint64, failed bool, err error)
   182  		msgResult, err := core.ApplyMessage(evm, msg, gaspool)
   183  		if err != nil {
   184  			statedb.RevertToSnapshot(snapshot)
   185  			log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err)
   186  			rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
   187  			continue
   188  		}
   189  		includedTxs = append(includedTxs, tx)
   190  		if hashError != nil {
   191  			return nil, nil, NewError(ErrorMissingBlockhash, hashError)
   192  		}
   193  		gasUsed += msgResult.UsedGas
   194  
   195  		// Receipt:
   196  		{
   197  			var root []byte
   198  			if chainConfig.IsByzantium(vmContext.BlockNumber) {
   199  				statedb.Finalise(true)
   200  			} else {
   201  				root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
   202  			}
   203  
   204  			// Create a new receipt for the transaction, storing the intermediate root and
   205  			// gas used by the tx.
   206  			receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed}
   207  			if msgResult.Failed() {
   208  				receipt.Status = types.ReceiptStatusFailed
   209  			} else {
   210  				receipt.Status = types.ReceiptStatusSuccessful
   211  			}
   212  			receipt.TxHash = tx.Hash()
   213  			receipt.GasUsed = msgResult.UsedGas
   214  
   215  			// If the transaction created a contract, store the creation address in the receipt.
   216  			if msg.To() == nil {
   217  				receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
   218  			}
   219  
   220  			// Set the receipt logs and create the bloom filter.
   221  			receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash)
   222  			receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
   223  			// These three are non-consensus fields:
   224  			//receipt.BlockHash
   225  			//receipt.BlockNumber
   226  			receipt.TransactionIndex = uint(txIndex)
   227  			receipts = append(receipts, receipt)
   228  		}
   229  
   230  		txIndex++
   231  	}
   232  	statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
   233  	// Add mining reward? (-1 means rewards are disabled)
   234  	if miningReward >= 0 {
   235  		// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases
   236  		// where
   237  		// - the coinbase suicided, or
   238  		// - there are only 'bad' transactions, which aren't executed. In those cases,
   239  		//   the coinbase gets no txfee, so isn't created, and thus needs to be touched
   240  		var (
   241  			blockReward = big.NewInt(miningReward)
   242  			minerReward = new(big.Int).Set(blockReward)
   243  			perOmmer    = new(big.Int).Div(blockReward, big.NewInt(32))
   244  		)
   245  		for _, ommer := range pre.Env.Ommers {
   246  			// Add 1/32th for each ommer included
   247  			minerReward.Add(minerReward, perOmmer)
   248  			// Add (8-delta)/8
   249  			reward := big.NewInt(8)
   250  			reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
   251  			reward.Mul(reward, blockReward)
   252  			reward.Div(reward, big.NewInt(8))
   253  			statedb.AddBalance(ommer.Address, reward)
   254  		}
   255  		statedb.AddBalance(pre.Env.Coinbase, minerReward)
   256  	}
   257  	// Commit block
   258  	root, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber))
   259  	if err != nil {
   260  		fmt.Fprintf(os.Stderr, "Could not commit state: %v", err)
   261  		return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err))
   262  	}
   263  	execRs := &ExecutionResult{
   264  		StateRoot:   root,
   265  		TxRoot:      types.DeriveSha(includedTxs, trie.NewStackTrie(nil)),
   266  		ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)),
   267  		Bloom:       types.CreateBloom(receipts),
   268  		LogsHash:    rlpHash(statedb.Logs()),
   269  		Receipts:    receipts,
   270  		Rejected:    rejectedTxs,
   271  		Difficulty:  (*math.HexOrDecimal256)(vmContext.Difficulty),
   272  		GasUsed:     (math.HexOrDecimal64)(gasUsed),
   273  		BaseFee:     (*math.HexOrDecimal256)(vmContext.BaseFee),
   274  	}
   275  	return statedb, execRs, nil
   276  }
   277  
   278  func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
   279  	sdb := state.NewDatabaseWithConfig(db, &trie.Config{Preimages: true})
   280  	statedb, _ := state.New(common.Hash{}, sdb, nil)
   281  	for addr, a := range accounts {
   282  		statedb.SetCode(addr, a.Code)
   283  		statedb.SetNonce(addr, a.Nonce)
   284  		statedb.SetBalance(addr, a.Balance)
   285  		for k, v := range a.Storage {
   286  			statedb.SetState(addr, k, v)
   287  		}
   288  	}
   289  	// Commit and re-open to start with a clean state.
   290  	root, _ := statedb.Commit(false)
   291  	statedb, _ = state.New(root, sdb, nil)
   292  	return statedb
   293  }
   294  
   295  func rlpHash(x interface{}) (h common.Hash) {
   296  	hw := sha3.NewLegacyKeccak256()
   297  	rlp.Encode(hw, x)
   298  	hw.Sum(h[:0])
   299  	return h
   300  }
   301  
   302  // calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
   303  // the caller does not provide an explicit difficulty, but instead provides only
   304  // parent timestamp + difficulty.
   305  // Note: this method only works for ethash engine.
   306  func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime uint64,
   307  	parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
   308  	uncleHash := parentUncleHash
   309  	if uncleHash == (common.Hash{}) {
   310  		uncleHash = types.EmptyUncleHash
   311  	}
   312  	parent := &types.Header{
   313  		ParentHash: common.Hash{},
   314  		UncleHash:  uncleHash,
   315  		Difficulty: parentDifficulty,
   316  		Number:     new(big.Int).SetUint64(number - 1),
   317  		Time:       parentTime,
   318  	}
   319  	return ethash.CalcDifficulty(config, currentTime, parent)
   320  }