From 3be569ab3212780948e4777f01a379b909e971d5 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Wed, 27 Sep 2023 11:32:56 +0300 Subject: [PATCH] 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)