-
Notifications
You must be signed in to change notification settings - Fork 7
/
msg_encoding.go
75 lines (64 loc) · 1.68 KB
/
msg_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
package f3
import (
"bytes"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/klauspost/compress/zstd"
)
var (
_ gMessageEncoding = (*cborGMessageEncoding)(nil)
_ gMessageEncoding = (*zstdGMessageEncoding)(nil)
)
type gMessageEncoding interface {
Encode(*gpbft.GMessage) ([]byte, error)
Decode([]byte) (*gpbft.GMessage, error)
}
type cborGMessageEncoding struct{}
func (c *cborGMessageEncoding) Encode(m *gpbft.GMessage) ([]byte, error) {
var buf bytes.Buffer
if err := m.MarshalCBOR(&buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c *cborGMessageEncoding) Decode(v []byte) (*gpbft.GMessage, error) {
r := bytes.NewReader(v)
var msg gpbft.GMessage
if err := msg.UnmarshalCBOR(r); err != nil {
return nil, err
}
return &msg, nil
}
type zstdGMessageEncoding struct {
cborEncoding cborGMessageEncoding
compressor *zstd.Encoder
decompressor *zstd.Decoder
}
func newZstdGMessageEncoding() (*zstdGMessageEncoding, error) {
writer, err := zstd.NewWriter(nil)
if err != nil {
return nil, err
}
reader, err := zstd.NewReader(nil)
if err != nil {
return nil, err
}
return &zstdGMessageEncoding{
compressor: writer,
decompressor: reader,
}, nil
}
func (c *zstdGMessageEncoding) Encode(m *gpbft.GMessage) ([]byte, error) {
cborEncoded, err := c.cborEncoding.Encode(m)
if err != nil {
return nil, err
}
compressed := c.compressor.EncodeAll(cborEncoded, make([]byte, 0, len(cborEncoded)))
return compressed, err
}
func (c *zstdGMessageEncoding) Decode(v []byte) (*gpbft.GMessage, error) {
cborEncoded, err := c.decompressor.DecodeAll(v, make([]byte, 0, len(v)))
if err != nil {
return nil, err
}
return c.cborEncoding.Decode(cborEncoded)
}