From 566afae9bf7db8615343400dfd080b006ffaa158 Mon Sep 17 00:00:00 2001 From: MariusVanDerWijden Date: Wed, 4 Jun 2025 14:42:59 +0200 Subject: [PATCH] accounts/abi: style changes --- accounts/abi/pack.go | 24 +++++++++--------------- accounts/abi/pack_test.go | 3 +-- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/accounts/abi/pack.go b/accounts/abi/pack.go index 78c2b87cbf..a4c73922d4 100644 --- a/accounts/abi/pack.go +++ b/accounts/abi/pack.go @@ -33,27 +33,21 @@ func packBytesSlice(bytes []byte, l int) []byte { 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 // t. func packElement(t Type, reflectValue reflect.Value) ([]byte, error) { switch t.T { - case IntTy, UintTy: - if err := validateNum(t, reflectValue); err != nil { - return nil, err + case UintTy: + // make sure to not pack a negative value into a uint type. + 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 case StringTy: return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil case AddressTy: diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index 0de1c10cf5..d1e3fbbf69 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -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 - _, err = abi.Pack("send", big.NewInt(-1)) - if err == nil { + if _, err := abi.Pack("send", big.NewInt(-1)); err == nil { t.Fatal("expected error when trying to pack negative big.Int into uint256 value") } }