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