mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-21 08:51:32 +00:00
Merge from panic branch
This commit is contained in:
@ -2,7 +2,9 @@ package binary
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"io"
|
||||
"reflect"
|
||||
"time"
|
||||
@ -38,6 +40,7 @@ const (
|
||||
func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
switch o := o.(type) {
|
||||
case nil:
|
||||
// SANITY CHECK
|
||||
panic("nil type unsupported")
|
||||
case byte:
|
||||
WriteByte(typeByte, w, n, err)
|
||||
@ -82,12 +85,16 @@ func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
WriteByte(typeTime, w, n, err)
|
||||
WriteTime(o, w, n, err)
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
|
||||
}
|
||||
}
|
||||
|
||||
func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
|
||||
type_ := ReadByte(r, n, err)
|
||||
if *err != nil {
|
||||
return
|
||||
}
|
||||
switch type_ {
|
||||
case typeByte:
|
||||
o = ReadByte(r, n, err)
|
||||
@ -118,15 +125,12 @@ func BasicCodecDecoder(r io.Reader, n *int64, err *error) (o interface{}) {
|
||||
case typeTime:
|
||||
o = ReadTime(r, n, err)
|
||||
default:
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
} else {
|
||||
panic(fmt.Sprintf("Unsupported type byte: %X", type_))
|
||||
}
|
||||
*err = errors.New(Fmt("Unsupported type byte: %X", type_))
|
||||
}
|
||||
return o
|
||||
return
|
||||
}
|
||||
|
||||
// Contract: Caller must ensure that types match.
|
||||
func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
|
||||
switch o1.(type) {
|
||||
case byte:
|
||||
@ -157,8 +161,10 @@ func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
|
||||
case time.Time:
|
||||
return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
|
||||
default:
|
||||
panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o1)))
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var BasicCodec = Codec{
|
||||
|
@ -70,6 +70,7 @@ func (info StructFieldInfo) unpack() (int, reflect.Type, Options) {
|
||||
func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
|
||||
rt := reflect.TypeOf(o)
|
||||
if rt.NumField() != 1 {
|
||||
// SANITY CHECK
|
||||
panic("Unexpected number of fields in struct-wrapped declaration of type")
|
||||
}
|
||||
return rt.Field(0).Type
|
||||
@ -78,6 +79,7 @@ func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
|
||||
func SetByteForType(typeByte byte, rt reflect.Type) {
|
||||
typeInfo := GetTypeInfo(rt)
|
||||
if typeInfo.Byte != 0x00 && typeInfo.Byte != typeByte {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Type %v already registered with type byte %X", rt, typeByte))
|
||||
}
|
||||
typeInfo.Byte = typeByte
|
||||
@ -122,6 +124,7 @@ type ConcreteType struct {
|
||||
func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo {
|
||||
it := GetTypeFromStructDeclaration(o)
|
||||
if it.Kind() != reflect.Interface {
|
||||
// SANITY CHECK
|
||||
panic("RegisterInterface expects an interface")
|
||||
}
|
||||
toType := make(map[byte]reflect.Type, 0)
|
||||
@ -131,9 +134,11 @@ func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo {
|
||||
typeByte := ctype.Byte
|
||||
SetByteForType(typeByte, crt)
|
||||
if typeByte == 0x00 {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Byte of 0x00 is reserved for nil (%v)", ctype))
|
||||
}
|
||||
if toType[typeByte] != nil {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Duplicate Byte for type %v and %v", ctype, toType[typeByte]))
|
||||
}
|
||||
toType[typeByte] = crt
|
||||
@ -177,6 +182,8 @@ func MakeTypeInfo(rt reflect.Type) *TypeInfo {
|
||||
return info
|
||||
}
|
||||
|
||||
// Contract: Caller must ensure that rt is supported
|
||||
// (e.g. is recursively composed of supported native types, and structs and slices.)
|
||||
func readReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, r io.Reader, n *int64, err *error) {
|
||||
|
||||
// Get typeInfo
|
||||
@ -360,6 +367,7 @@ func readReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, r io.Rea
|
||||
rv.SetBool(num > 0)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
@ -504,6 +512,7 @@ func writeReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, w io.Wr
|
||||
}
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
@ -526,6 +535,8 @@ func readByteJSON(o interface{}) (typeByte byte, rest interface{}, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Contract: Caller must ensure that rt is supported
|
||||
// (e.g. is recursively composed of supported native types, and structs and slices.)
|
||||
func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *error) {
|
||||
|
||||
// Get typeInfo
|
||||
@ -696,6 +707,7 @@ func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *erro
|
||||
rv.SetBool(bl)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
@ -821,6 +833,7 @@ func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64,
|
||||
WriteTo(jsonBytes, w, n, err)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
|
||||
)
|
||||
|
||||
// THESE PANICS ARE SANITY CHECKS
|
||||
|
||||
func BinaryBytes(o interface{}) []byte {
|
||||
w, n, err := new(bytes.Buffer), new(int64), new(error)
|
||||
WriteBinary(o, w, n, err)
|
||||
|
Reference in New Issue
Block a user