unpack_test.go 50 KB

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