github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/type_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:31</date> 10 //</624450063308951552> 11 12 13 package abi 14 15 import ( 16 "math/big" 17 "reflect" 18 "testing" 19 20 "github.com/davecgh/go-spew/spew" 21 "github.com/ethereum/go-ethereum/common" 22 ) 23 24 //typewithoutStringer是类型类型类型的别名,该类型只是不实现 25 //Stringer接口允许在下面的测试中打印类型详细信息。 26 type typeWithoutStringer Type 27 28 //测试类型分析器识别所有允许的类型。 29 func TestTypeRegexp(t *testing.T) { 30 tests := []struct { 31 blob string 32 components []ArgumentMarshaling 33 kind Type 34 }{ 35 {"bool", nil, Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}}, 36 {"bool[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool(nil)), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}}, 37 {"bool[2]", nil, Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}}, 38 {"bool[2][]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, 39 {"bool[][]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, 40 {"bool[][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, 41 {"bool[2][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, 42 {"bool[2][][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][][2]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, 43 {"bool[2][2][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, 44 {"bool[][][]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, 45 {"bool[][2][]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][2][]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}}, 46 {"int8", nil, Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}}, 47 {"int16", nil, Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}}, 48 {"int32", nil, Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}}, 49 {"int64", nil, Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}}, 50 {"int256", nil, Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}}, 51 {"int8[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, 52 {"int8[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, 53 {"int16[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, 54 {"int16[2]", nil, Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, 55 {"int32[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, 56 {"int32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, 57 {"int64[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, 58 {"int64[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, 59 {"int256[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, 60 {"int256[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, 61 {"uint8", nil, Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}}, 62 {"uint16", nil, Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}}, 63 {"uint32", nil, Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}}, 64 {"uint64", nil, Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}}, 65 {"uint256", nil, Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}}, 66 {"uint8[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, 67 {"uint8[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, 68 {"uint16[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, 69 {"uint16[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, 70 {"uint32[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, 71 {"uint32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, 72 {"uint64[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, 73 {"uint64[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, 74 {"uint256[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, 75 {"uint256[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, 76 {"bytes32", nil, Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}}, 77 {"bytes[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]byte{}), Elem: &Type{Kind: reflect.Slice, Type: reflect.TypeOf([]byte{}), T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, 78 {"bytes[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]byte{}), Elem: &Type{T: BytesTy, Type: reflect.TypeOf([]byte{}), Kind: reflect.Slice, stringKind: "bytes"}, stringKind: "bytes[2]"}}, 79 {"bytes32[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][32]byte{}), Elem: &Type{Kind: reflect.Array, Type: reflect.TypeOf([32]byte{}), T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, 80 {"bytes32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][32]byte{}), Elem: &Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, 81 {"string", nil, Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}}, 82 {"string[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, 83 {"string[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]string{}), Elem: &Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}, stringKind: "string[2]"}}, 84 {"address", nil, Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}}, 85 {"address[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, 86 {"address[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, 87 //正确实现固定类型时的TODO 88 //“固定”,无,类型, 89 //“Fixed128x128”,无,类型, 90 //“固定[”,无,类型, 91 //“固定[2]”,无,类型, 92 // 93 //“Fixed128x128[2]”,无,类型, 94 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct{ A int64 }{}), stringKind: "(int64)", 95 TupleElems: []*Type{{Kind: reflect.Int64, T: IntTy, Type: reflect.TypeOf(int64(0)), Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}}, 96 } 97 98 for _, tt := range tests { 99 typ, err := NewType(tt.blob, tt.components) 100 if err != nil { 101 t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) 102 } 103 if !reflect.DeepEqual(typ, tt.kind) { 104 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) 105 } 106 } 107 } 108 109 func TestTypeCheck(t *testing.T) { 110 for i, test := range []struct { 111 typ string 112 components []ArgumentMarshaling 113 input interface{} 114 err string 115 }{ 116 {"uint", nil, big.NewInt(1), "unsupported arg type: uint"}, 117 {"int", nil, big.NewInt(1), "unsupported arg type: int"}, 118 {"uint256", nil, big.NewInt(1), ""}, 119 {"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""}, 120 {"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""}, 121 {"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""}, 122 {"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""}, 123 {"uint8[][]", nil, [][]uint8{}, ""}, 124 {"int256", nil, big.NewInt(1), ""}, 125 {"uint8", nil, uint8(1), ""}, 126 {"uint16", nil, uint16(1), ""}, 127 {"uint32", nil, uint32(1), ""}, 128 {"uint64", nil, uint64(1), ""}, 129 {"int8", nil, int8(1), ""}, 130 {"int16", nil, int16(1), ""}, 131 {"int32", nil, int32(1), ""}, 132 {"int64", nil, int64(1), ""}, 133 {"uint24", nil, big.NewInt(1), ""}, 134 {"uint40", nil, big.NewInt(1), ""}, 135 {"uint48", nil, big.NewInt(1), ""}, 136 {"uint56", nil, big.NewInt(1), ""}, 137 {"uint72", nil, big.NewInt(1), ""}, 138 {"uint80", nil, big.NewInt(1), ""}, 139 {"uint88", nil, big.NewInt(1), ""}, 140 {"uint96", nil, big.NewInt(1), ""}, 141 {"uint104", nil, big.NewInt(1), ""}, 142 {"uint112", nil, big.NewInt(1), ""}, 143 {"uint120", nil, big.NewInt(1), ""}, 144 {"uint128", nil, big.NewInt(1), ""}, 145 {"uint136", nil, big.NewInt(1), ""}, 146 {"uint144", nil, big.NewInt(1), ""}, 147 {"uint152", nil, big.NewInt(1), ""}, 148 {"uint160", nil, big.NewInt(1), ""}, 149 {"uint168", nil, big.NewInt(1), ""}, 150 {"uint176", nil, big.NewInt(1), ""}, 151 {"uint184", nil, big.NewInt(1), ""}, 152 {"uint192", nil, big.NewInt(1), ""}, 153 {"uint200", nil, big.NewInt(1), ""}, 154 {"uint208", nil, big.NewInt(1), ""}, 155 {"uint216", nil, big.NewInt(1), ""}, 156 {"uint224", nil, big.NewInt(1), ""}, 157 {"uint232", nil, big.NewInt(1), ""}, 158 {"uint240", nil, big.NewInt(1), ""}, 159 {"uint248", nil, big.NewInt(1), ""}, 160 {"int24", nil, big.NewInt(1), ""}, 161 {"int40", nil, big.NewInt(1), ""}, 162 {"int48", nil, big.NewInt(1), ""}, 163 {"int56", nil, big.NewInt(1), ""}, 164 {"int72", nil, big.NewInt(1), ""}, 165 {"int80", nil, big.NewInt(1), ""}, 166 {"int88", nil, big.NewInt(1), ""}, 167 {"int96", nil, big.NewInt(1), ""}, 168 {"int104", nil, big.NewInt(1), ""}, 169 {"int112", nil, big.NewInt(1), ""}, 170 {"int120", nil, big.NewInt(1), ""}, 171 {"int128", nil, big.NewInt(1), ""}, 172 {"int136", nil, big.NewInt(1), ""}, 173 {"int144", nil, big.NewInt(1), ""}, 174 {"int152", nil, big.NewInt(1), ""}, 175 {"int160", nil, big.NewInt(1), ""}, 176 {"int168", nil, big.NewInt(1), ""}, 177 {"int176", nil, big.NewInt(1), ""}, 178 {"int184", nil, big.NewInt(1), ""}, 179 {"int192", nil, big.NewInt(1), ""}, 180 {"int200", nil, big.NewInt(1), ""}, 181 {"int208", nil, big.NewInt(1), ""}, 182 {"int216", nil, big.NewInt(1), ""}, 183 {"int224", nil, big.NewInt(1), ""}, 184 {"int232", nil, big.NewInt(1), ""}, 185 {"int240", nil, big.NewInt(1), ""}, 186 {"int248", nil, big.NewInt(1), ""}, 187 {"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"}, 188 {"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, 189 {"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, 190 {"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, 191 {"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"}, 192 {"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"}, 193 {"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"}, 194 {"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"}, 195 {"uint16", nil, uint16(1), ""}, 196 {"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, 197 {"uint16[]", nil, []uint16{1, 2, 3}, ""}, 198 {"uint16[]", nil, [3]uint16{1, 2, 3}, ""}, 199 {"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, 200 {"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, 201 {"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 202 {"uint16[3]", nil, []uint16{1, 2, 3}, ""}, 203 {"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 204 {"address[]", nil, []common.Address{{1}}, ""}, 205 {"address[1]", nil, []common.Address{{1}}, ""}, 206 {"address[1]", nil, [1]common.Address{{1}}, ""}, 207 {"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, 208 {"bytes32", nil, [32]byte{}, ""}, 209 {"bytes31", nil, [31]byte{}, ""}, 210 {"bytes30", nil, [30]byte{}, ""}, 211 {"bytes29", nil, [29]byte{}, ""}, 212 {"bytes28", nil, [28]byte{}, ""}, 213 {"bytes27", nil, [27]byte{}, ""}, 214 {"bytes26", nil, [26]byte{}, ""}, 215 {"bytes25", nil, [25]byte{}, ""}, 216 {"bytes24", nil, [24]byte{}, ""}, 217 {"bytes23", nil, [23]byte{}, ""}, 218 {"bytes22", nil, [22]byte{}, ""}, 219 {"bytes21", nil, [21]byte{}, ""}, 220 {"bytes20", nil, [20]byte{}, ""}, 221 {"bytes19", nil, [19]byte{}, ""}, 222 {"bytes18", nil, [18]byte{}, ""}, 223 {"bytes17", nil, [17]byte{}, ""}, 224 {"bytes16", nil, [16]byte{}, ""}, 225 {"bytes15", nil, [15]byte{}, ""}, 226 {"bytes14", nil, [14]byte{}, ""}, 227 {"bytes13", nil, [13]byte{}, ""}, 228 {"bytes12", nil, [12]byte{}, ""}, 229 {"bytes11", nil, [11]byte{}, ""}, 230 {"bytes10", nil, [10]byte{}, ""}, 231 {"bytes9", nil, [9]byte{}, ""}, 232 {"bytes8", nil, [8]byte{}, ""}, 233 {"bytes7", nil, [7]byte{}, ""}, 234 {"bytes6", nil, [6]byte{}, ""}, 235 {"bytes5", nil, [5]byte{}, ""}, 236 {"bytes4", nil, [4]byte{}, ""}, 237 {"bytes3", nil, [3]byte{}, ""}, 238 {"bytes2", nil, [2]byte{}, ""}, 239 {"bytes1", nil, [1]byte{}, ""}, 240 {"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, 241 {"bytes32", nil, common.Hash{1}, ""}, 242 {"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, 243 {"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, 244 {"bytes", nil, []byte{0, 1}, ""}, 245 {"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, 246 {"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"}, 247 {"string", nil, "hello world", ""}, 248 {"string", nil, string(""), ""}, 249 {"string", nil, []byte{}, "abi: cannot use slice as type string as argument"}, 250 {"bytes32[]", nil, [][32]byte{{}}, ""}, 251 {"function", nil, [24]byte{}, ""}, 252 {"bytes20", nil, common.Address{}, ""}, 253 {"address", nil, [20]byte{}, ""}, 254 {"address", nil, common.Address{}, ""}, 255 {"bytes32[]]", nil, "", "invalid arg type in abi"}, 256 {"invalidType", nil, "", "unsupported arg type: invalidType"}, 257 {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, 258 //简单元组 259 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { 260 A *big.Int 261 B *big.Int 262 }{}, ""}, 263 //元组切片 264 {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 265 A *big.Int 266 B *big.Int 267 }{}, ""}, 268 //元组数组 269 {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 270 A *big.Int 271 B *big.Int 272 }{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, 273 } { 274 typ, err := NewType(test.typ, test.components) 275 if err != nil && len(test.err) == 0 { 276 t.Fatal("unexpected parse error:", err) 277 } else if err != nil && len(test.err) != 0 { 278 if err.Error() != test.err { 279 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 280 } 281 continue 282 } 283 284 err = typeCheck(typ, reflect.ValueOf(test.input)) 285 if err != nil && len(test.err) == 0 { 286 t.Errorf("%d failed. Expected no err but got: %v", i, err) 287 continue 288 } 289 if err == nil && len(test.err) != 0 { 290 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 291 continue 292 } 293 294 if err != nil && len(test.err) != 0 && err.Error() != test.err { 295 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 296 } 297 } 298 } 299