github.com/isti4github/eth-ecc@v0.0.0-20201227085832-c337f2d99319/core/state_prefetcher.go (about)

     1  // Copyright 2019 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 core
    18  
    19  import (
    20  	"sync/atomic"
    21  
    22  	"github.com/Onther-Tech/go-ethereum/common"
    23  	"github.com/Onther-Tech/go-ethereum/consensus"
    24  	"github.com/Onther-Tech/go-ethereum/core/state"
    25  	"github.com/Onther-Tech/go-ethereum/core/types"
    26  	"github.com/Onther-Tech/go-ethereum/core/vm"
    27  	"github.com/Onther-Tech/go-ethereum/params"
    28  )
    29  
    30  // statePrefetcher is a basic Prefetcher, which blindly executes a block on top
    31  // of an arbitrary state with the goal of prefetching potentially useful state
    32  // data from disk before the main block processor start executing.
    33  type statePrefetcher struct {
    34  	config *params.ChainConfig // Chain configuration options
    35  	bc     *BlockChain         // Canonical block chain
    36  	engine consensus.Engine    // Consensus engine used for block rewards
    37  }
    38  
    39  // newStatePrefetcher initialises a new statePrefetcher.
    40  func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
    41  	return &statePrefetcher{
    42  		config: config,
    43  		bc:     bc,
    44  		engine: engine,
    45  	}
    46  }
    47  
    48  // Prefetch processes the state changes according to the Ethereum rules by running
    49  // the transaction messages using the statedb, but any changes are discarded. The
    50  // only goal is to pre-cache transaction signatures and state trie nodes.
    51  func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *uint32) {
    52  	var (
    53  		header  = block.Header()
    54  		gaspool = new(GasPool).AddGas(block.GasLimit())
    55  	)
    56  	// Iterate over and process the individual transactions
    57  	for i, tx := range block.Transactions() {
    58  		// If block precaching was interrupted, abort
    59  		if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
    60  			return
    61  		}
    62  		// Block precaching permitted to continue, execute the transaction
    63  		statedb.Prepare(tx.Hash(), block.Hash(), i)
    64  		if err := precacheTransaction(p.config, p.bc, nil, gaspool, statedb, header, tx, cfg); err != nil {
    65  			return // Ugh, something went horribly wrong, bail out
    66  		}
    67  	}
    68  }
    69  
    70  // precacheTransaction attempts to apply a transaction to the given state database
    71  // and uses the input parameters for its environment. The goal is not to execute
    72  // the transaction successfully, rather to warm up touched data slots.
    73  func precacheTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gaspool *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, cfg vm.Config) error {
    74  	// Convert the transaction into an executable message and pre-cache its sender
    75  	msg, err := tx.AsMessage(types.MakeSigner(config, header.Number))
    76  	if err != nil {
    77  		return err
    78  	}
    79  	// Create the EVM and execute the transaction
    80  	context := NewEVMContext(msg, header, bc, author)
    81  	vm := vm.NewEVM(context, statedb, config, cfg)
    82  
    83  	_, _, _, err = ApplyMessage(vm, msg, gaspool)
    84  	return err
    85  }