Commit 4880868c authored by Jeffrey Wilcke's avatar Jeffrey Wilcke

accounts/abi: fixed string and fixed size bytes packing

parent c3d52504
...@@ -197,13 +197,13 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) { ...@@ -197,13 +197,13 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) {
case reflect.Uint64: case reflect.Uint64:
return uint64(bigNum.Uint64()), nil return uint64(bigNum.Uint64()), nil
case reflect.Int8: case reflect.Int8:
return uint8(bigNum.Int64()), nil return int8(bigNum.Int64()), nil
case reflect.Int16: case reflect.Int16:
return uint16(bigNum.Int64()), nil return int16(bigNum.Int64()), nil
case reflect.Int32: case reflect.Int32:
return uint32(bigNum.Int64()), nil return int32(bigNum.Int64()), nil
case reflect.Int64: case reflect.Int64:
return uint64(bigNum.Int64()), nil return int64(bigNum.Int64()), nil
case reflect.Ptr: case reflect.Ptr:
return bigNum, nil return bigNum, nil
} }
......
This diff is collapsed.
...@@ -33,7 +33,7 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string { ...@@ -33,7 +33,7 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string {
// sliceTypeCheck checks that the given slice can by assigned to the reflection // sliceTypeCheck checks that the given slice can by assigned to the reflection
// type in t. // type in t.
func sliceTypeCheck(t Type, val reflect.Value) error { func sliceTypeCheck(t Type, val reflect.Value) error {
if !(val.Kind() == reflect.Slice || val.Kind() == reflect.Array) { if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return typeErr(formatSliceString(t.Kind, t.SliceSize), val.Type()) return typeErr(formatSliceString(t.Kind, t.SliceSize), val.Type())
} }
if t.IsArray && val.Len() != t.SliceSize { if t.IsArray && val.Len() != t.SliceSize {
...@@ -48,14 +48,13 @@ func sliceTypeCheck(t Type, val reflect.Value) error { ...@@ -48,14 +48,13 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
return sliceTypeCheck(*t.Elem, val.Index(0)) return sliceTypeCheck(*t.Elem, val.Index(0))
} }
elemKind := val.Type().Elem().Kind() if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind {
if elemKind != t.Elem.Kind {
return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), val.Type()) return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), val.Type())
} }
return nil return nil
} }
// typeCheck checks that thet given reflection val can be assigned to the reflection // typeCheck checks that the given reflection value can be assigned to the reflection
// type in t. // type in t.
func typeCheck(t Type, value reflect.Value) error { func typeCheck(t Type, value reflect.Value) error {
if t.IsSlice || t.IsArray { if t.IsSlice || t.IsArray {
......
...@@ -58,7 +58,7 @@ func (m Method) pack(method Method, args ...interface{}) ([]byte, error) { ...@@ -58,7 +58,7 @@ func (m Method) pack(method Method, args ...interface{}) ([]byte, error) {
} }
// check for a slice type (string, bytes, slice) // check for a slice type (string, bytes, slice)
if input.Type.T == StringTy || input.Type.T == BytesTy || input.Type.IsSlice || input.Type.IsArray { if input.Type.requiresLengthPrefix() {
// calculate the offset // calculate the offset
offset := len(method.Inputs)*32 + len(variableInput) offset := len(method.Inputs)*32 + len(variableInput)
// set the offset // set the offset
......
...@@ -59,7 +59,7 @@ func packElement(t Type, reflectValue reflect.Value) []byte { ...@@ -59,7 +59,7 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
reflectValue = mustArrayToByteSlice(reflectValue) reflectValue = mustArrayToByteSlice(reflectValue)
} }
return common.LeftPadBytes(reflectValue.Bytes(), 32) return common.RightPadBytes(reflectValue.Bytes(), 32)
} }
panic("abi: fatal error") panic("abi: fatal error")
} }
...@@ -89,6 +89,7 @@ func NewType(t string) (typ Type, err error) { ...@@ -89,6 +89,7 @@ func NewType(t string) (typ Type, err error) {
return Type{}, err return Type{}, err
} }
typ.Elem = &sliceType typ.Elem = &sliceType
typ.stringKind = sliceType.stringKind + t[len(res[1]):]
return typ, nil return typ, nil
} }
...@@ -110,6 +111,7 @@ func NewType(t string) (typ Type, err error) { ...@@ -110,6 +111,7 @@ func NewType(t string) (typ Type, err error) {
varSize = 256 varSize = 256
t += "256" t += "256"
} }
typ.stringKind = t
switch varType { switch varType {
case "int": case "int":
...@@ -149,7 +151,6 @@ func NewType(t string) (typ Type, err error) { ...@@ -149,7 +151,6 @@ func NewType(t string) (typ Type, err error) {
default: default:
return Type{}, fmt.Errorf("unsupported arg type: %s", t) return Type{}, fmt.Errorf("unsupported arg type: %s", t)
} }
typ.stringKind = t
return return
} }
...@@ -181,3 +182,9 @@ func (t Type) pack(v reflect.Value) ([]byte, error) { ...@@ -181,3 +182,9 @@ func (t Type) pack(v reflect.Value) ([]byte, error) {
return packElement(t, v), nil return packElement(t, v), nil
} }
// requireLengthPrefix returns whether the type requires any sort of length
// prefixing.
func (t Type) requiresLengthPrefix() bool {
return t.T != FixedBytesTy && (t.T == StringTy || t.T == BytesTy || t.IsSlice || t.IsArray)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment