state_prefetcher.go 3.8 KB

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