-
Notifications
You must be signed in to change notification settings - Fork 19
/
all_services.go
1114 lines (1012 loc) · 41.4 KB
/
all_services.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package protocol
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/core/activitystreak"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/banking"
"code.vegaprotocol.io/vega/core/blockchain"
"code.vegaprotocol.io/vega/core/blockchain/abci"
"code.vegaprotocol.io/vega/core/blockchain/nullchain"
"code.vegaprotocol.io/vega/core/bridges"
"code.vegaprotocol.io/vega/core/broker"
"code.vegaprotocol.io/vega/core/checkpoint"
ethclient "code.vegaprotocol.io/vega/core/client/eth"
"code.vegaprotocol.io/vega/core/collateral"
"code.vegaprotocol.io/vega/core/config"
"code.vegaprotocol.io/vega/core/datasource"
"code.vegaprotocol.io/vega/core/datasource/external/ethcall"
"code.vegaprotocol.io/vega/core/datasource/external/ethverifier"
"code.vegaprotocol.io/vega/core/datasource/spec"
"code.vegaprotocol.io/vega/core/datasource/spec/adaptors"
oracleAdaptors "code.vegaprotocol.io/vega/core/datasource/spec/adaptors"
"code.vegaprotocol.io/vega/core/delegation"
"code.vegaprotocol.io/vega/core/epochtime"
"code.vegaprotocol.io/vega/core/evtforward"
"code.vegaprotocol.io/vega/core/evtforward/ethereum"
"code.vegaprotocol.io/vega/core/execution"
"code.vegaprotocol.io/vega/core/execution/common"
"code.vegaprotocol.io/vega/core/genesis"
"code.vegaprotocol.io/vega/core/governance"
"code.vegaprotocol.io/vega/core/limits"
"code.vegaprotocol.io/vega/core/netparams"
"code.vegaprotocol.io/vega/core/netparams/checks"
"code.vegaprotocol.io/vega/core/netparams/dispatch"
"code.vegaprotocol.io/vega/core/nodewallets"
"code.vegaprotocol.io/vega/core/notary"
"code.vegaprotocol.io/vega/core/parties"
"code.vegaprotocol.io/vega/core/pow"
"code.vegaprotocol.io/vega/core/processor"
"code.vegaprotocol.io/vega/core/protocolupgrade"
"code.vegaprotocol.io/vega/core/referral"
"code.vegaprotocol.io/vega/core/rewards"
"code.vegaprotocol.io/vega/core/snapshot"
"code.vegaprotocol.io/vega/core/spam"
"code.vegaprotocol.io/vega/core/staking"
"code.vegaprotocol.io/vega/core/statevar"
"code.vegaprotocol.io/vega/core/stats"
"code.vegaprotocol.io/vega/core/teams"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/validators"
"code.vegaprotocol.io/vega/core/validators/erc20multisig"
"code.vegaprotocol.io/vega/core/vegatime"
"code.vegaprotocol.io/vega/core/vesting"
"code.vegaprotocol.io/vega/core/volumediscount"
"code.vegaprotocol.io/vega/libs/subscribers"
"code.vegaprotocol.io/vega/logging"
"code.vegaprotocol.io/vega/paths"
"code.vegaprotocol.io/vega/version"
)
type EthCallEngine interface {
Start()
StartAtHeight(height uint64, timestamp uint64)
Stop()
MakeResult(specID string, bytes []byte) (ethcall.Result, error)
CallSpec(ctx context.Context, id string, atBlock uint64) (ethcall.Result, error)
GetEthTime(ctx context.Context, atBlock uint64) (uint64, error)
GetRequiredConfirmations(id string) (uint64, error)
GetInitialTriggerTime(id string) (uint64, error)
OnSpecActivated(ctx context.Context, spec datasource.Spec) error
OnSpecDeactivated(ctx context.Context, spec datasource.Spec)
EnsureChainID(ctx context.Context, chainID string, blockInterval uint64, confirmWithClient bool)
}
type allServices struct {
ctx context.Context
log *logging.Logger
confWatcher *config.Watcher
confListenerIDs []int
conf config.Config
broker *broker.Broker
timeService *vegatime.Svc
epochService *epochtime.Svc
eventService *subscribers.Service
blockchainClient *blockchain.Client
stats *stats.Stats
vegaPaths paths.Paths
marketActivityTracker *common.MarketActivityTracker
statevar *statevar.Engine
snapshotEngine *snapshot.Engine
executionEngine *execution.Engine
governance *governance.Engine
collateral *collateral.Engine
oracle *spec.Engine
oracleAdaptors *adaptors.Adaptors
netParams *netparams.Store
delegation *delegation.Engine
limits *limits.Engine
rewards *rewards.Engine
checkpoint *checkpoint.Engine
spam *spam.Engine
pow processor.PoWEngine
builtinOracle *spec.Builtin
codec abci.Codec
ethereumOraclesVerifier *ethverifier.Verifier
partiesEngine *parties.SnapshottedEngine
assets *assets.Service
topology *validators.Topology
notary *notary.SnapshotNotary
ethCallEngine EthCallEngine
witness *validators.Witness
banking *banking.Engine
genesisHandler *genesis.Handler
protocolUpgradeEngine *protocolupgrade.Engine
teamsEngine *teams.SnapshottedEngine
referralProgram *referral.SnapshottedEngine
primaryEventForwarder *evtforward.Forwarder
primaryEventForwarderEngine EventForwarderEngine
primaryEthConfirmations *ethclient.EthereumConfirmations
primaryEthClient *ethclient.PrimaryClient
primaryBridgeView *bridges.ERC20LogicView
primaryMultisig *erc20multisig.Topology
secondaryEventForwarderEngine EventForwarderEngine
secondaryEthConfirmations *ethclient.EthereumConfirmations
secondaryEthClient *ethclient.SecondaryClient
secondaryBridgeView *bridges.ERC20LogicView
secondaryMultisig *erc20multisig.Topology
// staking
stakingAccounts *staking.Accounting
stakeVerifier *staking.StakeVerifier
stakeCheckpoint *staking.Checkpoint
commander *nodewallets.Commander
gastimator *processor.Gastimator
activityStreak *activitystreak.SnapshotEngine
vesting *vesting.SnapshotEngine
volumeDiscount *volumediscount.SnapshottedEngine
// l2 stuff
// TODO: instantiate
l2Clients *ethclient.L2Clients
l2Verifiers *ethverifier.L2Verifiers
l2CallEngines *L2EthCallEngines
}
func newServices(
ctx context.Context,
log *logging.Logger,
conf *config.Watcher,
nodeWallets *nodewallets.NodeWallets,
primaryEthClient *ethclient.PrimaryClient,
secondaryEthClient *ethclient.SecondaryClient,
primaryEthConfirmations *ethclient.EthereumConfirmations,
secondaryEthConfirmations *ethclient.EthereumConfirmations,
blockchainClient *blockchain.Client,
vegaPaths paths.Paths,
stats *stats.Stats,
l2Clients *ethclient.L2Clients,
) (_ *allServices, err error) {
svcs := &allServices{
ctx: ctx,
log: log,
confWatcher: conf,
conf: conf.Get(),
primaryEthClient: primaryEthClient,
secondaryEthClient: secondaryEthClient,
l2Clients: l2Clients,
primaryEthConfirmations: primaryEthConfirmations,
secondaryEthConfirmations: secondaryEthConfirmations,
blockchainClient: blockchainClient,
stats: stats,
vegaPaths: vegaPaths,
}
svcs.broker, err = broker.New(svcs.ctx, svcs.log, svcs.conf.Broker, stats.Blockchain)
if err != nil {
svcs.log.Error("unable to initialise broker", logging.Error(err))
return nil, err
}
svcs.timeService = vegatime.New(svcs.conf.Time, svcs.broker)
svcs.epochService = epochtime.NewService(svcs.log, svcs.conf.Epoch, svcs.broker)
// if we are not a validator, no need to instantiate the commander
if svcs.conf.IsValidator() {
// we cannot pass the Chain dependency here (that's set by the blockchain)
svcs.commander, err = nodewallets.NewCommander(
svcs.conf.NodeWallet, svcs.log, blockchainClient, nodeWallets.Vega, svcs.stats)
if err != nil {
return nil, err
}
}
svcs.genesisHandler = genesis.New(svcs.log, svcs.conf.Genesis)
svcs.genesisHandler.OnGenesisTimeLoaded(svcs.timeService.SetTimeNow)
svcs.eventService = subscribers.NewService(svcs.log, svcs.broker, svcs.conf.Broker.EventBusClientBufferSize)
svcs.collateral = collateral.New(svcs.log, svcs.conf.Collateral, svcs.timeService, svcs.broker)
svcs.limits = limits.New(svcs.log, svcs.conf.Limits, svcs.timeService, svcs.broker)
svcs.netParams = netparams.New(svcs.log, svcs.conf.NetworkParameters, svcs.broker)
svcs.primaryMultisig = erc20multisig.NewERC20MultisigTopology(svcs.conf.ERC20MultiSig, svcs.log, nil, svcs.broker, svcs.primaryEthClient, svcs.primaryEthConfirmations, svcs.netParams, "primary")
svcs.secondaryMultisig = erc20multisig.NewERC20MultisigTopology(svcs.conf.ERC20MultiSig, svcs.log, nil, svcs.broker, svcs.secondaryEthClient, svcs.secondaryEthConfirmations, svcs.netParams, "secondary")
if svcs.conf.IsValidator() {
svcs.topology = validators.NewTopology(svcs.log, svcs.conf.Validators, validators.WrapNodeWallets(nodeWallets), svcs.broker, svcs.conf.IsValidator(), svcs.commander, svcs.primaryMultisig, svcs.secondaryMultisig, svcs.timeService)
} else {
svcs.topology = validators.NewTopology(svcs.log, svcs.conf.Validators, nil, svcs.broker, svcs.conf.IsValidator(), nil, svcs.primaryMultisig, svcs.secondaryMultisig, svcs.timeService)
}
svcs.protocolUpgradeEngine = protocolupgrade.New(svcs.log, svcs.conf.ProtocolUpgrade, svcs.broker, svcs.topology, version.Get())
svcs.witness = validators.NewWitness(svcs.ctx, svcs.log, svcs.conf.Validators, svcs.topology, svcs.commander, svcs.timeService)
// this is done to go around circular deps...
svcs.primaryMultisig.SetWitness(svcs.witness)
svcs.secondaryMultisig.SetWitness(svcs.witness)
svcs.primaryEventForwarder = evtforward.New(svcs.log, svcs.conf.EvtForward, svcs.commander, svcs.timeService, svcs.topology)
if svcs.conf.HaveEthClient() {
svcs.primaryBridgeView = bridges.NewERC20LogicView(primaryEthClient, primaryEthConfirmations)
svcs.secondaryBridgeView = bridges.NewERC20LogicView(secondaryEthClient, secondaryEthConfirmations)
svcs.primaryEventForwarderEngine = evtforward.NewEngine(svcs.log, svcs.conf.EvtForward.Ethereum)
svcs.secondaryEventForwarderEngine = evtforward.NewEngine(svcs.log, svcs.conf.EvtForward.EVMBridges[0])
} else {
svcs.primaryEventForwarderEngine = evtforward.NewNoopEngine(svcs.log, svcs.conf.EvtForward.Ethereum)
svcs.secondaryEventForwarderEngine = evtforward.NewNoopEngine(svcs.log, ethereum.NewDefaultConfig())
}
svcs.oracle = spec.NewEngine(svcs.log, svcs.conf.Oracles, svcs.timeService, svcs.broker)
svcs.ethCallEngine = ethcall.NewEngine(svcs.log, svcs.conf.EvtForward.EthCall, svcs.conf.IsValidator(), svcs.primaryEthClient, svcs.primaryEventForwarder)
svcs.l2CallEngines = NewL2EthCallEngines(svcs.log, svcs.conf.EvtForward.EthCall, svcs.conf.IsValidator(), svcs.l2Clients, svcs.primaryEventForwarder, svcs.oracle.AddSpecActivationListener)
svcs.ethereumOraclesVerifier = ethverifier.New(svcs.log, svcs.witness, svcs.timeService, svcs.broker,
svcs.oracle, svcs.ethCallEngine, svcs.primaryEthConfirmations, svcs.conf.HaveEthClient())
svcs.l2Verifiers = ethverifier.NewL2Verifiers(svcs.log, svcs.witness, svcs.timeService, svcs.broker,
svcs.oracle, svcs.l2Clients, svcs.l2CallEngines, svcs.conf.IsValidator())
// Not using the activation event bus event here as on recovery the ethCallEngine needs to have all specs - is this necessary?
svcs.oracle.AddSpecActivationListener(svcs.ethCallEngine)
svcs.builtinOracle = spec.NewBuiltin(svcs.oracle, svcs.timeService)
svcs.oracleAdaptors = oracleAdaptors.New()
// this is done to go around circular deps again..s
svcs.primaryMultisig.SetEthereumEventSource(svcs.primaryEventForwarderEngine)
svcs.secondaryMultisig.SetEthereumEventSource(svcs.secondaryEventForwarderEngine)
svcs.stakingAccounts, svcs.stakeVerifier, svcs.stakeCheckpoint = staking.New(
svcs.log, svcs.conf.Staking, svcs.timeService, svcs.broker, svcs.witness, svcs.primaryEthClient, svcs.netParams, svcs.primaryEventForwarder, svcs.conf.HaveEthClient(), svcs.primaryEthConfirmations, svcs.primaryEventForwarderEngine,
)
svcs.epochService.NotifyOnEpoch(svcs.topology.OnEpochEvent, svcs.topology.OnEpochRestore)
svcs.epochService.NotifyOnEpoch(stats.OnEpochEvent, stats.OnEpochRestore)
svcs.teamsEngine = teams.NewSnapshottedEngine(svcs.broker, svcs.timeService)
svcs.partiesEngine = parties.NewSnapshottedEngine(svcs.broker)
svcs.statevar = statevar.New(svcs.log, svcs.conf.StateVar, svcs.broker, svcs.topology, svcs.commander)
svcs.marketActivityTracker = common.NewMarketActivityTracker(svcs.log, svcs.teamsEngine, svcs.stakingAccounts, svcs.broker)
svcs.notary = notary.NewWithSnapshot(svcs.log, svcs.conf.Notary, svcs.topology, svcs.broker, svcs.commander)
if svcs.conf.IsValidator() {
svcs.assets, err = assets.New(ctx, svcs.log, svcs.conf.Assets, nodeWallets.Ethereum, svcs.primaryEthClient, svcs.secondaryEthClient, svcs.broker, svcs.primaryBridgeView, svcs.secondaryBridgeView, svcs.notary, svcs.conf.HaveEthClient())
if err != nil {
return nil, fmt.Errorf("could not initialize assets engine: %w", err)
}
} else {
svcs.assets, err = assets.New(ctx, svcs.log, svcs.conf.Assets, nil, nil, nil, svcs.broker, nil, nil, svcs.notary, svcs.conf.HaveEthClient())
if err != nil {
return nil, fmt.Errorf("could not initialize assets engine: %w", err)
}
}
// TODO(): this is not pretty
svcs.topology.SetNotary(svcs.notary)
// The referral program is used to compute rewards, and can end when reaching
// the end of epoch. Since the engine will reject computations when the program
// is marked as ended, it needs to be one of the last service to register on
// epoch update, so the computation can happen for this epoch.
svcs.referralProgram = referral.NewSnapshottedEngine(svcs.broker, svcs.timeService, svcs.marketActivityTracker, svcs.stakingAccounts)
// The referral program engine must be notified of the epoch change *after* the
// market activity tracker, as it relies on computation that must happen, at
// the end of the epoch, in market activity tracker.
svcs.epochService.NotifyOnEpoch(svcs.referralProgram.OnEpoch, svcs.referralProgram.OnEpochRestore)
svcs.volumeDiscount = volumediscount.NewSnapshottedEngine(svcs.broker, svcs.marketActivityTracker)
svcs.epochService.NotifyOnEpoch(
svcs.volumeDiscount.OnEpoch,
svcs.volumeDiscount.OnEpochRestore,
)
svcs.banking = banking.New(svcs.log, svcs.conf.Banking, svcs.collateral, svcs.witness, svcs.timeService, svcs.assets, svcs.notary, svcs.broker, svcs.topology, svcs.marketActivityTracker, svcs.primaryBridgeView, svcs.secondaryBridgeView, svcs.primaryEventForwarderEngine, svcs.secondaryEventForwarderEngine)
// instantiate the execution engine
svcs.executionEngine = execution.NewEngine(
svcs.log, svcs.conf.Execution, svcs.timeService, svcs.collateral, svcs.oracle, svcs.broker, svcs.statevar,
svcs.marketActivityTracker, svcs.assets, svcs.referralProgram, svcs.volumeDiscount, svcs.banking,
)
svcs.epochService.NotifyOnEpoch(svcs.executionEngine.OnEpochEvent, svcs.executionEngine.OnEpochRestore)
svcs.epochService.NotifyOnEpoch(svcs.marketActivityTracker.OnEpochEvent, svcs.marketActivityTracker.OnEpochRestore)
svcs.epochService.NotifyOnEpoch(svcs.banking.OnEpoch, svcs.banking.OnEpochRestore)
svcs.gastimator = processor.NewGastimator(svcs.executionEngine)
svcs.spam = spam.New(svcs.log, svcs.conf.Spam, svcs.epochService, svcs.stakingAccounts)
if svcs.conf.Blockchain.ChainProvider == blockchain.ProviderNullChain {
// Use staking-loop to pretend a dummy builtin assets deposited with the faucet was staked
svcs.codec = &processor.NullBlockchainTxCodec{}
if svcs.conf.HaveEthClient() {
svcs.governance = governance.NewEngine(svcs.log, svcs.conf.Governance, svcs.stakingAccounts, svcs.timeService, svcs.broker, svcs.assets, svcs.witness, svcs.executionEngine, svcs.netParams, svcs.banking)
svcs.delegation = delegation.New(svcs.log, svcs.conf.Delegation, svcs.broker, svcs.topology, svcs.stakingAccounts, svcs.epochService, svcs.timeService)
} else {
stakingLoop := nullchain.NewStakingLoop(svcs.collateral, svcs.assets)
svcs.netParams.Watch([]netparams.WatchParam{
{
Param: netparams.RewardAsset,
Watcher: stakingLoop.OnStakingAsstUpdate,
},
}...)
svcs.governance = governance.NewEngine(svcs.log, svcs.conf.Governance, stakingLoop, svcs.timeService, svcs.broker, svcs.assets, svcs.witness, svcs.executionEngine, svcs.netParams, svcs.banking)
svcs.delegation = delegation.New(svcs.log, svcs.conf.Delegation, svcs.broker, svcs.topology, stakingLoop, svcs.epochService, svcs.timeService)
}
// disable spam protection based on config
if !svcs.conf.Blockchain.Null.SpamProtection {
svcs.spam.DisableSpamProtection() // Disable evaluation for the spam policies by the Spam Engine
}
} else {
svcs.codec = &processor.TxCodec{}
svcs.governance = governance.NewEngine(svcs.log, svcs.conf.Governance, svcs.stakingAccounts, svcs.timeService, svcs.broker, svcs.assets, svcs.witness, svcs.executionEngine, svcs.netParams, svcs.banking)
svcs.delegation = delegation.New(svcs.log, svcs.conf.Delegation, svcs.broker, svcs.topology, svcs.stakingAccounts, svcs.epochService, svcs.timeService)
}
svcs.activityStreak = activitystreak.NewSnapshotEngine(svcs.log, svcs.executionEngine, svcs.broker)
svcs.epochService.NotifyOnEpoch(
svcs.activityStreak.OnEpochEvent,
svcs.activityStreak.OnEpochRestore,
)
svcs.vesting = vesting.NewSnapshotEngine(svcs.log, svcs.collateral, svcs.activityStreak, svcs.broker, svcs.assets)
svcs.timeService.NotifyOnTick(svcs.vesting.OnTick)
svcs.rewards = rewards.New(svcs.log, svcs.conf.Rewards, svcs.broker, svcs.delegation, svcs.epochService, svcs.collateral, svcs.timeService, svcs.marketActivityTracker, svcs.topology, svcs.vesting, svcs.banking, svcs.activityStreak)
// register this after the rewards engine is created to make sure the on epoch is called in the right order.
svcs.epochService.NotifyOnEpoch(svcs.vesting.OnEpochEvent, svcs.vesting.OnEpochRestore)
svcs.registerTimeServiceCallbacks()
// checkpoint engine
svcs.checkpoint, err = checkpoint.New(svcs.log, svcs.conf.Checkpoint, svcs.assets, svcs.collateral, svcs.governance, svcs.netParams, svcs.delegation, svcs.epochService, svcs.topology, svcs.banking, svcs.stakeCheckpoint, svcs.primaryMultisig, svcs.marketActivityTracker, svcs.executionEngine)
if err != nil {
return nil, err
}
// register the callback to startup stuff when checkpoint is loaded
svcs.checkpoint.RegisterOnCheckpointLoaded(func(_ context.Context) {
// checkpoint have been loaded
// which means that genesis has been loaded as well
// we should be fully ready to start the event sourcing from ethereum
svcs.vesting.OnCheckpointLoaded()
})
svcs.genesisHandler.OnGenesisAppStateLoaded(
// be sure to keep this in order.
// the node upon genesis will load all asset first in the node
// state. This is important to happened first as we will load the
// asset which will be considered as the governance tokesvcs.
svcs.UponGenesis,
// This needs to happen always after, as it defined the network
// parameters, one of them is the Governance Token asset ID.
// which if not loaded in the previous state, then will make the node
// panic at startup.
svcs.netParams.UponGenesis,
svcs.topology.LoadValidatorsOnGenesis,
svcs.limits.UponGenesis,
svcs.checkpoint.UponGenesis,
)
svcs.snapshotEngine, err = snapshot.NewEngine(svcs.vegaPaths, svcs.conf.Snapshot, svcs.log, svcs.timeService, svcs.stats.Blockchain)
if err != nil {
return nil, fmt.Errorf("could not initialize the snapshot engine: %w", err)
}
// notify delegation, rewards, and accounting on changes in the validator pub key
svcs.topology.NotifyOnKeyChange(svcs.governance.ValidatorKeyChanged)
svcs.snapshotEngine.AddProviders(
svcs.checkpoint,
svcs.collateral,
svcs.governance,
svcs.delegation,
svcs.netParams,
svcs.epochService,
svcs.assets,
svcs.banking,
svcs.witness,
svcs.notary,
svcs.stakingAccounts,
svcs.stakeVerifier,
svcs.limits,
svcs.topology,
svcs.primaryEventForwarder,
svcs.executionEngine,
svcs.marketActivityTracker,
svcs.statevar,
svcs.primaryMultisig,
erc20multisig.NewEVMTopologies(svcs.secondaryMultisig),
svcs.protocolUpgradeEngine,
svcs.ethereumOraclesVerifier,
svcs.vesting,
svcs.activityStreak,
svcs.referralProgram,
svcs.volumeDiscount,
svcs.teamsEngine,
svcs.spam,
svcs.l2Verifiers)
pow := pow.New(svcs.log, svcs.conf.PoW)
if svcs.conf.Blockchain.ChainProvider == blockchain.ProviderNullChain {
pow.DisableVerification()
}
svcs.pow = pow
svcs.snapshotEngine.AddProviders(pow)
powWatchers := []netparams.WatchParam{
{
Param: netparams.SpamPoWNumberOfPastBlocks,
Watcher: pow.UpdateSpamPoWNumberOfPastBlocks,
},
{
Param: netparams.SpamPoWDifficulty,
Watcher: pow.UpdateSpamPoWDifficulty,
},
{
Param: netparams.SpamPoWHashFunction,
Watcher: pow.UpdateSpamPoWHashFunction,
},
{
Param: netparams.SpamPoWIncreasingDifficulty,
Watcher: pow.UpdateSpamPoWIncreasingDifficulty,
},
{
Param: netparams.SpamPoWNumberOfTxPerBlock,
Watcher: pow.UpdateSpamPoWNumberOfTxPerBlock,
},
}
// The team engine is used to know the team a party belongs to. The computation
// of the referral program rewards requires this information. Since the team
// switches happen when the end of epoch is reached, it needs to be one of the
// last services to register on epoch update, so the computation is made based
// on the team the parties belonged to during the epoch and not the new one.
svcs.epochService.NotifyOnEpoch(svcs.teamsEngine.OnEpoch, svcs.teamsEngine.OnEpochRestore)
// setup config reloads for all engines / services /etc
svcs.registerConfigWatchers()
// setup some network parameters runtime validations and network parameters
// updates dispatches this must come before we try to load from a snapshot,
// which happens in startBlockchain
if err := svcs.setupNetParameters(powWatchers); err != nil {
return nil, err
}
return svcs, nil
}
func (svcs *allServices) registerTimeServiceCallbacks() {
svcs.timeService.NotifyOnTick(
svcs.broker.OnTick,
svcs.epochService.OnTick,
svcs.builtinOracle.OnTick,
svcs.netParams.OnTick,
svcs.primaryMultisig.OnTick,
svcs.secondaryMultisig.OnTick,
svcs.witness.OnTick,
svcs.primaryEventForwarder.OnTick,
svcs.stakeVerifier.OnTick,
svcs.statevar.OnTick,
svcs.executionEngine.OnTick,
svcs.delegation.OnTick,
svcs.notary.OnTick,
svcs.banking.OnTick,
svcs.assets.OnTick,
svcs.limits.OnTick,
svcs.ethereumOraclesVerifier.OnTick,
svcs.l2Verifiers.OnTick,
)
}
func (svcs *allServices) Stop() {
svcs.confWatcher.Unregister(svcs.confListenerIDs)
svcs.primaryEventForwarderEngine.Stop()
svcs.secondaryEventForwarderEngine.Stop()
svcs.snapshotEngine.Close()
svcs.ethCallEngine.Stop()
}
func (svcs *allServices) registerConfigWatchers() {
svcs.confListenerIDs = svcs.confWatcher.OnConfigUpdateWithID(
func(cfg config.Config) { svcs.executionEngine.ReloadConf(cfg.Execution) },
func(cfg config.Config) { svcs.notary.ReloadConf(cfg.Notary) },
func(cfg config.Config) { svcs.primaryEventForwarderEngine.ReloadConf(cfg.EvtForward.Ethereum) },
func(cfg config.Config) { svcs.primaryEventForwarder.ReloadConf(cfg.EvtForward) },
func(cfg config.Config) {
if len(cfg.EvtForward.EVMBridges) > 0 {
svcs.secondaryEventForwarderEngine.ReloadConf(cfg.EvtForward.EVMBridges[0])
}
},
func(cfg config.Config) { svcs.topology.ReloadConf(cfg.Validators) },
func(cfg config.Config) { svcs.witness.ReloadConf(cfg.Validators) },
func(cfg config.Config) { svcs.assets.ReloadConf(cfg.Assets) },
func(cfg config.Config) { svcs.banking.ReloadConf(cfg.Banking) },
func(cfg config.Config) { svcs.governance.ReloadConf(cfg.Governance) },
func(cfg config.Config) { svcs.stats.ReloadConf(cfg.Stats) },
)
if svcs.conf.HaveEthClient() {
svcs.confListenerIDs = svcs.confWatcher.OnConfigUpdateWithID(
func(cfg config.Config) { svcs.l2Clients.ReloadConf(cfg.Ethereum) },
)
}
svcs.timeService.NotifyOnTick(svcs.confWatcher.OnTimeUpdate)
}
func (svcs *allServices) setupNetParameters(powWatchers []netparams.WatchParam) error {
// now we are going to setup some network parameters which can be done
// through runtime checks
// e.g: changing the governance asset require the Assets and Collateral engines, so we can ensure any changes there are made for a valid asset
if err := svcs.netParams.AddRules(
netparams.ParamStringRules(
netparams.RewardAsset,
checks.RewardAssetUpdate(svcs.log, svcs.assets, svcs.collateral),
),
); err != nil {
return err
}
spamWatchers := []netparams.WatchParam{}
if svcs.spam != nil {
spamWatchers = []netparams.WatchParam{
{
Param: netparams.SpamProtectionMaxVotes,
Watcher: svcs.spam.OnMaxVotesChanged,
},
{
Param: netparams.StakingAndDelegationRewardMinimumValidatorStake,
Watcher: svcs.spam.OnMinValidatorTokensChanged,
},
{
Param: netparams.SpamProtectionMaxProposals,
Watcher: svcs.spam.OnMaxProposalsChanged,
},
{
Param: netparams.SpamProtectionMaxDelegations,
Watcher: svcs.spam.OnMaxDelegationsChanged,
},
{
Param: netparams.SpamProtectionMinTokensForProposal,
Watcher: svcs.spam.OnMinTokensForProposalChanged,
},
{
Param: netparams.SpamProtectionMinTokensForVoting,
Watcher: svcs.spam.OnMinTokensForVotingChanged,
},
{
Param: netparams.SpamProtectionMinTokensForDelegation,
Watcher: svcs.spam.OnMinTokensForDelegationChanged,
},
{
Param: netparams.TransferMaxCommandsPerEpoch,
Watcher: svcs.spam.OnMaxTransfersChanged,
},
{
Param: netparams.SpamProtectionMinMultisigUpdates,
Watcher: svcs.spam.OnMinTokensForMultisigUpdatesChanged,
},
{
Param: netparams.SpamProtectionMaxMultisigUpdates,
Watcher: svcs.spam.OnMaxMultisigUpdatesChanged,
},
{
Param: netparams.ReferralProgramMinStakedVegaTokens,
Watcher: svcs.spam.OnMinTokensForReferral,
},
{
Param: netparams.SpamProtectionMaxCreateReferralSet,
Watcher: svcs.spam.OnMaxCreateReferralSet,
},
{
Param: netparams.SpamProtectionMaxUpdatePartyProfile,
Watcher: svcs.spam.OnMaxPartyProfile,
},
{
Param: netparams.SpamProtectionMaxUpdateReferralSet,
Watcher: svcs.spam.OnMaxUpdateReferralSet,
},
{
Param: netparams.SpamProtectionMaxApplyReferralCode,
Watcher: svcs.spam.OnMaxApplyReferralCode,
},
}
}
watchers := []netparams.WatchParam{
{
Param: netparams.SpamProtectionBalanceSnapshotFrequency,
Watcher: svcs.collateral.OnBalanceSnapshotFrequencyUpdated,
},
{
Param: netparams.MinEpochsInTeamForMetricRewardEligibility,
Watcher: svcs.marketActivityTracker.OnMinEpochsInTeamForRewardEligibilityUpdated,
},
{
Param: netparams.MinBlockCapacity,
Watcher: svcs.gastimator.OnMinBlockCapacityUpdate,
},
{
Param: netparams.MaxGasPerBlock,
Watcher: svcs.gastimator.OnMaxGasUpdate,
},
{
Param: netparams.DefaultGas,
Watcher: svcs.gastimator.OnDefaultGasUpdate,
},
{
Param: netparams.ValidatorsVoteRequired,
Watcher: svcs.protocolUpgradeEngine.OnRequiredMajorityChanged,
},
{
Param: netparams.ValidatorPerformanceScalingFactor,
Watcher: svcs.topology.OnPerformanceScalingChanged,
},
{
Param: netparams.ValidatorsEpochLength,
Watcher: svcs.topology.OnEpochLengthUpdate,
},
{
Param: netparams.NumberOfTendermintValidators,
Watcher: svcs.topology.UpdateNumberOfTendermintValidators,
},
{
Param: netparams.ValidatorIncumbentBonus,
Watcher: svcs.topology.UpdateValidatorIncumbentBonusFactor,
},
{
Param: netparams.NumberEthMultisigSigners,
Watcher: svcs.topology.UpdateNumberEthMultisigSigners,
},
{
Param: netparams.MultipleOfTendermintValidatorsForEtsatzSet,
Watcher: svcs.topology.UpdateErsatzValidatorsFactor,
},
{
Param: netparams.MinimumEthereumEventsForNewValidator,
Watcher: svcs.topology.UpdateMinimumEthereumEventsForNewValidator,
},
{
Param: netparams.StakingAndDelegationRewardMinimumValidatorStake,
Watcher: svcs.topology.UpdateMinimumRequireSelfStake,
},
{
Param: netparams.DelegationMinAmount,
Watcher: svcs.delegation.OnMinAmountChanged,
},
{
Param: netparams.RewardAsset,
Watcher: dispatch.RewardAssetUpdate(svcs.log, svcs.assets),
},
{
Param: netparams.MarketMarginScalingFactors,
Watcher: svcs.executionEngine.OnMarketMarginScalingFactorsUpdate,
},
{
Param: netparams.MarketFeeFactorsMakerFee,
Watcher: svcs.executionEngine.OnMarketFeeFactorsMakerFeeUpdate,
},
{
Param: netparams.MarketFeeFactorsInfrastructureFee,
Watcher: svcs.executionEngine.OnMarketFeeFactorsInfrastructureFeeUpdate,
},
{
Param: netparams.MarketValueWindowLength,
Watcher: svcs.executionEngine.OnMarketValueWindowLengthUpdate,
},
{
Param: netparams.BlockchainsPrimaryEthereumConfig,
Watcher: func(ctx context.Context, cfg interface{}) error {
ethCfg, err := types.EthereumConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid primary ethereum configuration: %w", err)
}
if err := svcs.primaryEthClient.UpdateEthereumConfig(ctx, ethCfg); err != nil {
return err
}
svcs.assets.SetBridgeChainID(ethCfg.ChainID(), true)
return svcs.primaryEventForwarderEngine.SetupEthereumEngine(svcs.primaryEthClient, svcs.primaryEventForwarder, svcs.conf.EvtForward.Ethereum, ethCfg, svcs.assets)
},
},
{
Param: netparams.BlockchainsEVMBridgeConfigs,
Watcher: func(ctx context.Context, cfg interface{}) error {
cfgs, err := types.EVMChainConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid secondary ethereum configuration: %w", err)
}
ethCfg := cfgs.Configs[0]
if err := svcs.secondaryEthClient.UpdateEthereumConfig(ctx, ethCfg); err != nil {
return err
}
svcs.assets.SetBridgeChainID(ethCfg.ChainID(), false)
var bridgeCfg ethereum.Config
if svcs.conf.HaveEthClient() {
bridgeCfg = svcs.conf.EvtForward.EVMBridges[0]
}
return svcs.secondaryEventForwarderEngine.SetupSecondaryEthereumEngine(svcs.secondaryEthClient, svcs.primaryEventForwarder, bridgeCfg, ethCfg, svcs.assets)
},
},
{
Param: netparams.MaxPeggedOrders,
Watcher: svcs.executionEngine.OnMaxPeggedOrderUpdate,
},
{
Param: netparams.MarketMinLpStakeQuantumMultiple,
Watcher: svcs.executionEngine.OnMinLpStakeQuantumMultipleUpdate,
},
{
Param: netparams.RewardMarketCreationQuantumMultiple,
Watcher: svcs.executionEngine.OnMarketCreationQuantumMultipleUpdate,
},
{
Param: netparams.MarketLiquidityMaximumLiquidityFeeFactorLevel,
Watcher: svcs.executionEngine.OnMarketLiquidityMaximumLiquidityFeeFactorLevelUpdate,
},
{
Param: netparams.MarketAuctionMinimumDuration,
Watcher: svcs.executionEngine.OnMarketAuctionMinimumDurationUpdate,
},
{
Param: netparams.MarketAuctionMaximumDuration,
Watcher: svcs.executionEngine.OnMarketAuctionMaximumDurationUpdate,
},
{
Param: netparams.MarketProbabilityOfTradingTauScaling,
Watcher: svcs.executionEngine.OnMarketProbabilityOfTradingTauScalingUpdate,
},
{
Param: netparams.MarketMinProbabilityOfTradingForLPOrders,
Watcher: svcs.executionEngine.OnMarketMinProbabilityOfTradingForLPOrdersUpdate,
},
// Liquidity version 2.
{
Param: netparams.MarketLiquidityBondPenaltyParameter,
Watcher: svcs.executionEngine.OnMarketLiquidityV2BondPenaltyUpdate,
},
{
Param: netparams.MarketLiquidityEarlyExitPenalty,
Watcher: svcs.executionEngine.OnMarketLiquidityV2EarlyExitPenaltyUpdate,
},
{
Param: netparams.MarketLiquidityMaximumLiquidityFeeFactorLevel,
Watcher: svcs.executionEngine.OnMarketLiquidityV2MaximumLiquidityFeeFactorLevelUpdate,
},
{
Param: netparams.MarketLiquiditySLANonPerformanceBondPenaltySlope,
Watcher: svcs.executionEngine.OnMarketLiquidityV2SLANonPerformanceBondPenaltySlopeUpdate,
},
{
Param: netparams.MarketLiquiditySLANonPerformanceBondPenaltyMax,
Watcher: svcs.executionEngine.OnMarketLiquidityV2SLANonPerformanceBondPenaltyMaxUpdate,
},
{
Param: netparams.MarketLiquidityStakeToCCYVolume,
Watcher: svcs.executionEngine.OnMarketLiquidityV2StakeToCCYVolumeUpdate,
},
{
Param: netparams.MarketLiquidityProvidersFeeCalculationTimeStep,
Watcher: svcs.executionEngine.OnMarketLiquidityV2ProvidersFeeCalculationTimeStep,
},
// End of liquidity version 2.
{
Param: netparams.ValidatorsEpochLength,
Watcher: svcs.epochService.OnEpochLengthUpdate,
},
{
Param: netparams.StakingAndDelegationRewardMaxPayoutPerParticipant,
Watcher: svcs.rewards.UpdateMaxPayoutPerParticipantForStakingRewardScheme,
},
{
Param: netparams.StakingAndDelegationRewardDelegatorShare,
Watcher: svcs.rewards.UpdateDelegatorShareForStakingRewardScheme,
},
{
Param: netparams.StakingAndDelegationRewardMinimumValidatorStake,
Watcher: svcs.rewards.UpdateMinimumValidatorStakeForStakingRewardScheme,
},
{
Param: netparams.RewardAsset,
Watcher: svcs.rewards.UpdateAssetForStakingAndDelegation,
},
{
Param: netparams.StakingAndDelegationRewardCompetitionLevel,
Watcher: svcs.rewards.UpdateCompetitionLevelForStakingRewardScheme,
},
{
Param: netparams.StakingAndDelegationRewardsMinValidators,
Watcher: svcs.rewards.UpdateMinValidatorsStakingRewardScheme,
},
{
Param: netparams.StakingAndDelegationRewardOptimalStakeMultiplier,
Watcher: svcs.rewards.UpdateOptimalStakeMultiplierStakingRewardScheme,
},
{
Param: netparams.ErsatzvalidatorsRewardFactor,
Watcher: svcs.rewards.UpdateErsatzRewardFactor,
},
{
Param: netparams.ValidatorsVoteRequired,
Watcher: svcs.witness.OnDefaultValidatorsVoteRequiredUpdate,
},
{
Param: netparams.ValidatorsVoteRequired,
Watcher: svcs.notary.OnDefaultValidatorsVoteRequiredUpdate,
},
{
Param: netparams.NetworkCheckpointTimeElapsedBetweenCheckpoints,
Watcher: svcs.checkpoint.OnTimeElapsedUpdate,
},
{
Param: netparams.SnapshotIntervalLength,
Watcher: svcs.snapshotEngine.OnSnapshotIntervalUpdate,
},
{
Param: netparams.ValidatorsVoteRequired,
Watcher: svcs.statevar.OnDefaultValidatorsVoteRequiredUpdate,
},
{
Param: netparams.FloatingPointUpdatesDuration,
Watcher: svcs.statevar.OnFloatingPointUpdatesDurationUpdate,
},
{
Param: netparams.TransferFeeFactor,
Watcher: svcs.banking.OnTransferFeeFactorUpdate,
},
{
Param: netparams.TransferFeeMaxQuantumAmount,
Watcher: svcs.banking.OnMaxQuantumAmountUpdate,
},
{
Param: netparams.TransferFeeDiscountDecayFraction,
Watcher: svcs.banking.OnTransferFeeDiscountDecayFractionUpdate,
},
{
Param: netparams.TransferFeeDiscountMinimumTrackedAmount,
Watcher: svcs.banking.OnTransferFeeDiscountMinimumTrackedAmountUpdate,
},
{
Param: netparams.GovernanceTransferMaxFraction,
Watcher: svcs.banking.OnMaxFractionChanged,
},
{
Param: netparams.GovernanceTransferMaxAmount,
Watcher: svcs.banking.OnMaxAmountChanged,
},
{
Param: netparams.TransferMinTransferQuantumMultiple,
Watcher: svcs.banking.OnMinTransferQuantumMultiple,
},
{
Param: netparams.SpamProtectionMinimumWithdrawalQuantumMultiple,
Watcher: svcs.banking.OnMinWithdrawQuantumMultiple,
},
{
Param: netparams.BlockchainsPrimaryEthereumConfig,
Watcher: func(_ context.Context, cfg interface{}) error {
// nothing to do if not a validator
if !svcs.conf.HaveEthClient() {
return nil
}
ethCfg, err := types.EthereumConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid primary ethereum configuration: %w", err)
}
svcs.primaryEthConfirmations.UpdateConfirmations(ethCfg.Confirmations())
return nil
},
},
{
Param: netparams.BlockchainsEVMBridgeConfigs,
Watcher: func(_ context.Context, cfg interface{}) error {
// nothing to do if not a validator
if !svcs.conf.HaveEthClient() {
return nil
}
ethCfg, err := types.EVMChainConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid secondary ethereum configuration: %w", err)
}
svcs.secondaryEthConfirmations.UpdateConfirmations(ethCfg.Configs[0].Confirmations())
return nil
},
},
{
Param: netparams.BlockchainsPrimaryEthereumConfig,
Watcher: func(ctx context.Context, cfg interface{}) error {
ethCfg, err := types.EthereumConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid primary ethereum configuration: %w", err)
}
// every 1 block for the main ethereum chain is acceptable
svcs.ethCallEngine.EnsureChainID(ctx, ethCfg.ChainID(), 1, svcs.conf.HaveEthClient())
// nothing to do if not a validator
if !svcs.conf.HaveEthClient() {
return nil
}
svcs.witness.SetPrimaryDefaultConfirmations(ethCfg.ChainID(), ethCfg.Confirmations())
return nil
},
},
{
Param: netparams.BlockchainsPrimaryEthereumConfig,
Watcher: func(_ context.Context, cfg interface{}) error {
ethCfg, err := types.EthereumConfigFromUntypedProto(cfg)
if err != nil {
return fmt.Errorf("invalid primary ethereum configuration: %w", err)
}
svcs.banking.OnPrimaryEthChainIDUpdated(ethCfg.ChainID())
return nil
},
},
{
Param: netparams.BlockchainsEVMBridgeConfigs,
Watcher: func(_ context.Context, cfgs interface{}) error {
ethCfgs, err := types.EVMChainConfigFromUntypedProto(cfgs)
if err != nil {
return fmt.Errorf("invalid secondary ethereum configuration: %w", err)
}
ethCfg := ethCfgs.Configs[0]
svcs.banking.OnSecondaryEthChainIDUpdated(ethCfg.ChainID())
svcs.witness.SetSecondaryDefaultConfirmations(ethCfg.ChainID(), ethCfg.Confirmations(), ethCfg.BlockTime())
return nil
},
},
{