2019-03-25 13:41:50 +03: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 (
|
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
const prefetchThread = 3
|
|
|
|
const checkInterval = 10
|
|
|
|
|
2019-03-25 13:41:50 +03: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
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// NewStatePrefetcher initialises a new statePrefetcher.
|
|
|
|
func NewStatePrefetcher(config *params.ChainConfig, bc *BlockChain, engine consensus.Engine) *statePrefetcher {
|
2019-03-25 13:41:50 +03:00
|
|
|
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.
|
2022-07-05 06:14:21 +03:00
|
|
|
func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg *vm.Config, interruptCh <-chan struct{}) {
|
2019-03-25 13:41:50 +03:00
|
|
|
var (
|
2022-07-05 06:14:21 +03:00
|
|
|
header = block.Header()
|
2023-08-23 12:46:08 +03:00
|
|
|
signer = types.MakeSigner(p.config, header.Number, header.Time)
|
2019-03-25 13:41:50 +03:00
|
|
|
)
|
2022-07-05 06:14:21 +03:00
|
|
|
transactions := block.Transactions()
|
|
|
|
txChan := make(chan int, prefetchThread)
|
|
|
|
// No need to execute the first batch, since the main processor will do it.
|
|
|
|
for i := 0; i < prefetchThread; i++ {
|
|
|
|
go func() {
|
2022-07-26 17:23:55 +03:00
|
|
|
newStatedb := statedb.CopyDoPrefetch()
|
2023-12-04 05:03:03 +03:00
|
|
|
if !p.config.IsHertzfix(header.Number) {
|
2023-11-29 07:37:43 +03:00
|
|
|
newStatedb.EnableWriteOnSharedStorage()
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
gaspool := new(GasPool).AddGas(block.GasLimit())
|
|
|
|
blockContext := NewEVMBlockContext(header, p.bc, nil)
|
|
|
|
evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, *cfg)
|
|
|
|
// Iterate over and process the individual transactions
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case txIndex := <-txChan:
|
|
|
|
tx := transactions[txIndex]
|
|
|
|
// Convert the transaction into an executable message and pre-cache its sender
|
2023-08-23 12:46:08 +03:00
|
|
|
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
|
|
|
|
msg.SkipAccountChecks = true
|
2022-07-05 06:14:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return // Also invalid block, bail out
|
|
|
|
}
|
2023-08-23 12:46:08 +03:00
|
|
|
newStatedb.SetTxContext(tx.Hash(), txIndex)
|
2022-07-05 06:14:21 +03:00
|
|
|
precacheTransaction(msg, p.config, gaspool, newStatedb, header, evm)
|
|
|
|
|
|
|
|
case <-interruptCh:
|
|
|
|
// If block precaching was interrupted, abort
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-12-03 11:00:26 +03:00
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
// it should be in a separate goroutine, to avoid blocking the critical path.
|
|
|
|
for i := 0; i < len(transactions); i++ {
|
2022-09-19 06:49:31 +03:00
|
|
|
select {
|
|
|
|
case txChan <- i:
|
|
|
|
case <-interruptCh:
|
|
|
|
return
|
|
|
|
}
|
2019-03-25 13:41:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// PrefetchMining 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 snapshot clean state. Only used for mining stage
|
2023-08-23 12:46:08 +03:00
|
|
|
func (p *statePrefetcher) PrefetchMining(txs TransactionsByPriceAndNonce, header *types.Header, gasLimit uint64, statedb *state.StateDB, cfg vm.Config, interruptCh <-chan struct{}, txCurr **types.Transaction) {
|
|
|
|
var signer = types.MakeSigner(p.config, header.Number, header.Time)
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
txCh := make(chan *types.Transaction, 2*prefetchThread)
|
|
|
|
for i := 0; i < prefetchThread; i++ {
|
|
|
|
go func(startCh <-chan *types.Transaction, stopCh <-chan struct{}) {
|
|
|
|
idx := 0
|
2022-07-26 17:23:55 +03:00
|
|
|
newStatedb := statedb.CopyDoPrefetch()
|
2023-12-04 05:03:03 +03:00
|
|
|
if !p.config.IsHertzfix(header.Number) {
|
2023-11-29 07:37:43 +03:00
|
|
|
newStatedb.EnableWriteOnSharedStorage()
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
gaspool := new(GasPool).AddGas(gasLimit)
|
|
|
|
blockContext := NewEVMBlockContext(header, p.bc, nil)
|
|
|
|
evm := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
|
|
|
|
// Iterate over and process the individual transactions
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case tx := <-startCh:
|
|
|
|
// Convert the transaction into an executable message and pre-cache its sender
|
2023-08-23 12:46:08 +03:00
|
|
|
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
|
|
|
|
msg.SkipAccountChecks = true
|
2022-07-05 06:14:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return // Also invalid block, bail out
|
|
|
|
}
|
|
|
|
idx++
|
2023-08-23 12:46:08 +03:00
|
|
|
newStatedb.SetTxContext(tx.Hash(), idx)
|
2022-07-05 06:14:21 +03:00
|
|
|
precacheTransaction(msg, p.config, gaspool, newStatedb, header, evm)
|
|
|
|
gaspool = new(GasPool).AddGas(gasLimit)
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(txCh, interruptCh)
|
|
|
|
}
|
2023-08-23 12:46:08 +03:00
|
|
|
go func(txset TransactionsByPriceAndNonce) {
|
2022-07-05 06:14:21 +03:00
|
|
|
count := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-interruptCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if count++; count%checkInterval == 0 {
|
|
|
|
txset.Forward(*txCurr)
|
|
|
|
}
|
2023-08-23 12:46:08 +03:00
|
|
|
tx := txset.PeekWithUnwrap()
|
2022-07-05 06:14:21 +03:00
|
|
|
if tx == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-31 07:38:09 +03:00
|
|
|
select {
|
|
|
|
case <-interruptCh:
|
|
|
|
return
|
|
|
|
case txCh <- tx:
|
|
|
|
}
|
|
|
|
|
|
|
|
txset.Shift()
|
2022-07-05 06:14:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(txs)
|
|
|
|
}
|
|
|
|
|
2019-03-25 13:41:50 +03:00
|
|
|
// 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.
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 16:19:12 +03:00
|
|
|
func precacheTransaction(msg *Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) error {
|
2021-02-25 17:26:57 +03:00
|
|
|
// Update the evm with the new transaction context.
|
|
|
|
evm.Reset(NewEVMTxContext(msg), statedb)
|
|
|
|
// Add addresses to access list if applicable
|
|
|
|
_, err := ApplyMessage(evm, msg, gaspool)
|
2023-08-23 12:46:08 +03:00
|
|
|
if err == nil {
|
2022-07-26 17:23:55 +03:00
|
|
|
statedb.Finalise(true)
|
|
|
|
}
|
2019-03-25 13:41:50 +03:00
|
|
|
return err
|
|
|
|
}
|