github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state_prefetcher.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2019 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package core
    28  
    29  import (
    30  	"math/big"
    31  	"sync/atomic"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/consensus"
    34  	"github.com/MetalBlockchain/subnet-evm/core/state"
    35  	"github.com/MetalBlockchain/subnet-evm/core/types"
    36  	"github.com/MetalBlockchain/subnet-evm/core/vm"
    37  	"github.com/MetalBlockchain/subnet-evm/params"
    38  )
    39  
    40  // statePrefetcher is a basic Prefetcher, which blindly executes a block on top
    41  // of an arbitrary state with the goal of prefetching potentially useful state
    42  // data from disk before the main block processor start executing.
    43  type statePrefetcher struct {
    44  	config *params.ChainConfig // Chain configuration options
    45  	bc     *BlockChain         // Canonical block chain
    46  	engine consensus.Engine    // Consensus engine used for block rewards
    47  }
    48  
    49  // newStatePrefetcher initialises a new statePrefetcher.
    50  func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
    51  	return &statePrefetcher{
    52  		config: config,
    53  		bc:     bc,
    54  		engine: engine,
    55  	}
    56  }
    57  
    58  // Prefetch processes the state changes according to the Ethereum rules by running
    59  // the transaction messages using the statedb, but any changes are discarded. The
    60  // only goal is to pre-cache transaction signatures and state trie nodes.
    61  func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *uint32) {
    62  	var (
    63  		header       = block.Header()
    64  		gaspool      = new(GasPool).AddGas(block.GasLimit())
    65  		blockContext = NewEVMBlockContext(header, p.bc, nil)
    66  		evm          = vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
    67  		signer       = types.MakeSigner(p.config, header.Number, new(big.Int).SetUint64(header.Time))
    68  	)
    69  	// Iterate over and process the individual transactions
    70  	byzantium := p.config.IsByzantium(block.Number())
    71  	for i, tx := range block.Transactions() {
    72  		// If block precaching was interrupted, abort
    73  		if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
    74  			return
    75  		}
    76  		// Convert the transaction into an executable message and pre-cache its sender
    77  		msg, err := tx.AsMessage(signer, header.BaseFee)
    78  		if err != nil {
    79  			return // Also invalid block, bail out
    80  		}
    81  		statedb.Prepare(tx.Hash(), i)
    82  		if err := precacheTransaction(msg, p.config, gaspool, statedb, header, evm); err != nil {
    83  			return // Ugh, something went horribly wrong, bail out
    84  		}
    85  		// If we're pre-byzantium, pre-load trie nodes for the intermediate root
    86  		if !byzantium {
    87  			statedb.IntermediateRoot(true)
    88  		}
    89  	}
    90  	// If were post-byzantium, pre-load trie nodes for the final root hash
    91  	if byzantium {
    92  		statedb.IntermediateRoot(true)
    93  	}
    94  }
    95  
    96  // precacheTransaction attempts to apply a transaction to the given state database
    97  // and uses the input parameters for its environment. The goal is not to execute
    98  // the transaction successfully, rather to warm up touched data slots.
    99  func precacheTransaction(msg types.Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) error {
   100  	// Update the evm with the new transaction context.
   101  	evm.Reset(NewEVMTxContext(msg), statedb)
   102  	// Add addresses to access list if applicable
   103  	_, err := ApplyMessage(evm, msg, gaspool)
   104  	return err
   105  }