-
Notifications
You must be signed in to change notification settings - Fork 179
/
script.go
146 lines (127 loc) · 3.68 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
package fvm
import (
"context"
"fmt"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/fvm/state"
"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
Value cadence.Value
Logs []string
Events []flow.Event
GasUsed uint64
MemoryUsed uint64
Err errors.Error
}
type ScriptProcessor interface {
Process(*VirtualMachine, Context, *ScriptProcedure, *state.StateHolder, *programs.Programs) error
}
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) Run(vm *VirtualMachine, ctx Context, sth *state.StateHolder, programs *programs.Programs) error {
for _, p := range ctx.ScriptProcessors {
err := p.Process(vm, ctx, proc, sth, programs)
txError, failure := errors.SplitErrorTypes(err)
if failure != nil {
if errors.IsALedgerFailure(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 {
proc.Err = txError
return nil
}
}
return nil
}
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
}
type ScriptInvoker struct{}
func NewScriptInvoker() ScriptInvoker {
return ScriptInvoker{}
}
func (i ScriptInvoker) Process(
vm *VirtualMachine,
ctx Context,
proc *ScriptProcedure,
sth *state.StateHolder,
programs *programs.Programs,
) error {
env := NewScriptEnvironment(proc.RequestContext, ctx, vm, sth, programs)
location := common.ScriptLocation(proc.ID[:])
value, err := vm.Runtime.ExecuteScript(
runtime.Script{
Source: proc.Script,
Arguments: proc.Arguments,
},
runtime.Context{
Interface: env,
Location: location,
},
)
if err != nil {
return errors.HandleRuntimeError(err)
}
proc.Value = value
proc.Logs = env.Logs()
proc.Events = env.Events()
proc.GasUsed = env.ComputationUsed()
proc.MemoryUsed = env.MemoryUsed()
return nil
}