mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
account/abi: optimize mustArrayToByteSlice
This commit is contained in:
parent
342285b139
commit
51ee9850fd
2 changed files with 23 additions and 14 deletions
|
|
@ -51,29 +51,37 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
||||||
case StringTy:
|
case StringTy:
|
||||||
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
||||||
case AddressTy:
|
case AddressTy:
|
||||||
|
var slices []byte
|
||||||
if reflectValue.Kind() == reflect.Array {
|
if reflectValue.Kind() == reflect.Array {
|
||||||
reflectValue = mustArrayToByteSlice(reflectValue)
|
slices = mustArrayToByteSlice(reflectValue)
|
||||||
|
} else {
|
||||||
|
slices = reflectValue.Bytes()
|
||||||
}
|
}
|
||||||
|
return common.LeftPadBytes(slices, 32), nil
|
||||||
return common.LeftPadBytes(reflectValue.Bytes(), 32), nil
|
|
||||||
case BoolTy:
|
case BoolTy:
|
||||||
if reflectValue.Bool() {
|
if reflectValue.Bool() {
|
||||||
return math.PaddedBigBytes(common.Big1, 32), nil
|
return math.PaddedBigBytes(common.Big1, 32), nil
|
||||||
}
|
}
|
||||||
return math.PaddedBigBytes(common.Big0, 32), nil
|
return math.PaddedBigBytes(common.Big0, 32), nil
|
||||||
case BytesTy:
|
case BytesTy:
|
||||||
|
var slices []byte
|
||||||
if reflectValue.Kind() == reflect.Array {
|
if reflectValue.Kind() == reflect.Array {
|
||||||
reflectValue = mustArrayToByteSlice(reflectValue)
|
slices = mustArrayToByteSlice(reflectValue)
|
||||||
}
|
} else {
|
||||||
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
|
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
|
||||||
return []byte{}, errors.New("bytes type is neither slice nor array")
|
return []byte{}, errors.New("bytes type is neither slice nor array")
|
||||||
}
|
}
|
||||||
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
|
slices = reflectValue.Bytes()
|
||||||
case FixedBytesTy, FunctionTy:
|
|
||||||
if reflectValue.Kind() == reflect.Array {
|
|
||||||
reflectValue = mustArrayToByteSlice(reflectValue)
|
|
||||||
}
|
}
|
||||||
return common.RightPadBytes(reflectValue.Bytes(), 32), nil
|
return packBytesSlice(slices, len(slices)), nil
|
||||||
|
case FixedBytesTy, FunctionTy:
|
||||||
|
var slices []byte
|
||||||
|
if reflectValue.Kind() == reflect.Array {
|
||||||
|
slices = mustArrayToByteSlice(reflectValue)
|
||||||
|
} else {
|
||||||
|
slices = reflectValue.Bytes()
|
||||||
|
}
|
||||||
|
return common.RightPadBytes(slices, 32), nil
|
||||||
default:
|
default:
|
||||||
return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T)
|
return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -89,10 +89,11 @@ func reflectIntType(unsigned bool, size int) reflect.Type {
|
||||||
|
|
||||||
// mustArrayToByteSlice creates a new byte slice with the exact same size as value
|
// mustArrayToByteSlice creates a new byte slice with the exact same size as value
|
||||||
// and copies the bytes in value to the new slice.
|
// and copies the bytes in value to the new slice.
|
||||||
func mustArrayToByteSlice(value reflect.Value) reflect.Value {
|
func mustArrayToByteSlice(value reflect.Value) []byte {
|
||||||
slice := reflect.ValueOf(make([]byte, value.Len()))
|
ptr := reflect.New(value.Type())
|
||||||
reflect.Copy(slice, value)
|
ptr.Elem().Set(value)
|
||||||
return slice
|
ret := ptr.Elem().Bytes()
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// set attempts to assign src to dst by either setting, copying or otherwise.
|
// set attempts to assign src to dst by either setting, copying or otherwise.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue