@@ -548,21 +548,21 @@ func configureFVM(blockchain *Blockchain, conf config, blocks *blocks) (*fvm.Vir
548548
549549 cadenceLogger := conf .Logger .Hook (CadenceHook {MainLogger : & conf .ServerLogger }).Level (zerolog .DebugLevel )
550550
551- config := runtime.Config {
551+ runtimeConfig := runtime.Config {
552552 Debugger : blockchain .debugger ,
553553 AccountLinkingEnabled : true ,
554554 AttachmentsEnabled : true ,
555555 CapabilityControllersEnabled : true ,
556556 CoverageReport : conf .CoverageReport ,
557557 }
558558 coverageReportedRuntime := & CoverageReportedRuntime {
559- Runtime : runtime .NewInterpreterRuntime (config ),
559+ Runtime : runtime .NewInterpreterRuntime (runtimeConfig ),
560560 CoverageReport : conf .CoverageReport ,
561- Environment : runtime .NewBaseInterpreterEnvironment (config ),
561+ Environment : runtime .NewBaseInterpreterEnvironment (runtimeConfig ),
562562 }
563563 customRuntimePool := reusableRuntime .NewCustomReusableCadenceRuntimePool (
564564 1 ,
565- config ,
565+ runtimeConfig ,
566566 func (config runtime.Config ) runtime.Runtime {
567567 return coverageReportedRuntime
568568 },
@@ -774,11 +774,13 @@ func configureTransactionValidator(conf config, blocks *blocks) *access.Transact
774774 )
775775}
776776
777- func (b * Blockchain ) newFVMContextFromHeader (header * flowgo.Header ) fvm.Context {
778- return fvm .NewContextFromParent (
777+ func (b * Blockchain ) setFVMContextFromHeader (header * flowgo.Header ) fvm.Context {
778+ b . vmCtx = fvm .NewContextFromParent (
779779 b .vmCtx ,
780780 fvm .WithBlockHeader (header ),
781781 )
782+
783+ return b .vmCtx
782784}
783785
784786func (b * Blockchain ) CurrentScript () (string , string ) {
@@ -1165,7 +1167,7 @@ func (b *Blockchain) executeBlock() ([]*types.TransactionResult, error) {
11651167 }
11661168
11671169 header := b .pendingBlock .Block ().Header
1168- blockContext := b .newFVMContextFromHeader (header )
1170+ blockContext := b .setFVMContextFromHeader (header )
11691171
11701172 // cannot execute a block that has already executed
11711173 if b .pendingBlock .ExecutionComplete () {
@@ -1193,7 +1195,7 @@ func (b *Blockchain) ExecuteNextTransaction() (*types.TransactionResult, error)
11931195 defer b .mu .Unlock ()
11941196
11951197 header := b .pendingBlock .Block ().Header
1196- blockContext := b .newFVMContextFromHeader (header )
1198+ blockContext := b .setFVMContextFromHeader (header )
11971199 return b .executeNextTransaction (blockContext )
11981200}
11991201
@@ -1405,8 +1407,10 @@ func (b *Blockchain) executeScriptAtBlockID(script []byte, arguments [][]byte, i
14051407 return nil , err
14061408 }
14071409
1408- header := requestedBlock .Header
1409- blockContext := b .newFVMContextFromHeader (header )
1410+ blockContext := fvm .NewContextFromParent (
1411+ b .vmCtx ,
1412+ fvm .WithBlockHeader (requestedBlock .Header ),
1413+ )
14101414
14111415 scriptProc := fvm .Script (script ).WithArguments (arguments ... )
14121416 b .currentCode = string (script )
@@ -1629,6 +1633,16 @@ func (b *Blockchain) SetClock(clock Clock) {
16291633 b .pendingBlock .SetClock (clock )
16301634}
16311635
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+
16321646func (b * Blockchain ) GetSourceFile (location common.Location ) string {
16331647
16341648 value , exists := b .sourceFileMap [location ]
@@ -1640,12 +1654,8 @@ func (b *Blockchain) GetSourceFile(location common.Location) string {
16401654 if ! isAddressLocation {
16411655 return location .ID ()
16421656 }
1643- view := b .pendingBlock .ledgerState .NewChild ()
1644-
1645- env := environment .NewScriptEnvironmentFromStorageSnapshot (
1646- b .vmCtx .EnvironmentParams ,
1647- view )
16481657
1658+ env := b .NewScriptEnvironment ()
16491659 r := b .vmCtx .Borrow (env )
16501660 defer b .vmCtx .Return (r )
16511661
0 commit comments