From dfc93fb948e147796fff3e7f311925c43c1523c0 Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Tue, 28 Jan 2025 16:09:53 +0100 Subject: [PATCH 1/2] bootstrap custom --- emulator/blockchain.go | 22 ++++++- emulator/customBootstrap.go | 112 ++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 emulator/customBootstrap.go diff --git a/emulator/blockchain.go b/emulator/blockchain.go index db5d31a3..82fdb5b1 100644 --- a/emulator/blockchain.go +++ b/emulator/blockchain.go @@ -34,6 +34,8 @@ import ( "encoding/hex" "errors" "fmt" + "github.com/onflow/flow-go/fvm/blueprints" + "github.com/onflow/flow-go/fvm/systemcontracts" "math" "strings" "sync" @@ -775,7 +777,25 @@ func bootstrapLedger( bootstrap := configureBootstrapProcedure(conf, flowAccountKey, conf.GenesisTokenSupply) - executionSnapshot, output, err := vm.Run(ctx, bootstrap, ledger) + // additional custom bootstrapping + customBootstrap := NewCustomBootstrap(bootstrap, func(ctx fvm.Context, executor *CustomBootStrapExecutor) error { + deployTo, err := conf.GetChainID().Chain().AddressAtIndex(systemcontracts.FlowFeesAccountIndex) + if err != nil { + return err + } + _, err = executor.InvokeMetaTransaction( + ctx, + fvm.Transaction( + blueprints.DeployBurnerContractTransaction(deployTo), + 0), + ) + if err != nil { + return err + } + return nil + }) + + executionSnapshot, output, err := vm.Run(ctx, customBootstrap, ledger) if err != nil { return nil, err } diff --git a/emulator/customBootstrap.go b/emulator/customBootstrap.go new file mode 100644 index 00000000..f4d41765 --- /dev/null +++ b/emulator/customBootstrap.go @@ -0,0 +1,112 @@ +package emulator + +import ( + "math" + + "github.com/onflow/flow-go/fvm" + "github.com/onflow/flow-go/fvm/errors" + "github.com/onflow/flow-go/fvm/storage" + "github.com/onflow/flow-go/fvm/storage/logical" +) + +type CustomBootStrapExecutor struct { + ctx fvm.Context + txnState storage.TransactionPreparer + baseExecutor fvm.ProcedureExecutor + executionFunc func(ctx fvm.Context, executor *CustomBootStrapExecutor) error +} + +func NewCustomBootStrapExecutor(ctx fvm.Context, txnState storage.TransactionPreparer, baseExecutor fvm.ProcedureExecutor, executionFunc func(ctx fvm.Context, executor *CustomBootStrapExecutor) error) *CustomBootStrapExecutor { + return &CustomBootStrapExecutor{ + ctx: ctx, + baseExecutor: baseExecutor, + txnState: txnState, + executionFunc: executionFunc, + } +} + +func (c *CustomBootStrapExecutor) InvokeMetaTransaction( + parentCtx fvm.Context, + tx *fvm.TransactionProcedure, +) ( + errors.CodedError, + error, +) { + ctx := fvm.NewContextFromParent(parentCtx, + fvm.WithAccountStorageLimit(false), + fvm.WithTransactionFeesEnabled(false), + fvm.WithAuthorizationChecksEnabled(false), + fvm.WithSequenceNumberCheckAndIncrementEnabled(false), + + fvm.WithMemoryAndInteractionLimitsDisabled(), + fvm.WithComputationLimit(math.MaxUint64), + ) + + executor := tx.NewExecutor(ctx, c.txnState) + err := fvm.Run(executor) + + return executor.Output().Err, err +} + +func (c *CustomBootStrapExecutor) Preprocess() error { + err := c.baseExecutor.Preprocess() + if err != nil { + return err + } + return nil +} + +func (c *CustomBootStrapExecutor) Execute() error { + err := c.baseExecutor.Execute() + if err != nil { + return err + } + return c.executionFunc(c.ctx, c) +} + +func (c *CustomBootStrapExecutor) Cleanup() { + +} + +func (c *CustomBootStrapExecutor) Output() fvm.ProcedureOutput { + return fvm.ProcedureOutput{} +} + +func (c *CustomBootstrap) NewExecutor(ctx fvm.Context, txnState storage.TransactionPreparer) fvm.ProcedureExecutor { + return NewCustomBootStrapExecutor(ctx, txnState, c.baseBootstrap.NewExecutor(ctx, txnState), c.executionFunc) +} + +func (c *CustomBootstrap) ComputationLimit(ctx fvm.Context) uint64 { + return math.MaxUint64 +} + +func (c *CustomBootstrap) MemoryLimit(ctx fvm.Context) uint64 { + return math.MaxUint64 +} + +func (c *CustomBootstrap) ShouldDisableMemoryAndInteractionLimits(ctx fvm.Context) bool { + return true +} + +func (c *CustomBootstrap) Type() fvm.ProcedureType { + return fvm.BootstrapProcedureType +} + +func (c *CustomBootstrap) ExecutionTime() logical.Time { + return 0 +} + +type CustomBootstrap struct { + baseBootstrap fvm.Procedure + executionFunc func(ctx fvm.Context, executor *CustomBootStrapExecutor) error +} + +func NewCustomBootstrap(baseBootstrap fvm.Procedure, executionFunc func(ctx fvm.Context, executor *CustomBootStrapExecutor) error) *CustomBootstrap { + return &CustomBootstrap{ + baseBootstrap: baseBootstrap, + executionFunc: executionFunc, + } +} + +var _ fvm.Procedure = &CustomBootstrap{} +var _ fvm.ProcedureExecutor = &CustomBootStrapExecutor{} From d47a42981ba4f38770e01ace9b93412c12c624c7 Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Wed, 19 Mar 2025 10:21:09 +0100 Subject: [PATCH 2/2] custom example --- emulator/blockchain.go | 6 ++++-- ...{customBootstrap.go => custom_bootstrap.go} | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) rename emulator/{customBootstrap.go => custom_bootstrap.go} (83%) diff --git a/emulator/blockchain.go b/emulator/blockchain.go index ebcfa780..2ac61050 100644 --- a/emulator/blockchain.go +++ b/emulator/blockchain.go @@ -34,8 +34,6 @@ import ( "encoding/hex" "errors" "fmt" - "github.com/onflow/flow-go/fvm/blueprints" - "github.com/onflow/flow-go/fvm/systemcontracts" "math" "strings" "sync" @@ -780,6 +778,8 @@ func bootstrapLedger( // additional custom bootstrapping customBootstrap := NewCustomBootstrap(bootstrap, func(ctx fvm.Context, executor *CustomBootStrapExecutor) error { + + /* Example usage: deployTo, err := conf.GetChainID().Chain().AddressAtIndex(systemcontracts.FlowFeesAccountIndex) if err != nil { return err @@ -793,6 +793,8 @@ func bootstrapLedger( if err != nil { return err } + */ + return nil }) diff --git a/emulator/customBootstrap.go b/emulator/custom_bootstrap.go similarity index 83% rename from emulator/customBootstrap.go rename to emulator/custom_bootstrap.go index f4d41765..ddb8f6aa 100644 --- a/emulator/customBootstrap.go +++ b/emulator/custom_bootstrap.go @@ -1,3 +1,21 @@ +/* + * Flow Emulator + * + * Copyright Flow Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package emulator import (