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
/
parameters.go
231 lines (213 loc) · 5.11 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
228
229
230
231
// 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(script *Script) (Entrypoint, Prim, error) {
var ep Entrypoint
var ok bool
var prim Prim
// get list of script entrypoints
eps, _ := script.Entrypoints(true)
switch p.Entrypoint {
case "default":
// rebase branch by prepending the path to the named default entrypoint
prefix := script.SearchEntrypointName("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 := script.SearchEntrypointName(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
}
// stay compatible with v005 transaction serialization
func (p Parameters) MarshalBinary() ([]byte, error) {
// single Unit value
// if len(p.Entrypoint) == 0 && p.Value != nil && p.Value.OpCode == D_UNIT {
if len(p.Entrypoint) == 0 && p.Value.OpCode == D_UNIT {
return []byte{0}, nil
}
// entrypoint format, compatible with v005
buf := bytes.NewBuffer([]byte{1})
n := 2
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)
n += 1 + len(p.Entrypoint)
}
// param as size + serialized data
binary.Write(buf, binary.BigEndian, uint32(0))
if err := p.Value.EncodeBuffer(buf); err != nil {
return nil, err
}
// patch data size
res := buf.Bytes()
binary.BigEndian.PutUint32(res[n:], uint32(len(res)-n-4))
return res, nil
}
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) UnmarshalBinary(data []byte) error {
if len(data) == 1 && data[0] == 0 {
p.Value = Prim{Type: PrimNullary, OpCode: D_UNIT}
p.Entrypoint = "default"
return nil
}
buf := bytes.NewBuffer(data[1:])
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])))
}
// read serialized data
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
}