/
decoder.go
69 lines (56 loc) · 2.16 KB
/
decoder.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
package types
import (
"fmt"
"github.com/streamingfast/bstream"
pbsolv1 "github.com/streamingfast/firehose-solana/types/pb/sf/solana/type/v1"
pbsolv2 "github.com/streamingfast/firehose-solana/types/pb/sf/solana/type/v2"
pbbstream "github.com/streamingfast/pbgo/sf/bstream/v1"
"google.golang.org/protobuf/proto"
)
// FIXME: Solana protocol will be the value 3, might not work everywehre ... we will see!
var Protocol_SOL = pbbstream.Protocol(3)
func PBSolBlockDecoder(blk *bstream.Block) (interface{}, error) {
if blk.Kind() != Protocol_SOL {
return nil, fmt.Errorf("expected kind %s, got %s", Protocol_SOL, blk.Kind())
}
if blk.Version() != 1 {
return nil, fmt.Errorf("this decoder only knows about version 1, got %d", blk.Version())
}
block := new(pbsolv2.Block)
payload, err := blk.Payload.Get()
if err != nil {
return nil, fmt.Errorf("getting payload: %s", err)
}
err = proto.Unmarshal(payload, block)
if err != nil {
return nil, fmt.Errorf("unable to decode payload: %s", err)
}
// This whole BlockDecoder method is being called through the `bstream.Block.ToNative()`
// method. Hence, it's a great place to add temporary data normalization calls to backport
// some features that were not in all blocks yet (because we did not re-process all blocks
// yet).
//
// Thoughts for the future: Ideally, we would leverage the version information here to take
// a decision, like `do X if version <= 2.1` so we would not pay the performance hit
// automatically instead of having to re-deploy a new version of bstream (which means
// rebuild everything mostly)
return block, nil
}
func PBSolanaBlockDecoder(blk *bstream.Block) (interface{}, error) {
if blk.Kind() != Protocol_SOL {
return nil, fmt.Errorf("expected kind %s, got %s", Protocol_SOL, blk.Kind())
}
if blk.Version() != 1 {
return nil, fmt.Errorf("this decoder only knows about version 1, got %d", blk.Version())
}
block := new(pbsolv1.Block)
payload, err := blk.Payload.Get()
if err != nil {
return nil, fmt.Errorf("getting payload: %s", err)
}
err = proto.Unmarshal(payload, block)
if err != nil {
return nil, fmt.Errorf("unable to decode payload: %s", err)
}
return block, nil
}