Skip to content

Commit

Permalink
Merge pull request #476 from m-Peter/configurable-blockchain-runtime-…
Browse files Browse the repository at this point in the history
…environment

Allow configuration of blockchain's interpreter runtime
  • Loading branch information
SupunS authored Sep 28, 2023
2 parents bcf6545 + 3be569a commit 75caeb6
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions emulator/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,21 +548,21 @@ 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,
CapabilityControllersEnabled: true,
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
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1629,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 @@ -1640,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 75caeb6

Please sign in to comment.