forked from hyperledger-archives/burrow
/
spec.go
313 lines (278 loc) · 7.73 KB
/
spec.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package abi
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/hyperledger/burrow/crypto"
)
// Token to use in deploy yaml in order to indicate call to the fallback function.
const FallbackFunctionName = "()"
// Spec is the ABI for contract decoded.
type Spec struct {
Constructor *FunctionSpec
Fallback *FunctionSpec
Functions map[string]*FunctionSpec
EventsByName map[string]*EventSpec
EventsByID map[EventID]*EventSpec
}
type specJSON struct {
Name string
Type string
Inputs []argumentJSON
Outputs []argumentJSON
StateMutability string
Anonymous bool
}
func NewSpec() *Spec {
return &Spec{
// Zero value for constructor and fallback function is assumed when those functions are not present
Constructor: &FunctionSpec{},
Fallback: &FunctionSpec{},
EventsByName: make(map[string]*EventSpec),
EventsByID: make(map[EventID]*EventSpec),
Functions: make(map[string]*FunctionSpec),
}
}
// ReadSpec takes an ABI and decodes it for futher use
func ReadSpec(specBytes []byte) (*Spec, error) {
var specJ []specJSON
err := json.Unmarshal(specBytes, &specJ)
if err != nil {
// The abi spec file might a bin file, with the Abi under the Abi field in json
var binFile struct {
Abi []specJSON
}
err = json.Unmarshal(specBytes, &binFile)
if err != nil {
return nil, err
}
specJ = binFile.Abi
}
abiSpec := NewSpec()
for _, s := range specJ {
switch s.Type {
case "constructor":
abiSpec.Constructor.Inputs, err = readArgSpec(s.Inputs)
if err != nil {
return nil, err
}
case "fallback":
abiSpec.Fallback.Inputs = make([]Argument, 0)
abiSpec.Fallback.Outputs = make([]Argument, 0)
abiSpec.Fallback.SetConstant()
abiSpec.Functions[FallbackFunctionName] = abiSpec.Fallback
case "event":
ev := new(EventSpec)
err = ev.unmarshalSpec(&s)
if err != nil {
return nil, err
}
abiSpec.EventsByName[ev.Name] = ev
abiSpec.EventsByID[ev.ID] = ev
case "function":
inputs, err := readArgSpec(s.Inputs)
if err != nil {
return nil, err
}
outputs, err := readArgSpec(s.Outputs)
if err != nil {
return nil, err
}
abiSpec.Functions[s.Name] = NewFunctionSpec(s.Name, inputs, outputs).SetConstant()
}
}
return abiSpec, nil
}
// MergeSpec takes multiple Specs and merges them into once structure. Note that
// the same function name or event name can occur in different abis, so there might be
// some information loss.
func MergeSpec(abiSpec []*Spec) *Spec {
newSpec := NewSpec()
for _, s := range abiSpec {
for n, f := range s.Functions {
newSpec.Functions[n] = f
}
// Different Abis can have the Event name, but with a different signature
// Loop over the signatures, as these are less likely to have collisions
for _, e := range s.EventsByID {
newSpec.EventsByName[e.Name] = e
newSpec.EventsByID[e.ID] = e
}
}
return newSpec
}
func (spec *Spec) GetEventAbi(id EventID, addresses crypto.Address) (*EventSpec, error) {
eventSpec, ok := spec.EventsByID[id]
if !ok {
return nil, fmt.Errorf("could not find ABI for event with ID %v", id)
}
return eventSpec, nil
}
// Pack ABI encodes a function call. The fname specifies which function should called, if
// it doesn't exist exist the fallback function will be called. If fname is the empty
// string, the constructor is called. The arguments must be specified in args. The count
// must match the function being called.
// Returns the ABI encoded function call, whether the function is constant according
// to the ABI (which means it does not modified contract state)
func (spec *Spec) Pack(fname string, args ...interface{}) ([]byte, *FunctionSpec, error) {
var funcSpec *FunctionSpec
var argSpec []Argument
if fname != "" {
if _, ok := spec.Functions[fname]; ok {
funcSpec = spec.Functions[fname]
} else {
return nil, nil, fmt.Errorf("unknown function in Pack: %s", fname)
}
} else {
if spec.Constructor.Inputs != nil {
funcSpec = spec.Constructor
} else {
return nil, nil, fmt.Errorf("contract does not have a constructor")
}
}
argSpec = funcSpec.Inputs
packed := make([]byte, 0)
if fname != "" {
packed = funcSpec.FunctionID[:]
}
packedArgs, err := Pack(argSpec, args...)
if err != nil {
return nil, nil, err
}
return append(packed, packedArgs...), funcSpec, nil
}
// Unpack decodes the return values from a function call
func (spec *Spec) Unpack(data []byte, fname string, args ...interface{}) error {
var funcSpec *FunctionSpec
var argSpec []Argument
if fname != "" {
if _, ok := spec.Functions[fname]; ok {
funcSpec = spec.Functions[fname]
} else {
funcSpec = spec.Fallback
}
} else {
funcSpec = spec.Constructor
}
argSpec = funcSpec.Outputs
if argSpec == nil {
return fmt.Errorf("unknown function in Unpack: %s", fname)
}
return unpack(argSpec, data, func(i int) interface{} {
return args[i]
})
}
func (spec *Spec) UnpackWithID(data []byte, args ...interface{}) error {
var argSpec []Argument
var id FunctionID
copy(id[:], data)
for _, fspec := range spec.Functions {
if id == fspec.FunctionID {
argSpec = fspec.Outputs
}
}
if argSpec == nil {
return fmt.Errorf("unknown function in UnpackWithID: %x", id)
}
return unpack(argSpec, data[4:], func(i int) interface{} {
return args[i]
})
}
func readArgSpec(argsJ []argumentJSON) ([]Argument, error) {
args := make([]Argument, len(argsJ))
var err error
for i, a := range argsJ {
args[i].Name = a.Name
args[i].Indexed = a.Indexed
baseType := a.Type
isArray := regexp.MustCompile(`(.*)\[([0-9]+)\]`)
m := isArray.FindStringSubmatch(a.Type)
if m != nil {
args[i].IsArray = true
args[i].ArrayLength, err = strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
baseType = m[1]
} else if strings.HasSuffix(a.Type, "[]") {
args[i].IsArray = true
baseType = strings.TrimSuffix(a.Type, "[]")
}
isM := regexp.MustCompile("(bytes|uint|int)([0-9]+)")
m = isM.FindStringSubmatch(baseType)
if m != nil {
M, err := strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
switch m[1] {
case "bytes":
if M < 1 || M > 32 {
return nil, fmt.Errorf("bytes%d is not valid type", M)
}
args[i].EVM = EVMBytes{M}
case "uint":
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("uint%d is not valid type", M)
}
args[i].EVM = EVMUint{M}
case "int":
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("uint%d is not valid type", M)
}
args[i].EVM = EVMInt{M}
}
continue
}
isMxN := regexp.MustCompile("(fixed|ufixed)([0-9]+)x([0-9]+)")
m = isMxN.FindStringSubmatch(baseType)
if m != nil {
M, err := strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
N, err := strconv.ParseUint(m[3], 10, 32)
if err != nil {
return nil, err
}
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("%s is not valid type", baseType)
}
if N == 0 || N > 80 {
return nil, fmt.Errorf("%s is not valid type", baseType)
}
if m[1] == "fixed" {
args[i].EVM = EVMFixed{N: N, M: M, signed: true}
} else if m[1] == "ufixed" {
args[i].EVM = EVMFixed{N: N, M: M, signed: false}
} else {
panic(m[1])
}
continue
}
switch baseType {
case "uint":
args[i].EVM = EVMUint{M: 256}
case "int":
args[i].EVM = EVMInt{M: 256}
case "address":
args[i].EVM = EVMAddress{}
case "bool":
args[i].EVM = EVMBool{}
case "fixed":
args[i].EVM = EVMFixed{M: 128, N: 8, signed: true}
case "ufixed":
args[i].EVM = EVMFixed{M: 128, N: 8, signed: false}
case "bytes":
args[i].EVM = EVMBytes{M: 0}
case "string":
args[i].EVM = EVMString{}
default:
// Assume it is a type of Contract
args[i].EVM = EVMAddress{}
}
}
return args, nil
}