github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/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/fff-chain/go-fff/consensus" 23 "github.com/fff-chain/go-fff/core/state" 24 "github.com/fff-chain/go-fff/core/types" 25 "github.com/fff-chain/go-fff/core/vm" 26 "github.com/fff-chain/go-fff/params" 27 ) 28 29 const prefetchThread = 2 30 31 // statePrefetcher is a basic Prefetcher, which blindly executes a block on top 32 // of an arbitrary state with the goal of prefetching potentially useful state 33 // data from disk before the main block processor start executing. 34 type statePrefetcher struct { 35 config *params.ChainConfig // Chain configuration options 36 bc *BlockChain // Canonical block chain 37 engine consensus.Engine // Consensus engine used for block rewards 38 } 39 40 // NewStatePrefetcher initialises a new statePrefetcher. 41 func NewStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher { 42 return &statePrefetcher{ 43 config: config, 44 bc: bc, 45 engine: engine, 46 } 47 } 48 49 // Prefetch processes the state changes according to the Ethereum rules by running 50 // the transaction messages using the statedb, but any changes are discarded. The 51 // only goal is to pre-cache transaction signatures and snapshot clean state. 52 func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *uint32) { 53 var ( 54 header = block.Header() 55 signer = types.MakeSigner(p.config, header.Number) 56 ) 57 transactions := block.Transactions() 58 sortTransactions := make([][]*types.Transaction, prefetchThread) 59 for i := 0; i < prefetchThread; i++ { 60 sortTransactions[i] = make([]*types.Transaction, 0, len(transactions)/prefetchThread) 61 } 62 for idx := range transactions { 63 threadIdx := idx % prefetchThread 64 sortTransactions[threadIdx] = append(sortTransactions[threadIdx], transactions[idx]) 65 } 66 // No need to execute the first batch, since the main processor will do it. 67 for i := 0; i < prefetchThread; i++ { 68 go func(idx int) { 69 newStatedb := statedb.Copy() 70 gaspool := new(GasPool).AddGas(block.GasLimit()) 71 blockContext := NewEVMBlockContext(header, p.bc, nil) 72 evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg) 73 // Iterate over and process the individual transactions 74 for i, tx := range sortTransactions[idx] { 75 // If block precaching was interrupted, abort 76 if interrupt != nil && atomic.LoadUint32(interrupt) == 1 { 77 return 78 } 79 // Convert the transaction into an executable message and pre-cache its sender 80 msg, err := tx.AsMessage(signer) 81 if err != nil { 82 return // Also invalid block, bail out 83 } 84 newStatedb.Prepare(tx.Hash(), header.Hash(), i) 85 precacheTransaction(msg, p.config, gaspool, newStatedb, header, evm) 86 } 87 }(i) 88 } 89 } 90 91 // precacheTransaction attempts to apply a transaction to the given state database 92 // and uses the input parameters for its environment. The goal is not to execute 93 // the transaction successfully, rather to warm up touched data slots. 94 func precacheTransaction(msg types.Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) { 95 // Update the evm with the new transaction context. 96 evm.Reset(NewEVMTxContext(msg), statedb) 97 // Add addresses to access list if applicable 98 ApplyMessage(evm, msg, gaspool) 99 }