pack_test.go 31 KB

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