From 8c6c4830814a3355ee74eddfbf86713163255b15 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 27 Sep 2023 11:30:15 +0300 Subject: [PATCH 1/3] Rename config variable to runtimeConfig to avoid confusion with Blockchain's config --- emulator/blockchain.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/emulator/blockchain.go b/emulator/blockchain.go index 717d0f40..4e06f36c 100644 --- a/emulator/blockchain.go +++ b/emulator/blockchain.go @@ -548,7 +548,7 @@ func configureFVM(blockchain *Blockchain, conf config, blocks *blocks) (*fvm.Vir cadenceLogger := conf.Logger.Hook(CadenceHook{MainLogger: &conf.ServerLogger}).Level(zerolog.DebugLevel) - config := runtime.Config{ + runtimeConfig := runtime.Config{ Debugger: blockchain.debugger, AccountLinkingEnabled: true, AttachmentsEnabled: true, @@ -556,13 +556,13 @@ func configureFVM(blockchain *Blockchain, conf config, blocks *blocks) (*fvm.Vir CoverageReport: conf.CoverageReport, } coverageReportedRuntime := &CoverageReportedRuntime{ - Runtime: runtime.NewInterpreterRuntime(config), + Runtime: runtime.NewInterpreterRuntime(runtimeConfig), CoverageReport: conf.CoverageReport, - Environment: runtime.NewBaseInterpreterEnvironment(config), + Environment: runtime.NewBaseInterpreterEnvironment(runtimeConfig), } customRuntimePool := reusableRuntime.NewCustomReusableCadenceRuntimePool( 1, - config, + runtimeConfig, func(config runtime.Config) runtime.Runtime { return coverageReportedRuntime }, From 6e8d85bbf32b240c99ef85f5bfd58db9e59c2eb7 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 27 Sep 2023 11:31:26 +0300 Subject: [PATCH 2/3] Blockchain's vmCtx should always be updated to point to the latest Header We also rename Blockchain's newFVMContextFromHeader() to setFVMContextFromHeader() --- emulator/blockchain.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/emulator/blockchain.go b/emulator/blockchain.go index 4e06f36c..e4e388b8 100644 --- a/emulator/blockchain.go +++ b/emulator/blockchain.go @@ -774,11 +774,13 @@ func configureTransactionValidator(conf config, blocks *blocks) *access.Transact ) } -func (b *Blockchain) newFVMContextFromHeader(header *flowgo.Header) fvm.Context { - return fvm.NewContextFromParent( +func (b *Blockchain) setFVMContextFromHeader(header *flowgo.Header) fvm.Context { + b.vmCtx = fvm.NewContextFromParent( b.vmCtx, fvm.WithBlockHeader(header), ) + + return b.vmCtx } func (b *Blockchain) CurrentScript() (string, string) { @@ -1165,7 +1167,7 @@ func (b *Blockchain) executeBlock() ([]*types.TransactionResult, error) { } header := b.pendingBlock.Block().Header - blockContext := b.newFVMContextFromHeader(header) + blockContext := b.setFVMContextFromHeader(header) // cannot execute a block that has already executed if b.pendingBlock.ExecutionComplete() { @@ -1193,7 +1195,7 @@ func (b *Blockchain) ExecuteNextTransaction() (*types.TransactionResult, error) defer b.mu.Unlock() header := b.pendingBlock.Block().Header - blockContext := b.newFVMContextFromHeader(header) + blockContext := b.setFVMContextFromHeader(header) return b.executeNextTransaction(blockContext) } @@ -1405,8 +1407,10 @@ func (b *Blockchain) executeScriptAtBlockID(script []byte, arguments [][]byte, i return nil, err } - header := requestedBlock.Header - blockContext := b.newFVMContextFromHeader(header) + blockContext := fvm.NewContextFromParent( + b.vmCtx, + fvm.WithBlockHeader(requestedBlock.Header), + ) scriptProc := fvm.Script(script).WithArguments(arguments...) b.currentCode = string(script) From 3be569ab3212780948e4777f01a379b909e971d5 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 27 Sep 2023 11:32:56 +0300 Subject: [PATCH 3/3] Introduce the Blockchain.NewScriptEnvironment() method We also update Blockchain.GetSourceFile() to use the Blockchain.NewScriptEnvironment() method. --- emulator/blockchain.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/emulator/blockchain.go b/emulator/blockchain.go index e4e388b8..1dd277b5 100644 --- a/emulator/blockchain.go +++ b/emulator/blockchain.go @@ -1633,6 +1633,16 @@ func (b *Blockchain) SetClock(clock Clock) { b.pendingBlock.SetClock(clock) } +// NewScriptEnvironment returns an environment.Environment by +// using as a storage snapshot the blockchain's ledger state. +// Useful for tools that use the emulator's blockchain as a library. +func (b *Blockchain) NewScriptEnvironment() environment.Environment { + return environment.NewScriptEnvironmentFromStorageSnapshot( + b.vmCtx.EnvironmentParams, + b.pendingBlock.ledgerState.NewChild(), + ) +} + func (b *Blockchain) GetSourceFile(location common.Location) string { value, exists := b.sourceFileMap[location] @@ -1644,12 +1654,8 @@ func (b *Blockchain) GetSourceFile(location common.Location) string { if !isAddressLocation { return location.ID() } - view := b.pendingBlock.ledgerState.NewChild() - - env := environment.NewScriptEnvironmentFromStorageSnapshot( - b.vmCtx.EnvironmentParams, - view) + env := b.NewScriptEnvironment() r := b.vmCtx.Borrow(env) defer b.vmCtx.Return(r)