forked from cerc-io/plugeth
rlp: remove support for signed integer types
There is no agreement on how to encode negative integers across implementations. cpp-ethereum doesn't support them either.
This commit is contained in:
parent
4f12f0697e
commit
93e858f88e
@ -54,7 +54,7 @@ type Decoder interface {
|
|||||||
// To decode into a Go string, the input must be an RLP string. The
|
// To decode into a Go string, the input must be an RLP string. The
|
||||||
// bytes are taken as-is and will not necessarily be valid UTF-8.
|
// bytes are taken as-is and will not necessarily be valid UTF-8.
|
||||||
//
|
//
|
||||||
// To decode into an integer type, the input must also be an RLP
|
// To decode into an unsigned integer type, the input must also be an RLP
|
||||||
// string. The bytes are interpreted as a big endian representation of
|
// string. The bytes are interpreted as a big endian representation of
|
||||||
// the integer. If the RLP string is larger than the bit size of the
|
// the integer. If the RLP string is larger than the bit size of the
|
||||||
// type, Decode will return an error. Decode also supports *big.Int.
|
// type, Decode will return an error. Decode also supports *big.Int.
|
||||||
@ -66,8 +66,9 @@ type Decoder interface {
|
|||||||
// []interface{}, for RLP lists
|
// []interface{}, for RLP lists
|
||||||
// []byte, for RLP strings
|
// []byte, for RLP strings
|
||||||
//
|
//
|
||||||
// Non-empty interface types are not supported, nor are bool, float32,
|
// Non-empty interface types are not supported, nor are booleans,
|
||||||
// float64, maps, channel types and functions.
|
// signed integers, floating point numbers, maps, channels and
|
||||||
|
// functions.
|
||||||
func Decode(r io.Reader, val interface{}) error {
|
func Decode(r io.Reader, val interface{}) error {
|
||||||
return NewStream(r).Decode(val)
|
return NewStream(r).Decode(val)
|
||||||
}
|
}
|
||||||
@ -97,8 +98,8 @@ func makeDecoder(typ reflect.Type) (dec decoder, err error) {
|
|||||||
return decodeBigInt, nil
|
return decodeBigInt, nil
|
||||||
case typ.AssignableTo(bigInt):
|
case typ.AssignableTo(bigInt):
|
||||||
return decodeBigIntNoPtr, nil
|
return decodeBigIntNoPtr, nil
|
||||||
case isInteger(kind):
|
case isUint(kind):
|
||||||
return makeNumDecoder(typ), nil
|
return decodeUint, nil
|
||||||
case kind == reflect.String:
|
case kind == reflect.String:
|
||||||
return decodeString, nil
|
return decodeString, nil
|
||||||
case kind == reflect.Slice || kind == reflect.Array:
|
case kind == reflect.Slice || kind == reflect.Array:
|
||||||
@ -114,30 +115,6 @@ func makeDecoder(typ reflect.Type) (dec decoder, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeNumDecoder(typ reflect.Type) decoder {
|
|
||||||
kind := typ.Kind()
|
|
||||||
switch {
|
|
||||||
case kind <= reflect.Int64:
|
|
||||||
return decodeInt
|
|
||||||
case kind <= reflect.Uint64:
|
|
||||||
return decodeUint
|
|
||||||
default:
|
|
||||||
panic("fallthrough")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeInt(s *Stream, val reflect.Value) error {
|
|
||||||
typ := val.Type()
|
|
||||||
num, err := s.uint(typ.Bits())
|
|
||||||
if err == errUintOverflow {
|
|
||||||
return decodeError{"input string too long", typ}
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
val.SetInt(int64(num))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeUint(s *Stream, val reflect.Value) error {
|
func decodeUint(s *Stream, val reflect.Value) error {
|
||||||
typ := val.Type()
|
typ := val.Type()
|
||||||
num, err := s.uint(typ.Bits())
|
num, err := s.uint(typ.Bits())
|
||||||
|
@ -171,7 +171,7 @@ func TestDecodeErrors(t *testing.T) {
|
|||||||
t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
|
t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := Decode(r, new(int)); err != io.EOF {
|
if err := Decode(r, new(uint)); err != io.EOF {
|
||||||
t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
|
t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,12 +184,12 @@ type decodeTest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type simplestruct struct {
|
type simplestruct struct {
|
||||||
A int
|
A uint
|
||||||
B string
|
B string
|
||||||
}
|
}
|
||||||
|
|
||||||
type recstruct struct {
|
type recstruct struct {
|
||||||
I int
|
I uint
|
||||||
Child *recstruct
|
Child *recstruct
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +202,7 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
sharedByteArray [5]byte
|
sharedByteArray [5]byte
|
||||||
sharedPtr = new(*int)
|
sharedPtr = new(*uint)
|
||||||
)
|
)
|
||||||
|
|
||||||
var decodeTests = []decodeTest{
|
var decodeTests = []decodeTest{
|
||||||
@ -217,13 +217,13 @@ var decodeTests = []decodeTest{
|
|||||||
{input: "C0", ptr: new(uint32), error: ErrExpectedString.Error()},
|
{input: "C0", ptr: new(uint32), error: ErrExpectedString.Error()},
|
||||||
|
|
||||||
// slices
|
// slices
|
||||||
{input: "C0", ptr: new([]int), value: []int{}},
|
{input: "C0", ptr: new([]uint), value: []uint{}},
|
||||||
{input: "C80102030405060708", ptr: new([]int), value: []int{1, 2, 3, 4, 5, 6, 7, 8}},
|
{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
|
||||||
|
|
||||||
// arrays
|
// arrays
|
||||||
{input: "C0", ptr: new([5]int), value: [5]int{}},
|
{input: "C0", ptr: new([5]uint), value: [5]uint{}},
|
||||||
{input: "C50102030405", ptr: new([5]int), value: [5]int{1, 2, 3, 4, 5}},
|
{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
|
||||||
{input: "C6010203040506", ptr: new([5]int), error: "rlp: input list has too many elements for [5]int"},
|
{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
|
||||||
|
|
||||||
// byte slices
|
// byte slices
|
||||||
{input: "01", ptr: new([]byte), value: []byte{1}},
|
{input: "01", ptr: new([]byte), value: []byte{1}},
|
||||||
@ -280,17 +280,17 @@ var decodeTests = []decodeTest{
|
|||||||
},
|
},
|
||||||
|
|
||||||
// pointers
|
// pointers
|
||||||
{input: "00", ptr: new(*int), value: (*int)(nil)},
|
{input: "00", ptr: new(*uint), value: (*uint)(nil)},
|
||||||
{input: "80", ptr: new(*int), value: (*int)(nil)},
|
{input: "80", ptr: new(*uint), value: (*uint)(nil)},
|
||||||
{input: "C0", ptr: new(*int), value: (*int)(nil)},
|
{input: "C0", ptr: new(*uint), value: (*uint)(nil)},
|
||||||
{input: "07", ptr: new(*int), value: intp(7)},
|
{input: "07", ptr: new(*uint), value: uintp(7)},
|
||||||
{input: "8108", ptr: new(*int), value: intp(8)},
|
{input: "8108", ptr: new(*uint), value: uintp(8)},
|
||||||
{input: "C109", ptr: new(*[]int), value: &[]int{9}},
|
{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
|
||||||
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
|
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
|
||||||
|
|
||||||
// pointer should be reset to nil
|
// pointer should be reset to nil
|
||||||
{input: "05", ptr: sharedPtr, value: intp(5)},
|
{input: "05", ptr: sharedPtr, value: uintp(5)},
|
||||||
{input: "80", ptr: sharedPtr, value: (*int)(nil)},
|
{input: "80", ptr: sharedPtr, value: (*uint)(nil)},
|
||||||
|
|
||||||
// interface{}
|
// interface{}
|
||||||
{input: "00", ptr: new(interface{}), value: []byte{0}},
|
{input: "00", ptr: new(interface{}), value: []byte{0}},
|
||||||
@ -301,7 +301,7 @@ var decodeTests = []decodeTest{
|
|||||||
{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
|
{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
|
||||||
}
|
}
|
||||||
|
|
||||||
func intp(i int) *int { return &i }
|
func uintp(i uint) *uint { return &i }
|
||||||
|
|
||||||
func runTests(t *testing.T, decode func([]byte, interface{}) error) {
|
func runTests(t *testing.T, decode func([]byte, interface{}) error) {
|
||||||
for i, test := range decodeTests {
|
for i, test := range decodeTests {
|
||||||
@ -434,8 +434,8 @@ func ExampleDecode() {
|
|||||||
input, _ := hex.DecodeString("C90A1486666F6F626172")
|
input, _ := hex.DecodeString("C90A1486666F6F626172")
|
||||||
|
|
||||||
type example struct {
|
type example struct {
|
||||||
A, B int
|
A, B uint
|
||||||
private int // private fields are ignored
|
private uint // private fields are ignored
|
||||||
String string
|
String string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,7 +447,7 @@ func ExampleDecode() {
|
|||||||
fmt.Printf("Decoded value: %#v\n", s)
|
fmt.Printf("Decoded value: %#v\n", s)
|
||||||
}
|
}
|
||||||
// Output:
|
// Output:
|
||||||
// Decoded value: rlp.example{A:10, B:20, private:0, String:"foobar"}
|
// Decoded value: rlp.example{A:0xa, B:0x14, private:0x0, String:"foobar"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleStream() {
|
func ExampleStream() {
|
||||||
|
@ -57,6 +57,6 @@ func genTypeInfo(typ reflect.Type) (info *typeinfo, err error) {
|
|||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInteger(k reflect.Kind) bool {
|
func isUint(k reflect.Kind) bool {
|
||||||
return k >= reflect.Int && k <= reflect.Uintptr
|
return k >= reflect.Uint && k <= reflect.Uintptr
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user