-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathstate_prefetcher.go
163 lines (151 loc) · 5.48 KB
/
state_prefetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
)
const prefetchThread = 3
const checkInterval = 10
// 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
chain *HeaderChain // Canonical block chain
}
// NewStatePrefetcher initialises a new statePrefetcher.
func NewStatePrefetcher(config *params.ChainConfig, chain *HeaderChain) *statePrefetcher {
return &statePrefetcher{
config: config,
chain: chain,
}
}
// 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.
func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, cfg *vm.Config, interruptCh <-chan struct{}) {
var (
header = block.Header()
signer = types.MakeSigner(p.config, header.Number, header.Time)
)
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() {
newStatedb := statedb.CopyDoPrefetch()
if !p.config.IsHertzfix(header.Number) {
newStatedb.EnableWriteOnSharedStorage()
}
gaspool := new(GasPool).AddGas(block.GasLimit())
blockContext := NewEVMBlockContext(header, p.chain, nil)
evm := vm.NewEVM(blockContext, newStatedb, 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
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
msg.SkipNonceChecks = true
msg.SkipFromEOACheck = true
if err != nil {
return // Also invalid block, bail out
}
newStatedb.SetTxContext(tx.Hash(), txIndex)
// We attempt to apply a transaction. The goal is not to execute
// the transaction successfully, rather to warm up touched data slots.
ApplyMessage(evm, msg, gaspool)
case <-interruptCh:
// If block precaching was interrupted, abort
return
}
}
}()
}
// it should be in a separate goroutine, to avoid blocking the critical path.
for i := 0; i < len(transactions); i++ {
select {
case txChan <- i:
case <-interruptCh:
return
}
}
}
// 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
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)
txCh := make(chan *types.Transaction, 2*prefetchThread)
for i := 0; i < prefetchThread; i++ {
go func(startCh <-chan *types.Transaction, stopCh <-chan struct{}) {
idx := 0
newStatedb := statedb.CopyDoPrefetch()
if !p.config.IsHertzfix(header.Number) {
newStatedb.EnableWriteOnSharedStorage()
}
gaspool := new(GasPool).AddGas(gasLimit)
blockContext := NewEVMBlockContext(header, p.chain, nil)
evm := vm.NewEVM(blockContext, newStatedb, 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
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
msg.SkipNonceChecks = true
msg.SkipFromEOACheck = true
if err != nil {
return // Also invalid block, bail out
}
idx++
newStatedb.SetTxContext(tx.Hash(), idx)
ApplyMessage(evm, msg, gaspool)
gaspool = new(GasPool).AddGas(gasLimit)
case <-stopCh:
return
}
}
}(txCh, interruptCh)
}
go func(txset TransactionsByPriceAndNonce) {
count := 0
for {
select {
case <-interruptCh:
return
default:
if count++; count%checkInterval == 0 {
txset.Forward(*txCurr)
}
tx := txset.PeekWithUnwrap()
if tx == nil {
return
}
select {
case <-interruptCh:
return
case txCh <- tx:
}
txset.Shift()
}
}
}(txs)
}