forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-encode.go
530 lines (466 loc) · 13.8 KB
/
binary-encode.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package amino
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"time"
"github.com/davecgh/go-spew/spew"
)
//----------------------------------------
// cdc.encodeReflectBinary
// This is the main entrypoint for encoding all types in binary form. This
// function calls encodeReflectBinary*, and generally those functions should
// only call this one, for the prefix bytes are only written here.
// The value may be a nil interface, but not a nil pointer.
// The following contracts apply to all similar encode methods.
// CONTRACT: rv is not a pointer
// CONTRACT: rv is valid.
func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if rv.Kind() == reflect.Ptr {
panic("not allowed to be called with a reflect.Ptr")
}
if !rv.IsValid() {
panic("not allowed to be called with invalid / zero Value")
}
if printLog {
spew.Printf("(E) encodeReflectBinary(info: %v, rv: %#v (%v), fopts: %v)\n",
info, rv.Interface(), rv.Type(), fopts)
defer func() {
fmt.Printf("(E) -> err: %v\n", err)
}()
}
// Handle override if rv implements json.Marshaler.
if info.IsAminoMarshaler {
// First, encode rv into repr instance.
var rrv reflect.Value
var rinfo *TypeInfo
rrv, err = toReprObject(rv)
if err != nil {
return
}
rinfo, err = cdc.getTypeInfoWlock(info.AminoMarshalReprType)
if err != nil {
return
}
// Then, encode the repr instance.
err = cdc.encodeReflectBinary(w, rinfo, rrv, fopts, bare)
return
}
switch info.Type.Kind() {
//----------------------------------------
// Complex
case reflect.Interface:
err = cdc.encodeReflectBinaryInterface(w, info, rv, fopts, bare)
case reflect.Array:
if info.Type.Elem().Kind() == reflect.Uint8 {
err = cdc.encodeReflectBinaryByteArray(w, info, rv, fopts)
} else if kind := info.Type.Elem().Kind(); kind == reflect.Slice || kind == reflect.Array {
// for proto3 compatibility, we do not allow multidimensional arrays,
// unless the elements involved are bytes (e.g. [][]byte)
if info.Type.Elem().Elem().Kind() != reflect.Uint8 { // byte is an alias for uint8
elem := info.Type.Elem()
for {
if elem.Kind() == reflect.Slice || elem.Kind() == reflect.Array {
elem = elem.Elem()
continue
}
if elem.Kind() == reflect.Uint8 { // byte is an alias for uint8
break
}
err = errors.New("multidimensional arrays not allowed")
break
}
}
} else {
err = cdc.encodeReflectBinaryList(w, info, rv, fopts, bare)
}
case reflect.Slice:
switch info.Type.Elem().Kind() {
case reflect.Uint8:
err = cdc.encodeReflectBinaryByteSlice(w, info, rv, fopts)
case reflect.Slice, reflect.Array:
// for proto3 compatibility, we do not allow multidimensional slices,
// unless the elements involved are bytes (e.g. [][]byte)
elem := info.Type.Elem()
for {
if elem.Kind() == reflect.Slice || elem.Kind() == reflect.Array {
elem = elem.Elem()
continue
}
if elem.Kind() == reflect.Uint8 { // byte is an alias for uint8
break
}
err = errors.New("multidimensional slices not allowed")
break
}
default:
err = cdc.encodeReflectBinaryList(w, info, rv, fopts, bare)
}
case reflect.Struct:
err = cdc.encodeReflectBinaryStruct(w, info, rv, fopts, bare)
//----------------------------------------
// Signed
case reflect.Int64:
if fopts.BinFixed64 {
err = EncodeInt64(w, rv.Int())
} else {
err = EncodeUvarint(w, uint64(rv.Int()))
}
case reflect.Int32:
if fopts.BinFixed32 {
err = EncodeInt32(w, int32(rv.Int()))
} else {
err = EncodeUvarint(w, uint64(rv.Int()))
}
case reflect.Int16:
err = EncodeInt16(w, int16(rv.Int()))
case reflect.Int8:
err = EncodeInt8(w, int8(rv.Int()))
case reflect.Int:
err = EncodeUvarint(w, uint64(rv.Int()))
//----------------------------------------
// Unsigned
case reflect.Uint64:
if fopts.BinFixed64 {
err = EncodeUint64(w, rv.Uint())
} else {
err = EncodeUvarint(w, rv.Uint())
}
case reflect.Uint32:
if fopts.BinFixed32 {
err = EncodeUint32(w, uint32(rv.Uint()))
} else {
err = EncodeUvarint(w, rv.Uint())
}
case reflect.Uint16:
err = EncodeUint16(w, uint16(rv.Uint()))
case reflect.Uint8:
err = EncodeUint8(w, uint8(rv.Uint()))
case reflect.Uint:
err = EncodeUvarint(w, rv.Uint())
//----------------------------------------
// Misc
case reflect.Bool:
err = EncodeBool(w, rv.Bool())
case reflect.Float64:
if !fopts.Unsafe {
err = errors.New("amino float* support requires `amino:\"unsafe\"`")
return
}
err = EncodeFloat64(w, rv.Float())
case reflect.Float32:
if !fopts.Unsafe {
err = errors.New("amino float* support requires `amino:\"unsafe\"`")
return
}
err = EncodeFloat32(w, float32(rv.Float()))
case reflect.String:
err = EncodeString(w, rv.String())
//----------------------------------------
// Default
default:
panic(fmt.Sprintf("unsupported type %v", info.Type.Kind()))
}
return err
}
func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryInterface")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Special case when rv is nil, write 0x00 to denote an empty byteslice.
if rv.IsNil() {
_, err = w.Write([]byte{0x00})
return
}
// Get concrete non-pointer reflect value & type.
var crv, isPtr, isNilPtr = derefPointers(rv.Elem())
if isPtr && crv.Kind() == reflect.Interface {
// See "MARKER: No interface-pointers" in codec.go
panic("should not happen")
}
if isNilPtr {
panic(fmt.Sprintf("Illegal nil-pointer of type %v for registered interface %v. "+
"For compatibility with other languages, nil-pointer interface values are forbidden.", crv.Type(), iinfo.Type))
}
var crt = crv.Type()
// Get *TypeInfo for concrete type.
var cinfo *TypeInfo
cinfo, err = cdc.getTypeInfoWlock(crt)
if err != nil {
return
}
if !cinfo.Registered {
err = fmt.Errorf("cannot encode unregistered concrete type %v", crt)
return
}
// For Proto3 compatibility, encode interfaces as ByteLength.
buf := bytes.NewBuffer(nil)
// Write disambiguation bytes if needed.
needDisamb := false
if iinfo.AlwaysDisambiguate {
needDisamb = true
} else if len(iinfo.Implementers[cinfo.Prefix]) > 1 {
needDisamb = true
}
if needDisamb {
_, err = buf.Write(append([]byte{0x00}, cinfo.Disamb[:]...))
if err != nil {
return
}
}
// Write prefix bytes.
_, err = buf.Write(cinfo.Prefix.Bytes())
if err != nil {
return
}
// Write actual concrete value.
err = cdc.encodeReflectBinary(buf, cinfo, crv, fopts, true)
if err != nil {
return
}
if bare {
// Write byteslice without byte-length prefixing.
_, err = w.Write(buf.Bytes())
} else {
// Write byte-length prefixed byteslice.
err = EncodeByteSlice(w, buf.Bytes())
}
return err
}
func (cdc *Codec) encodeReflectBinaryByteArray(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (err error) {
ert := info.Type.Elem()
if ert.Kind() != reflect.Uint8 {
panic("should not happen")
}
length := info.Type.Len()
// Get byteslice.
var byteslice []byte
if rv.CanAddr() {
byteslice = rv.Slice(0, length).Bytes()
} else {
byteslice = make([]byte, length)
reflect.Copy(reflect.ValueOf(byteslice), rv) // XXX: looks expensive!
}
// Write byte-length prefixed byteslice.
err = EncodeByteSlice(w, byteslice)
return
}
func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryList")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
ert := info.Type.Elem()
if ert.Kind() == reflect.Uint8 {
panic("should not happen")
}
einfo, err := cdc.getTypeInfoWlock(ert)
if err != nil {
return
}
// Proto3 byte-length prefixing incurs alloc cost on the encoder.
// Here we incur it for unpacked form for ease of dev.
buf := bytes.NewBuffer(nil)
// If elem is not already a ByteLength type, write in packed form.
// This is a Proto wart due to Proto backwards compatibility issues.
// Amino2 will probably migrate to use the List typ3. Please? :)
typ3 := typeToTyp3(einfo.Type, fopts)
if typ3 != Typ3ByteLength {
// Write elems in packed form.
for i := 0; i < rv.Len(); i++ {
// Get dereferenced element value (or zero).
var erv, _, _ = derefPointersZero(rv.Index(i))
// Write the element value.
err = cdc.encodeReflectBinary(buf, einfo, erv, fopts, false)
if err != nil {
return
}
}
} else { // typ3 == Typ3ByteLength
// NOTE: ert is for the element value, while einfo.Type is dereferenced.
isErtStructPointer := ert.Kind() == reflect.Ptr && einfo.Type.Kind() == reflect.Struct
// Write elems in unpacked form.
for i := 0; i < rv.Len(); i++ {
// Write elements as repeated fields of the parent struct.
err = encodeFieldNumberAndTyp3(buf, fopts.BinFieldNum, Typ3ByteLength)
if err != nil {
return
}
// Get dereferenced element value and info.
var erv, isDefault = isDefaultValue(rv.Index(i))
if isDefault {
// Special case if:
// - erv is a struct pointer and
// - field option has EmptyElements set
if isErtStructPointer && fopts.EmptyElements {
// NOTE: Not sure what to do here, but for future-proofing,
// we explicitly fail on nil pointers, just like
// Proto3's Golang client does.
// This also makes it easier to upgrade to Amino2
// which would enable the encoding of nil structs.
return errors.New("nil struct pointers not supported when empty_elements field tag is set")
}
// Nothing to encode, so the length is 0.
err = EncodeByte(buf, byte(0x00))
if err != nil {
return
}
} else {
// Write the element value as a ByteLength.
// In case of any inner lists in unpacked form.
efopts := fopts
efopts.BinFieldNum = 1
err = cdc.encodeReflectBinary(buf, einfo, erv, efopts, false)
if err != nil {
return
}
}
}
}
if bare {
// Write byteslice without byte-length prefixing.
_, err = w.Write(buf.Bytes())
} else {
// Write byte-length prefixed byteslice.
err = EncodeByteSlice(w, buf.Bytes())
}
return err
}
// CONTRACT: info.Type.Elem().Kind() == reflect.Uint8
func (cdc *Codec) encodeReflectBinaryByteSlice(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryByteSlice")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
ert := info.Type.Elem()
if ert.Kind() != reflect.Uint8 {
panic("should not happen")
}
// Write byte-length prefixed byte-slice.
var byteslice = rv.Bytes()
err = EncodeByteSlice(w, byteslice)
return
}
func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv reflect.Value,
fopts FieldOptions, bare bool) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryBinaryStruct")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Proto3 incurs a cost in writing non-root structs.
// Here we incur it for root structs as well for ease of dev.
buf := bytes.NewBuffer(nil)
switch info.Type {
case timeType:
// Special case: time.Time
err = EncodeTime(buf, rv.Interface().(time.Time))
if err != nil {
return
}
default:
for _, field := range info.Fields {
// Get type info for field.
var finfo *TypeInfo
finfo, err = cdc.getTypeInfoWlock(field.Type)
if err != nil {
return
}
// Get dereferenced field value and info.
var frv = rv.Field(field.Index)
var frvIsPtr = frv.Kind() == reflect.Ptr
var dfrv, isDefault = isDefaultValue(frv)
if isDefault && !field.WriteEmpty {
// Do not encode default value fields
// (except when `amino:"write_empty"` is set).
continue
}
if field.UnpackedList {
// Write repeated field entries for each list item.
err = cdc.encodeReflectBinaryList(buf, finfo, dfrv, field.FieldOptions, true)
if err != nil {
return
}
} else {
// write empty if explicitly set or if this is a pointer:
writeEmpty := field.WriteEmpty || frvIsPtr
err = cdc.writeFieldIfNotEmpty(buf, field.BinFieldNum, finfo, fopts, field.FieldOptions, dfrv, writeEmpty, false)
if err != nil {
return
}
}
}
}
if bare {
// Write byteslice without byte-length prefixing.
_, err = w.Write(buf.Bytes())
} else {
// Write byte-length prefixed byteslice.
err = EncodeByteSlice(w, buf.Bytes())
}
return err
}
//----------------------------------------
// Misc.
// Write field key.
func encodeFieldNumberAndTyp3(w io.Writer, num uint32, typ Typ3) (err error) {
if (typ & 0xF8) != 0 {
panic(fmt.Sprintf("invalid Typ3 byte %v", typ))
}
if num > (1<<29 - 1) {
panic(fmt.Sprintf("invalid field number %v", num))
}
// Pack Typ3 and field number.
var value64 = (uint64(num) << 3) | uint64(typ)
// Write uvarint value for field and Typ3.
var buf [10]byte
n := binary.PutUvarint(buf[:], value64)
_, err = w.Write(buf[0:n])
return
}
func (cdc *Codec) writeFieldIfNotEmpty(
buf *bytes.Buffer,
fieldNum uint32,
finfo *TypeInfo,
structsFopts FieldOptions, // the wrapping struct's FieldOptions if any
fieldOpts FieldOptions, // the field's FieldOptions
derefedVal reflect.Value,
isWriteEmpty bool,
bare bool,
) error {
lBeforeKey := buf.Len()
// Write field key (number and type).
err := encodeFieldNumberAndTyp3(buf, fieldNum, typeToTyp3(finfo.Type, fieldOpts))
if err != nil {
return err
}
lBeforeValue := buf.Len()
// Write field value from rv.
err = cdc.encodeReflectBinary(buf, finfo, derefedVal, fieldOpts, bare)
if err != nil {
return err
}
lAfterValue := buf.Len()
if !isWriteEmpty && lBeforeValue == lAfterValue-1 && buf.Bytes()[buf.Len()-1] == 0x00 {
// rollback typ3/fieldnum and last byte if
// not a pointer and empty:
buf.Truncate(lBeforeKey)
}
return nil
}