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 32
/
parameters.go
227 lines (210 loc) · 4.87 KB
/
parameters.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"strings"
)
type Parameters struct {
Entrypoint string `json:"entrypoint"`
Value Prim `json:"value"`
}
func (p Parameters) MarshalJSON() ([]byte, error) {
if p.Entrypoint == "" || (p.Entrypoint == DEFAULT && p.Value.OpCode == D_UNIT) {
return json.Marshal(p.Value)
}
type alias Parameters
return json.Marshal(alias(p))
}
func (p Parameters) MapEntrypoint(typ Type) (Entrypoint, Prim, error) {
var ep Entrypoint
var ok bool
var prim Prim
// get list of script entrypoints
eps, _ := typ.Entrypoints(true)
switch p.Entrypoint {
case DEFAULT:
// rebase branch by prepending the path to the named default entrypoint
prefix := typ.ResolveEntrypointPath(DEFAULT)
// can be [LR]+ or empty when entrypoint is used
branch := p.Branch(prefix, eps)
ep, ok = eps.FindBranch(branch)
if !ok {
ep, _ = eps.FindId(0)
prim = p.Value
} else {
prim = p.Unwrap(strings.TrimPrefix(ep.Branch, prefix))
}
case ROOT, "":
// search unnamed naked entrypoint
branch := p.Branch("", eps)
ep, ok = eps.FindBranch(branch)
if !ok {
ep, _ = eps.FindId(0)
}
prim = p.Unwrap(ep.Branch)
default:
// search for named entrypoint
ep, ok = eps[p.Entrypoint]
if !ok {
// entrypoint can be a combination of an annotated branch and more T_OR branches
// inside parameters, so lets find the named branch
prefix := typ.ResolveEntrypointPath(p.Entrypoint)
if prefix == "" {
// meh
return ep, prim, fmt.Errorf("micheline: missing entrypoint '%s'", p.Entrypoint)
}
// otherwise rebase using the annotated branch as prefix
branch := p.Branch(prefix, eps)
ep, ok = eps.FindBranch(branch)
if !ok {
return ep, prim, fmt.Errorf("micheline: missing entrypoint '%s' + %s", p.Entrypoint, prefix)
}
// unwrap the suffix branch only
prim = p.Unwrap(strings.TrimPrefix(ep.Branch, prefix))
} else {
prim = p.Value
}
}
return ep, prim, nil
}
func (p Parameters) Branch(prefix string, eps Entrypoints) string {
node := p.Value
if !node.IsValid() {
return ""
}
branch := prefix
done:
for {
switch node.OpCode {
case D_LEFT:
branch += "/L"
case D_RIGHT:
branch += "/R"
default:
break done
}
node = node.Args[0]
if _, ok := eps.FindBranch(branch); ok {
break done
}
}
return branch
}
func (p Parameters) Unwrap(branch string) Prim {
node := p.Value
branch = strings.TrimPrefix(branch, "/")
branch = strings.TrimSuffix(branch, "/")
for _, v := range strings.Split(branch, "/") {
if !node.IsValid() {
break
}
switch v {
case "L":
node = node.Args[0]
case "R":
node = node.Args[0]
}
}
return node
}
func (p Parameters) EncodeBuffer(buf *bytes.Buffer) error {
// marshal value first to catch any error before writing to buffer
val, err := p.Value.MarshalBinary()
if err != nil {
return err
}
switch p.Entrypoint {
case "", DEFAULT:
buf.WriteByte(0)
case ROOT:
buf.WriteByte(1)
case DO:
buf.WriteByte(2)
case SET_DELEGATE:
buf.WriteByte(3)
case REMOVE_DELEGATE:
buf.WriteByte(4)
default:
buf.WriteByte(255)
buf.WriteByte(byte(len(p.Entrypoint)))
buf.WriteString(p.Entrypoint)
}
binary.Write(buf, binary.BigEndian, uint32(len(val)))
buf.Write(val)
return nil
}
func (p Parameters) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := p.EncodeBuffer(buf)
return buf.Bytes(), err
}
func (p *Parameters) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
if data[0] == '[' {
// non-entrypoint calling convention
return json.Unmarshal(data, &p.Value)
} else {
// try entrypoint calling convention
type alias *Parameters
if err := json.Unmarshal(data, alias(p)); err != nil {
return err
}
if p.Value.IsValid() {
return nil
}
// try legacy calling convention for single prim values
p.Entrypoint = DEFAULT
return json.Unmarshal(data, &p.Value)
}
}
func (p *Parameters) DecodeBuffer(buf *bytes.Buffer) error {
if buf.Len() < 1 {
return io.ErrShortBuffer
}
tag := buf.Next(1)
if len(tag) == 0 {
return io.ErrShortBuffer
}
switch tag[0] {
case 0:
p.Entrypoint = DEFAULT
case 1:
p.Entrypoint = ROOT
case 2:
p.Entrypoint = DO
case 3:
p.Entrypoint = SET_DELEGATE
case 4:
p.Entrypoint = REMOVE_DELEGATE
default:
sz := buf.Next(1)
if len(sz) == 0 || buf.Len() < int(sz[0]) {
return io.ErrShortBuffer
}
p.Entrypoint = string(buf.Next(int(sz[0])))
}
if buf.Len() == 0 {
p.Value = Prim{Type: PrimNullary, OpCode: D_UNIT}
return nil
}
size := int(binary.BigEndian.Uint32(buf.Next(4)))
if buf.Len() < size {
return io.ErrShortBuffer
}
prim := Prim{}
if err := prim.DecodeBuffer(buf); err != nil {
return err
}
p.Value = prim
return nil
}
func (p *Parameters) UnmarshalBinary(data []byte) error {
return p.DecodeBuffer(bytes.NewBuffer(data))
}