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