prysm-pulse/beacon-chain/db/kv/encoding.go
Preston Van Loon 4072eb711f
Beacon State: More consistent nil return for state (#4854)
* More consistent nil return for state
* Merge refs/heads/master into nil-state
* Add a check for encode(nil)
* Merge branch 'nil-state' of github.com:prysmaticlabs/prysm into nil-state
* fix test, thanks @rauljordan
* fix tests
* gofmt
2020-02-13 20:34:50 +00:00

33 lines
580 B
Go

package kv
import (
"errors"
"reflect"
"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 || reflect.ValueOf(msg).IsNil() {
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
}