state_prefetcher.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/consensus"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/core/vm"
  23. "github.com/ethereum/go-ethereum/params"
  24. )
  25. const prefetchThread = 2
  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 snapshot clean state.
  45. func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg vm.Config, interrupt *uint32) {
  46. var (
  47. header = block.Header()
  48. signer = types.MakeSigner(p.config, header.Number)
  49. )
  50. transactions := block.Transactions()
  51. sortTransactions := make([][]*types.Transaction, prefetchThread)
  52. for i := 0; i < prefetchThread; i++ {
  53. sortTransactions[i] = make([]*types.Transaction, 0, len(transactions)/prefetchThread)
  54. }
  55. for idx := range transactions {
  56. threadIdx := idx % prefetchThread
  57. sortTransactions[threadIdx] = append(sortTransactions[threadIdx], transactions[idx])
  58. }
  59. // No need to execute the first batch, since the main processor will do it.
  60. for i := 0; i < prefetchThread; i++ {
  61. go func(idx int) {
  62. newStatedb := statedb.Copy()
  63. gaspool := new(GasPool).AddGas(block.GasLimit())
  64. blockContext := NewEVMBlockContext(header, p.bc, nil)
  65. evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
  66. // Iterate over and process the individual transactions
  67. for i, tx := range sortTransactions[idx] {
  68. // If block precaching was interrupted, abort
  69. if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
  70. return
  71. }
  72. // Convert the transaction into an executable message and pre-cache its sender
  73. msg, err := tx.AsMessage(signer)
  74. if err != nil {
  75. return // Also invalid block, bail out
  76. }
  77. newStatedb.Prepare(tx.Hash(), header.Hash(), i)
  78. precacheTransaction(msg, p.config, gaspool, newStatedb, header, evm)
  79. }
  80. }(i)
  81. }
  82. }
  83. // precacheTransaction attempts to apply a transaction to the given state database
  84. // and uses the input parameters for its environment. The goal is not to execute
  85. // the transaction successfully, rather to warm up touched data slots.
  86. func precacheTransaction(msg types.Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) {
  87. // Update the evm with the new transaction context.
  88. evm.Reset(NewEVMTxContext(msg), statedb)
  89. // Add addresses to access list if applicable
  90. ApplyMessage(evm, msg, gaspool)
  91. }