github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/precompiles/abi_test.go (about) 1 package precompiles_test 2 3 import ( 4 "encoding/hex" 5 "math/big" 6 "testing" 7 8 gethCommon "github.com/onflow/go-ethereum/common" 9 "github.com/stretchr/testify/require" 10 11 "github.com/onflow/flow-go/fvm/evm/precompiles" 12 ) 13 14 func TestABIEncodingDecodingFunctions(t *testing.T) { 15 t.Parallel() 16 17 t.Run("test address", func(t *testing.T) { 18 encodedAddress, err := hex.DecodeString("000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564") 19 require.NoError(t, err) 20 addr, err := precompiles.ReadAddress(encodedAddress, 0) 21 require.NoError(t, err) 22 expectedAddress := gethCommon.HexToAddress("e592427a0aece92de3edee1f18e0157c05861564") 23 require.Equal(t, expectedAddress, addr) 24 reEncoded := make([]byte, precompiles.EncodedAddressSize) 25 err = precompiles.EncodeAddress(addr, reEncoded, 0) 26 require.NoError(t, err) 27 require.Equal(t, encodedAddress, reEncoded) 28 }) 29 30 t.Run("test boolean", func(t *testing.T) { 31 encodedBool, err := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000001") 32 require.NoError(t, err) 33 ret, err := precompiles.ReadBool(encodedBool, 0) 34 require.NoError(t, err) 35 require.True(t, ret) 36 reEncoded := make([]byte, precompiles.EncodedBoolSize) 37 err = precompiles.EncodeBool(ret, reEncoded, 0) 38 require.NoError(t, err) 39 require.Equal(t, encodedBool, reEncoded) 40 }) 41 42 t.Run("test uint64", func(t *testing.T) { 43 encodedUint64, err := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000046") 44 require.NoError(t, err) 45 ret, err := precompiles.ReadUint64(encodedUint64, 0) 46 require.NoError(t, err) 47 expectedUint64 := uint64(70) 48 require.Equal(t, expectedUint64, ret) 49 reEncoded := make([]byte, precompiles.EncodedUint64Size) 50 err = precompiles.EncodeUint64(ret, reEncoded, 0) 51 require.NoError(t, err) 52 require.Equal(t, encodedUint64, reEncoded) 53 54 }) 55 56 t.Run("test read uint256", func(t *testing.T) { 57 encodedUint256, err := hex.DecodeString("1000000000000000000000000000000000000000000000000000000000000046") 58 require.NoError(t, err) 59 ret, err := precompiles.ReadUint256(encodedUint256, 0) 60 require.NoError(t, err) 61 expectedValue, success := new(big.Int).SetString("7237005577332262213973186563042994240829374041602535252466099000494570602566", 10) 62 require.True(t, success) 63 require.Equal(t, expectedValue, ret) 64 }) 65 66 t.Run("test fixed size bytes", func(t *testing.T) { 67 encodedFixedSizeBytes, err := hex.DecodeString("abcdef1200000000000000000000000000000000000000000000000000000000") 68 require.NoError(t, err) 69 ret, err := precompiles.ReadBytes4(encodedFixedSizeBytes, 0) 70 require.NoError(t, err) 71 require.Equal(t, encodedFixedSizeBytes[0:4], ret) 72 73 ret, err = precompiles.ReadBytes8(encodedFixedSizeBytes, 0) 74 require.NoError(t, err) 75 require.Equal(t, encodedFixedSizeBytes[0:8], ret) 76 77 ret, err = precompiles.ReadBytes32(encodedFixedSizeBytes, 0) 78 require.NoError(t, err) 79 require.Equal(t, encodedFixedSizeBytes[0:32], ret) 80 81 reEncoded := make([]byte, precompiles.EncodedBytes32Size) 82 err = precompiles.EncodeBytes32(ret, reEncoded, 0) 83 require.NoError(t, err) 84 require.Equal(t, encodedFixedSizeBytes, reEncoded) 85 }) 86 87 t.Run("test read bytes (variable size)", func(t *testing.T) { 88 encodedData, err := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000") 89 require.NoError(t, err) 90 91 ret, err := precompiles.ReadBytes(encodedData, 0) 92 require.NoError(t, err) 93 expectedData, err := hex.DecodeString("48656c6c6f20576f726c64") 94 require.NoError(t, err) 95 require.Equal(t, expectedData, ret) 96 97 bufferSize := precompiles.SizeNeededForBytesEncoding(expectedData) 98 buffer := make([]byte, bufferSize) 99 err = precompiles.EncodeBytes(expectedData, buffer, 0, precompiles.EncodedUint64Size) 100 require.NoError(t, err) 101 require.Equal(t, encodedData, buffer) 102 }) 103 104 t.Run("test size needed for encoding bytes", func(t *testing.T) { 105 // len zero 106 data := []byte{} 107 ret := precompiles.SizeNeededForBytesEncoding(data) 108 offsetAndLenEncodingSize := precompiles.EncodedUint64Size + precompiles.EncodedUint64Size 109 expectedSize := offsetAndLenEncodingSize + precompiles.FixedSizeUnitDataReadSize 110 require.Equal(t, expectedSize, ret) 111 112 // data size 1 113 data = []byte{1} 114 ret = precompiles.SizeNeededForBytesEncoding(data) 115 expectedSize = offsetAndLenEncodingSize + precompiles.FixedSizeUnitDataReadSize 116 require.Equal(t, expectedSize, ret) 117 118 // data size 32 119 data = make([]byte, 32) 120 ret = precompiles.SizeNeededForBytesEncoding(data) 121 expectedSize = offsetAndLenEncodingSize + precompiles.FixedSizeUnitDataReadSize 122 require.Equal(t, expectedSize, ret) 123 124 // data size 33 125 data = make([]byte, 33) 126 ret = precompiles.SizeNeededForBytesEncoding(data) 127 expectedSize = offsetAndLenEncodingSize + precompiles.FixedSizeUnitDataReadSize*2 128 require.Equal(t, expectedSize, ret) 129 }) 130 131 }