mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
accounts/abi: style changes
This commit is contained in:
parent
9574d543d0
commit
566afae9bf
2 changed files with 10 additions and 17 deletions
|
|
@ -33,26 +33,20 @@ func packBytesSlice(bytes []byte, l int) []byte {
|
||||||
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
|
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateNum returns an error if the underlying type of t is uint and the
|
|
||||||
// value of reflectValue is a negative big.Int
|
|
||||||
func validateNum(t Type, reflectValue reflect.Value) error {
|
|
||||||
if t.T == UintTy && reflectValue.Kind() == reflect.Ptr {
|
|
||||||
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
|
|
||||||
if val.Sign() == -1 {
|
|
||||||
return errInvalidSign
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// packElement packs the given reflect value according to the abi specification in
|
// packElement packs the given reflect value according to the abi specification in
|
||||||
// t.
|
// t.
|
||||||
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
||||||
switch t.T {
|
switch t.T {
|
||||||
case IntTy, UintTy:
|
case UintTy:
|
||||||
if err := validateNum(t, reflectValue); err != nil {
|
// make sure to not pack a negative value into a uint type.
|
||||||
return nil, err
|
if reflectValue.Kind() == reflect.Ptr {
|
||||||
|
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
|
||||||
|
if val.Sign() == -1 {
|
||||||
|
return nil, errInvalidSign
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return packNum(reflectValue), nil
|
||||||
|
case IntTy:
|
||||||
return packNum(reflectValue), nil
|
return packNum(reflectValue), nil
|
||||||
case StringTy:
|
case StringTy:
|
||||||
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
||||||
|
|
|
||||||
|
|
@ -179,8 +179,7 @@ func TestMethodPack(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// test that we can't pack a negative value for a parameter that is specified as a uint
|
// test that we can't pack a negative value for a parameter that is specified as a uint
|
||||||
_, err = abi.Pack("send", big.NewInt(-1))
|
if _, err := abi.Pack("send", big.NewInt(-1)); err == nil {
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error when trying to pack negative big.Int into uint256 value")
|
t.Fatal("expected error when trying to pack negative big.Int into uint256 value")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue