github.com/theQRL/go-zond@v0.2.1/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/theQRL/go-zond/consensus"
    23  	"github.com/theQRL/go-zond/core/state"
    24  	"github.com/theQRL/go-zond/core/types"
    25  	"github.com/theQRL/go-zond/core/vm"
    26  	"github.com/theQRL/go-zond/params"
    27  )
    28  
    29  // statePrefetcher is a basic Prefetcher, which blindly executes a block on top
    30  // of an arbitrary state with the goal of prefetching potentially useful state
    31  // data from disk before the main block processor start executing.
    32  type statePrefetcher struct {
    33  	config *params.ChainConfig // Chain configuration options
    34  	bc     *BlockChain         // Canonical block chain
    35  	engine consensus.Engine    // Consensus engine used for block rewards
    36  }
    37  
    38  // newStatePrefetcher initialises a new statePrefetcher.
    39  func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
    40  	return &statePrefetcher{
    41  		config: config,
    42  		bc:     bc,
    43  		engine: engine,
    44  	}
    45  }
    46  
    47  // Prefetch processes the state changes according to the Ethereum rules by running
    48  // the transaction messages using the statedb, but any changes are discarded. The
    49  // only goal is to pre-cache transaction signatures and state trie nodes.
    50  func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *atomic.Bool) {
    51  	var (
    52  		header       = block.Header()
    53  		gaspool      = new(GasPool).AddGas(block.GasLimit())
    54  		blockContext = NewZVMBlockContext(header, p.bc, nil)
    55  		zvm          = vm.NewZVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
    56  		signer       = types.MakeSigner(p.config)
    57  	)
    58  	// Iterate over and process the individual transactions
    59  	for i, tx := range block.Transactions() {
    60  		// If block precaching was interrupted, abort
    61  		if interrupt != nil && interrupt.Load() {
    62  			return
    63  		}
    64  		// Convert the transaction into an executable message and pre-cache its sender
    65  		msg, err := TransactionToMessage(tx, signer, header.BaseFee)
    66  		if err != nil {
    67  			return // Also invalid block, bail out
    68  		}
    69  		statedb.SetTxContext(tx.Hash(), i)
    70  		if err := precacheTransaction(msg, gaspool, statedb, zvm); err != nil {
    71  			return // Ugh, something went horribly wrong, bail out
    72  		}
    73  	}
    74  	// pre-load trie nodes for the final root hash
    75  	statedb.IntermediateRoot(true)
    76  }
    77  
    78  // precacheTransaction attempts to apply a transaction to the given state database
    79  // and uses the input parameters for its environment. The goal is not to execute
    80  // the transaction successfully, rather to warm up touched data slots.
    81  func precacheTransaction(msg *Message, gaspool *GasPool, statedb *state.StateDB, zvm *vm.ZVM) error {
    82  	// Update the zvm with the new transaction context.
    83  	zvm.Reset(NewZVMTxContext(msg), statedb)
    84  	// Add addresses to access list if applicable
    85  	_, err := ApplyMessage(zvm, msg, gaspool)
    86  	return err
    87  }