-
Notifications
You must be signed in to change notification settings - Fork 217
/
encoder.go
727 lines (627 loc) · 16.7 KB
/
encoder.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package eos
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"reflect"
"sort"
"time"
"github.com/eoscanada/eos-go/ecc"
"go.uber.org/zap"
)
// MarshalerBinary is the interface implemented by types
// that can marshal to an EOSIO binary description of themselves.
//
// **Warning** This is experimental, exposed only for internal usage for now.
type MarshalerBinary interface {
MarshalBinary(encoder *Encoder) error
}
func MarshalBinary(v interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
encoder := NewEncoder(buf)
err := encoder.Encode(v)
return buf.Bytes(), err
}
// --------------------------------------------------------------
// Encoder implements the EOS packing, similar to FC_BUFFER
// --------------------------------------------------------------
type Encoder struct {
output io.Writer
Order binary.ByteOrder
count int
}
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{
output: w,
Order: binary.LittleEndian,
count: 0,
}
}
func (e *Encoder) writeName(name Name) error {
val, err := StringToName(string(name))
if err != nil {
return fmt.Errorf("writeName: %w", err)
}
return e.writeUint64(val)
}
func (e *Encoder) Encode(v interface{}) (err error) {
switch cv := v.(type) {
case MarshalerBinary:
return cv.MarshalBinary(e)
case BaseVariant:
err = e.writeUVarInt(int(cv.TypeID))
if err != nil {
return
}
return e.Encode(cv.Impl)
case SafeString:
return e.writeString(string(cv))
case Name:
return e.writeName(cv)
case AccountName:
name := Name(cv)
return e.writeName(name)
case PermissionName:
name := Name(cv)
return e.writeName(name)
case ActionName:
name := Name(cv)
return e.writeName(name)
case TableName:
name := Name(cv)
return e.writeName(name)
case ScopeName:
name := Name(cv)
return e.writeName(name)
case string:
return e.writeString(cv)
case CompressionType:
return e.writeByte(byte(cv))
case TransactionStatus:
return e.writeByte(byte(cv))
case IDListMode:
return e.writeByte(byte(cv))
case byte:
return e.writeByte(cv)
case int8:
return e.writeByte(byte(cv))
case int16:
return e.writeInt16(cv)
case uint16:
return e.writeUint16(cv)
case int32:
return e.writeInt32(cv)
case uint32:
return e.writeUint32(cv)
case uint64:
return e.writeUint64(cv)
case Int64:
return e.writeUint64(uint64(cv))
case int64:
return e.writeInt64(cv)
case float32:
return e.writeFloat32(cv)
case float64:
return e.writeFloat64(cv)
case Varint32:
return e.writeVarInt32(int32(cv))
case Uint128:
return e.writeUint128(cv)
case Int128:
return e.writeUint128(Uint128(cv))
case Float128:
return e.writeUint128(Uint128(cv))
case Varuint32:
return e.writeUVarInt32(uint32(cv))
case bool:
return e.writeBool(cv)
case Bool:
return e.writeBool(bool(cv))
case JSONTime:
return e.writeJSONTime(cv)
case HexBytes:
return e.writeByteArray(cv)
case Checksum160:
return e.writeChecksum160(cv)
case Checksum256:
return e.writeChecksum256(cv)
case Checksum512:
return e.writeChecksum512(cv)
case []byte:
return e.writeByteArray(cv)
case ecc.PublicKey:
return e.writePublicKey(cv)
case ecc.Signature:
return e.writeSignature(cv)
case Tstamp:
return e.writeTstamp(cv)
case BlockTimestamp:
return e.writeBlockTimestamp(cv)
case CurrencyName:
return e.writeCurrencyName(cv)
case Symbol:
value, err := cv.ToUint64()
if err != nil {
return fmt.Errorf("encoding symbol: %w", err)
}
return e.writeUint64(value)
case SymbolCode:
return e.writeUint64(uint64(cv))
case Asset:
return e.writeAsset(cv)
case ActionData:
return e.writeActionData(cv)
case *ActionData:
return e.writeActionData(*cv)
case *Packet:
return e.writeBlockP2PMessageEnvelope(*cv)
case TimePoint:
return e.writeUint64(uint64(cv))
case TimePointSec:
return e.writeUint32(uint32(cv))
case nil:
default:
rv := reflect.Indirect(reflect.ValueOf(v))
t := rv.Type()
switch t.Kind() {
case reflect.Array:
l := t.Len()
if tracer.Enabled() {
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("array")
zlog.Debug("encode: array", zap.Int("length", l), typeField("type", v))
}
for i := 0; i < l; i++ {
if err = e.Encode(rv.Index(i).Interface()); err != nil {
return
}
}
case reflect.Slice:
l := rv.Len()
if err = e.writeUVarInt(l); err != nil {
return
}
if tracer.Enabled() {
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("slice")
zlog.Debug("encode: slice", zap.Int("length", l), typeField("type", v))
}
for i := 0; i < l; i++ {
if err = e.Encode(rv.Index(i).Interface()); err != nil {
return
}
}
case reflect.Struct:
l := rv.NumField()
if tracer.Enabled() {
zlog.Debug("encode: struct", zap.Int("fields", l), typeField("type", v))
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("struct")
}
for i := 0; i < l; i++ {
field := t.Field(i)
if tracer.Enabled() {
zlog.Debug("field", zap.String("field", field.Name))
}
tag := field.Tag.Get("eos")
if tag == "-" {
continue
}
if v := rv.Field(i); t.Field(i).Name != "_" {
if v.CanInterface() {
isPresent := true
if tag == "optional" {
isPresent = !v.IsZero()
e.writeBool(isPresent)
}
if isPresent {
if err = e.Encode(v.Interface()); err != nil {
return
}
}
}
}
}
case reflect.Map:
keys := rv.MapKeys()
keyCount := len(keys)
keyType := t.Key()
if tracer.Enabled() {
zlog.Debug("encode: map", zap.Int("key_count", keyCount), typeField("key_type", keyType))
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("map")
}
if err = e.writeUVarInt(keyCount); err != nil {
return
}
if keyCount == 0 {
return
}
keyKind, errCompare := basicKindFromReflect(keyType.Kind())
if errCompare != nil {
return fmt.Errorf("encode map: key of type %t must be comparable: %w", keyType, errCompare)
}
sort.Slice(keys, func(i, j int) bool {
left := keys[i]
right := keys[j]
// We have validate most of this already, only case that can still happens is in error in coverage
isLower, err := lt(keyKind, left, right)
if err != nil {
panic(fmt.Errorf("encode map: unable to compare keys: %w", err))
}
return isLower
})
for _, mapKey := range keys {
if err = e.Encode(mapKey.Interface()); err != nil {
return
}
if err = e.Encode(rv.MapIndex(mapKey).Interface()); err != nil {
return
}
}
default:
return errors.New("Encode: unsupported type " + t.String())
}
}
return
}
func (e *Encoder) toWriter(bytes []byte) (err error) {
e.count += len(bytes)
if tracer.Enabled() {
zlog.Debug(" appending", zap.Stringer("hex", HexBytes(bytes)), zap.Int("pos", e.count))
}
_, err = e.output.Write(bytes)
return
}
func (e *Encoder) writeByteArray(b []byte) error {
if tracer.Enabled() {
zlog.Debug("write byte array", zap.Int("len", len(b)))
}
if err := e.writeUVarInt(len(b)); err != nil {
return err
}
return e.toWriter(b)
}
func (e *Encoder) writeUVarInt(v int) (err error) {
if tracer.Enabled() {
zlog.Debug("write uvarint", zap.Int("val", v))
}
buf := make([]byte, 8)
l := binary.PutUvarint(buf, uint64(v))
return e.toWriter(buf[:l])
}
func (e *Encoder) writeUVarInt32(v uint32) (err error) {
if tracer.Enabled() {
zlog.Debug("write varuint32", zap.Uint32("val", v))
}
buf := make([]byte, binary.MaxVarintLen32)
l := binary.PutUvarint(buf, uint64(v))
return e.toWriter(buf[:l])
}
func (e *Encoder) writeVarInt(v int) (err error) {
if tracer.Enabled() {
zlog.Debug("write varint", zap.Int("val", v))
}
buf := make([]byte, 8)
l := binary.PutVarint(buf, int64(v))
return e.toWriter(buf[:l])
}
func (e *Encoder) writeVarInt32(v int32) (err error) {
if tracer.Enabled() {
zlog.Debug("write varint32", zap.Int32("val", v))
}
buf := make([]byte, binary.MaxVarintLen32)
l := binary.PutVarint(buf, int64(v))
return e.toWriter(buf[:l])
}
func (e *Encoder) writeByte(b byte) (err error) {
if tracer.Enabled() {
zlog.Debug("write byte", zap.Uint8("val", b))
}
return e.toWriter([]byte{b})
}
func (e *Encoder) writeBool(b bool) (err error) {
if tracer.Enabled() {
zlog.Debug("write bool", zap.Bool("val", b))
}
var out byte
if b {
out = 1
}
return e.writeByte(out)
}
func (e *Encoder) writeUint16(i uint16) (err error) {
if tracer.Enabled() {
zlog.Debug("write uint16", zap.Uint16("val", i))
}
buf := make([]byte, TypeSize.Uint16)
binary.LittleEndian.PutUint16(buf, i)
return e.toWriter(buf)
}
func (e *Encoder) writeInt16(i int16) (err error) {
if tracer.Enabled() {
zlog.Debug("write int16", zap.Int16("val", i))
}
return e.writeUint16(uint16(i))
}
func (e *Encoder) writeInt32(i int32) (err error) {
if tracer.Enabled() {
zlog.Debug("write int32", zap.Int32("val", i))
}
return e.writeUint32(uint32(i))
}
func (e *Encoder) writeUint32(i uint32) (err error) {
if tracer.Enabled() {
zlog.Debug("write uint32", zap.Uint32("val", i))
}
buf := make([]byte, TypeSize.Uint32)
binary.LittleEndian.PutUint32(buf, i)
return e.toWriter(buf)
}
func (e *Encoder) writeInt64(i int64) (err error) {
if tracer.Enabled() {
zlog.Debug("write int64", zap.Int64("val", i))
}
return e.writeUint64(uint64(i))
}
func (e *Encoder) writeUint64(i uint64) (err error) {
if tracer.Enabled() {
zlog.Debug("write uint64", zap.Uint64("val", i))
}
buf := make([]byte, TypeSize.Uint64)
binary.LittleEndian.PutUint64(buf, i)
return e.toWriter(buf)
}
func (e *Encoder) writeUint128(i Uint128) (err error) {
if tracer.Enabled() {
zlog.Debug("write uint128", zap.Stringer("hex", i), zap.Uint64("lo", i.Lo), zap.Uint64("hi", i.Hi))
}
buf := make([]byte, TypeSize.Uint128)
binary.LittleEndian.PutUint64(buf, i.Lo)
binary.LittleEndian.PutUint64(buf[TypeSize.Uint64:], i.Hi)
return e.toWriter(buf)
}
func (e *Encoder) writeFloat32(f float32) (err error) {
if tracer.Enabled() {
zlog.Debug("write float32", zap.Float32("val", f))
}
i := math.Float32bits(f)
buf := make([]byte, TypeSize.Uint32)
binary.LittleEndian.PutUint32(buf, i)
return e.toWriter(buf)
}
func (e *Encoder) writeFloat64(f float64) (err error) {
if tracer.Enabled() {
zlog.Debug("write float64", zap.Float64("val", f))
}
i := math.Float64bits(f)
buf := make([]byte, TypeSize.Uint64)
binary.LittleEndian.PutUint64(buf, i)
return e.toWriter(buf)
}
func (e *Encoder) writeString(s string) (err error) {
if tracer.Enabled() {
zlog.Debug("write string", zap.String("val", s))
}
return e.writeByteArray([]byte(s))
}
func (e *Encoder) writeChecksum160(checksum Checksum160) error {
if tracer.Enabled() {
zlog.Debug("write Checksum160", zap.Stringer("hex", HexBytes(checksum)))
}
if len(checksum) == 0 {
return e.toWriter(bytes.Repeat([]byte{0}, TypeSize.Checksum160))
}
return e.toWriter(checksum)
}
func (e *Encoder) writeChecksum256(checksum Checksum256) error {
if tracer.Enabled() {
zlog.Debug("write Checksum256", zap.Stringer("hex", HexBytes(checksum)))
}
if len(checksum) == 0 {
return e.toWriter(bytes.Repeat([]byte{0}, TypeSize.Checksum256))
}
return e.toWriter(checksum)
}
func (e *Encoder) writeChecksum512(checksum Checksum512) error {
if tracer.Enabled() {
zlog.Debug("write Checksum512", zap.Stringer("hex", HexBytes(checksum)))
}
if len(checksum) == 0 {
return e.toWriter(bytes.Repeat([]byte{0}, TypeSize.Checksum512))
}
return e.toWriter(checksum)
}
func (e *Encoder) writePublicKey(pk ecc.PublicKey) (err error) {
if tracer.Enabled() {
zlog.Debug("write public key", zap.Stringer("pubkey", pk))
}
err = pk.Validate()
if err != nil {
return fmt.Errorf("invalid public key: %w", err)
}
if err = e.writeByte(byte(pk.Curve)); err != nil {
return err
}
return e.toWriter(pk.Content)
}
func (e *Encoder) writeSignature(s ecc.Signature) (err error) {
if tracer.Enabled() {
zlog.Debug("write signature", zap.Stringer("sig", s))
}
err = s.Validate()
if err != nil {
return fmt.Errorf("invalid signature: %w", err)
}
if err = e.writeByte(byte(s.Curve)); err != nil {
return
}
return e.toWriter(s.Content)
}
func (e *Encoder) writeTstamp(t Tstamp) (err error) {
if tracer.Enabled() {
zlog.Debug("write tstamp", zap.Time("time", t.Time))
}
n := uint64(t.UnixNano())
return e.writeUint64(n)
}
func (e *Encoder) writeBlockTimestamp(bt BlockTimestamp) (err error) {
if tracer.Enabled() {
zlog.Debug("write block timestamp", zap.Time("time", bt.Time))
}
milliseconds := bt.UnixNano() / time.Millisecond.Nanoseconds()
slot := (milliseconds - 946684800000) / 500
return e.writeUint32(uint32(slot))
}
func (e *Encoder) writeCurrencyName(currency CurrencyName) (err error) {
// FIXME: this isn't really used.. we should implement serialization for the Symbol
// type only instead.
if tracer.Enabled() {
zlog.Debug("write currency", zap.String("name", string(currency)))
}
out := make([]byte, 7, 7)
copy(out, []byte(currency))
return e.toWriter(out)
}
func (e *Encoder) writeAsset(asset Asset) (err error) {
if tracer.Enabled() {
zlog.Debug("write asset", zap.Stringer("value", asset))
}
e.writeUint64(uint64(asset.Amount))
e.writeByte(asset.Precision)
symbol := make([]byte, 7, 7)
copy(symbol[:], []byte(asset.Symbol.Symbol))
return e.toWriter(symbol)
}
func (e *Encoder) writeJSONTime(tm JSONTime) (err error) {
if tracer.Enabled() {
zlog.Debug("write json time", zap.Time("time", tm.Time))
}
return e.writeUint32(uint32(tm.Unix()))
}
func (e *Encoder) writeBlockP2PMessageEnvelope(envelope Packet) (err error) {
if tracer.Enabled() {
zlog.Debug("p2p: write message envelope")
}
if envelope.P2PMessage != nil {
buf := new(bytes.Buffer)
subEncoder := NewEncoder(buf)
err = subEncoder.Encode(envelope.P2PMessage)
if err != nil {
err = fmt.Errorf("p2p message, %s", err)
return
}
envelope.Payload = buf.Bytes()
}
messageLen := uint32(len(envelope.Payload) + 1)
if tracer.Enabled() {
zlog.Debug("p2p: message length", zap.Uint32("len", messageLen))
}
err = e.writeUint32(messageLen)
if err == nil {
err = e.writeByte(byte(envelope.Type))
if err == nil {
return e.toWriter(envelope.Payload)
}
}
return
}
func (e *Encoder) writeActionData(actionData ActionData) (err error) {
if actionData.Data != nil {
//if reflect.TypeOf(actionData.Data) == reflect.TypeOf(&ActionData{}) {
// log.Fatal("pas cool")
//}
if tracer.Enabled() {
zlog.Debug("entering action data", typeField("type", actionData))
}
var d interface{}
d = actionData.Data
if reflect.TypeOf(d).Kind() == reflect.Ptr {
d = reflect.ValueOf(actionData.Data).Elem().Interface()
}
if reflect.TypeOf(d).Kind() == reflect.String { //todo : this is a very bad ack ......
data, err := hex.DecodeString(d.(string))
if err != nil {
return fmt.Errorf("ack, %s", err)
}
e.writeByteArray(data)
return nil
}
if tracer.Enabled() {
zlog.Debug("encoding action data", typeField("type", d))
}
raw, err := MarshalBinary(d)
if err != nil {
return err
}
if tracer.Enabled() {
zlog.Debug("writing action data", typeField("type", d))
}
return e.writeByteArray(raw)
}
return e.writeByteArray(actionData.HexData)
}
// lt evaluates the comparison a < b.
//
// Copied from text/template in Golang 1.19.2
func lt(valueKind kind, arg1, arg2 reflect.Value) (bool, error) {
arg1 = indirectInterface(arg1)
arg2 = indirectInterface(arg2)
truth := false
switch valueKind {
case floatKind:
truth = arg1.Float() < arg2.Float()
case intKind:
truth = arg1.Int() < arg2.Int()
case stringKind:
truth = arg1.String() < arg2.String()
case uintKind:
truth = arg1.Uint() < arg2.Uint()
default:
panic("invalid kind")
}
return truth, nil
}
// indirectInterface returns the concrete value in an interface value,
// or else the zero reflect.Value.
// That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x):
// the fact that x was an interface value is forgotten.
func indirectInterface(v reflect.Value) reflect.Value {
if v.Kind() != reflect.Interface {
return v
}
if v.IsNil() {
return reflect.Value{}
}
return v.Elem()
}
type kind int
const (
invalidKind kind = iota
boolKind
complexKind
intKind
floatKind
stringKind
uintKind
)
func basicKindFromReflect(v reflect.Kind) (kind, error) {
switch v {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intKind, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintKind, nil
case reflect.Float32, reflect.Float64:
return floatKind, nil
case reflect.String:
return stringKind, nil
}
return invalidKind, fmt.Errorf("invalid type %s for comparison", v)
}
func basicKind(v reflect.Value) (kind, error) {
return basicKindFromReflect(v.Kind())
}