/
state_prefetcher.go
110 lines (99 loc) · 4.49 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
// Modifications Copyright 2020 The klaytn Authors
// 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/>.
//
// This file is derived from core/state_prefetcher.go (2019/04/02).
// Modified and improved for the klaytn development.
package blockchain
import (
"sync/atomic"
"github.com/klaytn/klaytn/blockchain/state"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/blockchain/vm"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/consensus"
"github.com/klaytn/klaytn/params"
)
// 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 Klaytn 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, interrupt *uint32) {
var (
header = block.Header()
)
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
// If block precaching was interrupted, abort
if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
return
}
// Block precaching permitted to continue, execute the transaction
stateDB.Prepare(tx.Hash(), block.Hash(), i)
if err := precacheTransaction(p.config, p.bc, nil, stateDB, header, tx, cfg); err != nil {
return // Ugh, something went horribly wrong, bail out
}
}
}
// PrefetchTx processes the state changes according to the Klaytn rules by running
// a single transaction message using the statedb, but any changes are discarded. The
// only goal is to pre-cache transaction signatures and state trie nodes. It is used
// when fetcher works, so it fetches only a block.
func (p *statePrefetcher) PrefetchTx(block *types.Block, ti int, stateDB *state.StateDB, cfg vm.Config, interrupt *uint32) {
var (
header = block.Header()
tx = block.Transactions()[ti]
)
// If block precaching was interrupted, abort
if interrupt != nil && atomic.LoadUint32(interrupt) == 1 {
return
}
// Block precaching permitted to continue, execute the transaction
stateDB.Prepare(tx.Hash(), block.Hash(), ti)
if err := precacheTransaction(p.config, p.bc, nil, stateDB, header, tx, cfg); err != nil {
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.
func precacheTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, statedb *state.StateDB, header *types.Header, tx *types.Transaction, cfg vm.Config) error {
// Convert the transaction into an executable message and pre-cache its sender
msg, err := tx.AsMessageWithAccountKeyPicker(types.MakeSigner(config, header.Number), statedb, header.Number.Uint64())
if err != nil {
return err
}
// Create the EVM and execute the transaction
context := NewEVMContext(msg, header, bc, author)
vm := vm.NewEVM(context, statedb, config, &cfg)
_, _, kerr := ApplyMessage(vm, msg)
return kerr.ErrTxInvalid
}