unpack_test.go 52 KB

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