-
Notifications
You must be signed in to change notification settings - Fork 199
/
vmContainerFactory.go
276 lines (241 loc) · 9.73 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
package metachain
import (
"fmt"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"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"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/sharding/nodesCoordinator"
"github.com/ElrondNetwork/elrond-go/state"
"github.com/ElrondNetwork/elrond-go/vm"
systemVMFactory "github.com/ElrondNetwork/elrond-go/vm/factory"
systemVMProcess "github.com/ElrondNetwork/elrond-go/vm/process"
"github.com/ElrondNetwork/elrond-go/vm/systemSmartContracts"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
"github.com/ElrondNetwork/elrond-vm-common/parsers"
)
var _ process.VirtualMachinesContainerFactory = (*vmContainerFactory)(nil)
type vmContainerFactory struct {
chanceComputer nodesCoordinator.ChanceComputer
validatorAccountsDB state.AccountsAdapter
blockChainHook process.BlockChainHookHandler
cryptoHook vmcommon.CryptoHook
systemContracts vm.SystemSCContainer
economics process.EconomicsDataHandler
messageSigVerifier vm.MessageSignVerifier
nodesConfigProvider vm.NodesConfigProvider
gasSchedule core.GasScheduleNotifier
hasher hashing.Hasher
marshalizer marshal.Marshalizer
systemSCConfig *config.SystemSmartContractsConfig
addressPubKeyConverter core.PubkeyConverter
scFactory vm.SystemSCContainerFactory
shardCoordinator sharding.Coordinator
enableEpochsHandler common.EnableEpochsHandler
}
// ArgsNewVMContainerFactory defines the arguments needed to create a new VM container factory
type ArgsNewVMContainerFactory struct {
Economics process.EconomicsDataHandler
MessageSignVerifier vm.MessageSignVerifier
GasSchedule core.GasScheduleNotifier
NodesConfigProvider vm.NodesConfigProvider
Hasher hashing.Hasher
Marshalizer marshal.Marshalizer
SystemSCConfig *config.SystemSmartContractsConfig
ValidatorAccountsDB state.AccountsAdapter
ChanceComputer nodesCoordinator.ChanceComputer
ShardCoordinator sharding.Coordinator
PubkeyConv core.PubkeyConverter
BlockChainHook process.BlockChainHookHandler
EnableEpochsHandler common.EnableEpochsHandler
}
// NewVMContainerFactory is responsible for creating a new virtual machine factory object
func NewVMContainerFactory(args ArgsNewVMContainerFactory) (*vmContainerFactory, error) {
if check.IfNil(args.Economics) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilEconomicsData)
}
if check.IfNil(args.MessageSignVerifier) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilKeyGen)
}
if check.IfNil(args.NodesConfigProvider) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilNodesConfigProvider)
}
if check.IfNil(args.Hasher) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilHasher)
}
if check.IfNil(args.Marshalizer) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilMarshalizer)
}
if args.SystemSCConfig == nil {
return nil, fmt.Errorf("%w in NewVMContainerFactory", process.ErrNilSystemSCConfig)
}
if check.IfNil(args.ValidatorAccountsDB) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", vm.ErrNilValidatorAccountsDB)
}
if check.IfNil(args.ChanceComputer) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", vm.ErrNilChanceComputer)
}
if check.IfNil(args.GasSchedule) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", vm.ErrNilGasSchedule)
}
if check.IfNil(args.PubkeyConv) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", vm.ErrNilAddressPubKeyConverter)
}
if check.IfNil(args.ShardCoordinator) {
return nil, fmt.Errorf("%w in NewVMContainerFactory", vm.ErrNilShardCoordinator)
}
if check.IfNil(args.BlockChainHook) {
return nil, process.ErrNilBlockChainHook
}
if check.IfNil(args.EnableEpochsHandler) {
return nil, vm.ErrNilEnableEpochsHandler
}
cryptoHook := hooks.NewVMCryptoHook()
return &vmContainerFactory{
blockChainHook: args.BlockChainHook,
cryptoHook: cryptoHook,
economics: args.Economics,
messageSigVerifier: args.MessageSignVerifier,
gasSchedule: args.GasSchedule,
nodesConfigProvider: args.NodesConfigProvider,
hasher: args.Hasher,
marshalizer: args.Marshalizer,
systemSCConfig: args.SystemSCConfig,
validatorAccountsDB: args.ValidatorAccountsDB,
chanceComputer: args.ChanceComputer,
addressPubKeyConverter: args.PubkeyConv,
shardCoordinator: args.ShardCoordinator,
enableEpochsHandler: args.EnableEpochsHandler,
}, nil
}
// Create sets up all the needed virtual machines returning a container of all the VMs
func (vmf *vmContainerFactory) Create() (process.VirtualMachinesContainer, error) {
container := containers.NewVirtualMachinesContainer()
currVm, err := vmf.createSystemVM()
if err != nil {
return nil, err
}
err = container.Add(factory.SystemVirtualMachine, currVm)
if err != nil {
return nil, err
}
return container, nil
}
// Close closes the vm container factory
func (vmf *vmContainerFactory) Close() error {
return vmf.blockChainHook.Close()
}
// CreateForGenesis sets up all the needed virtual machines returning a container of all the VMs to be used in the genesis process
// The system VM will have to contain the following and only following system smartcontracts:
// staking SC, validator SC, ESDT SC and governance SC. Including more system smartcontracts (or less) will trigger root hash mismatch
// errors when trying to sync the first metablock after the genesis event.
func (vmf *vmContainerFactory) CreateForGenesis() (process.VirtualMachinesContainer, error) {
container := containers.NewVirtualMachinesContainer()
currVm, err := vmf.createSystemVMForGenesis()
if err != nil {
return nil, err
}
err = container.Add(factory.SystemVirtualMachine, currVm)
if err != nil {
return nil, err
}
return container, nil
}
func (vmf *vmContainerFactory) createSystemVMFactoryAndEEI() (vm.SystemSCContainerFactory, vm.ContextHandler, error) {
atArgumentParser := parsers.NewCallArgsParser()
vmContextArgs := systemSmartContracts.VMContextArgs{
BlockChainHook: vmf.blockChainHook,
CryptoHook: vmf.cryptoHook,
InputParser: atArgumentParser,
ValidatorAccountsDB: vmf.validatorAccountsDB,
ChanceComputer: vmf.chanceComputer,
EnableEpochsHandler: vmf.enableEpochsHandler,
}
systemEI, err := systemSmartContracts.NewVMContext(vmContextArgs)
if err != nil {
return nil, nil, err
}
argsNewSystemScFactory := systemVMFactory.ArgsNewSystemSCFactory{
SystemEI: systemEI,
SigVerifier: vmf.messageSigVerifier,
GasSchedule: vmf.gasSchedule,
NodesConfigProvider: vmf.nodesConfigProvider,
Hasher: vmf.hasher,
Marshalizer: vmf.marshalizer,
SystemSCConfig: vmf.systemSCConfig,
Economics: vmf.economics,
AddressPubKeyConverter: vmf.addressPubKeyConverter,
ShardCoordinator: vmf.shardCoordinator,
EnableEpochsHandler: vmf.enableEpochsHandler,
}
scFactory, err := systemVMFactory.NewSystemSCFactory(argsNewSystemScFactory)
if err != nil {
return nil, nil, err
}
return scFactory, systemEI, nil
}
func (vmf *vmContainerFactory) finalizeSystemVMCreation(systemEI vm.ContextHandler) (vmcommon.VMExecutionHandler, error) {
err := systemEI.SetSystemSCContainer(vmf.systemContracts)
if err != nil {
return nil, err
}
argsNewSystemVM := systemVMProcess.ArgsNewSystemVM{
SystemEI: systemEI,
SystemContracts: vmf.systemContracts,
VmType: factory.SystemVirtualMachine,
GasSchedule: vmf.gasSchedule,
}
systemVM, err := systemVMProcess.NewSystemVM(argsNewSystemVM)
if err != nil {
return nil, err
}
vmf.gasSchedule.RegisterNotifyHandler(systemVM)
return systemVM, nil
}
func (vmf *vmContainerFactory) createSystemVM() (vmcommon.VMExecutionHandler, error) {
scFactory, systemEI, err := vmf.createSystemVMFactoryAndEEI()
if err != nil {
return nil, err
}
vmf.systemContracts, err = scFactory.Create()
if err != nil {
return nil, err
}
vmf.scFactory = scFactory
return vmf.finalizeSystemVMCreation(systemEI)
}
// createSystemVMForGenesis will create the same VMExecutionHandler structure used when the mainnet genesis was created
func (vmf *vmContainerFactory) createSystemVMForGenesis() (vmcommon.VMExecutionHandler, error) {
scFactory, systemEI, err := vmf.createSystemVMFactoryAndEEI()
if err != nil {
return nil, err
}
vmf.systemContracts, err = scFactory.CreateForGenesis()
if err != nil {
return nil, err
}
return vmf.finalizeSystemVMCreation(systemEI)
}
// BlockChainHookImpl returns the created blockChainHookImpl
func (vmf *vmContainerFactory) BlockChainHookImpl() process.BlockChainHookHandler {
return vmf.blockChainHook
}
// SystemSmartContractContainer return the created system smart contracts
func (vmf *vmContainerFactory) SystemSmartContractContainer() vm.SystemSCContainer {
return vmf.systemContracts
}
// SystemSmartContractContainerFactory returns the system smart contract container factory
func (vmf *vmContainerFactory) SystemSmartContractContainerFactory() vm.SystemSCContainerFactory {
return vmf.scFactory
}
// IsInterfaceNil returns true if there is no value under the interface
func (vmf *vmContainerFactory) IsInterfaceNil() bool {
return vmf == nil
}