/
tree_estimatorV1.go
270 lines (247 loc) · 6.68 KB
/
tree_estimatorV1.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package ride
import (
"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/ride/ast"
)
type scopeValue struct {
id string
n ast.Node
}
type estimationScopeV1 struct {
values []scopeValue
functions map[string]int
builtin map[string]int
}
func newEstimationScopeV1(values []string, functions map[string]int) *estimationScopeV1 {
initial := make([]scopeValue, len(values))
for i, id := range values {
initial[i] = scopeValue{id: id}
}
return &estimationScopeV1{
values: initial,
functions: make(map[string]int),
builtin: functions,
}
}
func (s *estimationScopeV1) setValue(id string, n ast.Node) {
s.values = append(s.values, scopeValue{id: id, n: n})
}
func (s *estimationScopeV1) value(id string) (ast.Node, error) {
for i := len(s.values) - 1; i >= 0; i-- {
if s.values[i].id == id {
return s.values[i].n, nil
}
}
return nil, errors.Errorf("value '%s' not found", id)
}
func (s *estimationScopeV1) save() int {
return len(s.values)
}
func (s *estimationScopeV1) restore(p int) []scopeValue {
l := len(s.values)
if p < l {
r := make([]scopeValue, l-p)
copy(r, s.values[p:])
s.values = s.values[:p]
return r
}
return nil
}
func (s *estimationScopeV1) setFunction(id string, cost int) {
s.functions[id] = cost
}
func (s *estimationScopeV1) function(function ast.Function) (int, error) {
id := function.Name()
switch function.(type) {
case ast.UserFunction:
if c, ok := s.functions[id]; ok {
return c, nil
}
if c, ok := s.builtin[id]; ok {
return c, nil
}
return 0, errors.Errorf("function '%s' not found", id)
case ast.NativeFunction:
if c, ok := s.builtin[id]; ok {
return c, nil
}
return 0, errors.Errorf("system function '%s' not found", id)
default:
return 0, errors.Errorf("function '%s' not found", id)
}
}
type treeEstimatorV1 struct {
tree *ast.Tree
scope *estimationScopeV1
}
func newTreeEstimatorV1(tree *ast.Tree) (*treeEstimatorV1, error) {
r := &treeEstimatorV1{tree: tree}
switch tree.LibVersion {
case ast.LibV1:
r.scope = newEstimationScopeV1(ConstantsV1, CatalogueV2)
case ast.LibV2:
r.scope = newEstimationScopeV1(ConstantsV2, CatalogueV2)
case ast.LibV3:
r.scope = newEstimationScopeV1(ConstantsV3, CatalogueV3)
case ast.LibV4:
r.scope = newEstimationScopeV1(ConstantsV4, CatalogueV4)
case ast.LibV5:
r.scope = newEstimationScopeV1(ConstantsV5, CatalogueV5)
case ast.LibV6:
r.scope = newEstimationScopeV1(ConstantsV6, CatalogueV6)
case ast.LibV7:
r.scope = newEstimationScopeV1(ConstantsV7, CatalogueV7)
default:
return nil, errors.Errorf("unsupported library version %d", tree.LibVersion)
}
return r, nil
}
func (e *treeEstimatorV1) estimate() (int, int, map[string]int, error) {
if !e.tree.IsDApp() {
c, err := e.walk(e.tree.Verifier)
if err != nil {
return 0, 0, nil, err
}
return c, c, nil, nil
}
max := 0
m := make(map[string]int)
for i := 0; i < len(e.tree.Functions); i++ {
s := e.scope.save()
function, ok := e.tree.Functions[i].(*ast.FunctionDeclarationNode)
if !ok {
return 0, 0, nil, errors.New("invalid callable declaration")
}
c, err := e.walk(e.wrapFunction(function))
if err != nil {
return 0, 0, nil, err
}
m[function.Name] = c
if c > max {
max = c
}
e.scope.restore(s)
}
vc := 0
if e.tree.HasVerifier() {
verifier, ok := e.tree.Verifier.(*ast.FunctionDeclarationNode)
if !ok {
return 0, 0, nil, errors.New("invalid verifier declaration")
}
c, err := e.walk(e.wrapFunction(verifier))
if err != nil {
return 0, 0, nil, err
}
vc = c
if c > max {
max = c
}
}
return max, vc, m, nil
}
func (e *treeEstimatorV1) wrapFunction(node *ast.FunctionDeclarationNode) ast.Node {
args := make([]ast.Node, len(node.Arguments))
for i := range node.Arguments {
args[i] = ast.NewBooleanNode(true)
}
node.SetBlock(ast.NewFunctionCallNode(ast.UserFunction(node.Name), args))
var block ast.Node
block = ast.NewAssignmentNode(node.InvocationParameter, ast.NewBooleanNode(true), node)
for i := len(e.tree.Declarations) - 1; i >= 0; i-- {
e.tree.Declarations[i].SetBlock(block)
block = e.tree.Declarations[i]
}
return block
}
func (e *treeEstimatorV1) walk(node ast.Node) (int, error) {
switch n := node.(type) {
case *ast.LongNode, *ast.BytesNode, *ast.BooleanNode, *ast.StringNode:
return 1, nil
case *ast.ConditionalNode:
ce, err := e.walk(n.Condition)
if err != nil {
return 0, errors.Wrap(err, "failed to estimate the condition of if")
}
csp := e.scope.save()
te, err := e.walk(n.TrueExpression)
if err != nil {
return 0, errors.Wrap(err, "failed to estimate the true branch of if")
}
tsi := e.scope.restore(csp)
fe, err := e.walk(n.FalseExpression)
if err != nil {
return 0, errors.Wrap(err, "failed to estimate the false branch of if")
}
if te > fe {
e.scope.restore(csp)
e.scope.values = append(e.scope.values, tsi...)
return ce + te + 1, nil
}
return ce + fe + 1, nil
case *ast.AssignmentNode:
id := n.Name
e.scope.setValue(id, n.Expression)
c, err := e.walk(n.Block)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate block after declaration of variable '%s'", id)
}
return c + 5, nil
case *ast.ReferenceNode:
id := n.Name
v, err := e.scope.value(id)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate reference to '%s'", id)
}
if v != nil {
c, err := e.walk(v)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate expression of '%s'", id)
}
e.scope.setValue(id, nil)
return 2 + c, nil
}
return 2, nil
case *ast.FunctionDeclarationNode:
id := n.Name
tmp := e.scope.save()
for _, a := range n.Arguments {
e.scope.setValue(a, ast.NewBooleanNode(true))
}
fc, err := e.walk(n.Body)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate function '%s'", id)
}
ac := len(n.Arguments) * 5
e.scope.restore(tmp)
e.scope.setFunction(id, ac+fc)
bc, err := e.walk(n.Block)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate block after declaration of function '%s'", id)
}
return bc + 5, nil
case *ast.FunctionCallNode:
id := n.Function.Name()
function := n.Function
fc, err := e.scope.function(function)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate the call of function '%s'", id)
}
ac := 0
for i, a := range n.Arguments {
c, err := e.walk(a)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate parameter %d of function call '%s'", i, id)
}
ac += c
}
return fc + ac, nil
case *ast.PropertyNode:
c, err := e.walk(n.Object)
if err != nil {
return 0, errors.Wrapf(err, "failed to estimate getter '%s'", n.Name)
}
return c + 2, nil
default:
return 0, errors.Errorf("unsupported type of node '%T'", node)
}
}