-
Notifications
You must be signed in to change notification settings - Fork 180
/
manager.go
250 lines (217 loc) · 5.76 KB
/
manager.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package computation
import (
"context"
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/computation/computer"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/fvm"
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/executiondatasync/provider"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/logging"
)
const (
ReusableCadenceRuntimePoolSize = 1000
)
type ComputationManager interface {
ExecuteScript(
ctx context.Context,
script []byte,
arguments [][]byte,
blockHeader *flow.Header,
snapshot snapshot.StorageSnapshot,
) (
[]byte,
uint64,
error,
)
ComputeBlock(
ctx context.Context,
parentBlockExecutionResultID flow.Identifier,
block *entity.ExecutableBlock,
snapshot snapshot.StorageSnapshot,
) (
*execution.ComputationResult,
error,
)
GetAccount(
ctx context.Context,
addr flow.Address,
header *flow.Header,
snapshot snapshot.StorageSnapshot,
) (
*flow.Account,
error,
)
}
type ComputationConfig struct {
query.QueryConfig
CadenceTracing bool
ExtensiveTracing bool
DerivedDataCacheSize uint
MaxConcurrency int
// When NewCustomVirtualMachine is nil, the manager will create a standard
// fvm virtual machine via fvm.NewVirtualMachine. Otherwise, the manager
// will create a virtual machine using this function.
//
// Note that this is primarily used for testing.
NewCustomVirtualMachine func() fvm.VM
}
// Manager manages computation and execution
type Manager struct {
log zerolog.Logger
vm fvm.VM
blockComputer computer.BlockComputer
queryExecutor query.Executor
derivedChainData *derived.DerivedChainData
}
var _ ComputationManager = &Manager{}
func New(
logger zerolog.Logger,
metrics module.ExecutionMetrics,
tracer module.Tracer,
me module.Local,
protoState protocol.State,
vmCtx fvm.Context,
committer computer.ViewCommitter,
executionDataProvider provider.Provider,
params ComputationConfig,
) (*Manager, error) {
log := logger.With().Str("engine", "computation").Logger()
var vm fvm.VM
if params.NewCustomVirtualMachine != nil {
vm = params.NewCustomVirtualMachine()
} else {
vm = fvm.NewVirtualMachine()
}
chainID := vmCtx.Chain.ChainID()
options := DefaultFVMOptions(chainID, params.CadenceTracing, params.ExtensiveTracing)
vmCtx = fvm.NewContextFromParent(vmCtx, options...)
blockComputer, err := computer.NewBlockComputer(
vm,
vmCtx,
metrics,
tracer,
log.With().Str("component", "block_computer").Logger(),
committer,
me,
executionDataProvider,
nil, // TODO(ramtin): update me with proper consumers
protoState,
params.MaxConcurrency,
)
if err != nil {
return nil, fmt.Errorf("cannot create block computer: %w", err)
}
derivedChainData, err := derived.NewDerivedChainData(params.DerivedDataCacheSize)
if err != nil {
return nil, fmt.Errorf("cannot create derived data cache: %w", err)
}
queryExecutor := query.NewQueryExecutor(
params.QueryConfig,
logger,
metrics,
vm,
vmCtx,
derivedChainData,
query.NewProtocolStateWrapper(protoState),
)
e := Manager{
log: log,
vm: vm,
blockComputer: blockComputer,
queryExecutor: queryExecutor,
derivedChainData: derivedChainData,
}
return &e, nil
}
func (e *Manager) VM() fvm.VM {
return e.vm
}
func (e *Manager) ComputeBlock(
ctx context.Context,
parentBlockExecutionResultID flow.Identifier,
block *entity.ExecutableBlock,
snapshot snapshot.StorageSnapshot,
) (*execution.ComputationResult, error) {
e.log.Debug().
Hex("block_id", logging.Entity(block.Block)).
Msg("received complete block")
derivedBlockData := e.derivedChainData.GetOrCreateDerivedBlockData(
block.ID(),
block.ParentID())
result, err := e.blockComputer.ExecuteBlock(
ctx,
parentBlockExecutionResultID,
block,
snapshot,
derivedBlockData)
if err != nil {
return nil, fmt.Errorf("failed to execute block: %w", err)
}
e.log.Debug().
Hex("block_id", logging.Entity(result.ExecutableBlock.Block)).
Msg("computed block result")
return result, nil
}
func (e *Manager) ExecuteScript(
ctx context.Context,
code []byte,
arguments [][]byte,
blockHeader *flow.Header,
snapshot snapshot.StorageSnapshot,
) ([]byte, uint64, error) {
return e.queryExecutor.ExecuteScript(ctx,
code,
arguments,
blockHeader,
snapshot)
}
func (e *Manager) GetAccount(
ctx context.Context,
address flow.Address,
blockHeader *flow.Header,
snapshot snapshot.StorageSnapshot,
) (
*flow.Account,
error,
) {
return e.queryExecutor.GetAccount(
ctx,
address,
blockHeader,
snapshot)
}
func (e *Manager) QueryExecutor() query.Executor {
return e.queryExecutor
}
func DefaultFVMOptions(chainID flow.ChainID, cadenceTracing bool, extensiveTracing bool) []fvm.Option {
options := []fvm.Option{
fvm.WithChain(chainID.Chain()),
fvm.WithReusableCadenceRuntimePool(
reusableRuntime.NewReusableCadenceRuntimePool(
ReusableCadenceRuntimePoolSize,
runtime.Config{
TracingEnabled: cadenceTracing,
AccountLinkingEnabled: true,
// Attachments are enabled everywhere except for Mainnet
AttachmentsEnabled: chainID != flow.Mainnet,
// Capability Controllers are enabled everywhere except for Mainnet
CapabilityControllersEnabled: chainID != flow.Mainnet,
},
)),
fvm.WithEVMEnabled(true),
}
if extensiveTracing {
options = append(options, fvm.WithExtensiveTracing())
}
return options
}