prysm-pulse/beacon-chain/db/kv/encoding.go
Preston Van Loon 8ffe9858ec
Rename white/blacklist to allow/deny list (#6173)
* Rename white/blacklist to allow/deny list
* Deprecate flag properly
2020-06-08 04:35:15 +00:00

66 lines
1.4 KiB
Go

package kv
import (
"errors"
"reflect"
fastssz "github.com/ferranbt/fastssz"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func decode(data []byte, dst proto.Message) error {
data, err := snappy.Decode(nil, data)
if err != nil {
return err
}
if isSSZStorageFormat(dst) {
return dst.(fastssz.Unmarshaler).UnmarshalSSZ(data)
}
return proto.Unmarshal(data, dst)
}
func encode(msg proto.Message) ([]byte, error) {
if msg == nil || reflect.ValueOf(msg).IsNil() {
return nil, errors.New("cannot encode nil message")
}
var enc []byte
var err error
if isSSZStorageFormat(msg) {
enc, err = msg.(fastssz.Marshaler).MarshalSSZ()
if err != nil {
return nil, err
}
} else {
enc, err = proto.Marshal(msg)
if err != nil {
return nil, err
}
}
return snappy.Encode(nil, enc), nil
}
// isSSZStorageFormat returns true if the object type should be saved in SSZ encoded format.
func isSSZStorageFormat(obj interface{}) bool {
switch obj.(type) {
case *pb.BeaconState:
return true
case *ethpb.BeaconBlock:
return true
case *ethpb.Attestation:
return true
case *ethpb.Deposit:
return true
case *ethpb.AttesterSlashing:
return true
case *ethpb.ProposerSlashing:
return true
case *ethpb.VoluntaryExit:
return true
default:
return false
}
}