-
Notifications
You must be signed in to change notification settings - Fork 178
/
encoding.go
787 lines (665 loc) · 21.3 KB
/
encoding.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
// Package encoding provides byte serialization and deserialization of trie and ledger structs.
package encoding
import (
"fmt"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common/bitutils"
"github.com/onflow/flow-go/ledger/common/hash"
"github.com/onflow/flow-go/ledger/common/utils"
)
// Version captures the maximum version of encoding that this code supports
// in other words this code encodes the data with the latest version and
// can only decode data with version smaller or equal to this value
// bumping this number prevents older versions of the code to deal with the newer version of data
// codes should be updated with backward compatibility if needed
const Version = uint16(0)
// 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) (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 > Version {
return rest, version, fmt.Errorf("incompatible encoding decoding version (%d > %d): %w", version, Version, err)
}
// return the rest of bytes
return rest, version, nil
}
// CheckType extracts encoding byte from a raw encoded message
// checks it against the supported versions and returns the rest of rawInput (excluding encDecVersion bytes)
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 *ledger.KeyPart) []byte {
if kp == nil {
return []byte{}
}
// EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key part entity type
buffer = utils.AppendUint8(buffer, TypeKeyPart)
// encode the key part content
buffer = append(buffer, encodeKeyPart(kp)...)
return buffer
}
func encodeKeyPart(kp *ledger.KeyPart) []byte {
buffer := make([]byte, 0)
// 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
}
// DecodeKeyPart constructs a key part from an encoded key part
func DecodeKeyPart(encodedKeyPart []byte) (*ledger.KeyPart, error) {
// currently we ignore the version but in the future we
// can do switch case based on the version if needed
rest, _, err := CheckVersion(encodedKeyPart)
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
key, err := decodeKeyPart(rest)
if err != nil {
return nil, fmt.Errorf("error decoding key part: %w", err)
}
return key, nil
}
func decodeKeyPart(inp []byte) (*ledger.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)
}
return &ledger.KeyPart{Type: kpt, Value: kpv}, nil
}
// EncodeKey encodes a key into a byte slice
func EncodeKey(k *ledger.Key) []byte {
if k == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeKey)
// encode key content
buffer = append(buffer, encodeKey(k)...)
return buffer
}
// encodeKey encodes a key into a byte slice
func encodeKey(k *ledger.Key) []byte {
buffer := make([]byte, 0)
// encode number of key parts
buffer = utils.AppendUint16(buffer, uint16(len(k.KeyParts)))
// iterate over key parts
for _, kp := range k.KeyParts {
// encode the key part
encKP := encodeKeyPart(&kp)
// encode the len of the encoded key part
buffer = utils.AppendUint32(buffer, uint32(len(encKP)))
// append the encoded key part
buffer = append(buffer, encKP...)
}
return buffer
}
// DecodeKey constructs a key from an encoded key part
func DecodeKey(encodedKey []byte) (*ledger.Key, error) {
// check the enc dec version
rest, _, err := CheckVersion(encodedKey)
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
key, err := decodeKey(rest)
if err != nil {
return nil, fmt.Errorf("error decoding key: %w", err)
}
return key, nil
}
func decodeKey(inp []byte) (*ledger.Key, error) {
key := &ledger.Key{}
numOfParts, rest, err := utils.ReadUint16(inp)
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
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)
if err != nil {
return nil, fmt.Errorf("error decoding key (content): %w", err)
}
key.KeyParts = append(key.KeyParts, *kp)
}
return key, nil
}
// EncodeValue encodes a value into a byte slice
func EncodeValue(v ledger.Value) []byte {
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeValue)
// encode value
buffer = append(buffer, encodeValue(v)...)
return buffer
}
func encodeValue(v ledger.Value) []byte {
return v
}
// DecodeValue constructs a ledger value using an encoded byte slice
func DecodeValue(encodedValue []byte) (ledger.Value, error) {
// check enc dec version
rest, _, err := CheckVersion(encodedValue)
if err != nil {
return nil, err
}
// check the encoding type
rest, err = CheckType(rest, TypeValue)
if err != nil {
return nil, err
}
return rest, nil
}
// EncodePath encodes a path into a byte slice
func EncodePath(p ledger.Path) []byte {
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypePath)
// encode path
buffer = append(buffer, p[:]...)
return buffer
}
// DecodePath constructs a path value using an encoded byte slice
func DecodePath(encodedPath []byte) (ledger.Path, error) {
// check enc dec version
rest, _, err := CheckVersion(encodedPath)
if err != nil {
return ledger.DummyPath, err
}
// check the encoding type
rest, err = CheckType(rest, TypePath)
if err != nil {
return ledger.DummyPath, err
}
return decodePath(rest)
}
func decodePath(inp []byte) (ledger.Path, error) {
path, err := ledger.ToPath(inp)
if err != nil {
return path, fmt.Errorf("decode path failed: %w", err)
}
return path, nil
}
// EncodePayload encodes a ledger payload
func EncodePayload(p *ledger.Payload) []byte {
if p == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypePayload)
// append encoded payload content
buffer = append(buffer, encodePayload(p)...)
return buffer
}
func encodePayload(p *ledger.Payload) []byte {
buffer := make([]byte, 0)
// encode key
encK := encodeKey(&p.Key)
// encode encoded key size
buffer = utils.AppendUint32(buffer, uint32(len(encK)))
// append encoded key content
buffer = append(buffer, encK...)
// encode value
encV := encodeValue(p.Value)
// encode encoded value size
buffer = utils.AppendUint64(buffer, uint64(len(encV)))
// append encoded key content
buffer = append(buffer, encV...)
return buffer
}
// DecodePayload construct a payload from an encoded byte slice
func DecodePayload(encodedPayload []byte) (*ledger.Payload, error) {
// if empty don't decode
if len(encodedPayload) == 0 {
return nil, nil
}
// check the enc dec version
rest, _, err := CheckVersion(encodedPayload)
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)
}
return decodePayload(rest)
}
func decodePayload(inp []byte) (*ledger.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
encKey, rest, err := utils.ReadSlice(rest, int(encKeySize))
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// decode the key
key, err := decodeKey(encKey)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// read encoded value size
encValeSize, rest, err := utils.ReadUint64(rest)
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
// read encoded value
encValue, _, err := utils.ReadSlice(rest, int(encValeSize))
if err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
return &ledger.Payload{Key: *key, Value: encValue}, nil
}
// EncodeTrieUpdate encodes a trie update struct
func EncodeTrieUpdate(t *ledger.TrieUpdate) []byte {
if t == nil {
return []byte{}
}
// encode EncodingDecodingType
buffer := utils.AppendUint16([]byte{}, Version)
// encode key entity type
buffer = utils.AppendUint8(buffer, TypeTrieUpdate)
// append encoded payload content
buffer = append(buffer, encodeTrieUpdate(t)...)
return buffer
}
func encodeTrieUpdate(t *ledger.TrieUpdate) []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(ledger.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)
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) (*ledger.TrieUpdate, error) {
// if empty don't decode
if len(encodedTrieUpdate) == 0 {
return nil, nil
}
// check the enc dec version
rest, _, err := CheckVersion(encodedTrieUpdate)
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)
}
func decodeTrieUpdate(inp []byte) (*ledger.TrieUpdate, error) {
paths := make([]ledger.Path, 0)
payloads := make([]*ledger.Payload, 0)
// 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 := ledger.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)
}
var path ledger.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 = decodePath(encPath)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
paths = append(paths, path)
}
var payloadSize uint32
var encPayload []byte
var payload *ledger.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)
}
payload, err = decodePayload(encPayload)
if err != nil {
return nil, fmt.Errorf("error decoding trie update: %w", err)
}
payloads = append(payloads, payload)
}
return &ledger.TrieUpdate{RootHash: rh, Paths: paths, Payloads: payloads}, nil
}
// EncodeTrieProof encodes the content of a proof into a byte slice
func EncodeTrieProof(p *ledger.TrieProof) []byte {
if p == nil {
return []byte{}
}
// encode version
buffer := utils.AppendUint16([]byte{}, Version)
// encode proof entity type
buffer = utils.AppendUint8(buffer, TypeProof)
// append encoded proof content
proof := encodeTrieProof(p)
buffer = append(buffer, proof[:]...)
return buffer
}
func encodeTrieProof(p *ledger.TrieProof) []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(ledger.PathLen))
buffer = append(buffer, p.Path[:]...)
// include encoded payload size and content
encPayload := encodePayload(p.Payload)
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) (*ledger.TrieProof, error) {
// check the enc dec version
rest, _, err := CheckVersion(encodedProof)
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)
}
func decodeTrieProof(inp []byte) (*ledger.TrieProof, error) {
pInst := ledger.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.Bit(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 = ledger.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)
}
payload, err := decodePayload(encPayload)
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, 0)
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 = append(interims, interim)
}
pInst.Interims = interims
return pInst, nil
}
// EncodeTrieBatchProof encodes a batch proof into a byte slice
func EncodeTrieBatchProof(bp *ledger.TrieBatchProof) []byte {
if bp == nil {
return []byte{}
}
// encode version
buffer := utils.AppendUint16([]byte{}, Version)
// encode batch proof entity type
buffer = utils.AppendUint8(buffer, TypeBatchProof)
// encode batch proof content
buffer = append(buffer, encodeTrieBatchProof(bp)...)
return buffer
}
// encodeBatchProof encodes a batch proof into a byte slice
func encodeTrieBatchProof(bp *ledger.TrieBatchProof) []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)
// 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) (*ledger.TrieBatchProof, error) {
// check the enc dec version
rest, _, err := CheckVersion(encodedBatchProof)
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)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof: %w", err)
}
return bp, nil
}
func decodeTrieBatchProof(inp []byte) (*ledger.TrieBatchProof, error) {
bp := ledger.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)
if err != nil {
return nil, fmt.Errorf("error decoding batch proof (content): %w", err)
}
bp.Proofs = append(bp.Proofs, proof)
}
return bp, nil
}