prysm-pulse/beacon-chain/db/kv/encoding.go
Preston Van Loon eae2268dd1 DB: Prevent encoding a nil message (#4470)
* Prevent encoding a nil message
* Merge refs/heads/master into prevent-saving-nil-msg
2020-01-10 01:38:53 +00:00

32 lines
537 B
Go

package kv
import (
"errors"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
)
func decode(data []byte, dst proto.Message) error {
data, err := snappy.Decode(nil, data)
if err != nil {
return err
}
if err := proto.Unmarshal(data, dst); err != nil {
return err
}
return nil
}
func encode(msg proto.Message) ([]byte, error) {
if msg == nil {
return nil, errors.New("cannot encode nil message")
}
enc, err := proto.Marshal(msg)
if err != nil {
return nil, err
}
return snappy.Encode(nil, enc), nil
}