forked from centrifuge/go-substrate-rpc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
647 lines (597 loc) · 16.2 KB
/
codec.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
// Copyright 2018 Jsgenesis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package scale
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"math"
"math/big"
"reflect"
)
// Implementation for Parity codec in Go.
// Derived from https://github.com/paritytech/parity-codec/
// While Rust implementation uses Rust type system and is highly optimized, this one
// has to rely on Go's reflection and thus is notably slower.
// Feature parity is almost full, apart from the lack of support for u128 (which are missing in Go).
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
// Encoder is a wrapper around a Writer that allows encoding data items to a stream.
// Allows passing encoding options
type Encoder struct {
writer io.Writer
}
func NewEncoder(writer io.Writer) *Encoder {
return &Encoder{writer: writer}
}
// Write several bytes to the encoder.
func (pe Encoder) Write(bytes []byte) error {
c, err := pe.writer.Write(bytes)
if err != nil {
return err
}
if c < len(bytes) {
return fmt.Errorf("Could not write %d bytes to writer", len(bytes))
}
return nil
}
// PushByte writes a single byte to an encoder.
func (pe Encoder) PushByte(b byte) error {
return pe.Write([]byte{b})
}
// EncodeUintCompact writes an unsigned integer to the stream using the compact encoding.
// A typical usage is storing the length of a collection.
// Definition of compact encoding:
// 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
// xx xx xx 00 (0 ... 2**6 - 1) (u8)
// yL yL yL 01 / yH yH yH yL (2**6 ... 2**14 - 1) (u8, u16) low LH high
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
// nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
// Rust implementation: see impl<'a> Encode for CompactRef<'a, u64>
func (pe Encoder) EncodeUintCompact(v big.Int) error {
if v.Sign() == -1 {
return errors.New("Assertion error: EncodeUintCompact cannot process negative numbers")
}
if v.IsUint64() {
if v.Uint64() < 1<<30 {
if v.Uint64() < 1<<6 {
err := pe.PushByte(byte(v.Uint64()) << 2)
if err != nil {
return err
}
} else if v.Uint64() < 1<<14 {
err := binary.Write(pe.writer, binary.LittleEndian, uint16(v.Uint64()<<2)+1)
if err != nil {
return err
}
} else {
err := binary.Write(pe.writer, binary.LittleEndian, uint32(v.Uint64()<<2)+2)
if err != nil {
return err
}
}
return nil
}
}
numBytes := len(v.Bytes())
if numBytes > 255 {
return errors.New("Assertion error: numBytes>255 exeeds allowed for length prefix")
}
topSixBits := uint8(numBytes - 4)
lengthByte := topSixBits<<2 + 3
if topSixBits > 63 {
return errors.New("Assertion error: n<=63 needed to compact-encode substrate unsigned big integer")
}
err := pe.PushByte(lengthByte)
if err != nil {
return err
}
buf := v.Bytes()
Reverse(buf)
err = pe.Write(buf)
if err != nil {
return err
}
return nil
}
// Encode a value to the stream.
func (pe Encoder) Encode(value interface{}) error {
t := reflect.TypeOf(value)
// If the type implements encodeable, use that implementation
encodeable := reflect.TypeOf((*Encodeable)(nil)).Elem()
if t.Implements(encodeable) {
err := value.(Encodeable).Encode(pe)
if err != nil {
return err
}
return nil
}
tk := t.Kind()
switch tk {
// Boolean and numbers are trivially encoded via binary.Write
// It will use reflection again and take a performance hit
// TODO: consider handling every case directly
case reflect.Bool:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Int:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Uint:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uintptr:
fallthrough
case reflect.Float32:
fallthrough
case reflect.Float64:
err := binary.Write(pe.writer, binary.LittleEndian, value)
if err != nil {
return err
}
case reflect.Ptr:
rv := reflect.ValueOf(value)
if rv.IsNil() {
return errors.New("Encoding null pointers not supported; consider using Option type")
} else {
dereferenced := rv.Elem()
err := pe.Encode(dereferenced.Interface())
if err != nil {
return err
}
}
// Arrays: no compact-encoded length prefix
case reflect.Array:
rv := reflect.ValueOf(value)
l := rv.Len()
for i := 0; i < l; i++ {
err := pe.Encode(rv.Index(i).Interface())
if err != nil {
return err
}
}
// Slices: first compact-encode length, then each item individually
case reflect.Slice:
rv := reflect.ValueOf(value)
l := rv.Len()
len64 := uint64(l)
if len64 > math.MaxUint32 {
return errors.New("Attempted to serialize a collection with too many elements.")
}
err := pe.EncodeUintCompact(*big.NewInt(0).SetUint64(len64))
if err != nil {
return err
}
for i := 0; i < l; i++ {
err = pe.Encode(rv.Index(i).Interface())
if err != nil {
return err
}
}
// Strings are encoded as UTF-8 byte slices, just as in Rust
case reflect.String:
s := reflect.ValueOf(value).String()
err := pe.Encode([]byte(s))
if err != nil {
return err
}
case reflect.Struct:
rv := reflect.ValueOf(value)
for i := 0; i < rv.NumField(); i++ {
ft := rv.Type().Field(i)
tv, ok := ft.Tag.Lookup("scale")
if ok && tv == "-" {
continue
}
err := pe.Encode(rv.Field(i).Interface())
if err != nil {
return fmt.Errorf("type %s does not support Encodeable interface and could not be "+
"encoded field by field, error: %v", t, err)
}
}
// Currently unsupported types
case reflect.Complex64:
fallthrough
case reflect.Complex128:
fallthrough
case reflect.Chan:
fallthrough
case reflect.Func:
fallthrough
case reflect.Interface:
fallthrough
case reflect.Map:
fallthrough
case reflect.UnsafePointer:
fallthrough
case reflect.Invalid:
return fmt.Errorf("Type %s cannot be encoded", t.Kind())
default:
log.Println("not captured")
}
return nil
}
// EncodeOption stores optionally present value to the stream.
func (pe Encoder) EncodeOption(hasValue bool, value interface{}) error {
if !hasValue {
err := pe.PushByte(0)
if err != nil {
return err
}
} else {
err := pe.PushByte(1)
if err != nil {
return err
}
err = pe.Encode(value)
if err != nil {
return err
}
}
return nil
}
// Decoder is a wraper around a Reader that allows decoding data items from a stream.
type Decoder struct {
reader io.Reader
}
func NewDecoder(reader io.Reader) *Decoder {
return &Decoder{reader: reader}
}
// Read reads bytes from a stream into a buffer
func (pd Decoder) Read(bytes []byte) error {
c, err := pd.reader.Read(bytes)
if err != nil {
return err
}
if c < len(bytes) {
return fmt.Errorf("Cannot read the required number of bytes %d, only %d available", len(bytes), c)
}
return nil
}
// ReadOneByte reads a next byte from the stream.
// Named so to avoid a linter warning about a clash with io.ByteReader.ReadByte
func (pd Decoder) ReadOneByte() (byte, error) {
buf := []byte{0}
err := pd.Read(buf)
if err != nil {
return buf[0], err
}
return buf[0], nil
}
// Decode takes a pointer to a decodable value and populates it from the stream.
func (pd Decoder) Decode(target interface{}) error {
t0 := reflect.TypeOf(target)
if t0.Kind() != reflect.Ptr {
return errors.New("Target must be a pointer, but was " + fmt.Sprint(t0))
}
val := reflect.ValueOf(target)
if val.IsNil() {
return errors.New("Target is a nil pointer")
}
return pd.DecodeIntoReflectValue(val.Elem())
}
// DecodeIntoReflectValue populates a writable reflect.Value from the stream
func (pd Decoder) DecodeIntoReflectValue(target reflect.Value) error {
t := target.Type()
if !target.CanSet() {
return fmt.Errorf("Unsettable value %v", t)
}
// If the type implements decodeable, use that implementation
decodeable := reflect.TypeOf((*Decodeable)(nil)).Elem()
ptrType := reflect.PtrTo(t)
if ptrType.Implements(decodeable) {
var holder reflect.Value
if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
slice := reflect.MakeSlice(t, target.Len(), target.Len())
holder = reflect.New(t)
holder.Elem().Set(slice)
} else {
holder = reflect.New(t)
}
err := holder.Interface().(Decodeable).Decode(pd)
if err != nil {
return err
}
target.Set(holder.Elem())
return nil
}
switch t.Kind() {
// Boolean and numbers are trivially decoded via binary.Read
// It will use reflection again and take a performance hit
// TODO: consider handling every case directly
case reflect.Bool:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Int:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Uint:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uintptr:
fallthrough
case reflect.Float32:
fallthrough
case reflect.Float64:
intHolder := reflect.New(t)
intPointer := intHolder.Interface()
err := binary.Read(pd.reader, binary.LittleEndian, intPointer)
if err == io.EOF {
return errors.New("expected more bytes, but could not decode any more")
}
if err != nil {
return err
}
target.Set(intHolder.Elem())
// If you want to replicate Option<T> behavior in Rust, see OptionBool and an
// example type OptionInt8 in tests.
case reflect.Ptr:
isNil := target.IsNil()
if isNil {
// target.set
// return nil
}
ptr := target.Elem()
err := pd.DecodeIntoReflectValue(ptr)
if err != nil {
return err
}
// Arrays: derive the length from the array length
case reflect.Array:
targetLen := target.Len()
for i := 0; i < targetLen; i++ {
err := pd.DecodeIntoReflectValue(target.Index(i))
if err != nil {
return err
}
}
// Slices: first compact-encode length, then each item individually
case reflect.Slice:
codedLen64, _ := pd.DecodeUintCompact()
if codedLen64.Uint64() > math.MaxUint32 {
return errors.New("Encoded array length is higher than allowed by the protocol (32-bit unsigned integer)")
}
if codedLen64.Uint64() > uint64(maxInt) {
return errors.New("Encoded array length is higher than allowed by the platform")
}
codedLen := int(codedLen64.Uint64())
targetLen := target.Len()
if codedLen != targetLen {
if int(codedLen) > target.Cap() {
newSlice := reflect.MakeSlice(t, int(codedLen), int(codedLen))
target.Set(newSlice)
} else {
target.SetLen(int(codedLen))
}
}
for i := 0; i < codedLen; i++ {
err := pd.DecodeIntoReflectValue(target.Index(i))
if err != nil {
return err
}
}
// Strings are encoded as UTF-8 byte slices, just as in Rust
case reflect.String:
var b []byte
err := pd.Decode(&b)
if err != nil {
return err
}
target.SetString(string(b))
case reflect.Struct:
for i := 0; i < target.NumField(); i++ {
ft := target.Type().Field(i)
tv, ok := ft.Tag.Lookup("scale")
if ok && tv == "-" {
continue
}
err := pd.DecodeIntoReflectValue(target.Field(i))
if err != nil {
return fmt.Errorf("type %s does not support Decodeable interface and could not be "+
"decoded field by field, error: %v", ptrType, err)
}
}
// Currently unsupported types
case reflect.Complex64:
fallthrough
case reflect.Complex128:
fallthrough
case reflect.Chan:
fallthrough
case reflect.Func:
fallthrough
case reflect.Interface:
fallthrough
case reflect.Map:
fallthrough
case reflect.UnsafePointer:
fallthrough
case reflect.Invalid:
return fmt.Errorf("Type %s cannot be decoded", t.Kind())
}
return nil
}
// DecodeUintCompact decodes a compact-encoded integer. See EncodeUintCompact method.
func (pd Decoder) DecodeUintCompact() (*big.Int, error) {
b, _ := pd.ReadOneByte()
mode := b & 3
switch mode {
case 0:
// right shift to remove mode bits
return big.NewInt(0).SetUint64(uint64(b >> 2)), nil
case 1:
bb, err := pd.ReadOneByte()
if err != nil {
return nil, err
}
r := uint64(bb)
// * 2^6
r <<= 6
// right shift to remove mode bits and add to prev
r += uint64(b >> 2)
return big.NewInt(0).SetUint64(r), nil
case 2:
// value = 32 bits + mode
buf := make([]byte, 4)
buf[0] = b
err := pd.Read(buf[1:4])
if err != nil {
return nil, err
}
// set the buffer in little endian order
r := binary.LittleEndian.Uint32(buf)
// remove the last 2 mode bits
r >>= 2
return big.NewInt(0).SetUint64(uint64(r)) , nil
case 3:
// remove mode bits
l := b >> 2
if l > 63 { // Max upper bound of 536 is (67 - 4)
return nil, errors.New("Not supported: l>63 encountered when decoding a compact-encoded uint")
}
buf := make([]byte, l+4)
err := pd.Read(buf)
if err != nil {
return nil, err
}
Reverse(buf)
return new(big.Int).SetBytes(buf), nil
default:
return nil, errors.New("Code should be unreachable")
}
}
// Reverse reverses bytes in place (manipulates the underlying array)
func Reverse(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}
// DecodeOption decodes a optionally available value into a boolean presence field and a value.
func (pd Decoder) DecodeOption(hasValue *bool, valuePointer interface{}) error {
b, _ := pd.ReadOneByte()
switch b {
case 0:
*hasValue = false
case 1:
*hasValue = true
err := pd.Decode(valuePointer)
if err != nil {
return err
}
default:
return fmt.Errorf("Unknown byte prefix for encoded OptionBool: %d", b)
}
return nil
}
// Encodeable is an interface that defines a custom encoding rules for a data type.
// Should be defined for structs (not pointers to them).
// See OptionBool for an example implementation.
type Encodeable interface {
// ParityEncode encodes and write this structure into a stream
Encode(encoder Encoder) error
}
// Decodeable is an interface that defines a custom encoding rules for a data type.
// Should be defined for pointers to structs.
// See OptionBool for an example implementation.
type Decodeable interface {
// ParityDecode populates this structure from a stream (overwriting the current contents), return false on failure
Decode(decoder Decoder) error
}
// OptionBool is a structure that can store a boolean or a missing value.
// Note that encoding rules are slightly different from other "Option" fields.
type OptionBool struct {
hasValue bool
value bool
}
// NewOptionBoolEmpty creates an OptionBool without a value.
func NewOptionBoolEmpty() OptionBool {
return OptionBool{false, false}
}
// NewOptionBool creates an OptionBool with a value.
func NewOptionBool(value bool) OptionBool {
return OptionBool{true, value}
}
// ParityEncode implements encoding for OptionBool as per Rust implementation.
func (o OptionBool) Encode(encoder Encoder) error {
var err error
if !o.hasValue {
err = encoder.PushByte(0)
} else {
if o.value {
err = encoder.PushByte(1)
} else {
err = encoder.PushByte(2)
}
}
if err != nil {
return err
}
return nil
}
// ParityDecode implements decoding for OptionBool as per Rust implementation.
func (o *OptionBool) Decode(decoder Decoder) error {
b, _ := decoder.ReadOneByte()
switch b {
case 0:
o.hasValue = false
o.value = false
case 1:
o.hasValue = true
o.value = true
case 2:
o.hasValue = true
o.value = false
default:
return fmt.Errorf("Unknown byte prefix for encoded OptionBool: %d", b)
}
return nil
}
// ToKeyedVec replicates the behaviour of Rust's to_keyed_vec helper.
func ToKeyedVec(value interface{}, prependKey []byte) ([]byte, error) {
var buffer = bytes.NewBuffer(prependKey)
err := Encoder{buffer}.Encode(value)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}