unpack_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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": "uint32"}]`,
  55. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  56. want: uint32(1),
  57. },
  58. {
  59. def: `[{"type": "uint32"}]`,
  60. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  61. want: uint16(0),
  62. err: "abi: cannot unmarshal uint32 in to uint16",
  63. },
  64. {
  65. def: `[{"type": "uint17"}]`,
  66. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  67. want: uint16(0),
  68. err: "abi: cannot unmarshal *big.Int in to uint16",
  69. },
  70. {
  71. def: `[{"type": "uint17"}]`,
  72. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  73. want: big.NewInt(1),
  74. },
  75. {
  76. def: `[{"type": "int32"}]`,
  77. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  78. want: int32(1),
  79. },
  80. {
  81. def: `[{"type": "int32"}]`,
  82. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  83. want: int16(0),
  84. err: "abi: cannot unmarshal int32 in to int16",
  85. },
  86. {
  87. def: `[{"type": "int17"}]`,
  88. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  89. want: int16(0),
  90. err: "abi: cannot unmarshal *big.Int in to int16",
  91. },
  92. {
  93. def: `[{"type": "int17"}]`,
  94. enc: "0000000000000000000000000000000000000000000000000000000000000001",
  95. want: big.NewInt(1),
  96. },
  97. {
  98. def: `[{"type": "address"}]`,
  99. enc: "0000000000000000000000000100000000000000000000000000000000000000",
  100. want: common.Address{1},
  101. },
  102. {
  103. def: `[{"type": "bytes32"}]`,
  104. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  105. 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},
  106. },
  107. {
  108. def: `[{"type": "bytes"}]`,
  109. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  110. want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
  111. },
  112. {
  113. def: `[{"type": "bytes"}]`,
  114. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  115. want: [32]byte{},
  116. err: "abi: cannot unmarshal []uint8 in to [32]uint8",
  117. },
  118. {
  119. def: `[{"type": "bytes32"}]`,
  120. enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
  121. want: []byte(nil),
  122. err: "abi: cannot unmarshal [32]uint8 in to []uint8",
  123. },
  124. {
  125. def: `[{"type": "bytes32"}]`,
  126. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  127. want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
  128. },
  129. {
  130. def: `[{"type": "function"}]`,
  131. enc: "0100000000000000000000000000000000000000000000000000000000000000",
  132. want: [24]byte{1},
  133. },
  134. // slices
  135. {
  136. def: `[{"type": "uint8[]"}]`,
  137. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  138. want: []uint8{1, 2},
  139. },
  140. {
  141. def: `[{"type": "uint8[2]"}]`,
  142. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  143. want: [2]uint8{1, 2},
  144. },
  145. // multi dimensional, if these pass, all types that don't require length prefix should pass
  146. {
  147. def: `[{"type": "uint8[][]"}]`,
  148. enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  149. want: [][]uint8{{1, 2}, {1, 2}},
  150. },
  151. {
  152. def: `[{"type": "uint8[2][2]"}]`,
  153. enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  154. want: [2][2]uint8{{1, 2}, {1, 2}},
  155. },
  156. {
  157. def: `[{"type": "uint8[][2]"}]`,
  158. enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
  159. want: [2][]uint8{{1}, {1}},
  160. },
  161. {
  162. def: `[{"type": "uint8[2][]"}]`,
  163. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  164. want: [][2]uint8{{1, 2}},
  165. },
  166. {
  167. def: `[{"type": "uint16[]"}]`,
  168. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  169. want: []uint16{1, 2},
  170. },
  171. {
  172. def: `[{"type": "uint16[2]"}]`,
  173. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  174. want: [2]uint16{1, 2},
  175. },
  176. {
  177. def: `[{"type": "uint32[]"}]`,
  178. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  179. want: []uint32{1, 2},
  180. },
  181. {
  182. def: `[{"type": "uint32[2]"}]`,
  183. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  184. want: [2]uint32{1, 2},
  185. },
  186. {
  187. def: `[{"type": "uint64[]"}]`,
  188. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  189. want: []uint64{1, 2},
  190. },
  191. {
  192. def: `[{"type": "uint64[2]"}]`,
  193. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  194. want: [2]uint64{1, 2},
  195. },
  196. {
  197. def: `[{"type": "uint256[]"}]`,
  198. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  199. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  200. },
  201. {
  202. def: `[{"type": "uint256[3]"}]`,
  203. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  204. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  205. },
  206. {
  207. def: `[{"type": "int8[]"}]`,
  208. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  209. want: []int8{1, 2},
  210. },
  211. {
  212. def: `[{"type": "int8[2]"}]`,
  213. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  214. want: [2]int8{1, 2},
  215. },
  216. {
  217. def: `[{"type": "int16[]"}]`,
  218. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  219. want: []int16{1, 2},
  220. },
  221. {
  222. def: `[{"type": "int16[2]"}]`,
  223. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  224. want: [2]int16{1, 2},
  225. },
  226. {
  227. def: `[{"type": "int32[]"}]`,
  228. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  229. want: []int32{1, 2},
  230. },
  231. {
  232. def: `[{"type": "int32[2]"}]`,
  233. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  234. want: [2]int32{1, 2},
  235. },
  236. {
  237. def: `[{"type": "int64[]"}]`,
  238. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  239. want: []int64{1, 2},
  240. },
  241. {
  242. def: `[{"type": "int64[2]"}]`,
  243. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  244. want: [2]int64{1, 2},
  245. },
  246. {
  247. def: `[{"type": "int256[]"}]`,
  248. enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  249. want: []*big.Int{big.NewInt(1), big.NewInt(2)},
  250. },
  251. {
  252. def: `[{"type": "int256[3]"}]`,
  253. enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
  254. want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
  255. },
  256. // struct outputs
  257. {
  258. def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
  259. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  260. want: struct {
  261. Int1 *big.Int
  262. Int2 *big.Int
  263. }{big.NewInt(1), big.NewInt(2)},
  264. },
  265. {
  266. def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
  267. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  268. want: struct {
  269. Int1 *big.Int
  270. Int2 *big.Int
  271. }{},
  272. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  273. },
  274. {
  275. def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
  276. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  277. want: struct {
  278. Int1 *big.Int
  279. Int2 *big.Int
  280. }{},
  281. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  282. },
  283. {
  284. def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
  285. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  286. want: struct {
  287. Int1 *big.Int
  288. Int2 *big.Int
  289. }{},
  290. err: "abi: multiple outputs mapping to the same struct field 'Int'",
  291. },
  292. {
  293. def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
  294. enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
  295. want: struct {
  296. Int1 *big.Int
  297. Int2 *big.Int
  298. }{},
  299. err: "abi: purely underscored output cannot unpack to struct",
  300. },
  301. }
  302. func TestUnpack(t *testing.T) {
  303. for i, test := range unpackTests {
  304. t.Run(strconv.Itoa(i), func(t *testing.T) {
  305. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  306. abi, err := JSON(strings.NewReader(def))
  307. if err != nil {
  308. t.Fatalf("invalid ABI definition %s: %v", def, err)
  309. }
  310. encb, err := hex.DecodeString(test.enc)
  311. if err != nil {
  312. t.Fatalf("invalid hex: %s" + test.enc)
  313. }
  314. outptr := reflect.New(reflect.TypeOf(test.want))
  315. err = abi.Unpack(outptr.Interface(), "method", encb)
  316. if err := test.checkError(err); err != nil {
  317. t.Errorf("test %d (%v) failed: %v", i, test.def, err)
  318. return
  319. }
  320. out := outptr.Elem().Interface()
  321. if !reflect.DeepEqual(test.want, out) {
  322. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
  323. }
  324. })
  325. }
  326. }
  327. type methodMultiOutput struct {
  328. Int *big.Int
  329. String string
  330. }
  331. func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
  332. const definition = `[
  333. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  334. var expected = methodMultiOutput{big.NewInt(1), "hello"}
  335. abi, err := JSON(strings.NewReader(definition))
  336. require.NoError(err)
  337. // using buff to make the code readable
  338. buff := new(bytes.Buffer)
  339. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  340. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  341. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  342. buff.Write(common.RightPadBytes([]byte(expected.String), 32))
  343. return abi, buff.Bytes(), expected
  344. }
  345. func TestMethodMultiReturn(t *testing.T) {
  346. type reversed struct {
  347. String string
  348. Int *big.Int
  349. }
  350. abi, data, expected := methodMultiReturn(require.New(t))
  351. bigint := new(big.Int)
  352. var testCases = []struct {
  353. dest interface{}
  354. expected interface{}
  355. error string
  356. name string
  357. }{{
  358. &methodMultiOutput{},
  359. &expected,
  360. "",
  361. "Can unpack into structure",
  362. }, {
  363. &reversed{},
  364. &reversed{expected.String, expected.Int},
  365. "",
  366. "Can unpack into reversed structure",
  367. }, {
  368. &[]interface{}{&bigint, new(string)},
  369. &[]interface{}{&expected.Int, &expected.String},
  370. "",
  371. "Can unpack into a slice",
  372. }, {
  373. &[2]interface{}{&bigint, new(string)},
  374. &[2]interface{}{&expected.Int, &expected.String},
  375. "",
  376. "Can unpack into an array",
  377. }, {
  378. &[]interface{}{new(int), new(int)},
  379. &[]interface{}{&expected.Int, &expected.String},
  380. "abi: cannot unmarshal *big.Int in to int",
  381. "Can not unpack into a slice with wrong types",
  382. }, {
  383. &[]interface{}{new(int)},
  384. &[]interface{}{},
  385. "abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
  386. "Can not unpack into a slice with wrong types",
  387. }}
  388. for _, tc := range testCases {
  389. tc := tc
  390. t.Run(tc.name, func(t *testing.T) {
  391. require := require.New(t)
  392. err := abi.Unpack(tc.dest, "multi", data)
  393. if tc.error == "" {
  394. require.Nil(err, "Should be able to unpack method outputs.")
  395. require.Equal(tc.expected, tc.dest)
  396. } else {
  397. require.EqualError(err, tc.error)
  398. }
  399. })
  400. }
  401. }
  402. func TestMultiReturnWithArray(t *testing.T) {
  403. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  404. abi, err := JSON(strings.NewReader(definition))
  405. if err != nil {
  406. t.Fatal(err)
  407. }
  408. buff := new(bytes.Buffer)
  409. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
  410. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
  411. ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
  412. ret2, ret2Exp := new(uint64), uint64(8)
  413. if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
  414. t.Fatal(err)
  415. }
  416. if !reflect.DeepEqual(*ret1, ret1Exp) {
  417. t.Error("array result", *ret1, "!= Expected", ret1Exp)
  418. }
  419. if *ret2 != ret2Exp {
  420. t.Error("int result", *ret2, "!= Expected", ret2Exp)
  421. }
  422. }
  423. func TestUnmarshal(t *testing.T) {
  424. const definition = `[
  425. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  426. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  427. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  428. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  429. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  430. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  431. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  432. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  433. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  434. abi, err := JSON(strings.NewReader(definition))
  435. if err != nil {
  436. t.Fatal(err)
  437. }
  438. buff := new(bytes.Buffer)
  439. // marshall mixed bytes (mixedBytes)
  440. p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
  441. p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  442. mixedBytes := []interface{}{&p0, &p1}
  443. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  444. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  445. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  446. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  447. err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
  448. if err != nil {
  449. t.Error(err)
  450. } else {
  451. if !bytes.Equal(p0, p0Exp) {
  452. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  453. }
  454. if !bytes.Equal(p1[:], p1Exp) {
  455. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  456. }
  457. }
  458. // marshal int
  459. var Int *big.Int
  460. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  461. if err != nil {
  462. t.Error(err)
  463. }
  464. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  465. t.Error("expected Int to be 1 got", Int)
  466. }
  467. // marshal bool
  468. var Bool bool
  469. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  470. if err != nil {
  471. t.Error(err)
  472. }
  473. if !Bool {
  474. t.Error("expected Bool to be true")
  475. }
  476. // marshal dynamic bytes max length 32
  477. buff.Reset()
  478. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  479. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  480. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  481. buff.Write(bytesOut)
  482. var Bytes []byte
  483. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  484. if err != nil {
  485. t.Error(err)
  486. }
  487. if !bytes.Equal(Bytes, bytesOut) {
  488. t.Errorf("expected %x got %x", bytesOut, Bytes)
  489. }
  490. // marshall dynamic bytes max length 64
  491. buff.Reset()
  492. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  493. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  494. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  495. buff.Write(bytesOut)
  496. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  497. if err != nil {
  498. t.Error(err)
  499. }
  500. if !bytes.Equal(Bytes, bytesOut) {
  501. t.Errorf("expected %x got %x", bytesOut, Bytes)
  502. }
  503. // marshall dynamic bytes max length 64
  504. buff.Reset()
  505. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  506. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  507. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  508. buff.Write(bytesOut)
  509. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  510. if err != nil {
  511. t.Error(err)
  512. }
  513. if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
  514. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
  515. }
  516. // marshal dynamic bytes output empty
  517. err = abi.Unpack(&Bytes, "bytes", nil)
  518. if err == nil {
  519. t.Error("expected error")
  520. }
  521. // marshal dynamic bytes length 5
  522. buff.Reset()
  523. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  524. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  525. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  526. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  527. if err != nil {
  528. t.Error(err)
  529. }
  530. if !bytes.Equal(Bytes, []byte("hello")) {
  531. t.Errorf("expected %x got %x", bytesOut, Bytes)
  532. }
  533. // marshal dynamic bytes length 5
  534. buff.Reset()
  535. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  536. var hash common.Hash
  537. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  538. if err != nil {
  539. t.Error(err)
  540. }
  541. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  542. if hash != helloHash {
  543. t.Errorf("Expected %x to equal %x", hash, helloHash)
  544. }
  545. // marshal error
  546. buff.Reset()
  547. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  548. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  549. if err == nil {
  550. t.Error("expected error")
  551. }
  552. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  553. if err == nil {
  554. t.Error("expected error")
  555. }
  556. buff.Reset()
  557. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  558. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  559. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  560. // marshal int array
  561. var intArray [3]*big.Int
  562. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  563. if err != nil {
  564. t.Error(err)
  565. }
  566. var testAgainstIntArray [3]*big.Int
  567. testAgainstIntArray[0] = big.NewInt(1)
  568. testAgainstIntArray[1] = big.NewInt(2)
  569. testAgainstIntArray[2] = big.NewInt(3)
  570. for i, Int := range intArray {
  571. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  572. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  573. }
  574. }
  575. // marshal address slice
  576. buff.Reset()
  577. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  578. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  579. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  580. var outAddr []common.Address
  581. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  582. if err != nil {
  583. t.Fatal("didn't expect error:", err)
  584. }
  585. if len(outAddr) != 1 {
  586. t.Fatal("expected 1 item, got", len(outAddr))
  587. }
  588. if outAddr[0] != (common.Address{1}) {
  589. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  590. }
  591. // marshal multiple address slice
  592. buff.Reset()
  593. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  594. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  595. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  596. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  597. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  598. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  599. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  600. var outAddrStruct struct {
  601. A []common.Address
  602. B []common.Address
  603. }
  604. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  605. if err != nil {
  606. t.Fatal("didn't expect error:", err)
  607. }
  608. if len(outAddrStruct.A) != 1 {
  609. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  610. }
  611. if outAddrStruct.A[0] != (common.Address{1}) {
  612. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  613. }
  614. if len(outAddrStruct.B) != 2 {
  615. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  616. }
  617. if outAddrStruct.B[0] != (common.Address{2}) {
  618. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  619. }
  620. if outAddrStruct.B[1] != (common.Address{3}) {
  621. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  622. }
  623. // marshal invalid address slice
  624. buff.Reset()
  625. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  626. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  627. if err == nil {
  628. t.Fatal("expected error:", err)
  629. }
  630. }