-
Notifications
You must be signed in to change notification settings - Fork 179
/
engine.go
97 lines (80 loc) · 2.18 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package scripts
import (
"context"
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/engine/execution/state"
"github.com/onflow/flow-go/model/flow"
)
type Engine struct {
unit *engine.Unit
log zerolog.Logger
queryExecutor query.Executor
execState state.ScriptExecutionState
}
var _ execution.ScriptExecutor = (*Engine)(nil)
func New(
logger zerolog.Logger,
queryExecutor query.Executor,
execState state.ScriptExecutionState,
) *Engine {
return &Engine{
unit: engine.NewUnit(),
log: logger.With().Str("engine", "scripts").Logger(),
execState: execState,
queryExecutor: queryExecutor,
}
}
func (e *Engine) Ready() <-chan struct{} {
return e.unit.Ready()
}
func (e *Engine) Done() <-chan struct{} {
return e.unit.Done()
}
func (e *Engine) ExecuteScriptAtBlockID(
ctx context.Context,
script []byte,
arguments [][]byte,
blockID flow.Identifier,
) ([]byte, error) {
blockSnapshot, header, err := e.execState.CreateStorageSnapshot(blockID)
if err != nil {
return nil, fmt.Errorf("failed to create storage snapshot: %w", err)
}
return e.queryExecutor.ExecuteScript(
ctx,
script,
arguments,
header,
blockSnapshot)
}
func (e *Engine) GetRegisterAtBlockID(
ctx context.Context,
owner, key []byte,
blockID flow.Identifier,
) ([]byte, error) {
blockSnapshot, _, err := e.execState.CreateStorageSnapshot(blockID)
if err != nil {
return nil, fmt.Errorf("failed to create storage snapshot: %w", err)
}
id := flow.NewRegisterID(flow.BytesToAddress(owner), string(key))
data, err := blockSnapshot.Get(id)
if err != nil {
return nil, fmt.Errorf("failed to get the register (%s): %w", id, err)
}
return data, nil
}
func (e *Engine) GetAccount(
ctx context.Context,
addr flow.Address,
blockID flow.Identifier,
) (*flow.Account, error) {
blockSnapshot, header, err := e.execState.CreateStorageSnapshot(blockID)
if err != nil {
return nil, fmt.Errorf("failed to create storage snapshot: %w", err)
}
return e.queryExecutor.GetAccount(ctx, addr, header, blockSnapshot)
}