unpack_test.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. "encoding/hex"
  20. "fmt"
  21. "math/big"
  22. "reflect"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/stretchr/testify/require"
  28. )
  29. type unpackTest struct {
  30. def string // ABI definition JSON
  31. enc string // evm return data
  32. want interface{} // the expected output
  33. err string // empty or error if expected
  34. }
  35. func (test unpackTest) checkError(err error) error {
  36. if err != nil {
  37. if len(test.err) == 0 {
  38. return fmt.Errorf("expected no err but got: %v", err)
  39. } else if err.Error() != test.err {
  40. return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
  41. }
  42. } else if len(test.err) > 0 {
  43. return fmt.Errorf("expected err: %v but got none", test.err)
  44. }
  45. return nil
  46. }
  47. var unpackTests = []unpackTest{
  48. {
  49. def: `[{ "type": "bool" }]`,
  50. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  51. want: true,
  52. },
  53. {
  54. def: `[{ "type": "bool" }]`,
  55. enc: "0000000000000000000000000000000000000000000000000000000000000000",
  56. want: false,
  57. },
  58. {
  59. def: `[{ "type": "bool" }]`,
  60. enc: "0000000000000000000000000000000000000000000000000001000000000001",
  61. want: false,
  62. err: "abi: improperly encoded boolean value",
  63. },
  64. {
  65. def: `[{ "type": "bool" }]`,
  66. enc: "0000000000000000000000000000000000000000000000000000000000000003",
  67. want: false,
  68. err: "abi: improperly encoded boolean value",
  69. },
  70. {
  71. def: `[{"type": "uint32"}]`,
  72. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  73. want: uint32(1),
  74. },
  75. {
  76. def: `[{"type": "uint32"}]`,
  77. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  78. want: uint16(0),
  79. err: "abi: cannot unmarshal uint32 in to uint16",
  80. },
  81. {
  82. def: `[{"type": "uint17"}]`,
  83. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  84. want: uint16(0),
  85. err: "abi: cannot unmarshal *big.Int in to uint16",
  86. },
  87. {
  88. def: `[{"type": "uint17"}]`,
  89. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  90. want: big.NewInt(1),
  91. },
  92. {
  93. def: `[{"type": "int32"}]`,
  94. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  95. want: int32(1),
  96. },
  97. {
  98. def: `[{"type": "int32"}]`,
  99. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  100. want: int16(0),
  101. err: "abi: cannot unmarshal int32 in to int16",
  102. },
  103. {
  104. def: `[{"type": "int17"}]`,
  105. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  106. want: int16(0),
  107. err: "abi: cannot unmarshal *big.Int in to int16",
  108. },
  109. {
  110. def: `[{"type": "int17"}]`,
  111. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  112. want: big.NewInt(1),
  113. },
  114. {
  115. def: `[{"type": "int256"}]`,
  116. enc: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  117. want: big.NewInt(-1),
  118. },
  119. {
  120. def: `[{"type": "address"}]`,
  121. enc: "0000000000000000000000000100000000000000000000000000000000000000",
  122. want: common.Address{1},
  123. },
  124. {
  125. def: `[{"type": "bytes32"}]`,
  126. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  127. want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  128. },
  129. {
  130. def: `[{"type": "bytes"}]`,
  131. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  132. want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  133. },
  134. {
  135. def: `[{"type": "bytes"}]`,
  136. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  137. want: [32]byte{},
  138. err: "abi: cannot unmarshal []uint8 in to [32]uint8",
  139. },
  140. {
  141. def: `[{"type": "bytes32"}]`,
  142. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  143. want: []byte(nil),
  144. err: "abi: cannot unmarshal [32]uint8 in to []uint8",
  145. },
  146. {
  147. def: `[{"type": "bytes32"}]`,
  148. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  149. want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  150. },
  151. {
  152. def: `[{"type": "function"}]`,
  153. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  154. want: [24]byte{1},
  155. },
  156. // slices
  157. {
  158. def: `[{"type": "uint8[]"}]`,
  159. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  160. want: []uint8{1, 2},
  161. },
  162. {
  163. def: `[{"type": "uint8[2]"}]`,
  164. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  165. want: [2]uint8{1, 2},
  166. },
  167. // multi dimensional, if these pass, all types that don't require length prefix should pass
  168. {
  169. def: `[{"type": "uint8[][]"}]`,
  170. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  171. want: [][]uint8{{1, 2}, {1, 2}},
  172. },
  173. {
  174. def: `[{"type": "uint8[2][2]"}]`,
  175. enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  176. want: [2][2]uint8{{1, 2}, {1, 2}},
  177. },
  178. {
  179. def: `[{"type": "uint8[][2]"}]`,
  180. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
  181. want: [2][]uint8{{1}, {1}},
  182. },
  183. {
  184. def: `[{"type": "uint8[2][]"}]`,
  185. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  186. want: [][2]uint8{{1, 2}},
  187. },
  188. {
  189. def: `[{"type": "uint8[2][]"}]`,
  190. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  191. want: [][2]uint8{{1, 2}, {1, 2}},
  192. },
  193. {
  194. def: `[{"type": "uint16[]"}]`,
  195. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  196. want: []uint16{1, 2},
  197. },
  198. {
  199. def: `[{"type": "uint16[2]"}]`,
  200. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  201. want: [2]uint16{1, 2},
  202. },
  203. {
  204. def: `[{"type": "uint32[]"}]`,
  205. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  206. want: []uint32{1, 2},
  207. },
  208. {
  209. def: `[{"type": "uint32[2]"}]`,
  210. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  211. want: [2]uint32{1, 2},
  212. },
  213. {
  214. def: `[{"type": "uint32[2][3][4]"}]`,
  215. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
  216. want: [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}}},
  217. },
  218. {
  219. def: `[{"type": "uint64[]"}]`,
  220. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  221. want: []uint64{1, 2},
  222. },
  223. {
  224. def: `[{"type": "uint64[2]"}]`,
  225. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  226. want: [2]uint64{1, 2},
  227. },
  228. {
  229. def: `[{"type": "uint256[]"}]`,
  230. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  231. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  232. },
  233. {
  234. def: `[{"type": "uint256[3]"}]`,
  235. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  236. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  237. },
  238. {
  239. def: `[{"type": "string[4]"}]`,
  240. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b476f2d657468657265756d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000",
  241. want: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"},
  242. },
  243. {
  244. def: `[{"type": "string[]"}]`,
  245. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000",
  246. want: []string{"Ethereum", "go-ethereum"},
  247. },
  248. {
  249. def: `[{"type": "int8[]"}]`,
  250. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  251. want: []int8{1, 2},
  252. },
  253. {
  254. def: `[{"type": "int8[2]"}]`,
  255. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  256. want: [2]int8{1, 2},
  257. },
  258. {
  259. def: `[{"type": "int16[]"}]`,
  260. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  261. want: []int16{1, 2},
  262. },
  263. {
  264. def: `[{"type": "int16[2]"}]`,
  265. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  266. want: [2]int16{1, 2},
  267. },
  268. {
  269. def: `[{"type": "int32[]"}]`,
  270. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  271. want: []int32{1, 2},
  272. },
  273. {
  274. def: `[{"type": "int32[2]"}]`,
  275. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  276. want: [2]int32{1, 2},
  277. },
  278. {
  279. def: `[{"type": "int64[]"}]`,
  280. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  281. want: []int64{1, 2},
  282. },
  283. {
  284. def: `[{"type": "int64[2]"}]`,
  285. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  286. want: [2]int64{1, 2},
  287. },
  288. {
  289. def: `[{"type": "int256[]"}]`,
  290. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  291. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  292. },
  293. {
  294. def: `[{"type": "int256[3]"}]`,
  295. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  296. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  297. },
  298. // struct outputs
  299. {
  300. def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
  301. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  302. want: struct {
  303. Int1 *big.Int
  304. Int2 *big.Int
  305. }{big.NewInt(1), big.NewInt(2)},
  306. },
  307. {
  308. def: `[{"name":"int_one","type":"int256"}]`,
  309. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  310. want: struct {
  311. IntOne *big.Int
  312. }{big.NewInt(1)},
  313. },
  314. {
  315. def: `[{"name":"int__one","type":"int256"}]`,
  316. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  317. want: struct {
  318. IntOne *big.Int
  319. }{big.NewInt(1)},
  320. },
  321. {
  322. def: `[{"name":"int_one_","type":"int256"}]`,
  323. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  324. want: struct {
  325. IntOne *big.Int
  326. }{big.NewInt(1)},
  327. },
  328. {
  329. def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`,
  330. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  331. want: struct {
  332. IntOne *big.Int
  333. Intone *big.Int
  334. }{big.NewInt(1), big.NewInt(2)},
  335. },
  336. {
  337. def: `[{"name":"___","type":"int256"}]`,
  338. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  339. want: struct {
  340. IntOne *big.Int
  341. Intone *big.Int
  342. }{},
  343. err: "abi: purely underscored output cannot unpack to struct",
  344. },
  345. {
  346. def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
  347. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  348. want: struct {
  349. Int1 *big.Int
  350. Int2 *big.Int
  351. }{},
  352. err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
  353. },
  354. {
  355. def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
  356. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  357. want: struct {
  358. Int1 *big.Int
  359. Int2 *big.Int
  360. }{},
  361. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  362. },
  363. {
  364. def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
  365. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  366. want: struct {
  367. Int1 *big.Int
  368. Int2 *big.Int
  369. }{},
  370. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  371. },
  372. {
  373. def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
  374. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  375. want: struct {
  376. Int1 *big.Int
  377. Int2 *big.Int
  378. }{},
  379. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  380. },
  381. {
  382. def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
  383. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  384. want: struct {
  385. Int1 *big.Int
  386. Int2 *big.Int
  387. }{},
  388. err: "abi: purely underscored output cannot unpack to struct",
  389. },
  390. }
  391. func TestUnpack(t *testing.T) {
  392. for i, test := range unpackTests {
  393. t.Run(strconv.Itoa(i), func(t *testing.T) {
  394. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  395. abi, err := JSON(strings.NewReader(def))
  396. if err != nil {
  397. t.Fatalf("invalid ABI definition %s: %v", def, err)
  398. }
  399. encb, err := hex.DecodeString(test.enc)
  400. if err != nil {
  401. t.Fatalf("invalid hex: %s" + test.enc)
  402. }
  403. outptr := reflect.New(reflect.TypeOf(test.want))
  404. err = abi.Unpack(outptr.Interface(), "method", encb)
  405. if err := test.checkError(err); err != nil {
  406. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  407. return
  408. }
  409. out := outptr.Elem().Interface()
  410. if !reflect.DeepEqual(test.want, out) {
  411. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  412. }
  413. })
  414. }
  415. }
  416. func TestUnpackSetDynamicArrayOutput(t *testing.T) {
  417. abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. var (
  422. marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
  423. marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
  424. out32 [][32]byte
  425. out15 [][15]byte
  426. )
  427. // test 32
  428. err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. if len(out32) != 2 {
  433. t.Fatalf("expected array with 2 values, got %d", len(out32))
  434. }
  435. expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
  436. if !bytes.Equal(out32[0][:], expected) {
  437. t.Errorf("expected %x, got %x\n", expected, out32[0])
  438. }
  439. expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
  440. if !bytes.Equal(out32[1][:], expected) {
  441. t.Errorf("expected %x, got %x\n", expected, out32[1])
  442. }
  443. // test 15
  444. err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15)
  445. if err != nil {
  446. t.Fatal(err)
  447. }
  448. if len(out15) != 2 {
  449. t.Fatalf("expected array with 2 values, got %d", len(out15))
  450. }
  451. expected = common.Hex2Bytes("307830313233343500000000000000")
  452. if !bytes.Equal(out15[0][:], expected) {
  453. t.Errorf("expected %x, got %x\n", expected, out15[0])
  454. }
  455. expected = common.Hex2Bytes("307839383736353400000000000000")
  456. if !bytes.Equal(out15[1][:], expected) {
  457. t.Errorf("expected %x, got %x\n", expected, out15[1])
  458. }
  459. }
  460. type methodMultiOutput struct {
  461. Int *big.Int
  462. String string
  463. }
  464. func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
  465. const definition = `[
  466. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  467. var expected = methodMultiOutput{big.NewInt(1), "hello"}
  468. abi, err := JSON(strings.NewReader(definition))
  469. require.NoError(err)
  470. // using buff to make the code readable
  471. buff := new(bytes.Buffer)
  472. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  473. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  474. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  475. buff.Write(common.RightPadBytes([]byte(expected.String), 32))
  476. return abi, buff.Bytes(), expected
  477. }
  478. func TestMethodMultiReturn(t *testing.T) {
  479. type reversed struct {
  480. String string
  481. Int *big.Int
  482. }
  483. abi, data, expected := methodMultiReturn(require.New(t))
  484. bigint := new(big.Int)
  485. var testCases = []struct {
  486. dest interface{}
  487. expected interface{}
  488. error string
  489. name string
  490. }{{
  491. &methodMultiOutput{},
  492. &expected,
  493. "",
  494. "Can unpack into structure",
  495. }, {
  496. &reversed{},
  497. &reversed{expected.String, expected.Int},
  498. "",
  499. "Can unpack into reversed structure",
  500. }, {
  501. &[]interface{}{&bigint, new(string)},
  502. &[]interface{}{&expected.Int, &expected.String},
  503. "",
  504. "Can unpack into a slice",
  505. }, {
  506. &[2]interface{}{&bigint, new(string)},
  507. &[2]interface{}{&expected.Int, &expected.String},
  508. "",
  509. "Can unpack into an array",
  510. }, {
  511. &[]interface{}{new(int), new(int)},
  512. &[]interface{}{&expected.Int, &expected.String},
  513. "abi: cannot unmarshal *big.Int in to int",
  514. "Can not unpack into a slice with wrong types",
  515. }, {
  516. &[]interface{}{new(int)},
  517. &[]interface{}{},
  518. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  519. "Can not unpack into a slice with wrong types",
  520. }}
  521. for _, tc := range testCases {
  522. tc := tc
  523. t.Run(tc.name, func(t *testing.T) {
  524. require := require.New(t)
  525. err := abi.Unpack(tc.dest, "multi", data)
  526. if tc.error == "" {
  527. require.Nil(err, "Should be able to unpack method outputs.")
  528. require.Equal(tc.expected, tc.dest)
  529. } else {
  530. require.EqualError(err, tc.error)
  531. }
  532. })
  533. }
  534. }
  535. func TestMultiReturnWithArray(t *testing.T) {
  536. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  537. abi, err := JSON(strings.NewReader(definition))
  538. if err != nil {
  539. t.Fatal(err)
  540. }
  541. buff := new(bytes.Buffer)
  542. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  543. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  544. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  545. ret2, ret2Exp := new(uint64), uint64(8)
  546. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  547. t.Fatal(err)
  548. }
  549. if !reflect.DeepEqual(*ret1, ret1Exp) {
  550. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  551. }
  552. if *ret2 != ret2Exp {
  553. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  554. }
  555. }
  556. func TestMultiReturnWithStringArray(t *testing.T) {
  557. const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
  558. abi, err := JSON(strings.NewReader(definition))
  559. if err != nil {
  560. t.Fatal(err)
  561. }
  562. buff := new(bytes.Buffer)
  563. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
  564. temp, _ := big.NewInt(0).SetString("30000000000000000000", 10)
  565. ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
  566. ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
  567. ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
  568. ret4, ret4Exp := new(bool), false
  569. if err := abi.Unpack(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
  570. t.Fatal(err)
  571. }
  572. if !reflect.DeepEqual(*ret1, ret1Exp) {
  573. t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
  574. }
  575. if !reflect.DeepEqual(*ret2, ret2Exp) {
  576. t.Error("address result", *ret2, "!= Expected", ret2Exp)
  577. }
  578. if !reflect.DeepEqual(*ret3, ret3Exp) {
  579. t.Error("string array result", *ret3, "!= Expected", ret3Exp)
  580. }
  581. if !reflect.DeepEqual(*ret4, ret4Exp) {
  582. t.Error("bool result", *ret4, "!= Expected", ret4Exp)
  583. }
  584. }
  585. func TestMultiReturnWithStringSlice(t *testing.T) {
  586. const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
  587. abi, err := JSON(strings.NewReader(definition))
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. buff := new(bytes.Buffer)
  592. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
  593. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
  594. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
  595. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
  596. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
  597. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
  598. buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
  599. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
  600. buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
  601. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
  602. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
  603. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
  604. ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
  605. ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
  606. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  607. t.Fatal(err)
  608. }
  609. if !reflect.DeepEqual(*ret1, ret1Exp) {
  610. t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
  611. }
  612. if !reflect.DeepEqual(*ret2, ret2Exp) {
  613. t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
  614. }
  615. }
  616. func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
  617. // Similar to TestMultiReturnWithArray, but with a special case in mind:
  618. // values of nested static arrays count towards the size as well, and any element following
  619. // after such nested array argument should be read with the correct offset,
  620. // so that it does not read content from the previous array argument.
  621. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
  622. abi, err := JSON(strings.NewReader(definition))
  623. if err != nil {
  624. t.Fatal(err)
  625. }
  626. buff := new(bytes.Buffer)
  627. // construct the test array, each 3 char element is joined with 61 '0' chars,
  628. // to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
  629. buff.Write(common.Hex2Bytes(strings.Join([]string{
  630. "", //empty, to apply the 61-char separator to the first element as well.
  631. "111", "112", "113", "121", "122", "123",
  632. "211", "212", "213", "221", "222", "223",
  633. "311", "312", "313", "321", "322", "323",
  634. "411", "412", "413", "421", "422", "423",
  635. }, "0000000000000000000000000000000000000000000000000000000000000")))
  636. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
  637. ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
  638. {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
  639. {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
  640. {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
  641. {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
  642. }
  643. ret2, ret2Exp := new(uint64), uint64(0x9876)
  644. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  645. t.Fatal(err)
  646. }
  647. if !reflect.DeepEqual(*ret1, ret1Exp) {
  648. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  649. }
  650. if *ret2 != ret2Exp {
  651. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  652. }
  653. }
  654. func TestUnmarshal(t *testing.T) {
  655. const definition = `[
  656. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  657. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  658. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  659. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  660. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  661. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  662. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  663. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  664. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  665. abi, err := JSON(strings.NewReader(definition))
  666. if err != nil {
  667. t.Fatal(err)
  668. }
  669. buff := new(bytes.Buffer)
  670. // marshall mixed bytes (mixedBytes)
  671. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  672. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  673. mixedBytes := []interface{}{&p0, &p1}
  674. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  675. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  676. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  677. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  678. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  679. if err != nil {
  680. t.Error(err)
  681. } else {
  682. if !bytes.Equal(p0, p0Exp) {
  683. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  684. }
  685. if !bytes.Equal(p1[:], p1Exp) {
  686. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  687. }
  688. }
  689. // marshal int
  690. var Int *big.Int
  691. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  692. if err != nil {
  693. t.Error(err)
  694. }
  695. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  696. t.Error("expected Int to be 1 got", Int)
  697. }
  698. // marshal bool
  699. var Bool bool
  700. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  701. if err != nil {
  702. t.Error(err)
  703. }
  704. if !Bool {
  705. t.Error("expected Bool to be true")
  706. }
  707. // marshal dynamic bytes max length 32
  708. buff.Reset()
  709. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  710. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  711. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  712. buff.Write(bytesOut)
  713. var Bytes []byte
  714. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  715. if err != nil {
  716. t.Error(err)
  717. }
  718. if !bytes.Equal(Bytes, bytesOut) {
  719. t.Errorf("expected %x got %x", bytesOut, Bytes)
  720. }
  721. // marshall dynamic bytes max length 64
  722. buff.Reset()
  723. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  724. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  725. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  726. buff.Write(bytesOut)
  727. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  728. if err != nil {
  729. t.Error(err)
  730. }
  731. if !bytes.Equal(Bytes, bytesOut) {
  732. t.Errorf("expected %x got %x", bytesOut, Bytes)
  733. }
  734. // marshall dynamic bytes max length 64
  735. buff.Reset()
  736. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  737. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  738. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  739. buff.Write(bytesOut)
  740. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  741. if err != nil {
  742. t.Error(err)
  743. }
  744. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  745. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  746. }
  747. // marshal dynamic bytes output empty
  748. err = abi.Unpack(&Bytes, "bytes", nil)
  749. if err == nil {
  750. t.Error("expected error")
  751. }
  752. // marshal dynamic bytes length 5
  753. buff.Reset()
  754. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  755. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  756. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  757. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  758. if err != nil {
  759. t.Error(err)
  760. }
  761. if !bytes.Equal(Bytes, []byte("hello")) {
  762. t.Errorf("expected %x got %x", bytesOut, Bytes)
  763. }
  764. // marshal dynamic bytes length 5
  765. buff.Reset()
  766. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  767. var hash common.Hash
  768. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  769. if err != nil {
  770. t.Error(err)
  771. }
  772. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  773. if hash != helloHash {
  774. t.Errorf("Expected %x to equal %x", hash, helloHash)
  775. }
  776. // marshal error
  777. buff.Reset()
  778. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  779. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  780. if err == nil {
  781. t.Error("expected error")
  782. }
  783. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  784. if err == nil {
  785. t.Error("expected error")
  786. }
  787. buff.Reset()
  788. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  789. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  790. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  791. // marshal int array
  792. var intArray [3]*big.Int
  793. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  794. if err != nil {
  795. t.Error(err)
  796. }
  797. var testAgainstIntArray [3]*big.Int
  798. testAgainstIntArray[0] = big.NewInt(1)
  799. testAgainstIntArray[1] = big.NewInt(2)
  800. testAgainstIntArray[2] = big.NewInt(3)
  801. for i, Int := range intArray {
  802. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  803. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  804. }
  805. }
  806. // marshal address slice
  807. buff.Reset()
  808. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  809. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  810. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  811. var outAddr []common.Address
  812. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  813. if err != nil {
  814. t.Fatal("didn't expect error:", err)
  815. }
  816. if len(outAddr) != 1 {
  817. t.Fatal("expected 1 item, got", len(outAddr))
  818. }
  819. if outAddr[0] != (common.Address{1}) {
  820. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  821. }
  822. // marshal multiple address slice
  823. buff.Reset()
  824. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  825. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  826. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  827. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  828. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  829. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  830. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  831. var outAddrStruct struct {
  832. A []common.Address
  833. B []common.Address
  834. }
  835. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  836. if err != nil {
  837. t.Fatal("didn't expect error:", err)
  838. }
  839. if len(outAddrStruct.A) != 1 {
  840. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  841. }
  842. if outAddrStruct.A[0] != (common.Address{1}) {
  843. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  844. }
  845. if len(outAddrStruct.B) != 2 {
  846. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  847. }
  848. if outAddrStruct.B[0] != (common.Address{2}) {
  849. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  850. }
  851. if outAddrStruct.B[1] != (common.Address{3}) {
  852. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  853. }
  854. // marshal invalid address slice
  855. buff.Reset()
  856. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  857. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  858. if err == nil {
  859. t.Fatal("expected error:", err)
  860. }
  861. }
  862. func TestUnpackTuple(t *testing.T) {
  863. const simpleTuple = `[{"name":"tuple","constant":false,"outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
  864. abi, err := JSON(strings.NewReader(simpleTuple))
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. buff := new(bytes.Buffer)
  869. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
  870. buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
  871. v := struct {
  872. Ret struct {
  873. A *big.Int
  874. B *big.Int
  875. }
  876. }{Ret: struct {
  877. A *big.Int
  878. B *big.Int
  879. }{new(big.Int), new(big.Int)}}
  880. err = abi.Unpack(&v, "tuple", buff.Bytes())
  881. if err != nil {
  882. t.Error(err)
  883. } else {
  884. if v.Ret.A.Cmp(big.NewInt(1)) != 0 {
  885. t.Errorf("unexpected value unpacked: want %x, got %x", 1, v.Ret.A)
  886. }
  887. if v.Ret.B.Cmp(big.NewInt(-1)) != 0 {
  888. t.Errorf("unexpected value unpacked: want %x, got %x", v.Ret.B, -1)
  889. }
  890. }
  891. // Test nested tuple
  892. const nestedTuple = `[{"name":"tuple","constant":false,"outputs":[
  893. {"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]},
  894. {"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]},
  895. {"type":"uint256","name":"a"}
  896. ]}]`
  897. abi, err = JSON(strings.NewReader(nestedTuple))
  898. if err != nil {
  899. t.Fatal(err)
  900. }
  901. buff.Reset()
  902. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
  903. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
  904. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
  905. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
  906. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
  907. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
  908. buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
  909. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
  910. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
  911. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
  912. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
  913. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
  914. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
  915. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
  916. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
  917. type T struct {
  918. X *big.Int `abi:"x"`
  919. Z *big.Int `abi:"y"` // Test whether the abi tag works.
  920. }
  921. type S struct {
  922. A *big.Int
  923. B []*big.Int
  924. C []T
  925. }
  926. type Ret struct {
  927. FieldS S `abi:"s"`
  928. FieldT T `abi:"t"`
  929. A *big.Int
  930. }
  931. var ret Ret
  932. var expected = Ret{
  933. FieldS: S{
  934. A: big.NewInt(1),
  935. B: []*big.Int{big.NewInt(1), big.NewInt(2)},
  936. C: []T{
  937. {big.NewInt(1), big.NewInt(2)},
  938. {big.NewInt(2), big.NewInt(1)},
  939. },
  940. },
  941. FieldT: T{
  942. big.NewInt(0), big.NewInt(1),
  943. },
  944. A: big.NewInt(1),
  945. }
  946. err = abi.Unpack(&ret, "tuple", buff.Bytes())
  947. if err != nil {
  948. t.Error(err)
  949. }
  950. if reflect.DeepEqual(ret, expected) {
  951. t.Error("unexpected unpack value")
  952. }
  953. }
  954. func TestOOMMaliciousInput(t *testing.T) {
  955. oomTests := []unpackTest{
  956. {
  957. def: `[{"type": "uint8[]"}]`,
  958. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  959. "0000000000000000000000000000000000000000000000000000000000000003" + // num elems
  960. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  961. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  962. },
  963. { // Length larger than 64 bits
  964. def: `[{"type": "uint8[]"}]`,
  965. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  966. "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
  967. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  968. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  969. },
  970. { // Offset very large (over 64 bits)
  971. def: `[{"type": "uint8[]"}]`,
  972. enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
  973. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  974. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  975. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  976. },
  977. { // Offset very large (below 64 bits)
  978. def: `[{"type": "uint8[]"}]`,
  979. enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
  980. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  981. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  982. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  983. },
  984. { // Offset negative (as 64 bit)
  985. def: `[{"type": "uint8[]"}]`,
  986. enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
  987. "0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  988. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  989. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  990. },
  991. { // Negative length
  992. def: `[{"type": "uint8[]"}]`,
  993. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  994. "000000000000000000000000000000000000000000000000f000000000000002" + // num elems
  995. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  996. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  997. },
  998. { // Very large length
  999. def: `[{"type": "uint8[]"}]`,
  1000. enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  1001. "0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
  1002. "0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1003. "0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1004. },
  1005. }
  1006. for i, test := range oomTests {
  1007. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  1008. abi, err := JSON(strings.NewReader(def))
  1009. if err != nil {
  1010. t.Fatalf("invalid ABI definition %s: %v", def, err)
  1011. }
  1012. encb, err := hex.DecodeString(test.enc)
  1013. if err != nil {
  1014. t.Fatalf("invalid hex: %s" + test.enc)
  1015. }
  1016. _, err = abi.Methods["method"].Outputs.UnpackValues(encb)
  1017. if err == nil {
  1018. t.Fatalf("Expected error on malicious input, test %d", i)
  1019. }
  1020. }
  1021. }