-
Notifications
You must be signed in to change notification settings - Fork 178
/
script.go
215 lines (181 loc) · 4.8 KB
/
script.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package fvm
import (
"context"
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/hash"
)
type ScriptProcedure struct {
ID flow.Identifier
Script []byte
Arguments [][]byte
RequestContext context.Context
// TODO(patrick): remove
ProcedureOutput
}
func Script(code []byte) *ScriptProcedure {
scriptHash := hash.DefaultHasher.ComputeHash(code)
return &ScriptProcedure{
Script: code,
ID: flow.HashToID(scriptHash),
RequestContext: context.Background(),
}
}
func (proc *ScriptProcedure) WithArguments(args ...[]byte) *ScriptProcedure {
return &ScriptProcedure{
ID: proc.ID,
Script: proc.Script,
RequestContext: proc.RequestContext,
Arguments: args,
}
}
func (proc *ScriptProcedure) WithRequestContext(
reqContext context.Context,
) *ScriptProcedure {
return &ScriptProcedure{
ID: proc.ID,
Script: proc.Script,
RequestContext: reqContext,
Arguments: proc.Arguments,
}
}
func NewScriptWithContextAndArgs(
code []byte,
reqContext context.Context,
args ...[]byte,
) *ScriptProcedure {
scriptHash := hash.DefaultHasher.ComputeHash(code)
return &ScriptProcedure{
ID: flow.HashToID(scriptHash),
Script: code,
RequestContext: reqContext,
Arguments: args,
}
}
func (proc *ScriptProcedure) NewExecutor(
ctx Context,
txnState storage.Transaction,
) ProcedureExecutor {
return newScriptExecutor(ctx, proc, txnState)
}
func (proc *ScriptProcedure) SetOutput(output ProcedureOutput) {
proc.ProcedureOutput = output
}
func (proc *ScriptProcedure) ComputationLimit(ctx Context) uint64 {
computationLimit := ctx.ComputationLimit
// if ctx.ComputationLimit is also zero, fallback to the default computation limit
if computationLimit == 0 {
computationLimit = DefaultComputationLimit
}
return computationLimit
}
func (proc *ScriptProcedure) MemoryLimit(ctx Context) uint64 {
memoryLimit := ctx.MemoryLimit
// if ctx.MemoryLimit is also zero, fallback to the default memory limit
if memoryLimit == 0 {
memoryLimit = DefaultMemoryLimit
}
return memoryLimit
}
func (proc *ScriptProcedure) ShouldDisableMemoryAndInteractionLimits(
ctx Context,
) bool {
return ctx.DisableMemoryAndInteractionLimits
}
func (ScriptProcedure) Type() ProcedureType {
return ScriptProcedureType
}
func (proc *ScriptProcedure) ExecutionTime() logical.Time {
return logical.EndOfBlockExecutionTime
}
type scriptExecutor struct {
ctx Context
proc *ScriptProcedure
txnState storage.Transaction
env environment.Environment
output ProcedureOutput
}
func newScriptExecutor(
ctx Context,
proc *ScriptProcedure,
txnState storage.Transaction,
) *scriptExecutor {
return &scriptExecutor{
ctx: ctx,
proc: proc,
txnState: txnState,
env: environment.NewScriptEnv(
proc.RequestContext,
ctx.TracerSpan,
ctx.EnvironmentParams,
txnState),
}
}
func (executor *scriptExecutor) Cleanup() {
// Do nothing.
}
func (executor *scriptExecutor) Output() ProcedureOutput {
return executor.output
}
func (executor *scriptExecutor) Preprocess() error {
// Do nothing.
return nil
}
func (executor *scriptExecutor) Execute() error {
err := executor.execute()
txError, failure := errors.SplitErrorTypes(err)
if failure != nil {
if errors.IsLedgerFailure(failure) {
return fmt.Errorf(
"cannot execute the script, this error usually happens if "+
"the reference block for this script is not set to a "+
"recent block: %w",
failure)
}
return failure
}
if txError != nil {
executor.output.Err = txError
}
return nil
}
func (executor *scriptExecutor) execute() error {
meterParams, err := getBodyMeterParameters(
executor.ctx,
executor.proc,
executor.txnState)
if err != nil {
return fmt.Errorf("error getting meter parameters: %w", err)
}
txnId, err := executor.txnState.BeginNestedTransactionWithMeterParams(
meterParams)
if err != nil {
return err
}
errs := errors.NewErrorsCollector()
errs.Collect(executor.executeScript())
_, err = executor.txnState.CommitNestedTransaction(txnId)
errs.Collect(err)
return errs.ErrorOrNil()
}
func (executor *scriptExecutor) executeScript() error {
rt := executor.env.BorrowCadenceRuntime()
defer executor.env.ReturnCadenceRuntime(rt)
value, err := rt.ExecuteScript(
runtime.Script{
Source: executor.proc.Script,
Arguments: executor.proc.Arguments,
},
common.ScriptLocation(executor.proc.ID))
if err != nil {
return err
}
executor.output.Value = value
return executor.output.PopulateEnvironmentValues(executor.env)
}