-
Notifications
You must be signed in to change notification settings - Fork 175
/
manager.go
139 lines (113 loc) · 3.68 KB
/
manager.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package computation
import (
"context"
"fmt"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/computation/computer"
"github.com/onflow/flow-go/engine/execution/state/delta"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/logging"
)
type VirtualMachine interface {
Run(fvm.Context, fvm.Procedure, state.Ledger) error
GetAccount(fvm.Context, flow.Address, state.Ledger) (*flow.Account, error)
}
type ComputationManager interface {
ExecuteScript([]byte, [][]byte, *flow.Header, *delta.View) ([]byte, error)
ComputeBlock(
ctx context.Context,
block *entity.ExecutableBlock,
view *delta.View,
) (*execution.ComputationResult, error)
GetAccount(addr flow.Address, header *flow.Header, view *delta.View) (*flow.Account, error)
}
// Manager manages computation and execution
type Manager struct {
log zerolog.Logger
me module.Local
protoState protocol.State
vm VirtualMachine
vmCtx fvm.Context
blockComputer computer.BlockComputer
}
func New(
logger zerolog.Logger,
metrics module.ExecutionMetrics,
tracer module.Tracer,
me module.Local,
protoState protocol.State,
vm VirtualMachine,
vmCtx fvm.Context,
) (*Manager, error) {
log := logger.With().Str("engine", "computation").Logger()
blockComputer, err := computer.NewBlockComputer(
vm,
vmCtx,
metrics,
tracer,
log.With().Str("component", "block_computer").Logger(),
)
if err != nil {
return nil, fmt.Errorf("cannot create block computer: %w", err)
}
e := Manager{
log: log,
me: me,
protoState: protoState,
vm: vm,
vmCtx: vmCtx,
blockComputer: blockComputer,
}
return &e, nil
}
func (e *Manager) ExecuteScript(code []byte, arguments [][]byte, blockHeader *flow.Header, view *delta.View) ([]byte, error) {
blockCtx := fvm.NewContextFromParent(e.vmCtx, fvm.WithBlockHeader(blockHeader))
script := fvm.Script(code).WithArguments(arguments...)
err := e.vm.Run(blockCtx, script, view)
if err != nil {
return nil, fmt.Errorf("failed to execute script (internal error): %w", err)
}
if script.Err != nil {
return nil, fmt.Errorf("failed to execute script at block (%s): %s", blockHeader.ID(), script.Err.Error())
}
encodedValue, err := jsoncdc.Encode(script.Value)
if err != nil {
return nil, fmt.Errorf("failed to encode runtime value: %w", err)
}
return encodedValue, nil
}
func (e *Manager) ComputeBlock(
ctx context.Context,
block *entity.ExecutableBlock,
view *delta.View,
) (*execution.ComputationResult, error) {
e.log.Debug().
Hex("block_id", logging.Entity(block.Block)).
Msg("received complete block")
result, err := e.blockComputer.ExecuteBlock(ctx, block, view)
if err != nil {
e.log.Error().
Hex("block_id", logging.Entity(block.Block)).
Msg("failed to compute block result")
return nil, fmt.Errorf("failed to execute block: %w", err)
}
e.log.Debug().
Hex("block_id", logging.Entity(result.ExecutableBlock.Block)).
Msg("computed block result")
return result, nil
}
func (e *Manager) GetAccount(address flow.Address, blockHeader *flow.Header, view *delta.View) (*flow.Account, error) {
blockCtx := fvm.NewContextFromParent(e.vmCtx, fvm.WithBlockHeader(blockHeader))
account, err := e.vm.GetAccount(blockCtx, address, view)
if err != nil {
return nil, fmt.Errorf("failed to get account at block (%s): %w", blockHeader.ID(), err)
}
return account, nil
}