Skip to content

Commit

Permalink
Introduce the Blockchain.NewScriptEnvironment() method
Browse files Browse the repository at this point in the history
We also update Blockchain.GetSourceFile() to use the
Blockchain.NewScriptEnvironment() method.
  • Loading branch information
m-Peter committed Sep 27, 2023
1 parent 6e8d85b commit 3be569a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions emulator/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)

Expand Down

0 comments on commit 3be569a

Please sign in to comment.