forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
509 lines (431 loc) · 12.5 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
package eos
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"reflect"
"time"
"math"
"github.com/eoscanada/eos-go/ecc"
"go.uber.org/zap"
)
// --------------------------------------------------------------
// 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: %s", err)
}
return e.writeUint64(val)
}
func (e *Encoder) Encode(v interface{}) (err error) {
switch cv := v.(type) {
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(uint8(cv))
case TransactionStatus:
return e.writeByte(uint8(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 Uint64:
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.writeVarInt(int(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.writeUVarInt(int(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: %s", err)
}
return e.writeUint64(value)
case SymbolCode:
return e.writeUint64(uint64(cv))
case Asset:
return e.writeAsset(cv)
// case *OptionalProducerSchedule:
// isPresent := cv != nil
// e.writeBool(isPresent)
// if isPresent {
// }
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.writeUint64(uint64(cv))
case nil:
default:
rv := reflect.Indirect(reflect.ValueOf(v))
t := rv.Type()
switch t.Kind() {
case reflect.Array:
l := t.Len()
defer func(prev *zap.Logger) { encoderLog = prev }(encoderLog)
encoderLog = encoderLog.Named("array")
encoderLog.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
}
defer func(prev *zap.Logger) { encoderLog = prev }(encoderLog)
encoderLog = encoderLog.Named("slice")
encoderLog.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()
encoderLog.Debug("encode: struct", zap.Int("fields", l), typeField("type", v))
defer func(prev *zap.Logger) { encoderLog = prev }(encoderLog)
encoderLog = encoderLog.Named("struct")
for i := 0; i < l; i++ {
field := t.Field(i)
encoderLog.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.IsNil()
e.writeBool(isPresent)
}
if isPresent {
if err = e.Encode(v.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)
encoderLog.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 {
encoderLog.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) {
encoderLog.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) writeVarInt(v int) (err error) {
encoderLog.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) writeByte(b byte) (err error) {
encoderLog.Debug("write byte", zap.Uint8("val", b))
return e.toWriter([]byte{b})
}
func (e *Encoder) writeBool(b bool) (err error) {
encoderLog.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) {
encoderLog.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) {
encoderLog.Debug("write int16", zap.Int16("val", i))
return e.writeUint16(uint16(i))
}
func (e *Encoder) writeInt32(i int32) (err error) {
encoderLog.Debug("write int32", zap.Int32("val", i))
return e.writeUint32(uint32(i))
}
func (e *Encoder) writeUint32(i uint32) (err error) {
encoderLog.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) {
encoderLog.Debug("write int64", zap.Int64("val", i))
return e.writeUint64(uint64(i))
}
func (e *Encoder) writeUint64(i uint64) (err error) {
encoderLog.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) {
encoderLog.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) {
encoderLog.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) {
encoderLog.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) {
encoderLog.Debug("write string", zap.String("val", s))
return e.writeByteArray([]byte(s))
}
func (e *Encoder) writeChecksum160(checksum Checksum160) error {
encoderLog.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 {
encoderLog.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 {
encoderLog.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) {
encoderLog.Debug("write public key", zap.Stringer("pubkey", pk))
if len(pk.Content) != 33 {
return fmt.Errorf("public key %q should be 33 bytes, was %d", hex.EncodeToString(pk.Content), len(pk.Content))
}
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) {
encoderLog.Debug("write signature", zap.Stringer("sig", s))
if len(s.Content) != 65 {
return fmt.Errorf("signature should be 65 bytes, was %d", len(s.Content))
}
if err = e.writeByte(byte(s.Curve)); err != nil {
return
}
return e.toWriter(s.Content) // should write 65 bytes
}
func (e *Encoder) writeTstamp(t Tstamp) (err error) {
encoderLog.Debug("write tstamp", zap.Time("time", t.Time))
n := uint64(t.UnixNano())
return e.writeUint64(n)
}
func (e *Encoder) writeBlockTimestamp(bt BlockTimestamp) (err error) {
encoderLog.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.
encoderLog.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) {
encoderLog.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) {
encoderLog.Debug("write json time", zap.Time("time", tm.Time))
return e.writeUint32(uint32(tm.Unix()))
}
func (e *Encoder) writeBlockP2PMessageEnvelope(envelope Packet) (err error) {
encoderLog.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)
encoderLog.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")
//}
encoderLog.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
}
encoderLog.Debug("encoding action data", typeField("type", d))
raw, err := MarshalBinary(d)
if err != nil {
return err
}
encoderLog.Debug("writing action data", typeField("type", d))
return e.writeByteArray(raw)
}
return e.writeByteArray(actionData.HexData)
}
func MarshalBinary(v interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
encoder := NewEncoder(buf)
err := encoder.Encode(v)
return buf.Bytes(), err
}