Skip to content

Commit 3be569a

Browse files
committed
Introduce the Blockchain.NewScriptEnvironment() method
We also update Blockchain.GetSourceFile() to use the Blockchain.NewScriptEnvironment() method.
1 parent 6e8d85b commit 3be569a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

emulator/blockchain.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,16 @@ func (b *Blockchain) SetClock(clock Clock) {
16331633
b.pendingBlock.SetClock(clock)
16341634
}
16351635

1636+
// NewScriptEnvironment returns an environment.Environment by
1637+
// using as a storage snapshot the blockchain's ledger state.
1638+
// Useful for tools that use the emulator's blockchain as a library.
1639+
func (b *Blockchain) NewScriptEnvironment() environment.Environment {
1640+
return environment.NewScriptEnvironmentFromStorageSnapshot(
1641+
b.vmCtx.EnvironmentParams,
1642+
b.pendingBlock.ledgerState.NewChild(),
1643+
)
1644+
}
1645+
16361646
func (b *Blockchain) GetSourceFile(location common.Location) string {
16371647

16381648
value, exists := b.sourceFileMap[location]
@@ -1644,12 +1654,8 @@ func (b *Blockchain) GetSourceFile(location common.Location) string {
16441654
if !isAddressLocation {
16451655
return location.ID()
16461656
}
1647-
view := b.pendingBlock.ledgerState.NewChild()
1648-
1649-
env := environment.NewScriptEnvironmentFromStorageSnapshot(
1650-
b.vmCtx.EnvironmentParams,
1651-
view)
16521657

1658+
env := b.NewScriptEnvironment()
16531659
r := b.vmCtx.Borrow(env)
16541660
defer b.vmCtx.Return(r)
16551661

0 commit comments

Comments
 (0)