summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHang Su <87964331+ahangsu@users.noreply.github.com>2022-01-25 22:58:05 -0500
committerGitHub <noreply@github.com>2022-01-25 22:58:05 -0500
commit762bc80cec5d623f46c2678914ca6bb097de836d (patch)
tree395d8e2ae74856da561b8eea500c0f6edeb6db3b
parentc2ba596bd4d45bd92288f0fda3e9373040e4fd19 (diff)
Revert `fillBytes` method to `bigIntToBytes` for lower golang version (#3498)
* revert fillbytes method for lower version of golang * awww shut up dude * use copy * Use DEPRECATED Co-authored-by: Will Winder <wwinder.unh@gmail.com> Co-authored-by: Will Winder <wwinder.unh@gmail.com>
-rw-r--r--data/abi/abi_encode.go24
-rw-r--r--data/abi/abi_encode_test.go25
-rw-r--r--data/abi/abi_json_test.go9
3 files changed, 41 insertions, 17 deletions
diff --git a/data/abi/abi_encode.go b/data/abi/abi_encode.go
index 8e5a49086..ac25588f0 100644
--- a/data/abi/abi_encode.go
+++ b/data/abi/abi_encode.go
@@ -25,6 +25,23 @@ import (
"strings"
)
+// bigIntToBytes casts non-negative big integer to byte slice with specific byte length
+// DEPRECATED: THIS IS A WORKAROUND FOR `fillBytes` METHOD BEFORE GOLANG 1.15+
+// SHOULD BE REMOVED AFTER WE MOVE TO HIGHER VERSION
+func bigIntToBytes(x *big.Int, byteLen uint) ([]byte, error) {
+ if x.Cmp(big.NewInt(0)) < 0 {
+ return nil, fmt.Errorf("ABI: big Int To Bytes error: should pass in non-negative integer")
+ }
+ if uint(x.BitLen()) > byteLen*8 {
+ return nil, fmt.Errorf("ABI: big Int To Bytes error: integer byte length > given byte length")
+ }
+
+ buffer := make([]byte, byteLen)
+ intBytes := x.Bytes()
+ copy(buffer[int(byteLen)-len(intBytes):], intBytes)
+ return buffer, nil
+}
+
// typeCastToTuple cast an array-like ABI type into an ABI tuple type.
func (t Type) typeCastToTuple(tupLen ...int) (Type, error) {
var childT []Type
@@ -170,13 +187,14 @@ func encodeInt(intValue interface{}, bitSize uint16) ([]byte, error) {
return nil, fmt.Errorf("passed in numeric value should be non negative")
}
- castedBytes := make([]byte, bitSize/8)
-
if bigInt.Cmp(new(big.Int).Lsh(big.NewInt(1), uint(bitSize))) >= 0 {
return nil, fmt.Errorf("input value bit size %d > abi type bit size %d", bigInt.BitLen(), bitSize)
}
- bigInt.FillBytes(castedBytes)
+ castedBytes, err := bigIntToBytes(bigInt, uint(bitSize/8))
+ if err != nil {
+ return nil, err
+ }
return castedBytes, nil
}
diff --git a/data/abi/abi_encode_test.go b/data/abi/abi_encode_test.go
index 66060f5ce..620013c23 100644
--- a/data/abi/abi_encode_test.go
+++ b/data/abi/abi_encode_test.go
@@ -83,8 +83,8 @@ func TestEncodeValid(t *testing.T) {
randomInt, err := rand.Int(rand.Reader, upperLimit)
require.NoError(t, err, "cryptographic random int init fail")
- expected := make([]byte, intSize/8)
- randomInt.FillBytes(expected)
+ expected, err := bigIntToBytes(randomInt, uint(intSize/8))
+ require.NoError(t, err, "big int to byte conversion error")
uintEncode, err := uintType.Encode(randomInt)
require.NoError(t, err, "encoding from uint type fail")
@@ -122,8 +122,9 @@ func TestEncodeValid(t *testing.T) {
encodedUfixed, err := typeUfixed.Encode(randomInt)
require.NoError(t, err, "ufixed encode fail")
- expected := make([]byte, size/8)
- randomInt.FillBytes(expected)
+ expected, err := bigIntToBytes(randomInt, uint(size/8))
+ require.NoError(t, err, "big int to byte conversion error")
+
require.Equal(t, expected, encodedUfixed, "encode ufixed not match with expected")
}
// (2^[bitSize] - 1) / (10^[precision]) test
@@ -141,8 +142,8 @@ func TestEncodeValid(t *testing.T) {
randomAddrInt, err := rand.Int(rand.Reader, upperLimit)
require.NoError(t, err, "cryptographic random int init fail")
- addrBytesExpected := make([]byte, addressByteSize)
- randomAddrInt.FillBytes(addrBytesExpected)
+ addrBytesExpected, err := bigIntToBytes(randomAddrInt, uint(addressByteSize))
+ require.NoError(t, err, "big int to byte conversion error")
addrBytesActual, err := addressType.Encode(addrBytesExpected)
require.NoError(t, err, "address encode fail")
@@ -421,8 +422,8 @@ func TestDecodeValid(t *testing.T) {
randomAddrInt, err := rand.Int(rand.Reader, upperLimit)
require.NoError(t, err, "cryptographic random int init fail")
- expected := make([]byte, addressByteSize)
- randomAddrInt.FillBytes(expected)
+ expected, err := bigIntToBytes(randomAddrInt, uint(addressByteSize))
+ require.NoError(t, err, "big int to byte conversion error")
actual, err := addressType.Decode(expected)
require.NoError(t, err, "decoding address should not return error")
@@ -951,8 +952,10 @@ func addPrimitiveRandomValues(t *testing.T, pool *map[BaseType][]testUnit) {
for i := 0; i < addressTestCaseCount; i++ {
randAddrVal, err := rand.Int(rand.Reader, maxAddress)
require.NoError(t, err, "generate random value for address, should be no error")
- addrBytes := make([]byte, addressByteSize)
- randAddrVal.FillBytes(addrBytes)
+
+ addrBytes, err := bigIntToBytes(randAddrVal, uint(addressByteSize))
+ require.NoError(t, err, "big int to byte conversion error")
+
(*pool)[Address][i] = testUnit{serializedType: addressType.String(), value: addrBytes}
}
categorySelfRoundTripTest(t, (*pool)[Address])
@@ -1162,7 +1165,7 @@ func TestParseArgJSONtoByteSlice(t *testing.T) {
for i, test := range tests {
t.Run(fmt.Sprintf("index=%d", i), func(t *testing.T) {
- applicationArgs := [][]byte{}
+ applicationArgs := make([][]byte, 0)
err := ParseArgJSONtoByteSlice(test.argTypes, test.jsonArgs, &applicationArgs)
require.NoError(t, err)
require.Equal(t, test.expectedAppArgs, applicationArgs)
diff --git a/data/abi/abi_json_test.go b/data/abi/abi_json_test.go
index 49083fdea..0c3a006e3 100644
--- a/data/abi/abi_json_test.go
+++ b/data/abi/abi_json_test.go
@@ -31,14 +31,17 @@ func TestRandomAddressEquality(t *testing.T) {
upperLimit := new(big.Int).Lsh(big.NewInt(1), addressByteSize<<3)
var addrBasics basics.Address
- var addrABI []byte = make([]byte, addressByteSize)
+ var addrABI = make([]byte, addressByteSize)
for testCaseIndex := 0; testCaseIndex < addressTestCaseCount; testCaseIndex++ {
randomAddrInt, err := rand.Int(rand.Reader, upperLimit)
require.NoError(t, err, "cryptographic random int init fail")
- randomAddrInt.FillBytes(addrBasics[:])
- randomAddrInt.FillBytes(addrABI)
+ expected, err := bigIntToBytes(randomAddrInt, uint(addressByteSize))
+ require.NoError(t, err, "big int to byte conversion error")
+
+ copy(addrABI[:], expected)
+ copy(addrBasics[:], expected)
checkSumBasics := addrBasics.GetChecksum()
checkSumABI, err := addressCheckSum(addrABI)