github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/abi/pack_test.go (about) 1 package abi 2 3 import ( 4 "bytes" 5 "math" 6 "math/big" 7 "reflect" 8 "strings" 9 "testing" 10 11 "github.com/neatlab/neatio/utilities/common" 12 ) 13 14 func TestPack(t *testing.T) { 15 for i, test := range []struct { 16 typ string 17 components []ArgumentMarshaling 18 input interface{} 19 output []byte 20 }{ 21 { 22 "uint8", 23 nil, 24 uint8(2), 25 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 26 }, 27 { 28 "uint8[]", 29 nil, 30 []uint8{1, 2}, 31 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 32 }, 33 { 34 "uint16", 35 nil, 36 uint16(2), 37 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 38 }, 39 { 40 "uint16[]", 41 nil, 42 []uint16{1, 2}, 43 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 44 }, 45 { 46 "uint32", 47 nil, 48 uint32(2), 49 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 50 }, 51 { 52 "uint32[]", 53 nil, 54 []uint32{1, 2}, 55 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 56 }, 57 { 58 "uint64", 59 nil, 60 uint64(2), 61 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 62 }, 63 { 64 "uint64[]", 65 nil, 66 []uint64{1, 2}, 67 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 68 }, 69 { 70 "uint256", 71 nil, 72 big.NewInt(2), 73 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 74 }, 75 { 76 "uint256[]", 77 nil, 78 []*big.Int{big.NewInt(1), big.NewInt(2)}, 79 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 80 }, 81 { 82 "int8", 83 nil, 84 int8(2), 85 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 86 }, 87 { 88 "int8[]", 89 nil, 90 []int8{1, 2}, 91 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 92 }, 93 { 94 "int16", 95 nil, 96 int16(2), 97 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 98 }, 99 { 100 "int16[]", 101 nil, 102 []int16{1, 2}, 103 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 104 }, 105 { 106 "int32", 107 nil, 108 int32(2), 109 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 110 }, 111 { 112 "int32[]", 113 nil, 114 []int32{1, 2}, 115 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 116 }, 117 { 118 "int64", 119 nil, 120 int64(2), 121 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 122 }, 123 { 124 "int64[]", 125 nil, 126 []int64{1, 2}, 127 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 128 }, 129 { 130 "int256", 131 nil, 132 big.NewInt(2), 133 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), 134 }, 135 { 136 "int256[]", 137 nil, 138 []*big.Int{big.NewInt(1), big.NewInt(2)}, 139 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), 140 }, 141 { 142 "bytes1", 143 nil, 144 [1]byte{1}, 145 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 146 }, 147 { 148 "bytes2", 149 nil, 150 [2]byte{1}, 151 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 152 }, 153 { 154 "bytes3", 155 nil, 156 [3]byte{1}, 157 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 158 }, 159 { 160 "bytes4", 161 nil, 162 [4]byte{1}, 163 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 164 }, 165 { 166 "bytes5", 167 nil, 168 [5]byte{1}, 169 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 170 }, 171 { 172 "bytes6", 173 nil, 174 [6]byte{1}, 175 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 176 }, 177 { 178 "bytes7", 179 nil, 180 [7]byte{1}, 181 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 182 }, 183 { 184 "bytes8", 185 nil, 186 [8]byte{1}, 187 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 188 }, 189 { 190 "bytes9", 191 nil, 192 [9]byte{1}, 193 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 194 }, 195 { 196 "bytes10", 197 nil, 198 [10]byte{1}, 199 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 200 }, 201 { 202 "bytes11", 203 nil, 204 [11]byte{1}, 205 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 206 }, 207 { 208 "bytes12", 209 nil, 210 [12]byte{1}, 211 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 212 }, 213 { 214 "bytes13", 215 nil, 216 [13]byte{1}, 217 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 218 }, 219 { 220 "bytes14", 221 nil, 222 [14]byte{1}, 223 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 224 }, 225 { 226 "bytes15", 227 nil, 228 [15]byte{1}, 229 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 230 }, 231 { 232 "bytes16", 233 nil, 234 [16]byte{1}, 235 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 236 }, 237 { 238 "bytes17", 239 nil, 240 [17]byte{1}, 241 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 242 }, 243 { 244 "bytes18", 245 nil, 246 [18]byte{1}, 247 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 248 }, 249 { 250 "bytes19", 251 nil, 252 [19]byte{1}, 253 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 254 }, 255 { 256 "bytes20", 257 nil, 258 [20]byte{1}, 259 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 260 }, 261 { 262 "bytes21", 263 nil, 264 [21]byte{1}, 265 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 266 }, 267 { 268 "bytes22", 269 nil, 270 [22]byte{1}, 271 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 272 }, 273 { 274 "bytes23", 275 nil, 276 [23]byte{1}, 277 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 278 }, 279 { 280 "bytes24", 281 nil, 282 [24]byte{1}, 283 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 284 }, 285 { 286 "bytes25", 287 nil, 288 [25]byte{1}, 289 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 290 }, 291 { 292 "bytes26", 293 nil, 294 [26]byte{1}, 295 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 296 }, 297 { 298 "bytes27", 299 nil, 300 [27]byte{1}, 301 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 302 }, 303 { 304 "bytes28", 305 nil, 306 [28]byte{1}, 307 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 308 }, 309 { 310 "bytes29", 311 nil, 312 [29]byte{1}, 313 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 314 }, 315 { 316 "bytes30", 317 nil, 318 [30]byte{1}, 319 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 320 }, 321 { 322 "bytes31", 323 nil, 324 [31]byte{1}, 325 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 326 }, 327 { 328 "bytes32", 329 nil, 330 [32]byte{1}, 331 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 332 }, 333 { 334 "uint32[2][3][4]", 335 nil, 336 [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, 337 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"), 338 }, 339 { 340 "address[]", 341 nil, 342 []common.Address{{1}, {2}}, 343 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), 344 }, 345 { 346 "bytes32[]", 347 nil, 348 []common.Hash{{1}, {2}}, 349 common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), 350 }, 351 { 352 "function", 353 nil, 354 [24]byte{1}, 355 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 356 }, 357 { 358 "string", 359 nil, 360 "foobar", 361 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), 362 }, 363 { 364 "string[]", 365 nil, 366 []string{"hello", "foobar"}, 367 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + 368 "0000000000000000000000000000000000000000000000000000000000000040" + 369 "0000000000000000000000000000000000000000000000000000000000000080" + 370 "0000000000000000000000000000000000000000000000000000000000000005" + 371 "68656c6c6f000000000000000000000000000000000000000000000000000000" + 372 "0000000000000000000000000000000000000000000000000000000000000006" + 373 "666f6f6261720000000000000000000000000000000000000000000000000000"), 374 }, 375 { 376 "string[2]", 377 nil, 378 []string{"hello", "foobar"}, 379 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + 380 "0000000000000000000000000000000000000000000000000000000000000080" + 381 "0000000000000000000000000000000000000000000000000000000000000005" + 382 "68656c6c6f000000000000000000000000000000000000000000000000000000" + 383 "0000000000000000000000000000000000000000000000000000000000000006" + 384 "666f6f6261720000000000000000000000000000000000000000000000000000"), 385 }, 386 { 387 "bytes32[][]", 388 nil, 389 [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, 390 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + 391 "0000000000000000000000000000000000000000000000000000000000000040" + 392 "00000000000000000000000000000000000000000000000000000000000000a0" + 393 "0000000000000000000000000000000000000000000000000000000000000002" + 394 "0100000000000000000000000000000000000000000000000000000000000000" + 395 "0200000000000000000000000000000000000000000000000000000000000000" + 396 "0000000000000000000000000000000000000000000000000000000000000003" + 397 "0300000000000000000000000000000000000000000000000000000000000000" + 398 "0400000000000000000000000000000000000000000000000000000000000000" + 399 "0500000000000000000000000000000000000000000000000000000000000000"), 400 }, 401 402 { 403 "bytes32[][2]", 404 nil, 405 [][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}}, 406 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + 407 "00000000000000000000000000000000000000000000000000000000000000a0" + 408 "0000000000000000000000000000000000000000000000000000000000000002" + 409 "0100000000000000000000000000000000000000000000000000000000000000" + 410 "0200000000000000000000000000000000000000000000000000000000000000" + 411 "0000000000000000000000000000000000000000000000000000000000000003" + 412 "0300000000000000000000000000000000000000000000000000000000000000" + 413 "0400000000000000000000000000000000000000000000000000000000000000" + 414 "0500000000000000000000000000000000000000000000000000000000000000"), 415 }, 416 417 { 418 "bytes32[3][2]", 419 nil, 420 [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, 421 common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + 422 "0200000000000000000000000000000000000000000000000000000000000000" + 423 "0300000000000000000000000000000000000000000000000000000000000000" + 424 "0300000000000000000000000000000000000000000000000000000000000000" + 425 "0400000000000000000000000000000000000000000000000000000000000000" + 426 "0500000000000000000000000000000000000000000000000000000000000000"), 427 }, 428 { 429 430 "tuple", 431 []ArgumentMarshaling{ 432 {Name: "a", Type: "int64"}, 433 {Name: "b", Type: "int256"}, 434 {Name: "c", Type: "int256"}, 435 {Name: "d", Type: "bool"}, 436 {Name: "e", Type: "bytes32[3][2]"}, 437 }, 438 struct { 439 A int64 440 B *big.Int 441 C *big.Int 442 D bool 443 E [][]common.Hash 444 }{1, big.NewInt(1), big.NewInt(-1), true, [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, 445 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001" + 446 "0000000000000000000000000000000000000000000000000000000000000001" + 447 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + 448 "0000000000000000000000000000000000000000000000000000000000000001" + 449 "0100000000000000000000000000000000000000000000000000000000000000" + 450 "0200000000000000000000000000000000000000000000000000000000000000" + 451 "0300000000000000000000000000000000000000000000000000000000000000" + 452 "0300000000000000000000000000000000000000000000000000000000000000" + 453 "0400000000000000000000000000000000000000000000000000000000000000" + 454 "0500000000000000000000000000000000000000000000000000000000000000"), 455 }, 456 { 457 458 "tuple", 459 []ArgumentMarshaling{ 460 {Name: "a", Type: "string"}, 461 {Name: "b", Type: "int64"}, 462 {Name: "c", Type: "bytes"}, 463 {Name: "d", Type: "string[]"}, 464 {Name: "e", Type: "int256[]"}, 465 {Name: "f", Type: "address[]"}, 466 }, 467 struct { 468 FieldA string `abi:"a"` 469 FieldB int64 `abi:"b"` 470 C []byte 471 D []string 472 E []*big.Int 473 F []common.Address 474 }{"foobar", 1, []byte{1}, []string{"foo", "bar"}, []*big.Int{big.NewInt(1), big.NewInt(-1)}, []common.Address{{1}, {2}}}, 475 common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0" + 476 "0000000000000000000000000000000000000000000000000000000000000001" + 477 "0000000000000000000000000000000000000000000000000000000000000100" + 478 "0000000000000000000000000000000000000000000000000000000000000140" + 479 "0000000000000000000000000000000000000000000000000000000000000220" + 480 "0000000000000000000000000000000000000000000000000000000000000280" + 481 "0000000000000000000000000000000000000000000000000000000000000006" + 482 "666f6f6261720000000000000000000000000000000000000000000000000000" + 483 "0000000000000000000000000000000000000000000000000000000000000001" + 484 "0100000000000000000000000000000000000000000000000000000000000000" + 485 "0000000000000000000000000000000000000000000000000000000000000002" + 486 "0000000000000000000000000000000000000000000000000000000000000040" + 487 "0000000000000000000000000000000000000000000000000000000000000080" + 488 "0000000000000000000000000000000000000000000000000000000000000003" + 489 "666f6f0000000000000000000000000000000000000000000000000000000000" + 490 "0000000000000000000000000000000000000000000000000000000000000003" + 491 "6261720000000000000000000000000000000000000000000000000000000000" + 492 "0000000000000000000000000000000000000000000000000000000000000002" + 493 "0000000000000000000000000000000000000000000000000000000000000001" + 494 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + 495 "0000000000000000000000000000000000000000000000000000000000000002" + 496 "0000000000000000000000000100000000000000000000000000000000000000" + 497 "0000000000000000000000000200000000000000000000000000000000000000"), 498 }, 499 { 500 501 "tuple", 502 []ArgumentMarshaling{ 503 {Name: "a", Type: "tuple", Components: []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256[]"}}}, 504 {Name: "b", Type: "int256[]"}, 505 }, 506 struct { 507 A struct { 508 FieldA *big.Int `abi:"a"` 509 B []*big.Int 510 } 511 B []*big.Int 512 }{ 513 A: struct { 514 FieldA *big.Int `abi:"a"` 515 B []*big.Int 516 }{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(0)}}, 517 B: []*big.Int{big.NewInt(1), big.NewInt(0)}}, 518 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + 519 "00000000000000000000000000000000000000000000000000000000000000e0" + 520 "0000000000000000000000000000000000000000000000000000000000000001" + 521 "0000000000000000000000000000000000000000000000000000000000000040" + 522 "0000000000000000000000000000000000000000000000000000000000000002" + 523 "0000000000000000000000000000000000000000000000000000000000000001" + 524 "0000000000000000000000000000000000000000000000000000000000000000" + 525 "0000000000000000000000000000000000000000000000000000000000000002" + 526 "0000000000000000000000000000000000000000000000000000000000000001" + 527 "0000000000000000000000000000000000000000000000000000000000000000"), 528 }, 529 { 530 531 "tuple[]", 532 []ArgumentMarshaling{ 533 {Name: "a", Type: "int256"}, 534 {Name: "b", Type: "int256[]"}, 535 }, 536 []struct { 537 A *big.Int 538 B []*big.Int 539 }{ 540 {big.NewInt(-1), []*big.Int{big.NewInt(1), big.NewInt(0)}}, 541 {big.NewInt(1), []*big.Int{big.NewInt(2), big.NewInt(-1)}}, 542 }, 543 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + 544 "0000000000000000000000000000000000000000000000000000000000000040" + 545 "00000000000000000000000000000000000000000000000000000000000000e0" + 546 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + 547 "0000000000000000000000000000000000000000000000000000000000000040" + 548 "0000000000000000000000000000000000000000000000000000000000000002" + 549 "0000000000000000000000000000000000000000000000000000000000000001" + 550 "0000000000000000000000000000000000000000000000000000000000000000" + 551 "0000000000000000000000000000000000000000000000000000000000000001" + 552 "0000000000000000000000000000000000000000000000000000000000000040" + 553 "0000000000000000000000000000000000000000000000000000000000000002" + 554 "0000000000000000000000000000000000000000000000000000000000000002" + 555 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 556 }, 557 { 558 559 "tuple[2]", 560 []ArgumentMarshaling{ 561 {Name: "a", Type: "int256"}, 562 {Name: "b", Type: "int256"}, 563 }, 564 [2]struct { 565 A *big.Int 566 B *big.Int 567 }{ 568 {big.NewInt(-1), big.NewInt(1)}, 569 {big.NewInt(1), big.NewInt(-1)}, 570 }, 571 common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + 572 "0000000000000000000000000000000000000000000000000000000000000001" + 573 "0000000000000000000000000000000000000000000000000000000000000001" + 574 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 575 }, 576 { 577 578 "tuple[2]", 579 []ArgumentMarshaling{ 580 {Name: "a", Type: "int256[]"}, 581 }, 582 [2]struct { 583 A []*big.Int 584 }{ 585 {[]*big.Int{big.NewInt(-1), big.NewInt(1)}}, 586 {[]*big.Int{big.NewInt(1), big.NewInt(-1)}}, 587 }, 588 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + 589 "00000000000000000000000000000000000000000000000000000000000000c0" + 590 "0000000000000000000000000000000000000000000000000000000000000020" + 591 "0000000000000000000000000000000000000000000000000000000000000002" + 592 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + 593 "0000000000000000000000000000000000000000000000000000000000000001" + 594 "0000000000000000000000000000000000000000000000000000000000000020" + 595 "0000000000000000000000000000000000000000000000000000000000000002" + 596 "0000000000000000000000000000000000000000000000000000000000000001" + 597 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), 598 }, 599 } { 600 typ, err := NewType(test.typ, "", test.components) 601 if err != nil { 602 t.Fatalf("%v failed. Unexpected parse error: %v", i, err) 603 } 604 output, err := typ.pack(reflect.ValueOf(test.input)) 605 if err != nil { 606 t.Fatalf("%v failed. Unexpected pack error: %v", i, err) 607 } 608 609 if !bytes.Equal(output, test.output) { 610 t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i, typ.String(), test.output, output) 611 } 612 } 613 } 614 615 func TestMethodPack(t *testing.T) { 616 abi, err := JSON(strings.NewReader(jsondata2)) 617 if err != nil { 618 t.Fatal(err) 619 } 620 621 sig := abi.Methods["slice"].ID() 622 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 623 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 624 625 packed, err := abi.Pack("slice", []uint32{1, 2}) 626 if err != nil { 627 t.Error(err) 628 } 629 630 if !bytes.Equal(packed, sig) { 631 t.Errorf("expected %x got %x", sig, packed) 632 } 633 634 var addrA, addrB = common.Address{1}, common.Address{2} 635 sig = abi.Methods["sliceAddress"].ID() 636 sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) 637 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 638 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 639 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 640 641 packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) 642 if err != nil { 643 t.Fatal(err) 644 } 645 if !bytes.Equal(packed, sig) { 646 t.Errorf("expected %x got %x", sig, packed) 647 } 648 649 var addrC, addrD = common.Address{3}, common.Address{4} 650 sig = abi.Methods["sliceMultiAddress"].ID() 651 sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) 652 sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) 653 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 654 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 655 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 656 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 657 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 658 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 659 660 packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) 661 if err != nil { 662 t.Fatal(err) 663 } 664 if !bytes.Equal(packed, sig) { 665 t.Errorf("expected %x got %x", sig, packed) 666 } 667 668 sig = abi.Methods["slice256"].ID() 669 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 670 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 671 672 packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) 673 if err != nil { 674 t.Error(err) 675 } 676 677 if !bytes.Equal(packed, sig) { 678 t.Errorf("expected %x got %x", sig, packed) 679 } 680 681 a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}} 682 sig = abi.Methods["nestedArray"].ID() 683 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 684 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 685 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 686 sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...) 687 sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) 688 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 689 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 690 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 691 packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD}) 692 if err != nil { 693 t.Fatal(err) 694 } 695 if !bytes.Equal(packed, sig) { 696 t.Errorf("expected %x got %x", sig, packed) 697 } 698 699 sig = abi.Methods["nestedArray2"].ID() 700 sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) 701 sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) 702 sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...) 703 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 704 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 705 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 706 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 707 packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}}) 708 if err != nil { 709 t.Fatal(err) 710 } 711 if !bytes.Equal(packed, sig) { 712 t.Errorf("expected %x got %x", sig, packed) 713 } 714 715 sig = abi.Methods["nestedSlice"].ID() 716 sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) 717 sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...) 718 sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) 719 sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) 720 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 721 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 722 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 723 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 724 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 725 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 726 packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}}) 727 if err != nil { 728 t.Fatal(err) 729 } 730 if !bytes.Equal(packed, sig) { 731 t.Errorf("expected %x got %x", sig, packed) 732 } 733 } 734 735 func TestPackNumber(t *testing.T) { 736 tests := []struct { 737 value reflect.Value 738 packed []byte 739 }{ 740 741 {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, 742 {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, 743 {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, 744 745 {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, 746 {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, 747 {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, 748 {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, 749 750 {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, 751 {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, 752 {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, 753 {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, 754 755 {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, 756 {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, 757 {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, 758 {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, 759 } 760 for i, tt := range tests { 761 packed := packNum(tt.value) 762 if !bytes.Equal(packed, tt.packed) { 763 t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) 764 } 765 } 766 }