-
Notifications
You must be signed in to change notification settings - Fork 179
/
bootstrap.go
946 lines (806 loc) · 25.9 KB
/
bootstrap.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
package fvm
import (
"fmt"
"math"
"github.com/onflow/cadence"
"github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/flow-go/fvm/blueprints"
"github.com/onflow/flow-go/fvm/derived"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/meter"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/epochs"
)
var (
DefaultAccountCreationFee = mustParseUFix64(
"account creation fee",
"0.00100000")
DefaultMinimumStorageReservation = mustParseUFix64(
"minimum storage reservation",
"0.00100000")
DefaultStorageMBPerFLOW = mustParseUFix64(
"storage mb per flow",
"100.00000000")
// DefaultTransactionFees are the default transaction fee parameters if
// transaction fees are on. Surge factor is 1.0, inclusion effort cost is
// 0.0001 (because the static inclusion effort is 1.0) and execution effort
// cost is 0.0 because dynamic execution fees are off. If they are off
// (which is the default behaviour) that means the transaction fees are 0.0.
DefaultTransactionFees = BootstrapProcedureFeeParameters{
SurgeFactor: mustParseUFix64("fee surge factor", "1.0"),
InclusionEffortCost: mustParseUFix64(
"fee inclusion effort cost",
"0.00001"),
ExecutionEffortCost: mustParseUFix64(
"fee execution effort cost",
"0.0"),
}
// DefaultVersionFreezePeriod is the default NodeVersionBeacon freeze period -
// the number of blocks in the future where the version changes are frozen.
DefaultVersionFreezePeriod = cadence.UInt64(1000)
)
func mustParseUFix64(name string, valueString string) cadence.UFix64 {
value, err := cadence.NewUFix64(valueString)
if err != nil {
panic(fmt.Errorf("invalid default %s: %w", name, err))
}
return value
}
// A BootstrapProcedure is an invokable that can be used to bootstrap the ledger state
// with the default accounts and contracts required by the Flow virtual machine.
type BootstrapProcedure struct {
BootstrapParams
}
type BootstrapParams struct {
rootBlock *flow.Header
// genesis parameters
accountKeys BootstrapAccountKeys
initialTokenSupply cadence.UFix64
accountCreationFee cadence.UFix64
minimumStorageReservation cadence.UFix64
storagePerFlow cadence.UFix64
restrictedAccountCreationEnabled cadence.Bool
// versionFreezePeriod is the number of blocks in the future where the version
// changes are frozen. The Node version beacon manages the freeze period,
// but this is the value used when first deploying the contract, during the
// bootstrap procedure.
versionFreezePeriod cadence.UInt64
// TODO: restrictedContractDeployment should be a bool after RestrictedDeploymentEnabled is removed from the context
// restrictedContractDeployment of nil means that the contract deployment is taken from the fvm Context instead of from the state.
// This can be used to mimic behaviour on chain before the restrictedContractDeployment is set with a service account transaction.
restrictedContractDeployment *bool
transactionFees BootstrapProcedureFeeParameters
executionEffortWeights meter.ExecutionEffortWeights
executionMemoryWeights meter.ExecutionMemoryWeights
// executionMemoryLimit of 0 means that it won't be set in the state. The FVM will use the default value from the context.
executionMemoryLimit uint64
// config values for epoch smart-contracts
epochConfig epochs.EpochConfig
// list of initial network participants for whom we will create/stake flow
// accounts and retrieve epoch-related resources
identities flow.IdentityList
}
type BootstrapAccountKeys struct {
ServiceAccountPublicKeys []flow.AccountPublicKey
FungibleTokenAccountPublicKeys []flow.AccountPublicKey
FlowTokenAccountPublicKeys []flow.AccountPublicKey
FlowFeesAccountPublicKeys []flow.AccountPublicKey
NodeAccountPublicKeys []flow.AccountPublicKey
}
type BootstrapProcedureFeeParameters struct {
SurgeFactor cadence.UFix64
InclusionEffortCost cadence.UFix64
ExecutionEffortCost cadence.UFix64
}
type BootstrapProcedureOption func(*BootstrapProcedure) *BootstrapProcedure
func WithInitialTokenSupply(supply cadence.UFix64) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.initialTokenSupply = supply
return bp
}
}
// WithBootstrapAccountKeys sets the public keys of the accounts that will be created during bootstrapping
// by default all accounts are created with the ServiceAccountPublicKey specified when calling `Bootstrap`.
func WithBootstrapAccountKeys(keys BootstrapAccountKeys) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.accountKeys = keys
return bp
}
}
func WithAccountCreationFee(fee cadence.UFix64) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.accountCreationFee = fee
return bp
}
}
func WithTransactionFee(fees BootstrapProcedureFeeParameters) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.transactionFees = fees
return bp
}
}
func WithExecutionEffortWeights(weights meter.ExecutionEffortWeights) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.executionEffortWeights = weights
return bp
}
}
func WithExecutionMemoryWeights(weights meter.ExecutionMemoryWeights) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.executionMemoryWeights = weights
return bp
}
}
func WithExecutionMemoryLimit(limit uint64) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.executionMemoryLimit = limit
return bp
}
}
func WithMinimumStorageReservation(reservation cadence.UFix64) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.minimumStorageReservation = reservation
return bp
}
}
func WithEpochConfig(epochConfig epochs.EpochConfig) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.epochConfig = epochConfig
return bp
}
}
func WithRootBlock(rootBlock *flow.Header) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.rootBlock = rootBlock
return bp
}
}
func WithIdentities(identities flow.IdentityList) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.identities = identities
return bp
}
}
func WithStorageMBPerFLOW(ratio cadence.UFix64) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.storagePerFlow = ratio
return bp
}
}
func WithRestrictedAccountCreationEnabled(enabled cadence.Bool) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.restrictedAccountCreationEnabled = enabled
return bp
}
}
func WithRestrictedContractDeployment(restricted *bool) BootstrapProcedureOption {
return func(bp *BootstrapProcedure) *BootstrapProcedure {
bp.restrictedContractDeployment = restricted
return bp
}
}
// Bootstrap returns a new BootstrapProcedure instance configured with the provided
// genesis parameters.
func Bootstrap(
serviceAccountPublicKey flow.AccountPublicKey,
opts ...BootstrapProcedureOption,
) *BootstrapProcedure {
bootstrapProcedure := &BootstrapProcedure{
BootstrapParams: BootstrapParams{
accountKeys: BootstrapAccountKeys{
ServiceAccountPublicKeys: []flow.AccountPublicKey{serviceAccountPublicKey},
FungibleTokenAccountPublicKeys: []flow.AccountPublicKey{serviceAccountPublicKey},
FlowTokenAccountPublicKeys: []flow.AccountPublicKey{serviceAccountPublicKey},
NodeAccountPublicKeys: []flow.AccountPublicKey{serviceAccountPublicKey},
},
transactionFees: BootstrapProcedureFeeParameters{0, 0, 0},
epochConfig: epochs.DefaultEpochConfig(),
versionFreezePeriod: DefaultVersionFreezePeriod,
},
}
for _, applyOption := range opts {
bootstrapProcedure = applyOption(bootstrapProcedure)
}
return bootstrapProcedure
}
func (b *BootstrapProcedure) NewExecutor(
ctx Context,
txnState storage.Transaction,
) ProcedureExecutor {
return newBootstrapExecutor(b.BootstrapParams, ctx, txnState)
}
func (BootstrapProcedure) SetOutput(output ProcedureOutput) {
// do nothing
}
func (proc *BootstrapProcedure) ComputationLimit(_ Context) uint64 {
return math.MaxUint64
}
func (proc *BootstrapProcedure) MemoryLimit(_ Context) uint64 {
return math.MaxUint64
}
func (proc *BootstrapProcedure) ShouldDisableMemoryAndInteractionLimits(_ Context) bool {
return true
}
func (BootstrapProcedure) Type() ProcedureType {
return BootstrapProcedureType
}
func (proc *BootstrapProcedure) ExecutionTime() logical.Time {
return 0
}
type bootstrapExecutor struct {
BootstrapParams
ctx Context
txnState storage.Transaction
accountCreator environment.BootstrapAccountCreator
}
func newBootstrapExecutor(
params BootstrapParams,
ctx Context,
txnState storage.Transaction,
) *bootstrapExecutor {
return &bootstrapExecutor{
BootstrapParams: params,
ctx: NewContextFromParent(
ctx,
WithContractDeploymentRestricted(false)),
txnState: txnState,
}
}
func (b *bootstrapExecutor) Cleanup() {
// Do nothing.
}
func (b *bootstrapExecutor) Output() ProcedureOutput {
return ProcedureOutput{}
}
func (b *bootstrapExecutor) Preprocess() error {
// Do nothing.
return nil
}
func (b *bootstrapExecutor) Execute() error {
b.rootBlock = flow.Genesis(flow.ChainID(b.ctx.Chain.String())).Header
// initialize the account addressing state
b.accountCreator = environment.NewBootstrapAccountCreator(
b.txnState,
b.ctx.Chain,
environment.NewAccounts(b.txnState))
service := b.createServiceAccount()
fungibleToken := b.deployFungibleToken()
flowToken := b.deployFlowToken(service, fungibleToken)
storageFees := b.deployStorageFees(service, fungibleToken, flowToken)
feeContract := b.deployFlowFees(service, fungibleToken, flowToken, storageFees)
if b.initialTokenSupply > 0 {
b.mintInitialTokens(service, fungibleToken, flowToken, b.initialTokenSupply)
}
b.deployServiceAccount(service, fungibleToken, flowToken, feeContract)
b.setupParameters(
service,
b.accountCreationFee,
b.minimumStorageReservation,
b.storagePerFlow,
b.restrictedAccountCreationEnabled,
)
b.setupFees(
service,
feeContract,
b.transactionFees.SurgeFactor,
b.transactionFees.InclusionEffortCost,
b.transactionFees.ExecutionEffortCost,
)
b.setContractDeploymentRestrictions(service, b.restrictedContractDeployment)
b.setupExecutionWeights(service)
b.setupStorageForServiceAccounts(service, fungibleToken, flowToken, feeContract)
b.createMinter(service, flowToken)
b.deployDKG(service)
b.deployQC(service)
b.deployIDTableStaking(service, fungibleToken, flowToken, feeContract)
b.deployEpoch(service, fungibleToken, flowToken, feeContract)
b.deployVersionBeacon(service, b.versionFreezePeriod)
// deploy staking proxy contract to the service account
b.deployStakingProxyContract(service)
// deploy locked tokens contract to the service account
b.deployLockedTokensContract(service, fungibleToken, flowToken)
// deploy staking collection contract to the service account
b.deployStakingCollection(service, fungibleToken, flowToken)
b.registerNodes(service, fungibleToken, flowToken)
// set the list of nodes which are allowed to stake in this network
b.setStakingAllowlist(service, b.identities.NodeIDs())
return nil
}
func (b *bootstrapExecutor) createAccount(publicKeys []flow.AccountPublicKey) flow.Address {
address, err := b.accountCreator.CreateBootstrapAccount(publicKeys)
if err != nil {
panic(fmt.Sprintf("failed to create account: %s", err))
}
return address
}
func (b *bootstrapExecutor) createServiceAccount() flow.Address {
address, err := b.accountCreator.CreateBootstrapAccount(
b.accountKeys.ServiceAccountPublicKeys)
if err != nil {
panic(fmt.Sprintf("failed to create service account: %s", err))
}
return address
}
func (b *bootstrapExecutor) deployFungibleToken() flow.Address {
fungibleToken := b.createAccount(b.accountKeys.FungibleTokenAccountPublicKeys)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployFungibleTokenContractTransaction(fungibleToken),
0),
)
panicOnMetaInvokeErrf("failed to deploy fungible token contract: %s", txError, err)
return fungibleToken
}
func (b *bootstrapExecutor) deployFlowToken(service, fungibleToken flow.Address) flow.Address {
flowToken := b.createAccount(b.accountKeys.FlowTokenAccountPublicKeys)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployFlowTokenContractTransaction(
service,
fungibleToken,
flowToken),
0),
)
panicOnMetaInvokeErrf("failed to deploy Flow token contract: %s", txError, err)
return flowToken
}
func (b *bootstrapExecutor) deployFlowFees(service, fungibleToken, flowToken, storageFees flow.Address) flow.Address {
flowFees := b.createAccount(b.accountKeys.FlowFeesAccountPublicKeys)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployTxFeesContractTransaction(
service,
fungibleToken,
flowToken,
storageFees,
flowFees,
),
0),
)
panicOnMetaInvokeErrf("failed to deploy fees contract: %s", txError, err)
return flowFees
}
func (b *bootstrapExecutor) deployStorageFees(service, fungibleToken, flowToken flow.Address) flow.Address {
contract := contracts.FlowStorageFees(
fungibleToken.HexWithPrefix(),
flowToken.HexWithPrefix(),
)
// deploy storage fees contract on the service account
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployStorageFeesContractTransaction(
service,
contract),
0),
)
panicOnMetaInvokeErrf("failed to deploy storage fees contract: %s", txError, err)
return service
}
func (b *bootstrapExecutor) createMinter(service, flowToken flow.Address) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.CreateFlowTokenMinterTransaction(
service,
flowToken),
0),
)
panicOnMetaInvokeErrf("failed to create flow token minter: %s", txError, err)
}
func (b *bootstrapExecutor) deployDKG(service flow.Address) {
contract := contracts.FlowDKG()
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(service, contract, "FlowDKG"),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy DKG contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployQC(service flow.Address) {
contract := contracts.FlowQC()
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(service, contract, "FlowClusterQC"),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy QC contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployIDTableStaking(service, fungibleToken, flowToken, flowFees flow.Address) {
contract := contracts.FlowIDTableStaking(
fungibleToken.HexWithPrefix(),
flowToken.HexWithPrefix(),
flowFees.HexWithPrefix(),
true)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployIDTableStakingTransaction(service,
contract,
b.epochConfig.EpochTokenPayout,
b.epochConfig.RewardCut),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy IDTableStaking contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployEpoch(service, fungibleToken, flowToken, flowFees flow.Address) {
contract := contracts.FlowEpoch(
fungibleToken.HexWithPrefix(),
flowToken.HexWithPrefix(),
service.HexWithPrefix(),
service.HexWithPrefix(),
service.HexWithPrefix(),
flowFees.HexWithPrefix(),
)
context := NewContextFromParent(b.ctx,
WithBlockHeader(b.rootBlock),
WithBlocks(&environment.NoopBlockFinder{}),
)
txError, err := b.invokeMetaTransaction(
context,
Transaction(
blueprints.DeployEpochTransaction(service, contract, b.epochConfig),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy Epoch contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployServiceAccount(service, fungibleToken, flowToken, feeContract flow.Address) {
contract := contracts.FlowServiceAccount(
fungibleToken.HexWithPrefix(),
flowToken.HexWithPrefix(),
feeContract.HexWithPrefix(),
service.HexWithPrefix(),
)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(
service,
contract,
"FlowServiceAccount"),
0),
)
panicOnMetaInvokeErrf("failed to deploy service account contract: %s", txError, err)
}
func (b *bootstrapExecutor) mintInitialTokens(
service, fungibleToken, flowToken flow.Address,
initialSupply cadence.UFix64,
) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.MintFlowTokenTransaction(
fungibleToken,
flowToken,
service,
initialSupply),
0),
)
panicOnMetaInvokeErrf("failed to mint initial token supply: %s", txError, err)
}
func (b *bootstrapExecutor) setupParameters(
service flow.Address,
addressCreationFee,
minimumStorageReservation,
storagePerFlow cadence.UFix64,
restrictedAccountCreationEnabled cadence.Bool,
) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.SetupParametersTransaction(
service,
addressCreationFee,
minimumStorageReservation,
storagePerFlow,
restrictedAccountCreationEnabled,
),
0),
)
panicOnMetaInvokeErrf("failed to setup parameters: %s", txError, err)
}
func (b *bootstrapExecutor) setupFees(
service, flowFees flow.Address,
surgeFactor, inclusionEffortCost, executionEffortCost cadence.UFix64,
) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.SetupFeesTransaction(
service,
flowFees,
surgeFactor,
inclusionEffortCost,
executionEffortCost,
),
0),
)
panicOnMetaInvokeErrf("failed to setup fees: %s", txError, err)
}
func (b *bootstrapExecutor) setupExecutionWeights(service flow.Address) {
// if executionEffortWeights were not set skip this part and just use the defaults.
if b.executionEffortWeights != nil {
b.setupExecutionEffortWeights(service)
}
// if executionMemoryWeights were not set skip this part and just use the defaults.
if b.executionMemoryWeights != nil {
b.setupExecutionMemoryWeights(service)
}
if b.executionMemoryLimit != 0 {
b.setExecutionMemoryLimitTransaction(service)
}
}
func (b *bootstrapExecutor) setupExecutionEffortWeights(service flow.Address) {
weights := b.executionEffortWeights
uintWeights := make(map[uint]uint64, len(weights))
for i, weight := range weights {
uintWeights[uint(i)] = weight
}
tb, err := blueprints.SetExecutionEffortWeightsTransaction(service, uintWeights)
if err != nil {
panic(fmt.Sprintf("failed to setup execution effort weights %s", err.Error()))
}
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
tb,
0),
)
panicOnMetaInvokeErrf("failed to setup execution effort weights: %s", txError, err)
}
func (b *bootstrapExecutor) setupExecutionMemoryWeights(service flow.Address) {
weights := b.executionMemoryWeights
uintWeights := make(map[uint]uint64, len(weights))
for i, weight := range weights {
uintWeights[uint(i)] = weight
}
tb, err := blueprints.SetExecutionMemoryWeightsTransaction(service, uintWeights)
if err != nil {
panic(fmt.Sprintf("failed to setup execution memory weights %s", err.Error()))
}
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
tb,
0),
)
panicOnMetaInvokeErrf("failed to setup execution memory weights: %s", txError, err)
}
func (b *bootstrapExecutor) setExecutionMemoryLimitTransaction(service flow.Address) {
tb, err := blueprints.SetExecutionMemoryLimitTransaction(service, b.executionMemoryLimit)
if err != nil {
panic(fmt.Sprintf("failed to setup execution memory limit %s", err.Error()))
}
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
tb,
0),
)
panicOnMetaInvokeErrf("failed to setup execution memory limit: %s", txError, err)
}
func (b *bootstrapExecutor) setupStorageForServiceAccounts(
service, fungibleToken, flowToken, feeContract flow.Address,
) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.SetupStorageForServiceAccountsTransaction(
service,
fungibleToken,
flowToken,
feeContract),
0),
)
panicOnMetaInvokeErrf("failed to setup storage for service accounts: %s", txError, err)
}
func (b *bootstrapExecutor) setStakingAllowlist(
service flow.Address,
allowedIDs []flow.Identifier,
) {
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.SetStakingAllowlistTransaction(
service,
allowedIDs,
),
0),
)
panicOnMetaInvokeErrf("failed to set staking allow-list: %s", txError, err)
}
func (b *bootstrapExecutor) registerNodes(service, fungibleToken, flowToken flow.Address) {
for _, id := range b.identities {
// create a staking account for the node
nodeAddress := b.createAccount(b.accountKeys.NodeAccountPublicKeys)
// give a vault resource to the staking account
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.SetupAccountTransaction(fungibleToken,
flowToken,
nodeAddress),
0,
),
)
panicOnMetaInvokeErrf("failed to setup machine account: %s", txError, err)
// fund the staking account
txError, err = b.invokeMetaTransaction(
b.ctx,
Transaction(blueprints.FundAccountTransaction(service,
fungibleToken,
flowToken,
nodeAddress),
0),
)
panicOnMetaInvokeErrf("failed to fund node staking account: %s", txError, err)
// register the node
// for collection/consensus nodes this will also create the machine account
// and set it up with the QC/DKG participant resource
txError, err = b.invokeMetaTransaction(
b.ctx,
Transaction(blueprints.RegisterNodeTransaction(service,
flowToken,
nodeAddress,
id),
0),
)
panicOnMetaInvokeErrf("failed to register node: %s", txError, err)
}
}
func (b *bootstrapExecutor) deployStakingProxyContract(service flow.Address) {
contract := contracts.FlowStakingProxy()
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(service, contract, "StakingProxy"),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy StakingProxy contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployVersionBeacon(
service flow.Address,
versionFreezePeriod cadence.UInt64,
) {
tx := blueprints.DeployNodeVersionBeaconTransaction(service, versionFreezePeriod)
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
tx,
0,
),
)
panicOnMetaInvokeErrf("failed to deploy NodeVersionBeacon contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployLockedTokensContract(
service flow.Address, fungibleTokenAddress,
flowTokenAddress flow.Address,
) {
publicKeys, err := flow.EncodeRuntimeAccountPublicKeys(b.accountKeys.ServiceAccountPublicKeys)
if err != nil {
panic(err)
}
contract := contracts.FlowLockedTokens(
fungibleTokenAddress.Hex(),
flowTokenAddress.Hex(),
service.Hex(),
service.Hex(),
service.Hex())
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployLockedTokensTransaction(service, contract, publicKeys),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy LockedTokens contract: %s", txError, err)
}
func (b *bootstrapExecutor) deployStakingCollection(
service flow.Address,
fungibleTokenAddress, flowTokenAddress flow.Address,
) {
contract := contracts.FlowStakingCollection(
fungibleTokenAddress.Hex(),
flowTokenAddress.Hex(),
service.Hex(),
service.Hex(),
service.Hex(),
service.Hex(),
service.Hex(),
service.Hex(),
service.Hex())
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(service, contract, "FlowStakingCollection"),
0,
),
)
panicOnMetaInvokeErrf("failed to deploy FlowStakingCollection contract: %s", txError, err)
}
func (b *bootstrapExecutor) setContractDeploymentRestrictions(
service flow.Address,
deployment *bool,
) {
if deployment == nil {
return
}
txBody, err := blueprints.SetIsContractDeploymentRestrictedTransaction(service, *deployment)
if err != nil {
panic(err)
}
txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
txBody,
0,
),
)
panicOnMetaInvokeErrf("failed to deploy FlowStakingCollection contract: %s", txError, err)
}
func panicOnMetaInvokeErrf(msg string, txError errors.CodedError, err error) {
if txError != nil {
panic(fmt.Sprintf(msg, txError.Error()))
}
if err != nil {
panic(fmt.Sprintf(msg, err.Error()))
}
}
func FungibleTokenAddress(chain flow.Chain) flow.Address {
address, _ := chain.AddressAtIndex(environment.FungibleTokenAccountIndex)
return address
}
func FlowTokenAddress(chain flow.Chain) flow.Address {
address, _ := chain.AddressAtIndex(environment.FlowTokenAccountIndex)
return address
}
// invokeMetaTransaction invokes a meta transaction inside the context of an
// outer transaction.
//
// Errors that occur in a meta transaction are propagated as a single error
// that can be captured by the Cadence runtime and eventually disambiguated by
// the parent context.
func (b *bootstrapExecutor) invokeMetaTransaction(
parentCtx Context,
tx *TransactionProcedure,
) (
errors.CodedError,
error,
) {
// do not deduct fees or check storage in meta transactions
ctx := NewContextFromParent(parentCtx,
WithAccountStorageLimit(false),
WithTransactionFeesEnabled(false),
WithAuthorizationChecksEnabled(false),
WithSequenceNumberCheckAndIncrementEnabled(false),
// disable interaction and computation limits for bootstrapping
WithMemoryAndInteractionLimitsDisabled(),
WithComputationLimit(math.MaxUint64),
)
// use new derived transaction data for each meta transaction.
// It's not necessary to cache during bootstrapping and most transactions are contract deploys anyway.
prog, err := derived.NewEmptyDerivedBlockData().
NewDerivedTransactionData(0, 0)
if err != nil {
return nil, err
}
txn := &storage.SerialTransaction{
NestedTransaction: b.txnState,
DerivedTransactionCommitter: prog,
}
err = Run(tx.NewExecutor(ctx, txn))
return tx.Err, err
}