github.com/klaytn/klaytn@v1.12.1/blockchain/state_prefetcher.go (about) 1 // Modifications Copyright 2020 The klaytn Authors 2 // Copyright 2019 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/state_prefetcher.go (2019/04/02). 19 // Modified and improved for the klaytn development. 20 21 package blockchain 22 23 import ( 24 "sync/atomic" 25 26 "github.com/klaytn/klaytn/blockchain/state" 27 "github.com/klaytn/klaytn/blockchain/types" 28 "github.com/klaytn/klaytn/blockchain/vm" 29 "github.com/klaytn/klaytn/common" 30 "github.com/klaytn/klaytn/consensus" 31 "github.com/klaytn/klaytn/params" 32 ) 33 34 // statePrefetcher is a basic Prefetcher, which blindly executes a block on top 35 // of an arbitrary state with the goal of prefetching potentially useful state 36 // data from disk before the main block processor start executing. 37 type statePrefetcher struct { 38 config *params.ChainConfig // Chain configuration options 39 bc *BlockChain // Canonical block chain 40 engine consensus.Engine // Consensus engine used for block rewards 41 } 42 43 // newStatePrefetcher initialises a new statePrefetcher. 44 func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher { 45 return &statePrefetcher{ 46 config: config, 47 bc: bc, 48 engine: engine, 49 } 50 } 51 52 // Prefetch processes the state changes according to the Klaytn rules by running 53 // the transaction messages using the statedb, but any changes are discarded. The 54 // only goal is to pre-cache transaction signatures and state trie nodes. 55 func (p *statePrefetcher) Prefetch(block *types.Block, stateDB *state.StateDB, cfg vm.Config, interrupt *uint32) { 56 header := block.Header() 57 // Iterate over and process the individual transactions 58 for i, tx := range block.Transactions() { 59 // If block precaching was interrupted, abort 60 if interrupt != nil && atomic.LoadUint32(interrupt) == 1 { 61 return 62 } 63 // Block precaching permitted to continue, execute the transaction 64 stateDB.SetTxContext(tx.Hash(), block.Hash(), i) 65 if err := precacheTransaction(p.config, p.bc, nil, stateDB, header, tx, cfg); err != nil { 66 return // Ugh, something went horribly wrong, bail out 67 } 68 } 69 } 70 71 // PrefetchTx processes the state changes according to the Klaytn rules by running 72 // a single transaction message using the statedb, but any changes are discarded. The 73 // only goal is to pre-cache transaction signatures and state trie nodes. It is used 74 // when fetcher works, so it fetches only a block. 75 func (p *statePrefetcher) PrefetchTx(block *types.Block, ti int, stateDB *state.StateDB, cfg vm.Config, interrupt *uint32) { 76 var ( 77 header = block.Header() 78 tx = block.Transactions()[ti] 79 ) 80 81 // If block precaching was interrupted, abort 82 if interrupt != nil && atomic.LoadUint32(interrupt) == 1 { 83 return 84 } 85 86 // Block precaching permitted to continue, execute the transaction 87 stateDB.SetTxContext(tx.Hash(), block.Hash(), ti) 88 if err := precacheTransaction(p.config, p.bc, nil, stateDB, header, tx, cfg); err != nil { 89 return // Ugh, something went horribly wrong, bail out 90 } 91 } 92 93 // precacheTransaction attempts to apply a transaction to the given state database 94 // and uses the input parameters for its environment. The goal is not to execute 95 // the transaction successfully, rather to warm up touched data slots. 96 func precacheTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, statedb *state.StateDB, header *types.Header, tx *types.Transaction, cfg vm.Config) error { 97 // Convert the transaction into an executable message and pre-cache its sender 98 msg, err := tx.AsMessageWithAccountKeyPicker(types.MakeSigner(config, header.Number), statedb, header.Number.Uint64()) 99 if err != nil { 100 return err 101 } 102 // Create the EVM and execute the transaction 103 blockContext := NewEVMBlockContext(header, bc, author) 104 txContext := NewEVMTxContext(msg, header) 105 vm := vm.NewEVM(blockContext, txContext, statedb, config, &cfg) 106 107 _, err = ApplyMessage(vm, msg) 108 return err 109 }