github.com/klaytn/klaytn@v1.12.1/accounts/abi/type_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from accounts/abi/type_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package abi 22 23 import ( 24 "math/big" 25 "reflect" 26 "testing" 27 28 "github.com/davecgh/go-spew/spew" 29 "github.com/klaytn/klaytn/common" 30 ) 31 32 // typeWithoutStringer is a alias for the Type type which simply doesn't implement 33 // the stringer interface to allow printing type details in the tests below. 34 type typeWithoutStringer Type 35 36 // Tests that all allowed types get recognized by the type parser. 37 func TestTypeRegexp(t *testing.T) { 38 tests := []struct { 39 blob string 40 components []ArgumentMarshaling 41 kind Type 42 }{ 43 {"bool", nil, Type{T: BoolTy, stringKind: "bool"}}, 44 {"bool[]", nil, Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}}, 45 {"bool[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}}, 46 {"bool[2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, 47 {"bool[][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, 48 {"bool[][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, 49 {"bool[2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, 50 {"bool[2][][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, 51 {"bool[2][2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, 52 {"bool[][][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, 53 {"bool[][2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}}, 54 {"int8", nil, Type{Size: 8, T: IntTy, stringKind: "int8"}}, 55 {"int16", nil, Type{Size: 16, T: IntTy, stringKind: "int16"}}, 56 {"int32", nil, Type{Size: 32, T: IntTy, stringKind: "int32"}}, 57 {"int64", nil, Type{Size: 64, T: IntTy, stringKind: "int64"}}, 58 {"int256", nil, Type{Size: 256, T: IntTy, stringKind: "int256"}}, 59 {"int8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, 60 {"int8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, 61 {"int16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, 62 {"int16[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, 63 {"int32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, 64 {"int32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, 65 {"int64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, 66 {"int64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, 67 {"int256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, 68 {"int256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, 69 {"uint8", nil, Type{Size: 8, T: UintTy, stringKind: "uint8"}}, 70 {"uint16", nil, Type{Size: 16, T: UintTy, stringKind: "uint16"}}, 71 {"uint32", nil, Type{Size: 32, T: UintTy, stringKind: "uint32"}}, 72 {"uint64", nil, Type{Size: 64, T: UintTy, stringKind: "uint64"}}, 73 {"uint256", nil, Type{Size: 256, T: UintTy, stringKind: "uint256"}}, 74 {"uint8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, 75 {"uint8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, 76 {"uint16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, 77 {"uint16[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, 78 {"uint32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, 79 {"uint32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, 80 {"uint64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, 81 {"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, 82 {"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, 83 {"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, 84 {"bytes32", nil, Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}}, 85 {"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, 86 {"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}}, 87 {"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, 88 {"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, 89 {"string", nil, Type{T: StringTy, stringKind: "string"}}, 90 {"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, 91 {"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}}, 92 {"address", nil, Type{Size: 20, T: AddressTy, stringKind: "address"}}, 93 {"address[]", nil, Type{T: SliceTy, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, 94 {"address[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, 95 // TODO when fixed types are implemented properly 96 // {"fixed", nil, Type{}}, 97 // {"fixed128x128", nil, Type{}}, 98 // {"fixed[]", nil, Type{}}, 99 // {"fixed[2]", nil, Type{}}, 100 // {"fixed128x128[]", nil, Type{}}, 101 // {"fixed128x128[2]", nil, Type{}}, 102 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{ 103 T: TupleTy, TupleType: reflect.TypeOf(struct { 104 A int64 `json:"a"` 105 }{}), stringKind: "(int64)", 106 TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}, 107 }}, 108 {"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{ 109 T: TupleTy, TupleType: reflect.TypeOf(struct { 110 ATypicalParamName int64 `json:"aTypicalParamName"` 111 }{}), stringKind: "(int64)", 112 TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}, 113 }}, 114 } 115 116 for _, tt := range tests { 117 typ, err := NewType(tt.blob, "", tt.components) 118 if err != nil { 119 t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) 120 } 121 if !reflect.DeepEqual(typ, tt.kind) { 122 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) 123 } 124 } 125 } 126 127 func TestTypeCheck(t *testing.T) { 128 for i, test := range []struct { 129 typ string 130 components []ArgumentMarshaling 131 input interface{} 132 err string 133 }{ 134 {"uint", nil, big.NewInt(1), "unsupported arg type: uint"}, 135 {"int", nil, big.NewInt(1), "unsupported arg type: int"}, 136 {"uint256", nil, big.NewInt(1), ""}, 137 {"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""}, 138 {"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""}, 139 {"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""}, 140 {"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""}, 141 {"uint8[][]", nil, [][]uint8{}, ""}, 142 {"int256", nil, big.NewInt(1), ""}, 143 {"uint8", nil, uint8(1), ""}, 144 {"uint16", nil, uint16(1), ""}, 145 {"uint32", nil, uint32(1), ""}, 146 {"uint64", nil, uint64(1), ""}, 147 {"int8", nil, int8(1), ""}, 148 {"int16", nil, int16(1), ""}, 149 {"int32", nil, int32(1), ""}, 150 {"int64", nil, int64(1), ""}, 151 {"uint24", nil, big.NewInt(1), ""}, 152 {"uint40", nil, big.NewInt(1), ""}, 153 {"uint48", nil, big.NewInt(1), ""}, 154 {"uint56", nil, big.NewInt(1), ""}, 155 {"uint72", nil, big.NewInt(1), ""}, 156 {"uint80", nil, big.NewInt(1), ""}, 157 {"uint88", nil, big.NewInt(1), ""}, 158 {"uint96", nil, big.NewInt(1), ""}, 159 {"uint104", nil, big.NewInt(1), ""}, 160 {"uint112", nil, big.NewInt(1), ""}, 161 {"uint120", nil, big.NewInt(1), ""}, 162 {"uint128", nil, big.NewInt(1), ""}, 163 {"uint136", nil, big.NewInt(1), ""}, 164 {"uint144", nil, big.NewInt(1), ""}, 165 {"uint152", nil, big.NewInt(1), ""}, 166 {"uint160", nil, big.NewInt(1), ""}, 167 {"uint168", nil, big.NewInt(1), ""}, 168 {"uint176", nil, big.NewInt(1), ""}, 169 {"uint184", nil, big.NewInt(1), ""}, 170 {"uint192", nil, big.NewInt(1), ""}, 171 {"uint200", nil, big.NewInt(1), ""}, 172 {"uint208", nil, big.NewInt(1), ""}, 173 {"uint216", nil, big.NewInt(1), ""}, 174 {"uint224", nil, big.NewInt(1), ""}, 175 {"uint232", nil, big.NewInt(1), ""}, 176 {"uint240", nil, big.NewInt(1), ""}, 177 {"uint248", nil, big.NewInt(1), ""}, 178 {"int24", nil, big.NewInt(1), ""}, 179 {"int40", nil, big.NewInt(1), ""}, 180 {"int48", nil, big.NewInt(1), ""}, 181 {"int56", nil, big.NewInt(1), ""}, 182 {"int72", nil, big.NewInt(1), ""}, 183 {"int80", nil, big.NewInt(1), ""}, 184 {"int88", nil, big.NewInt(1), ""}, 185 {"int96", nil, big.NewInt(1), ""}, 186 {"int104", nil, big.NewInt(1), ""}, 187 {"int112", nil, big.NewInt(1), ""}, 188 {"int120", nil, big.NewInt(1), ""}, 189 {"int128", nil, big.NewInt(1), ""}, 190 {"int136", nil, big.NewInt(1), ""}, 191 {"int144", nil, big.NewInt(1), ""}, 192 {"int152", nil, big.NewInt(1), ""}, 193 {"int160", nil, big.NewInt(1), ""}, 194 {"int168", nil, big.NewInt(1), ""}, 195 {"int176", nil, big.NewInt(1), ""}, 196 {"int184", nil, big.NewInt(1), ""}, 197 {"int192", nil, big.NewInt(1), ""}, 198 {"int200", nil, big.NewInt(1), ""}, 199 {"int208", nil, big.NewInt(1), ""}, 200 {"int216", nil, big.NewInt(1), ""}, 201 {"int224", nil, big.NewInt(1), ""}, 202 {"int232", nil, big.NewInt(1), ""}, 203 {"int240", nil, big.NewInt(1), ""}, 204 {"int248", nil, big.NewInt(1), ""}, 205 {"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"}, 206 {"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, 207 {"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, 208 {"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, 209 {"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"}, 210 {"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"}, 211 {"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"}, 212 {"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"}, 213 {"uint16", nil, uint16(1), ""}, 214 {"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, 215 {"uint16[]", nil, []uint16{1, 2, 3}, ""}, 216 {"uint16[]", nil, [3]uint16{1, 2, 3}, ""}, 217 {"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, 218 {"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, 219 {"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 220 {"uint16[3]", nil, []uint16{1, 2, 3}, ""}, 221 {"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 222 {"address[]", nil, []common.Address{{1}}, ""}, 223 {"address[1]", nil, []common.Address{{1}}, ""}, 224 {"address[1]", nil, [1]common.Address{{1}}, ""}, 225 {"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, 226 {"bytes32", nil, [32]byte{}, ""}, 227 {"bytes31", nil, [31]byte{}, ""}, 228 {"bytes30", nil, [30]byte{}, ""}, 229 {"bytes29", nil, [29]byte{}, ""}, 230 {"bytes28", nil, [28]byte{}, ""}, 231 {"bytes27", nil, [27]byte{}, ""}, 232 {"bytes26", nil, [26]byte{}, ""}, 233 {"bytes25", nil, [25]byte{}, ""}, 234 {"bytes24", nil, [24]byte{}, ""}, 235 {"bytes23", nil, [23]byte{}, ""}, 236 {"bytes22", nil, [22]byte{}, ""}, 237 {"bytes21", nil, [21]byte{}, ""}, 238 {"bytes20", nil, [20]byte{}, ""}, 239 {"bytes19", nil, [19]byte{}, ""}, 240 {"bytes18", nil, [18]byte{}, ""}, 241 {"bytes17", nil, [17]byte{}, ""}, 242 {"bytes16", nil, [16]byte{}, ""}, 243 {"bytes15", nil, [15]byte{}, ""}, 244 {"bytes14", nil, [14]byte{}, ""}, 245 {"bytes13", nil, [13]byte{}, ""}, 246 {"bytes12", nil, [12]byte{}, ""}, 247 {"bytes11", nil, [11]byte{}, ""}, 248 {"bytes10", nil, [10]byte{}, ""}, 249 {"bytes9", nil, [9]byte{}, ""}, 250 {"bytes8", nil, [8]byte{}, ""}, 251 {"bytes7", nil, [7]byte{}, ""}, 252 {"bytes6", nil, [6]byte{}, ""}, 253 {"bytes5", nil, [5]byte{}, ""}, 254 {"bytes4", nil, [4]byte{}, ""}, 255 {"bytes3", nil, [3]byte{}, ""}, 256 {"bytes2", nil, [2]byte{}, ""}, 257 {"bytes1", nil, [1]byte{}, ""}, 258 {"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, 259 {"bytes32", nil, common.Hash{1}, ""}, 260 {"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, 261 {"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, 262 {"bytes", nil, []byte{0, 1}, ""}, 263 {"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, 264 {"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"}, 265 {"string", nil, "hello world", ""}, 266 {"string", nil, string(""), ""}, 267 {"string", nil, []byte{}, "abi: cannot use slice as type string as argument"}, 268 {"bytes32[]", nil, [][32]byte{{}}, ""}, 269 {"function", nil, [24]byte{}, ""}, 270 {"bytes20", nil, common.Address{}, ""}, 271 {"address", nil, [20]byte{}, ""}, 272 {"address", nil, common.Address{}, ""}, 273 {"bytes32[]]", nil, "", "invalid arg type in abi"}, 274 {"invalidType", nil, "", "unsupported arg type: invalidType"}, 275 {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, 276 // simple tuple 277 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { 278 A *big.Int 279 B *big.Int 280 }{}, ""}, 281 // tuple slice 282 {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 283 A *big.Int 284 B *big.Int 285 }{}, ""}, 286 // tuple array 287 {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 288 A *big.Int 289 B *big.Int 290 }{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, 291 } { 292 typ, err := NewType(test.typ, "", test.components) 293 if err != nil && len(test.err) == 0 { 294 t.Fatal("unexpected parse error:", err) 295 } else if err != nil && len(test.err) != 0 { 296 if err.Error() != test.err { 297 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 298 } 299 continue 300 } 301 302 err = typeCheck(typ, reflect.ValueOf(test.input)) 303 if err != nil && len(test.err) == 0 { 304 t.Errorf("%d failed. Expected no err but got: %v", i, err) 305 continue 306 } 307 if err == nil && len(test.err) != 0 { 308 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 309 continue 310 } 311 312 if err != nil && len(test.err) != 0 && err.Error() != test.err { 313 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 314 } 315 } 316 } 317 318 func TestInternalType(t *testing.T) { 319 components := []ArgumentMarshaling{{Name: "a", Type: "int64"}} 320 internalType := "struct a.b[]" 321 kind := Type{ 322 T: TupleTy, 323 TupleType: reflect.TypeOf(struct { 324 A int64 `json:"a"` 325 }{}), 326 stringKind: "(int64)", 327 TupleRawName: "ab[]", 328 TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, 329 TupleRawNames: []string{"a"}, 330 } 331 332 blob := "tuple" 333 typ, err := NewType(blob, internalType, components) 334 if err != nil { 335 t.Errorf("type %q: failed to parse type string: %v", blob, err) 336 } 337 if !reflect.DeepEqual(typ, kind) { 338 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(kind))) 339 } 340 } 341 342 func TestGetTypeSize(t *testing.T) { 343 testCases := []struct { 344 typ string 345 components []ArgumentMarshaling 346 typSize int 347 }{ 348 // simple array 349 {"uint256[2]", nil, 32 * 2}, 350 {"address[3]", nil, 32 * 3}, 351 {"bytes32[4]", nil, 32 * 4}, 352 // array array 353 {"uint256[2][3][4]", nil, 32 * (2 * 3 * 4)}, 354 // array tuple 355 {"tuple[2]", []ArgumentMarshaling{{Name: "x", Type: "bytes32"}, {Name: "y", Type: "bytes32"}}, (32 * 2) * 2}, 356 // simple tuple 357 {"tuple", []ArgumentMarshaling{{Name: "x", Type: "uint256"}, {Name: "y", Type: "uint256"}}, 32 * 2}, 358 // tuple array 359 {"tuple", []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}}, 32 * 2}, 360 // tuple tuple 361 {"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32"}}}}, 32}, 362 {"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}, {Name: "y", Type: "uint256"}}}}, 32 * (2 + 1)}, 363 } 364 365 for i, data := range testCases { 366 typ, err := NewType(data.typ, "", data.components) 367 if err != nil { 368 t.Errorf("type %q: failed to parse type string: %v", data.typ, err) 369 } 370 371 result := getTypeSize(typ) 372 if result != data.typSize { 373 t.Errorf("case %d type %q: get type size error: actual: %d expected: %d", i, data.typ, result, data.typSize) 374 } 375 } 376 }