Skip to content

Commit

Permalink
Merge pull request #4668 from onflow/gregor/script-execution/script-e…
Browse files Browse the repository at this point in the history
…ngine-interfaces

[Access] Script execution engine interface changes
  • Loading branch information
sideninja authored Sep 5, 2023
2 parents d9ef5ca + 32c5011 commit 1c4fd72
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 35 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ generate-mocks: install-mock-generators
mockery --name 'ExecutionState' --dir=engine/execution/state --case=underscore --output="engine/execution/state/mock" --outpkg="mock"
mockery --name 'BlockComputer' --dir=engine/execution/computation/computer --case=underscore --output="engine/execution/computation/computer/mock" --outpkg="mock"
mockery --name 'ComputationManager' --dir=engine/execution/computation --case=underscore --output="engine/execution/computation/mock" --outpkg="mock"
mockery --name 'Executor' --dir=engine/execution/computation/query --case=underscore --output="engine/execution/computation/query/mock" --outpkg="mock"
mockery --name 'EpochComponentsFactory' --dir=engine/collection/epochmgr --case=underscore --output="engine/collection/epochmgr/mock" --outpkg="mock"
mockery --name 'Backend' --dir=engine/collection/rpc --case=underscore --output="engine/collection/rpc/mock" --outpkg="mock"
mockery --name 'ProviderEngine' --dir=engine/execution/provider --case=underscore --output="engine/execution/provider/mock" --outpkg="mock"
Expand Down
4 changes: 2 additions & 2 deletions cmd/execution_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,11 +901,11 @@ func (exeNode *ExecutionNode) LoadIngestionEngine(

// create scripts engine for handling script execution
func (exeNode *ExecutionNode) LoadScriptsEngine(node *NodeConfig) (module.ReadyDoneAware, error) {
// for RPC to load it

exeNode.scriptsEng = scripts.New(
node.Logger,
node.State,
exeNode.computationManager,
exeNode.computationManager.QueryExecutor(),
exeNode.executionState,
)

Expand Down
6 changes: 5 additions & 1 deletion engine/execution/computation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func New(
}

chainID := vmCtx.Chain.ChainID()

options := []fvm.Option{
fvm.WithReusableCadenceRuntimePool(
reusableRuntime.NewReusableCadenceRuntimePool(
Expand All @@ -121,6 +120,7 @@ func New(
},
)),
}

if params.ExtensiveTracing {
options = append(options, fvm.WithExtensiveTracing())
}
Expand Down Expand Up @@ -239,3 +239,7 @@ func (e *Manager) GetAccount(
blockHeader,
snapshot)
}

func (e *Manager) QueryExecutor() query.Executor {
return e.queryExecutor
}
84 changes: 84 additions & 0 deletions engine/execution/computation/query/mock/executor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 29 additions & 16 deletions engine/execution/scripts/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,47 @@ import (

"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/engine/execution/state"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
)

// ScriptExecutionState is a subset of the `state.ExecutionState` interface purposed to only access the state
// used for script execution and not mutate the execution state of the blockchain.
type ScriptExecutionState interface {
// NewStorageSnapshot creates a new ready-only view at the given state commitment.
NewStorageSnapshot(flow.StateCommitment) snapshot.StorageSnapshot

// StateCommitmentByBlockID returns the final state commitment for the provided block ID.
StateCommitmentByBlockID(context.Context, flow.Identifier) (flow.StateCommitment, error)

// HasState returns true if the state with the given state commitment exists in memory
HasState(flow.StateCommitment) bool
}

type Engine struct {
unit *engine.Unit
log zerolog.Logger
state protocol.State
computationManager computation.ComputationManager
execState state.ExecutionState
unit *engine.Unit
log zerolog.Logger
state protocol.State
queryExecutor query.Executor
execState ScriptExecutionState
}

var _ execution.ScriptExecutor = (*Engine)(nil)

func New(
logger zerolog.Logger,
state protocol.State,
computationManager computation.ComputationManager,
execState state.ExecutionState,
queryExecutor query.Executor,
execState ScriptExecutionState,
) *Engine {
return &Engine{
unit: engine.NewUnit(),
log: logger.With().Str("engine", "scripts").Logger(),
state: state,
execState: execState,
computationManager: computationManager,
unit: engine.NewUnit(),
log: logger.With().Str("engine", "scripts").Logger(),
state: state,
execState: execState,
queryExecutor: queryExecutor,
}
}

Expand Down Expand Up @@ -73,7 +86,7 @@ func (e *Engine) ExecuteScriptAtBlockID(

blockSnapshot := e.execState.NewStorageSnapshot(stateCommit)

return e.computationManager.ExecuteScript(
return e.queryExecutor.ExecuteScript(
ctx,
script,
arguments,
Expand Down Expand Up @@ -131,5 +144,5 @@ func (e *Engine) GetAccount(

blockSnapshot := e.execState.NewStorageSnapshot(stateCommit)

return e.computationManager.GetAccount(ctx, addr, block, blockSnapshot)
return e.queryExecutor.GetAccount(ctx, addr, block, blockSnapshot)
}
32 changes: 16 additions & 16 deletions engine/execution/scripts/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

computation "github.com/onflow/flow-go/engine/execution/computation/mock"
queryMock "github.com/onflow/flow-go/engine/execution/computation/query/mock"
stateMock "github.com/onflow/flow-go/engine/execution/state/mock"
"github.com/onflow/flow-go/model/flow"
protocol "github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/utils/unittest"
)

type testingContext struct {
t *testing.T
engine *Engine
state *protocol.State
executionState *stateMock.ExecutionState
computationManager *computation.ComputationManager
mu *sync.Mutex
t *testing.T
engine *Engine
state *protocol.State
executionState *stateMock.ExecutionState
queryExecutor *queryMock.Executor
mu *sync.Mutex
}

func (ctx *testingContext) stateCommitmentExist(blockID flow.Identifier, commit flow.StateCommitment) {
Expand All @@ -32,17 +32,17 @@ func (ctx *testingContext) stateCommitmentExist(blockID flow.Identifier, commit
func runWithEngine(t *testing.T, fn func(ctx testingContext)) {
log := unittest.Logger()

computationManager := new(computation.ComputationManager)
queryExecutor := new(queryMock.Executor)
protocolState := new(protocol.State)
execState := new(stateMock.ExecutionState)

engine := New(log, protocolState, computationManager, execState)
engine := New(log, protocolState, queryExecutor, execState)
fn(testingContext{
t: t,
engine: engine,
computationManager: computationManager,
executionState: execState,
state: protocolState,
t: t,
engine: engine,
queryExecutor: queryExecutor,
executionState: execState,
state: protocolState,
})
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func TestExecuteScriptAtBlockID(t *testing.T) {
ctx.executionState.On("HasState", *blockA.StartState).Return(true)

// Successful call to computation manager
ctx.computationManager.
ctx.queryExecutor.
On("ExecuteScript", mock.Anything, script, [][]byte(nil), blockA.Block.Header, nil).
Return(scriptResult, nil)

Expand All @@ -80,7 +80,7 @@ func TestExecuteScriptAtBlockID(t *testing.T) {
assert.Equal(t, scriptResult, res)

// Assert other components were called as expected
ctx.computationManager.AssertExpectations(t)
ctx.queryExecutor.AssertExpectations(t)
ctx.executionState.AssertExpectations(t)
ctx.state.AssertExpectations(t)
})
Expand Down

0 comments on commit 1c4fd72

Please sign in to comment.