2019-03-25 10:41:50 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
2019-03-25 10:41:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// statePrefetcher is a basic Prefetcher, which blindly executes a block on top
|
|
|
|
// of an arbitrary state with the goal of prefetching potentially useful state
|
|
|
|
// data from disk before the main block processor start executing.
|
|
|
|
type statePrefetcher struct {
|
|
|
|
config *params.ChainConfig // Chain configuration options
|
|
|
|
bc *BlockChain // Canonical block chain
|
|
|
|
engine consensus.Engine // Consensus engine used for block rewards
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStatePrefetcher initialises a new statePrefetcher.
|
|
|
|
func newStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
|
|
|
|
return &statePrefetcher{
|
|
|
|
config: config,
|
|
|
|
bc: bc,
|
|
|
|
engine: engine,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefetch processes the state changes according to the Ethereum rules by running
|
|
|
|
// the transaction messages using the statedb, but any changes are discarded. The
|
|
|
|
// only goal is to pre-cache transaction signatures and state trie nodes.
|
2021-03-12 17:26:06 +00:00
|
|
|
func (p *statePrefetcher) Prefetch(block *types.Block, ibs *state.IntraBlockState, cfg vm.Config, interrupt *uint32) {
|
2019-03-25 10:41:50 +00:00
|
|
|
var (
|
2021-02-25 14:26:57 +00:00
|
|
|
header = block.Header()
|
|
|
|
gaspool = new(GasPool).AddGas(block.GasLimit())
|
|
|
|
blockContext = NewEVMBlockContext(header, p.bc, nil)
|
2021-03-12 17:26:06 +00:00
|
|
|
evm = vm.NewEVM(blockContext, vm.TxContext{}, ibs, p.config, cfg)
|
2021-02-25 14:26:57 +00:00
|
|
|
signer = types.MakeSigner(p.config, header.Number)
|
2019-03-25 10:41:50 +00:00
|
|
|
)
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
// If block precaching was interrupted, abort
|
|
|
|
if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
|
|
|
|
return
|
|
|
|
}
|
2021-02-25 14:26:57 +00:00
|
|
|
// Convert the transaction into an executable message and pre-cache its sender
|
|
|
|
msg, err := tx.AsMessage(signer)
|
|
|
|
if err != nil {
|
|
|
|
return // Also invalid block, bail out
|
|
|
|
}
|
2021-03-12 17:26:06 +00:00
|
|
|
ibs.Prepare(tx.Hash(), block.Hash(), i)
|
|
|
|
if err := precacheTransaction(msg, p.config, gaspool, ibs, header, evm); err != nil {
|
2019-03-25 10:41:50 +00:00
|
|
|
return // Ugh, something went horribly wrong, bail out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// precacheTransaction attempts to apply a transaction to the given state database
|
|
|
|
// and uses the input parameters for its environment. The goal is not to execute
|
|
|
|
// the transaction successfully, rather to warm up touched data slots.
|
2021-03-18 10:44:22 +00:00
|
|
|
func precacheTransaction(msg types.Message, config *params.ChainConfig, gaspool *GasPool, ibs vm.IntraBlockState, header *types.Header, evm *vm.EVM) error { //nolint:unparam
|
2021-02-25 14:26:57 +00:00
|
|
|
// Update the evm with the new transaction context.
|
2021-03-12 17:26:06 +00:00
|
|
|
evm.Reset(NewEVMTxContext(msg), ibs)
|
2021-02-25 14:26:57 +00:00
|
|
|
// Add addresses to access list if applicable
|
|
|
|
_, err := ApplyMessage(evm, msg, gaspool, true, false)
|
2019-03-25 10:41:50 +00:00
|
|
|
return err
|
|
|
|
}
|