-
Notifications
You must be signed in to change notification settings - Fork 6
/
node.go
261 lines (236 loc) · 5.55 KB
/
node.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
package base
import (
"encoding/hex"
"fmt"
"regexp"
"strings"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/types"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Node - struct for parsing micheline
type Node struct {
Prim string `json:"prim,omitempty"`
Args []*Node `json:"args,omitempty"`
Annots []string `json:"annots,omitempty"`
StringValue *string `json:"string,omitempty"`
BytesValue *string `json:"bytes,omitempty"`
IntValue *types.BigInt `json:"int,omitempty"`
}
// UnmarshalJSON -
func (node *Node) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return consts.ErrInvalidJSON
}
if data[0] == '[' {
node.Prim = consts.PrimArray
node.Args = make([]*Node, 0)
return json.Unmarshal(data, &node.Args)
} else if data[0] == '{' {
type buf Node
return json.Unmarshal(data, (*buf)(node))
}
return consts.ErrInvalidJSON
}
// MarshalJSON -
func (node *Node) MarshalJSON() ([]byte, error) {
if node.Prim == consts.PrimArray {
return json.Marshal(node.Args)
}
type buf Node
return json.Marshal((*buf)(node))
}
// GetAnnotations - returns all node`s annotations recursively
func (node *Node) GetAnnotations() map[string]struct{} {
annots := make(map[string]struct{})
for i := range node.Annots {
if len(node.Annots[i]) == 0 {
continue
}
if node.Annots[i][0] == consts.AnnotPrefixFieldName || node.Annots[i][0] == consts.AnnotPrefixrefixTypeName {
annots[node.Annots[i][1:]] = struct{}{}
}
}
for i := range node.Args {
for k := range node.Args[i].GetAnnotations() {
annots[k] = struct{}{}
}
}
return annots
}
// Hash -
func (node *Node) Hash() (string, error) {
var s strings.Builder
var prim string
switch {
case node.Prim != "":
if node.Prim != consts.RENAME && node.Prim != consts.CAST && node.Prim != consts.PrimArray {
hashCode, err := getHashCode(node.Prim)
if err != nil {
return "", err
}
s.WriteString(hashCode)
}
for i := range node.Args {
childHashCode, err := node.Args[i].Hash()
if err != nil {
return "", err
}
s.WriteString(childHashCode)
}
return s.String(), nil
case node.BytesValue != nil:
prim = consts.BYTES
case node.IntValue != nil:
prim = consts.INT
case node.StringValue != nil:
prim = consts.STRING
}
hashCode, err := getHashCode(prim)
if err != nil {
return "", err
}
s.WriteString(hashCode)
return s.String(), nil
}
// Compare -
func (node *Node) Compare(second *Node) bool {
if node.Prim != second.Prim {
return false
}
if len(node.Args) != len(second.Args) {
return false
}
for i := range node.Args {
if !node.Args[i].Compare(second.Args[i]) {
return false
}
}
return true
}
// String - converts node info to string
func (node *Node) String() string {
return node.print(0) + "\n"
}
func (node *Node) print(depth int) string {
var s strings.Builder
s.WriteByte('\n')
s.WriteString(strings.Repeat(consts.DefaultIndent, depth))
switch {
case node.Prim != "":
s.WriteString(node.Prim)
for i := range node.Args {
s.WriteString(node.Args[i].print(depth + 1))
}
case node.IntValue != nil:
s.WriteString(fmt.Sprintf("Int=%d", *node.IntValue))
case node.BytesValue != nil:
s.WriteString(fmt.Sprintf("Bytes=%s", *node.BytesValue))
case node.StringValue != nil:
s.WriteString(fmt.Sprintf("String=%s", *node.StringValue))
}
return s.String()
}
var lambdaPackedReg = regexp.MustCompile("^0502[0-9a-f]{8}0[3-9]")
// IsLambda -
func (node *Node) IsLambda() bool {
if node.BytesValue == nil {
return false
}
input := *node.BytesValue
if len(input) < 24 {
return false
}
if !lambdaPackedReg.MatchString(input) {
return false
}
b, err := hex.DecodeString(input[22:24])
if err != nil {
return false
}
if len(b) != 1 {
return false
}
if 0x0c > b[0] || 0x75 < b[0] {
return false
}
return 0x58 >= b[0] || 0x6f <= b[0]
}
// Fingerprint -
func (node *Node) Fingerprint(isCode bool) (string, error) {
var fgpt strings.Builder
switch node.Prim {
case consts.PrimArray:
for i := range node.Args {
buf, err := node.Args[i].Fingerprint(isCode)
if err != nil {
return "", err
}
fgpt.WriteString(buf)
}
default:
if node.Prim != "" {
if skip(node.Prim, isCode) {
return "", nil
}
if !pass(node.Prim, isCode) {
code, err := getCode(node.Prim)
if err != nil {
return "", err
}
fgpt.WriteString(code)
}
for i := range node.Args {
itemFgpt, err := node.Args[i].Fingerprint(isCode)
if err != nil {
return "", err
}
fgpt.WriteString(itemFgpt)
}
} else {
var prim string
switch {
case node.StringValue != nil:
prim = consts.STRING
case node.BytesValue != nil:
prim = consts.BYTES
case node.IntValue != nil:
prim = consts.INT
}
if prim != "" {
code, err := getCode(prim)
if err != nil {
return "", err
}
fgpt.WriteString(code)
}
}
}
return fgpt.String(), nil
}
func skip(prim string, isCode bool) bool {
p := strings.ToLower(prim)
return isCode && (p == consts.CAST || p == consts.RENAME)
}
func pass(prim string, isCode bool) bool {
p := strings.ToLower(prim)
return !isCode && (p == consts.PAIR || p == consts.OR)
}
func getCode(prim string) (string, error) {
code, ok := codes[prim]
if ok {
return code, nil
}
for template, code := range regCodes {
str := template.String()
if str[0] != prim[0] {
continue
}
if template.MatchString(prim) {
return code, nil
}
}
return "00", errors.Errorf("Unknown primitive: %s", prim)
}