-
Notifications
You must be signed in to change notification settings - Fork 199
/
vmContainerFactory.go
356 lines (306 loc) · 12.8 KB
/
vmContainerFactory.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package shard
import (
"fmt"
"io"
"sort"
arwen12 "github.com/ElrondNetwork/arwen-wasm-vm/v1_2/arwen"
arwenHost12 "github.com/ElrondNetwork/arwen-wasm-vm/v1_2/arwen/host"
arwen13 "github.com/ElrondNetwork/arwen-wasm-vm/v1_3/arwen"
arwenHost13 "github.com/ElrondNetwork/arwen-wasm-vm/v1_3/arwen/host"
arwen14 "github.com/ElrondNetwork/arwen-wasm-vm/v1_4/arwen"
arwenHost14 "github.com/ElrondNetwork/arwen-wasm-vm/v1_4/arwen/host"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/factory"
"github.com/ElrondNetwork/elrond-go/process/factory/containers"
"github.com/ElrondNetwork/elrond-go/process/smartContract/hooks"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
var _ process.VirtualMachinesContainerFactory = (*vmContainerFactory)(nil)
var logVMContainerFactory = logger.GetOrCreate("vmContainerFactory")
type vmContainerFactory struct {
config config.VirtualMachineConfig
blockChainHook process.BlockChainHookHandler
cryptoHook vmcommon.CryptoHook
blockGasLimit uint64
gasSchedule core.GasScheduleNotifier
builtinFunctions vmcommon.BuiltInFunctionContainer
epochNotifier process.EpochNotifier
epochConfig config.EnableEpochs
container process.VirtualMachinesContainer
arwenVersions []config.ArwenVersionByEpoch
arwenChangeLocker common.Locker
esdtTransferParser vmcommon.ESDTTransferParser
}
// ArgVMContainerFactory defines the arguments needed to the new VM factory
type ArgVMContainerFactory struct {
Config config.VirtualMachineConfig
BlockGasLimit uint64
GasSchedule core.GasScheduleNotifier
EpochNotifier process.EpochNotifier
EpochConfig config.EnableEpochs
ArwenChangeLocker common.Locker
ESDTTransferParser vmcommon.ESDTTransferParser
BuiltInFunctions vmcommon.BuiltInFunctionContainer
BlockChainHook process.BlockChainHookHandler
}
// NewVMContainerFactory is responsible for creating a new virtual machine factory object
func NewVMContainerFactory(args ArgVMContainerFactory) (*vmContainerFactory, error) {
if check.IfNil(args.GasSchedule) {
return nil, process.ErrNilGasSchedule
}
if check.IfNil(args.EpochNotifier) {
return nil, process.ErrNilEpochNotifier
}
if check.IfNilReflect(args.ArwenChangeLocker) {
return nil, process.ErrNilLocker
}
if check.IfNil(args.ESDTTransferParser) {
return nil, process.ErrNilESDTTransferParser
}
if check.IfNil(args.BuiltInFunctions) {
return nil, process.ErrNilBuiltInFunction
}
if check.IfNil(args.BlockChainHook) {
return nil, process.ErrNilBlockChainHook
}
cryptoHook := hooks.NewVMCryptoHook()
vmf := &vmContainerFactory{
config: args.Config,
blockChainHook: args.BlockChainHook,
cryptoHook: cryptoHook,
blockGasLimit: args.BlockGasLimit,
gasSchedule: args.GasSchedule,
builtinFunctions: args.BuiltInFunctions,
epochNotifier: args.EpochNotifier,
epochConfig: args.EpochConfig,
container: nil,
arwenChangeLocker: args.ArwenChangeLocker,
esdtTransferParser: args.ESDTTransferParser,
}
vmf.arwenVersions = args.Config.ArwenVersions
vmf.sortArwenVersions()
err := vmf.validateArwenVersions()
if err != nil {
return nil, err
}
return vmf, nil
}
func (vmf *vmContainerFactory) sortArwenVersions() {
sort.Slice(vmf.arwenVersions, func(i, j int) bool {
return vmf.arwenVersions[i].StartEpoch < vmf.arwenVersions[j].StartEpoch
})
}
func (vmf *vmContainerFactory) validateArwenVersions() error {
if len(vmf.arwenVersions) == 0 {
return ErrEmptyVersionsByEpochsList
}
currentEpoch := uint32(0)
for idx, ver := range vmf.arwenVersions {
if idx == 0 && ver.StartEpoch != 0 {
return fmt.Errorf("%w first version should start on epoch 0",
ErrInvalidVersionOnEpochValues)
}
if idx > 0 && currentEpoch >= ver.StartEpoch {
return fmt.Errorf("%w, StartEpoch is greater or equal to next epoch StartEpoch value, version %s",
ErrInvalidVersionOnEpochValues, ver.Version)
}
currentEpoch = ver.StartEpoch
if len(ver.Version) > common.MaxSoftwareVersionLengthInBytes {
return fmt.Errorf("%w for version %s",
ErrInvalidVersionStringTooLong, ver.Version)
}
}
return nil
}
// Create sets up all the needed virtual machine returning a container of all the VMs
func (vmf *vmContainerFactory) Create() (process.VirtualMachinesContainer, error) {
container := containers.NewVirtualMachinesContainer()
vmf.arwenChangeLocker.Lock()
version := vmf.getMatchingVersion(vmf.epochNotifier.CurrentEpoch())
currentVM, err := vmf.createArwenVM(version)
if err != nil {
vmf.arwenChangeLocker.Unlock()
return nil, err
}
err = container.Add(factory.ArwenVirtualMachine, currentVM)
if err != nil {
vmf.arwenChangeLocker.Unlock()
return nil, err
}
// The vmContainerFactory keeps a reference to the container it has created,
// in order to replace, from within the container, the VM instances that
// become out-of-date after specific epochs.
vmf.container = container
vmf.arwenChangeLocker.Unlock()
vmf.epochNotifier.RegisterNotifyHandler(vmf)
vmf.gasSchedule.RegisterNotifyHandler(vmf)
return container, nil
}
// Close closes the vm container factory
func (vmf *vmContainerFactory) Close() error {
errContainer := vmf.container.Close()
if errContainer != nil {
logVMContainerFactory.Error("cannot close container", "error", errContainer)
}
errBlockchain := vmf.blockChainHook.Close()
if errBlockchain != nil {
logVMContainerFactory.Error("cannot close blockchain hook implementation", "error", errBlockchain)
}
if errContainer != nil {
return errContainer
}
return errBlockchain
}
// GasScheduleChange updates the gas schedule map in all the components
func (vmf *vmContainerFactory) GasScheduleChange(gasSchedule map[string]map[string]uint64) {
// clear compiled codes always before changing gasSchedule
vmf.blockChainHook.ClearCompiledCodes()
for _, key := range vmf.container.Keys() {
currentVM, err := vmf.container.Get(key)
if err != nil {
logVMContainerFactory.Error("cannot get VM on GasSchedule Change", "error", err)
continue
}
currentVM.GasScheduleChange(gasSchedule)
}
}
// EpochConfirmed updates the VM version in the container, depending on the epoch
func (vmf *vmContainerFactory) EpochConfirmed(epoch uint32, _ uint64) {
vmf.ensureCorrectArwenVersion(epoch)
}
func (vmf *vmContainerFactory) ensureCorrectArwenVersion(epoch uint32) {
newVersion := vmf.getMatchingVersion(epoch)
currentArwenVM, err := vmf.container.Get(factory.ArwenVirtualMachine)
if err != nil {
logVMContainerFactory.Error("cannot retrieve Arwen VM from container", "epoch", epoch, "error", err)
return
}
if !vmf.shouldReplaceArwenInstance(newVersion, currentArwenVM) {
return
}
vmf.arwenChangeLocker.Lock()
defer vmf.arwenChangeLocker.Unlock()
vmf.closePreviousVM(currentArwenVM)
newArwenVM, err := vmf.createArwenVM(newVersion)
if err != nil {
logVMContainerFactory.Error("cannot replace Arwen VM", "epoch", epoch, "error", err)
return
}
err = vmf.container.Replace(factory.ArwenVirtualMachine, newArwenVM)
if err != nil {
logVMContainerFactory.Error("cannot replace Arwen VM", "epoch", epoch, "error", err)
return
}
logVMContainerFactory.Debug("Arwen VM replaced", "epoch", epoch)
}
func (vmf *vmContainerFactory) shouldReplaceArwenInstance(
newVersion config.ArwenVersionByEpoch,
currentVM vmcommon.VMExecutionHandler,
) bool {
specificVersionRequired := newVersion.Version != "*"
differentVersion := newVersion.Version != currentVM.GetVersion()
return specificVersionRequired && differentVersion
}
func (vmf *vmContainerFactory) createArwenVM(version config.ArwenVersionByEpoch) (vmcommon.VMExecutionHandler, error) {
currentVM, err := vmf.createInProcessArwenVMByVersion(version)
if err != nil {
return nil, err
}
return currentVM, nil
}
func (vmf *vmContainerFactory) getMatchingVersion(epoch uint32) config.ArwenVersionByEpoch {
matchingVersion := vmf.arwenVersions[len(vmf.arwenVersions)-1]
for idx := 0; idx < len(vmf.arwenVersions)-1; idx++ {
crtVer := vmf.arwenVersions[idx]
nextVer := vmf.arwenVersions[idx+1]
if crtVer.StartEpoch <= epoch && epoch < nextVer.StartEpoch {
return crtVer
}
}
return matchingVersion
}
func (vmf *vmContainerFactory) createInProcessArwenVMByVersion(version config.ArwenVersionByEpoch) (vmcommon.VMExecutionHandler, error) {
logVMContainerFactory.Debug("createInProcessArwenVM", "version", version)
switch version.Version {
case "v1.2":
return vmf.createInProcessArwenVMV12()
case "v1.3":
return vmf.createInProcessArwenVMV13()
default:
return vmf.createInProcessArwenVMV14()
}
}
func (vmf *vmContainerFactory) createInProcessArwenVMV12() (vmcommon.VMExecutionHandler, error) {
hostParameters := &arwen12.VMHostParameters{
VMType: factory.ArwenVirtualMachine,
BlockGasLimit: vmf.blockGasLimit,
GasSchedule: vmf.gasSchedule.LatestGasSchedule(),
ProtocolBuiltinFunctions: vmf.builtinFunctions.Keys(),
ElrondProtectedKeyPrefix: []byte(core.ElrondProtectedKeyPrefix),
ArwenV2EnableEpoch: vmf.epochConfig.SCDeployEnableEpoch,
AheadOfTimeEnableEpoch: vmf.epochConfig.AheadOfTimeGasUsageEnableEpoch,
DynGasLockEnableEpoch: vmf.epochConfig.SCDeployEnableEpoch,
ArwenV3EnableEpoch: vmf.epochConfig.RepairCallbackEnableEpoch,
}
return arwenHost12.NewArwenVM(vmf.blockChainHook, hostParameters)
}
func (vmf *vmContainerFactory) createInProcessArwenVMV13() (vmcommon.VMExecutionHandler, error) {
hostParameters := &arwen13.VMHostParameters{
VMType: factory.ArwenVirtualMachine,
BlockGasLimit: vmf.blockGasLimit,
GasSchedule: vmf.gasSchedule.LatestGasSchedule(),
BuiltInFuncContainer: vmf.builtinFunctions,
ElrondProtectedKeyPrefix: []byte(core.ElrondProtectedKeyPrefix),
ArwenV2EnableEpoch: vmf.epochConfig.SCDeployEnableEpoch,
AheadOfTimeEnableEpoch: vmf.epochConfig.AheadOfTimeGasUsageEnableEpoch,
DynGasLockEnableEpoch: vmf.epochConfig.SCDeployEnableEpoch,
ArwenV3EnableEpoch: vmf.epochConfig.RepairCallbackEnableEpoch,
}
return arwenHost13.NewArwenVM(vmf.blockChainHook, hostParameters)
}
func (vmf *vmContainerFactory) createInProcessArwenVMV14() (vmcommon.VMExecutionHandler, error) {
hostParameters := &arwen14.VMHostParameters{
VMType: factory.ArwenVirtualMachine,
BlockGasLimit: vmf.blockGasLimit,
GasSchedule: vmf.gasSchedule.LatestGasSchedule(),
BuiltInFuncContainer: vmf.builtinFunctions,
ElrondProtectedKeyPrefix: []byte(core.ElrondProtectedKeyPrefix),
ESDTTransferParser: vmf.esdtTransferParser,
EpochNotifier: vmf.epochNotifier,
WasmerSIGSEGVPassthrough: vmf.config.WasmerSIGSEGVPassthrough,
TimeOutForSCExecutionInMilliseconds: vmf.config.TimeOutForSCExecutionInMilliseconds,
MultiESDTTransferAsyncCallBackEnableEpoch: vmf.epochConfig.MultiESDTTransferFixOnCallBackOnEnableEpoch,
FixOOGReturnCodeEnableEpoch: vmf.epochConfig.FixOOGReturnCodeEnableEpoch,
RemoveNonUpdatedStorageEnableEpoch: vmf.epochConfig.RemoveNonUpdatedStorageEnableEpoch,
CreateNFTThroughExecByCallerEnableEpoch: vmf.epochConfig.CreateNFTThroughExecByCallerEnableEpoch,
UseDifferentGasCostForReadingCachedStorageEpoch: vmf.epochConfig.StorageAPICostOptimizationEnableEpoch,
DisableExecByCallerEnableEpoch: vmf.epochConfig.DisableExecByCallerEnableEpoch,
FixFailExecutionOnErrorEnableEpoch: vmf.epochConfig.FailExecutionOnEveryAPIErrorEnableEpoch,
ManagedCryptoAPIEnableEpoch: vmf.epochConfig.ManagedCryptoAPIsEnableEpoch,
RefactorContextEnableEpoch: vmf.epochConfig.RefactorContextEnableEpoch,
CheckExecuteReadOnlyEnableEpoch: vmf.epochConfig.CheckExecuteOnReadOnlyEnableEpoch,
}
return arwenHost14.NewArwenVM(vmf.blockChainHook, hostParameters)
}
func (vmf *vmContainerFactory) closePreviousVM(vm vmcommon.VMExecutionHandler) {
vmf.blockChainHook.ClearCompiledCodes()
logVMContainerFactory.Debug("AOT compilation cache cleared")
vmAsCloser, ok := vm.(io.Closer)
if ok {
err := vmAsCloser.Close()
logVMContainerFactory.LogIfError(err, "closePreviousVM", "error", err)
}
}
// BlockChainHookImpl returns the created blockChainHookImpl
func (vmf *vmContainerFactory) BlockChainHookImpl() process.BlockChainHookHandler {
return vmf.blockChainHook
}
// IsInterfaceNil returns true if there is no value under the interface
func (vmf *vmContainerFactory) IsInterfaceNil() bool {
return vmf == nil
}