This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
entrypoint.go
174 lines (153 loc) · 3.63 KB
/
entrypoint.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"fmt"
"strings"
)
type Entrypoint struct {
Id int `json:"id"`
Name string `json:"name"`
Branch string `json:"branch"`
Typedef []Typedef `json:"type"`
Prim *Prim `json:"prim,omitempty"`
}
func (e Entrypoint) Type() Type {
if e.Prim == nil {
e.Prim = &Prim{} // invalid
}
typ := NewType(*e.Prim)
if !typ.HasLabel() {
typ.Prim.Anno = []string{"@" + e.Name}
}
return typ
}
func (e Entrypoint) IsCallback() bool {
if e.Prim == nil {
return false
}
return e.Prim.IsPair() && e.Prim.Args.Last().OpCode == T_CONTRACT
}
type Entrypoints map[string]Entrypoint
func (e Entrypoints) FindBranch(branch string) (Entrypoint, bool) {
if branch == "" {
return Entrypoint{}, false
}
for _, v := range e {
if v.Branch == branch {
return v, true
}
}
return Entrypoint{}, false
}
func (e Entrypoints) FindId(id int) (Entrypoint, bool) {
for _, v := range e {
if v.Id == id {
return v, true
}
}
return Entrypoint{}, false
}
func (t Type) Entrypoints(withPrim bool) (Entrypoints, error) {
e := make(Entrypoints)
if !t.IsValid() {
return e, nil
}
if err := listEntrypoints(e, "", t.Prim); err != nil {
return nil, err
}
if !withPrim {
for n, v := range e {
v.Prim = nil
e[n] = v
}
}
return e, nil
}
// returns path to named entrypoint
func (t Type) ResolveEntrypointPath(name string) string {
if !t.IsValid() {
return ""
}
return resolveEntrypointPath(name, "", t.Prim)
}
func resolveEntrypointPath(name, branch string, node Prim) string {
if node.GetVarAnnoAny() == name {
return branch
}
if node.OpCode == T_OR && (len(branch) == 0 || !node.HasAnno()) {
b := resolveEntrypointPath(name, branch+"/L", node.Args[0])
if b != "" {
return b
}
b = resolveEntrypointPath(name, branch+"/R", node.Args[1])
if b != "" {
return b
}
}
return ""
}
// Explicit list of prefixes for detecting entrypoints.
//
// This is necessary to resolve ambiguities in contract designs that
// use T_OR as call parameter.
// - to handle conflicts between T_OR used for call params vs used for marking entrypoint
// skip annotated T_OR branches (exclude the root T_OR and any branch called 'default')
var knownEntrypointPrefixes = []string{"_Liq_entry_"}
func isKnownEntrypointPrefix(s string) bool {
if len(s) == 0 {
return false
}
for _, v := range knownEntrypointPrefixes {
if strings.HasPrefix(s, v) {
return true
}
}
return false
}
// walks T_OR expressions and stores each non-T_OR branch as entrypoint
func listEntrypoints(e Entrypoints, branch string, node Prim) error {
// prefer % annotations
name := node.GetVarAnnoAny()
if node.OpCode == T_OR && !isKnownEntrypointPrefix(name) {
if l := len(node.Args); l != 2 {
return fmt.Errorf("micheline: expected 2 arguments for T_OR, got %d", l)
}
if err := listEntrypoints(e, branch+"/L", node.Args[0]); err != nil {
return err
}
if err := listEntrypoints(e, branch+"/R", node.Args[1]); err != nil {
return err
}
return nil
}
// need unique entrypoint name
if name == "" {
if len(e) == 0 {
name = DEFAULT
} else {
name = fmt.Sprintf("%s_%d", CONST_ENTRYPOINT, len(e))
}
}
// process non-T_OR branches
cp := node.Clone()
ep := Entrypoint{
Id: len(e),
Branch: branch,
Name: name,
Prim: &cp,
}
if node.IsScalarType() || node.IsContainerType() {
ep.Typedef = []Typedef{buildTypedef("", node)}
ep.Typedef[0].Name = ""
} else {
td := buildTypedef("", node)
if len(td.Args) > 0 {
ep.Typedef = td.Args
} else {
ep.Typedef = []Typedef{td}
}
}
e[name] = ep
return nil
}