-
Notifications
You must be signed in to change notification settings - Fork 178
/
trie_encoder.go
888 lines (752 loc) · 25.4 KB
/
trie_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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
package ledger
import (
"fmt"
"github.com/onflow/flow-go/ledger/common/bitutils"
"github.com/onflow/flow-go/ledger/common/hash"
"github.com/onflow/flow-go/ledger/common/utils"
)
// Versions capture the maximum version of encoding this code supports.
// I.e. this code encodes data with the latest version and only decodes
// data with version smaller or equal to these versions.
// Bumping a version number prevents older versions of code from handling
// the newer version of data. New code handling new data version
// should be updated to also support backward compatibility if needed.
const (
// CAUTION: if payload key encoding is changed, convertEncodedPayloadKey()
// must be modified to convert encoded payload key from one version to
// another version.
PayloadVersion = uint16(1)
TrieUpdateVersion = uint16(0) // Use payload version 0 encoding
TrieProofVersion = uint16(0) // Use payload version 0 encoding
TrieBatchProofVersion = uint16(0) // Use payload version 0 encoding
)
// Type capture the type of encoded entity (e.g. State, Key, Value, Path)
type Type uint8
const (
// TypeUnknown - unknown type
TypeUnknown = iota
// TypeState - type for State
TypeState
// TypeKeyPart - type for KeyParts (a subset of key)
TypeKeyPart
// TypeKey - type for Keys (unique identifier to reference a location in ledger)
TypeKey
// TypeValue - type for Ledger Values
TypeValue
// TypePath - type for Paths (trie storage location of a key value pair)
TypePath
// TypePayload - type for Payloads (stored at trie nodes including key value pair )
TypePayload
// TypeProof type for Proofs
// (all data needed to verify a key value pair at specific state)
TypeProof
// TypeBatchProof - type for BatchProofs
TypeBatchProof
// TypeQuery - type for ledger query
TypeQuery
// TypeUpdate - type for ledger update
TypeUpdate
// TypeTrieUpdate - type for trie update
TypeTrieUpdate
// this is used to flag types from the future
typeUnsuported
)
func (e Type) String() string {
return [...]string{"Unknown", "State", "KeyPart", "Key", "Value", "Path", "Payload", "Proof", "BatchProof", "Query", "Update", "Trie Update"}[e]
}
// CheckVersion extracts encoding bytes from a raw encoded message
// checks it against the supported versions and returns the rest of rawInput (excluding encDecVersion bytes)
func CheckVersion(rawInput []byte, maxVersion uint16) (rest []byte, version uint16, err error) {
version, rest, err = utils.ReadUint16(rawInput)
if err != nil {
return rest, version, fmt.Errorf("error checking the encoding decoding version: %w", err)
}
// error on versions coming from future till a time-machine is invented
if version > maxVersion {
return rest, version, fmt.Errorf("incompatible encoding decoding version (%d > %d): %w", version, maxVersion, err)
}
// return the rest of bytes
return rest, version, nil
}
// CheckType extracts encoding byte from a raw encoded message
// checks it against expected type and returns the rest of rawInput (excluding type byte)
func CheckType(rawInput []byte, expectedType uint8) (rest []byte, err error) {
t, r, err := utils.ReadUint8(rawInput)
if err != nil {
return r, fmt.Errorf("error checking type of the encoded entity: %w", err)
}
// error if type is known for this code
if t >= typeUnsuported {
return r, fmt.Errorf("unknown entity type in the encoded data (%d > %d)", t, typeUnsuported)
}
// error if type is known for this code
if t != expectedType {
return r, fmt.Errorf("unexpected entity type, got (%v) but (%v) was expected", Type(t), Type(expectedType))
}
// return the rest of bytes
return r, nil
}
// EncodeKeyPart encodes a key part into a byte slice
func EncodeKeyPart(kp *KeyPart) []byte {
if kp == nil {
return []byte{}
}
// encode version
buffer := utils.AppendUint16([]byte{}, PayloadVersion)
// encode key part entity type
buffer = utils.AppendUint8(buffer, TypeKeyPart)
// encode the key part content
buffer = append(buffer, encodeKeyPart(kp, PayloadVersion)...)
return buffer
}
func encodeKeyPart(kp *KeyPart, version uint16) []byte {
buffer := make([]byte, 0, encodedKeyPartLength(kp, version))
return encodeAndAppendKeyPart(buffer, kp, version)
}
func encodeAndAppendKeyPart(buffer []byte, kp *KeyPart, _ uint16) []byte {
// encode "Type" field of the key part
buffer = utils.AppendUint16(buffer, kp.Type)
// encode "Value" field of the key part
buffer = append(buffer, kp.Value...)
return buffer
}
func encodedKeyPartLength(kp *KeyPart, _ uint16) int {
// Key part is encoded as: type (2 bytes) + value
return 2 + len(kp.Value)
}
// DecodeKeyPart constructs a key part from an encoded key part
func DecodeKeyPart(encodedKeyPart []byte) (*KeyPart, error) {
rest, version, err := CheckVersion(encodedKeyPart, PayloadVersion)
if err != nil {
return nil, fmt.Errorf("error decoding key part: %w", err)
}
// check the type
rest, err = CheckType(rest, TypeKeyPart)
if err != nil {
return nil, fmt.Errorf("error decoding key part: %w", err)
}
// decode the key part content (zerocopy)
key, err := decodeKeyPart(rest, true, version)
if err != nil {
return nil, fmt.Errorf("error decoding key part: %w", err)
}
return key, nil
}
// decodeKeyPart decodes inp into KeyPart. If zeroCopy is true, KeyPart
// references data in inp. Otherwise, it is copied.
func decodeKeyPart(inp []byte, zeroCopy bool, _ uint16) (*KeyPart, error) {
// read key part type and the rest is the key item part
kpt, kpv, err := utils.ReadUint16(inp)
if err != nil {
return nil, fmt.Errorf("error decoding key part (content): %w", err)
}
if zeroCopy {
return &KeyPart{Type: kpt, Value: kpv}, nil
}
v := make([]byte, len(kpv))
copy(v, kpv)
return &KeyPart{Type: kpt, Value: v}, nil
}
// EncodeKey encodes a key into a byte slice
func EncodeKey(k *Key) []byte {
if k == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, PayloadVersion)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeKey)
// encode key content
buffer = append(buffer, encodeKey(k, PayloadVersion)...)
return buffer
}
// encodeKey encodes a key into a byte slice
func encodeKey(k *Key, version uint16) []byte {
buffer := make([]byte, 0, encodedKeyLength(k, version))
return encodeAndAppendKey(buffer, k, version)
}
func encodeAndAppendKey(buffer []byte, k *Key, version uint16) []byte {
// encode number of key parts
buffer = utils.AppendUint16(buffer, uint16(len(k.KeyParts)))
// iterate over key parts
for _, kp := range k.KeyParts {
// encode the len of the encoded key part
buffer = utils.AppendUint32(buffer, uint32(encodedKeyPartLength(&kp, version)))
// encode the key part
buffer = encodeAndAppendKeyPart(buffer, &kp, version)
}
return buffer
}
func encodedKeyLength(k *Key, version uint16) int {
// Key is encoded as: number of key parts (2 bytes) and for each key part,
// the key part size (4 bytes) + encoded key part (n bytes).
size := 2 + 4*len(k.KeyParts)
for _, kp := range k.KeyParts {
size += encodedKeyPartLength(&kp, version)
}
return size
}
// DecodeKey constructs a key from an encoded key part
func DecodeKey(encodedKey []byte) (*Key, error) {
// check the enc dec version
rest, version, err := CheckVersion(encodedKey, PayloadVersion)
if err != nil {
return nil, fmt.Errorf("error decoding key: %w", err)
}
// check the encoding type
rest, err = CheckType(rest, TypeKey)
if err != nil {
return nil, fmt.Errorf("error decoding key: %w", err)
}
// decode the key content (zerocopy)
key, err := decodeKey(rest, true, version)
if err != nil {
return nil, fmt.Errorf("error decoding key: %w", err)
}
return key, nil
}
// decodeKey decodes inp into Key. If zeroCopy is true, returned key
// references data in inp. Otherwise, it is copied.
func decodeKey(inp []byte, zeroCopy bool, version uint16) (*Key, error) {
key := &Key{}
numOfParts, rest, err := utils.ReadUint16(inp)
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
if numOfParts == 0 {
return key, nil
}
key.KeyParts = make([]KeyPart, numOfParts)
for i := 0; i < int(numOfParts); i++ {
var kpEncSize uint32
var kpEnc []byte
// read encoded key part size
kpEncSize, rest, err = utils.ReadUint32(rest)
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
// read encoded key part
kpEnc, rest, err = utils.ReadSlice(rest, int(kpEncSize))
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
// decode encoded key part
kp, err := decodeKeyPart(kpEnc, zeroCopy, version)
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
key.KeyParts[i] = *kp
}
return key, nil
}
// EncodeValue encodes a value into a byte slice
func EncodeValue(v Value) []byte {
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, PayloadVersion)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeValue)
// encode value
buffer = append(buffer, encodeValue(v, PayloadVersion)...)
return buffer
}
func encodeValue(v Value, _ uint16) []byte {
return v
}
func encodeAndAppendValue(buffer []byte, v Value, _ uint16) []byte {
return append(buffer, v...)
}
func encodedValueLength(v Value, _ uint16) int {
return len(v)
}
// DecodeValue constructs a ledger value using an encoded byte slice
func DecodeValue(encodedValue []byte) (Value, error) {
// check enc dec version
rest, _, err := CheckVersion(encodedValue, PayloadVersion)
if err != nil {
return nil, err
}
// check the encoding type
rest, err = CheckType(rest, TypeValue)
if err != nil {
return nil, err
}
return rest, nil
}
// EncodePayload encodes a ledger payload
func EncodePayload(p *Payload) []byte {
if p == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, PayloadVersion)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypePayload)
// append encoded payload content
buffer = append(buffer, encodePayload(p, PayloadVersion)...)
return buffer
}
// EncodeAndAppendPayloadWithoutPrefix encodes a ledger payload
// without prefix (version and type) and appends to buffer.
// If payload is nil, unmodified buffer is returned.
func EncodeAndAppendPayloadWithoutPrefix(buffer []byte, p *Payload, version uint16) []byte {
if p == nil {
return buffer
}
return encodeAndAppendPayload(buffer, p, version)
}
func EncodedPayloadLengthWithoutPrefix(p *Payload, version uint16) int {
return encodedPayloadLength(p, version)
}
func encodePayload(p *Payload, version uint16) []byte {
buffer := make([]byte, 0, encodedPayloadLength(p, version))
return encodeAndAppendPayload(buffer, p, version)
}
// convertEncodedPayloadKey returns encoded payload key in toVersion
// converted from encoded payload key in fromVersion.
func convertEncodedPayloadKey(key []byte, fromVersion uint16, toVersion uint16) []byte {
// No conversion is needed for now because
// payload key encoding version 0 is the same as version 1.
return key
}
func encodeAndAppendPayload(buffer []byte, p *Payload, version uint16) []byte {
// convert payload encoded key from PayloadVersion to version.
encKey := convertEncodedPayloadKey(p.encKey, PayloadVersion, version)
// encode encoded key size
buffer = utils.AppendUint32(buffer, uint32(len(encKey)))
// append encoded key
buffer = append(buffer, encKey...)
// encode encoded value size
encodedValueLen := encodedValueLength(p.Value(), version)
switch version {
case 0:
// In version 0, encoded value length is encoded as 8 bytes.
buffer = utils.AppendUint64(buffer, uint64(encodedValueLen))
default:
// In version 1 and later, encoded value length is encoded as 4 bytes.
buffer = utils.AppendUint32(buffer, uint32(encodedValueLen))
}
// encode value
buffer = encodeAndAppendValue(buffer, p.Value(), version)
return buffer
}
func encodedPayloadLength(p *Payload, version uint16) int {
if p == nil {
return 0
}
// Error isn't checked here because encoded key will be used directly
// in later commit and no error will be returned.
k, _ := p.Key()
switch version {
case 0:
// In version 0, payload is encoded as:
// encode key length (4 bytes) + encoded key +
// encoded value length (8 bytes) + encode value
return 4 + encodedKeyLength(&k, version) + 8 + encodedValueLength(p.Value(), version)
default:
// In version 1 and later, payload is encoded as:
// encode key length (4 bytes) + encoded key +
// encoded value length (4 bytes) + encode value
return 4 + encodedKeyLength(&k, version) + 4 + encodedValueLength(p.Value(), version)
}
}
// DecodePayload construct a payload from an encoded byte slice
func DecodePayload(encodedPayload []byte) (*Payload, error) {
// if empty don't decode
if len(encodedPayload) == 0 {
return nil, nil
}
// check the enc dec version
rest, version, err := CheckVersion(encodedPayload, PayloadVersion)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// check the encoding type
rest, err = CheckType(rest, TypePayload)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// decode payload (zerocopy)
return decodePayload(rest, true, version)
}
// DecodePayloadWithoutPrefix constructs a payload from encoded byte slice
// without prefix (version and type). If zeroCopy is true, returned payload
// references data in encodedPayload. Otherwise, it is copied.
func DecodePayloadWithoutPrefix(encodedPayload []byte, zeroCopy bool, version uint16) (*Payload, error) {
// if empty don't decode
if len(encodedPayload) == 0 {
return nil, nil
}
return decodePayload(encodedPayload, zeroCopy, version)
}
// decodePayload decodes inp into payload. If zeroCopy is true,
// returned payload references data in inp. Otherwise, it is copied.
func decodePayload(inp []byte, zeroCopy bool, version uint16) (*Payload, error) {
// read encoded key size
encKeySize, rest, err := utils.ReadUint32(inp)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// read encoded key
ek, rest, err := utils.ReadSlice(rest, int(encKeySize))
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// convert payload encoded key from version to PayloadVersion.
encKey := convertEncodedPayloadKey(ek, version, PayloadVersion)
// read encoded value size
var encValueSize int
switch version {
case 0:
var size uint64
size, rest, err = utils.ReadUint64(rest)
encValueSize = int(size)
default:
var size uint32
size, rest, err = utils.ReadUint32(rest)
encValueSize = int(size)
}
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// read encoded value
encValue, _, err := utils.ReadSlice(rest, encValueSize)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
if zeroCopy {
return &Payload{encKey, encValue}, nil
}
k := make([]byte, len(encKey))
copy(k, encKey)
v := make([]byte, len(encValue))
copy(v, encValue)
return &Payload{k, v}, nil
}
// EncodeTrieUpdate encodes a trie update struct
func EncodeTrieUpdate(t *TrieUpdate) []byte {
if t == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, TrieUpdateVersion)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeTrieUpdate)
// append encoded payload content
buffer = append(buffer, encodeTrieUpdate(t, TrieUpdateVersion)...)
return buffer
}
func encodeTrieUpdate(t *TrieUpdate, version uint16) []byte {
buffer := make([]byte, 0)
// encode root hash (size and data)
buffer = utils.AppendUint16(buffer, uint16(len(t.RootHash)))
buffer = append(buffer, t.RootHash[:]...)
// encode number of paths
buffer = utils.AppendUint32(buffer, uint32(t.Size()))
if t.Size() == 0 {
return buffer
}
// encode paths
// encode path size (assuming all paths are the same size)
buffer = utils.AppendUint16(buffer, uint16(PathLen))
for _, path := range t.Paths {
buffer = append(buffer, path[:]...)
}
// we assume same number of payloads
// encode payloads
for _, pl := range t.Payloads {
encPl := encodePayload(pl, version)
buffer = utils.AppendUint32(buffer, uint32(len(encPl)))
buffer = append(buffer, encPl...)
}
return buffer
}
// DecodeTrieUpdate construct a trie update from an encoded byte slice
func DecodeTrieUpdate(encodedTrieUpdate []byte) (*TrieUpdate, error) {
// if empty don't decode
if len(encodedTrieUpdate) == 0 {
return nil, nil
}
// check the enc dec version
rest, version, err := CheckVersion(encodedTrieUpdate, TrieUpdateVersion)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
// check the encoding type
rest, err = CheckType(rest, TypeTrieUpdate)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
return decodeTrieUpdate(rest, version)
}
func decodeTrieUpdate(inp []byte, version uint16) (*TrieUpdate, error) {
// decode root hash
rhSize, rest, err := utils.ReadUint16(inp)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
rhBytes, rest, err := utils.ReadSlice(rest, int(rhSize))
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
rh, err := ToRootHash(rhBytes)
if err != nil {
return nil, fmt.Errorf("decode trie update failed: %w", err)
}
// decode number of paths
numOfPaths, rest, err := utils.ReadUint32(rest)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
// decode path size
pathSize, rest, err := utils.ReadUint16(rest)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
paths := make([]Path, numOfPaths)
payloads := make([]*Payload, numOfPaths)
var path Path
var encPath []byte
for i := 0; i < int(numOfPaths); i++ {
encPath, rest, err = utils.ReadSlice(rest, int(pathSize))
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
path, err = ToPath(encPath)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
paths[i] = path
}
var payloadSize uint32
var encPayload []byte
var payload *Payload
for i := 0; i < int(numOfPaths); i++ {
payloadSize, rest, err = utils.ReadUint32(rest)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
encPayload, rest, err = utils.ReadSlice(rest, int(payloadSize))
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
// Decode payload (zerocopy)
payload, err = decodePayload(encPayload, true, version)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
payloads[i] = payload
}
return &TrieUpdate{RootHash: rh, Paths: paths, Payloads: payloads}, nil
}
// EncodeTrieProof encodes the content of a proof into a byte slice
func EncodeTrieProof(p *TrieProof) []byte {
if p == nil {
return []byte{}
}
// encode version
buffer := utils.AppendUint16([]byte{}, TrieProofVersion)
// encode proof entity type
buffer = utils.AppendUint8(buffer, TypeProof)
// append encoded proof content
proof := encodeTrieProof(p, TrieProofVersion)
buffer = append(buffer, proof...)
return buffer
}
func encodeTrieProof(p *TrieProof, version uint16) []byte {
// first byte is reserved for inclusion flag
buffer := make([]byte, 1)
if p.Inclusion {
// set the first bit to 1 if it is an inclusion proof
buffer[0] |= 1 << 7
}
// steps are encoded as a single byte
buffer = utils.AppendUint8(buffer, p.Steps)
// include flags size and content
buffer = utils.AppendUint8(buffer, uint8(len(p.Flags)))
buffer = append(buffer, p.Flags...)
// include path size and content
buffer = utils.AppendUint16(buffer, uint16(PathLen))
buffer = append(buffer, p.Path[:]...)
// include encoded payload size and content
encPayload := encodePayload(p.Payload, version)
buffer = utils.AppendUint64(buffer, uint64(len(encPayload)))
buffer = append(buffer, encPayload...)
// and finally include all interims (hash values)
// number of interims
buffer = utils.AppendUint8(buffer, uint8(len(p.Interims)))
for _, inter := range p.Interims {
buffer = utils.AppendUint16(buffer, uint16(len(inter)))
buffer = append(buffer, inter[:]...)
}
return buffer
}
// DecodeTrieProof construct a proof from an encoded byte slice
func DecodeTrieProof(encodedProof []byte) (*TrieProof, error) {
// check the enc dec version
rest, version, err := CheckVersion(encodedProof, TrieProofVersion)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
// check the encoding type
rest, err = CheckType(rest, TypeProof)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
return decodeTrieProof(rest, version)
}
func decodeTrieProof(inp []byte, version uint16) (*TrieProof, error) {
pInst := NewTrieProof()
// Inclusion flag
byteInclusion, rest, err := utils.ReadSlice(inp, 1)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
pInst.Inclusion = bitutils.ReadBit(byteInclusion, 0) == 1
// read steps
steps, rest, err := utils.ReadUint8(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
pInst.Steps = steps
// read flags
flagsSize, rest, err := utils.ReadUint8(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
flags, rest, err := utils.ReadSlice(rest, int(flagsSize))
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
pInst.Flags = flags
// read path
pathSize, rest, err := utils.ReadUint16(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
path, rest, err := utils.ReadSlice(rest, int(pathSize))
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
pInst.Path, err = ToPath(path)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
// read payload
encPayloadSize, rest, err := utils.ReadUint64(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
encPayload, rest, err := utils.ReadSlice(rest, int(encPayloadSize))
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
// Decode payload (zerocopy)
payload, err := decodePayload(encPayload, true, version)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
pInst.Payload = payload
// read interims
interimsLen, rest, err := utils.ReadUint8(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
interims := make([]hash.Hash, interimsLen)
var interimSize uint16
var interim hash.Hash
var interimBytes []byte
for i := 0; i < int(interimsLen); i++ {
interimSize, rest, err = utils.ReadUint16(rest)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
interimBytes, rest, err = utils.ReadSlice(rest, int(interimSize))
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
interim, err = hash.ToHash(interimBytes)
if err != nil {
return nil, fmt.Errorf("error decoding proof: %w", err)
}
interims[i] = interim
}
pInst.Interims = interims
return pInst, nil
}
// EncodeTrieBatchProof encodes a batch proof into a byte slice
func EncodeTrieBatchProof(bp *TrieBatchProof) []byte {
if bp == nil {
return []byte{}
}
// encode version
buffer := utils.AppendUint16([]byte{}, TrieBatchProofVersion)
// encode batch proof entity type
buffer = utils.AppendUint8(buffer, TypeBatchProof)
// encode batch proof content
buffer = append(buffer, encodeTrieBatchProof(bp, TrieBatchProofVersion)...)
return buffer
}
// encodeBatchProof encodes a batch proof into a byte slice
func encodeTrieBatchProof(bp *TrieBatchProof, version uint16) []byte {
buffer := make([]byte, 0)
// encode number of proofs
buffer = utils.AppendUint32(buffer, uint32(len(bp.Proofs)))
// iterate over proofs
for _, p := range bp.Proofs {
// encode the proof
encP := encodeTrieProof(p, version)
// encode the len of the encoded proof
buffer = utils.AppendUint64(buffer, uint64(len(encP)))
// append the encoded proof
buffer = append(buffer, encP...)
}
return buffer
}
// DecodeTrieBatchProof constructs a batch proof from an encoded byte slice
func DecodeTrieBatchProof(encodedBatchProof []byte) (*TrieBatchProof, error) {
// check the enc dec version
rest, version, err := CheckVersion(encodedBatchProof, TrieBatchProofVersion)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof: %w", err)
}
// check the encoding type
rest, err = CheckType(rest, TypeBatchProof)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof: %w", err)
}
// decode the batch proof content
bp, err := decodeTrieBatchProof(rest, version)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof: %w", err)
}
return bp, nil
}
func decodeTrieBatchProof(inp []byte, version uint16) (*TrieBatchProof, error) {
bp := NewTrieBatchProof()
// number of proofs
numOfProofs, rest, err := utils.ReadUint32(inp)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof (content): %w", err)
}
for i := 0; i < int(numOfProofs); i++ {
var encProofSize uint64
var encProof []byte
// read encoded proof size
encProofSize, rest, err = utils.ReadUint64(rest)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof (content): %w", err)
}
// read encoded proof
encProof, rest, err = utils.ReadSlice(rest, int(encProofSize))
if err != nil {
return nil, fmt.Errorf("error decoding batch proof (content): %w", err)
}
// decode encoded proof
proof, err := decodeTrieProof(encProof, version)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof (content): %w", err)
}
bp.Proofs = append(bp.Proofs, proof)
}
return bp, nil
}