abi_test.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. // Copyright 2015 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. "fmt"
  20. "log"
  21. "math/big"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. // formatSilceOutput add padding to the value and adds a size
  29. func formatSliceOutput(v ...[]byte) []byte {
  30. off := common.LeftPadBytes(big.NewInt(int64(len(v))).Bytes(), 32)
  31. output := append(off, make([]byte, 0, len(v)*32)...)
  32. for _, value := range v {
  33. output = append(output, common.LeftPadBytes(value, 32)...)
  34. }
  35. return output
  36. }
  37. // quick helper padding
  38. func pad(input []byte, size int, left bool) []byte {
  39. if left {
  40. return common.LeftPadBytes(input, size)
  41. }
  42. return common.RightPadBytes(input, size)
  43. }
  44. func TestTypeCheck(t *testing.T) {
  45. for i, test := range []struct {
  46. typ string
  47. input interface{}
  48. err string
  49. }{
  50. {"uint", big.NewInt(1), ""},
  51. {"int", big.NewInt(1), ""},
  52. {"uint30", big.NewInt(1), ""},
  53. {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"},
  54. {"uint16", uint16(1), ""},
  55. {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
  56. {"uint16[]", []uint16{1, 2, 3}, ""},
  57. {"uint16[]", [3]uint16{1, 2, 3}, ""},
  58. {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"},
  59. {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
  60. {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  61. {"uint16[3]", []uint16{1, 2, 3}, ""},
  62. {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
  63. {"address[]", []common.Address{common.Address{1}}, ""},
  64. {"address[1]", []common.Address{common.Address{1}}, ""},
  65. {"address[1]", [1]common.Address{common.Address{1}}, ""},
  66. {"address[2]", [1]common.Address{common.Address{1}}, "abi: cannot use [1]array as type [2]array as argument"},
  67. {"bytes32", [32]byte{}, ""},
  68. {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
  69. {"bytes32", common.Hash{1}, ""},
  70. {"bytes31", [31]byte{}, ""},
  71. {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
  72. {"bytes", []byte{0, 1}, ""},
  73. {"bytes", [2]byte{0, 1}, ""},
  74. {"bytes", common.Hash{1}, ""},
  75. {"string", "hello world", ""},
  76. {"bytes32[]", [][32]byte{[32]byte{}}, ""},
  77. } {
  78. typ, err := NewType(test.typ)
  79. if err != nil {
  80. t.Fatal("unexpected parse error:", err)
  81. }
  82. err = typeCheck(typ, reflect.ValueOf(test.input))
  83. if err != nil && len(test.err) == 0 {
  84. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  85. continue
  86. }
  87. if err == nil && len(test.err) != 0 {
  88. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  89. continue
  90. }
  91. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  92. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  93. }
  94. }
  95. }
  96. func TestSimpleMethodUnpack(t *testing.T) {
  97. for i, test := range []struct {
  98. def string // definition of the **output** ABI params
  99. marshalledOutput []byte // evm return data
  100. expectedOut interface{} // the expected output
  101. outVar string // the output variable (e.g. uint32, *big.Int, etc)
  102. err string // empty or error if expected
  103. }{
  104. {
  105. `[ { "type": "uint32" } ]`,
  106. pad([]byte{1}, 32, true),
  107. uint32(1),
  108. "uint32",
  109. "",
  110. },
  111. {
  112. `[ { "type": "uint32" } ]`,
  113. pad([]byte{1}, 32, true),
  114. nil,
  115. "uint16",
  116. "abi: cannot unmarshal uint32 in to uint16",
  117. },
  118. {
  119. `[ { "type": "uint17" } ]`,
  120. pad([]byte{1}, 32, true),
  121. nil,
  122. "uint16",
  123. "abi: cannot unmarshal *big.Int in to uint16",
  124. },
  125. {
  126. `[ { "type": "uint17" } ]`,
  127. pad([]byte{1}, 32, true),
  128. big.NewInt(1),
  129. "*big.Int",
  130. "",
  131. },
  132. {
  133. `[ { "type": "int32" } ]`,
  134. pad([]byte{1}, 32, true),
  135. int32(1),
  136. "int32",
  137. "",
  138. },
  139. {
  140. `[ { "type": "int32" } ]`,
  141. pad([]byte{1}, 32, true),
  142. nil,
  143. "int16",
  144. "abi: cannot unmarshal int32 in to int16",
  145. },
  146. {
  147. `[ { "type": "int17" } ]`,
  148. pad([]byte{1}, 32, true),
  149. nil,
  150. "int16",
  151. "abi: cannot unmarshal *big.Int in to int16",
  152. },
  153. {
  154. `[ { "type": "int17" } ]`,
  155. pad([]byte{1}, 32, true),
  156. big.NewInt(1),
  157. "*big.Int",
  158. "",
  159. },
  160. {
  161. `[ { "type": "address" } ]`,
  162. pad(pad([]byte{1}, 20, false), 32, true),
  163. common.Address{1},
  164. "address",
  165. "",
  166. },
  167. {
  168. `[ { "type": "bytes32" } ]`,
  169. pad([]byte{1}, 32, false),
  170. pad([]byte{1}, 32, false),
  171. "bytes",
  172. "",
  173. },
  174. {
  175. `[ { "type": "bytes32" } ]`,
  176. pad([]byte{1}, 32, false),
  177. pad([]byte{1}, 32, false),
  178. "hash",
  179. "",
  180. },
  181. {
  182. `[ { "type": "bytes32" } ]`,
  183. pad([]byte{1}, 32, false),
  184. pad([]byte{1}, 32, false),
  185. "interface",
  186. "",
  187. },
  188. } {
  189. abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  190. abi, err := JSON(strings.NewReader(abiDefinition))
  191. if err != nil {
  192. t.Errorf("%d failed. %v", i, err)
  193. continue
  194. }
  195. var outvar interface{}
  196. switch test.outVar {
  197. case "uint8":
  198. var v uint8
  199. err = abi.Unpack(&v, "method", test.marshalledOutput)
  200. outvar = v
  201. case "uint16":
  202. var v uint16
  203. err = abi.Unpack(&v, "method", test.marshalledOutput)
  204. outvar = v
  205. case "uint32":
  206. var v uint32
  207. err = abi.Unpack(&v, "method", test.marshalledOutput)
  208. outvar = v
  209. case "uint64":
  210. var v uint64
  211. err = abi.Unpack(&v, "method", test.marshalledOutput)
  212. outvar = v
  213. case "int8":
  214. var v int8
  215. err = abi.Unpack(&v, "method", test.marshalledOutput)
  216. outvar = v
  217. case "int16":
  218. var v int16
  219. err = abi.Unpack(&v, "method", test.marshalledOutput)
  220. outvar = v
  221. case "int32":
  222. var v int32
  223. err = abi.Unpack(&v, "method", test.marshalledOutput)
  224. outvar = v
  225. case "int64":
  226. var v int64
  227. err = abi.Unpack(&v, "method", test.marshalledOutput)
  228. outvar = v
  229. case "*big.Int":
  230. var v *big.Int
  231. err = abi.Unpack(&v, "method", test.marshalledOutput)
  232. outvar = v
  233. case "address":
  234. var v common.Address
  235. err = abi.Unpack(&v, "method", test.marshalledOutput)
  236. outvar = v
  237. case "bytes":
  238. var v []byte
  239. err = abi.Unpack(&v, "method", test.marshalledOutput)
  240. outvar = v
  241. case "hash":
  242. var v common.Hash
  243. err = abi.Unpack(&v, "method", test.marshalledOutput)
  244. outvar = v
  245. case "interface":
  246. err = abi.Unpack(&outvar, "method", test.marshalledOutput)
  247. default:
  248. t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar)
  249. continue
  250. }
  251. if err != nil && len(test.err) == 0 {
  252. t.Errorf("%d failed. Expected no err but got: %v", i, err)
  253. continue
  254. }
  255. if err == nil && len(test.err) != 0 {
  256. t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
  257. continue
  258. }
  259. if err != nil && len(test.err) != 0 && err.Error() != test.err {
  260. t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
  261. continue
  262. }
  263. if err == nil {
  264. // bit of an ugly hack for hash type but I don't feel like finding a proper solution
  265. if test.outVar == "hash" {
  266. tmp := outvar.(common.Hash) // without assignment it's unaddressable
  267. outvar = tmp[:]
  268. }
  269. if !reflect.DeepEqual(test.expectedOut, outvar) {
  270. t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar)
  271. }
  272. }
  273. }
  274. }
  275. func TestUnpackSetInterfaceSlice(t *testing.T) {
  276. var (
  277. var1 = new(uint8)
  278. var2 = new(uint8)
  279. )
  280. out := []interface{}{var1, var2}
  281. abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`))
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
  286. err = abi.Unpack(&out, "ints", marshalledReturn)
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if *var1 != 1 {
  291. t.Error("expected var1 to be 1, got", *var1)
  292. }
  293. if *var2 != 2 {
  294. t.Error("expected var2 to be 2, got", *var2)
  295. }
  296. out = []interface{}{var1}
  297. err = abi.Unpack(&out, "ints", marshalledReturn)
  298. expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)"
  299. if err == nil || err.Error() != expErr {
  300. t.Error("expected err:", expErr, "Got:", err)
  301. }
  302. }
  303. func TestPack(t *testing.T) {
  304. for i, test := range []struct {
  305. typ string
  306. input interface{}
  307. output []byte
  308. }{
  309. {"uint16", uint16(2), pad([]byte{2}, 32, true)},
  310. {"uint16[]", []uint16{1, 2}, formatSliceOutput([]byte{1}, []byte{2})},
  311. {"bytes20", [20]byte{1}, pad([]byte{1}, 32, false)},
  312. {"uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, formatSliceOutput([]byte{1}, []byte{2})},
  313. {"address[]", []common.Address{common.Address{1}, common.Address{2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))},
  314. {"bytes32[]", []common.Hash{common.Hash{1}, common.Hash{2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))},
  315. } {
  316. typ, err := NewType(test.typ)
  317. if err != nil {
  318. t.Fatal("unexpected parse error:", err)
  319. }
  320. output, err := typ.pack(reflect.ValueOf(test.input))
  321. if err != nil {
  322. t.Fatal("unexpected pack error:", err)
  323. }
  324. if !bytes.Equal(output, test.output) {
  325. t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
  326. }
  327. }
  328. }
  329. func TestMethodPack(t *testing.T) {
  330. abi, err := JSON(strings.NewReader(jsondata2))
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. sig := abi.Methods["slice"].Id()
  335. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  336. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  337. packed, err := abi.Pack("slice", []uint32{1, 2})
  338. if err != nil {
  339. t.Error(err)
  340. }
  341. if !bytes.Equal(packed, sig) {
  342. t.Errorf("expected %x got %x", sig, packed)
  343. }
  344. var addrA, addrB = common.Address{1}, common.Address{2}
  345. sig = abi.Methods["sliceAddress"].Id()
  346. sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
  347. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  348. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  349. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  350. packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. if !bytes.Equal(packed, sig) {
  355. t.Errorf("expected %x got %x", sig, packed)
  356. }
  357. var addrC, addrD = common.Address{3}, common.Address{4}
  358. sig = abi.Methods["sliceMultiAddress"].Id()
  359. sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
  360. sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
  361. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  362. sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
  363. sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
  364. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  365. sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
  366. sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
  367. packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
  368. if err != nil {
  369. t.Fatal(err)
  370. }
  371. if !bytes.Equal(packed, sig) {
  372. t.Errorf("expected %x got %x", sig, packed)
  373. }
  374. sig = abi.Methods["slice256"].Id()
  375. sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
  376. sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
  377. packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
  378. if err != nil {
  379. t.Error(err)
  380. }
  381. if !bytes.Equal(packed, sig) {
  382. t.Errorf("expected %x got %x", sig, packed)
  383. }
  384. }
  385. const jsondata = `
  386. [
  387. { "type" : "function", "name" : "balance", "constant" : true },
  388. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  389. ]`
  390. const jsondata2 = `
  391. [
  392. { "type" : "function", "name" : "balance", "constant" : true },
  393. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
  394. { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
  395. { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
  396. { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
  397. { "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
  398. { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
  399. { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
  400. { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
  401. { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
  402. { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
  403. { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
  404. { "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
  405. { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
  406. ]`
  407. func TestReader(t *testing.T) {
  408. Uint256, _ := NewType("uint256")
  409. exp := ABI{
  410. Methods: map[string]Method{
  411. "balance": Method{
  412. "balance", true, nil, nil,
  413. },
  414. "send": Method{
  415. "send", false, []Argument{
  416. Argument{"amount", Uint256, false},
  417. }, nil,
  418. },
  419. },
  420. }
  421. abi, err := JSON(strings.NewReader(jsondata))
  422. if err != nil {
  423. t.Error(err)
  424. }
  425. // deep equal fails for some reason
  426. t.Skip()
  427. if !reflect.DeepEqual(abi, exp) {
  428. t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
  429. }
  430. }
  431. func TestTestNumbers(t *testing.T) {
  432. abi, err := JSON(strings.NewReader(jsondata2))
  433. if err != nil {
  434. t.Error(err)
  435. t.FailNow()
  436. }
  437. if _, err := abi.Pack("balance"); err != nil {
  438. t.Error(err)
  439. }
  440. if _, err := abi.Pack("balance", 1); err == nil {
  441. t.Error("expected error for balance(1)")
  442. }
  443. if _, err := abi.Pack("doesntexist", nil); err == nil {
  444. t.Errorf("doesntexist shouldn't exist")
  445. }
  446. if _, err := abi.Pack("doesntexist", 1); err == nil {
  447. t.Errorf("doesntexist(1) shouldn't exist")
  448. }
  449. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  450. t.Error(err)
  451. }
  452. i := new(int)
  453. *i = 1000
  454. if _, err := abi.Pack("send", i); err == nil {
  455. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  456. }
  457. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  458. t.Error(err)
  459. }
  460. }
  461. func TestTestString(t *testing.T) {
  462. abi, err := JSON(strings.NewReader(jsondata2))
  463. if err != nil {
  464. t.Error(err)
  465. t.FailNow()
  466. }
  467. if _, err := abi.Pack("string", "hello world"); err != nil {
  468. t.Error(err)
  469. }
  470. }
  471. func TestTestBool(t *testing.T) {
  472. abi, err := JSON(strings.NewReader(jsondata2))
  473. if err != nil {
  474. t.Error(err)
  475. t.FailNow()
  476. }
  477. if _, err := abi.Pack("bool", true); err != nil {
  478. t.Error(err)
  479. }
  480. }
  481. func TestTestSlice(t *testing.T) {
  482. abi, err := JSON(strings.NewReader(jsondata2))
  483. if err != nil {
  484. t.Error(err)
  485. t.FailNow()
  486. }
  487. slice := make([]uint64, 2)
  488. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  489. t.Error(err)
  490. }
  491. if _, err := abi.Pack("uint64[]", slice); err != nil {
  492. t.Error(err)
  493. }
  494. }
  495. func TestMethodSignature(t *testing.T) {
  496. String, _ := NewType("string")
  497. m := Method{"foo", false, []Argument{Argument{"bar", String, false}, Argument{"baz", String, false}}, nil}
  498. exp := "foo(string,string)"
  499. if m.Sig() != exp {
  500. t.Error("signature mismatch", exp, "!=", m.Sig())
  501. }
  502. idexp := crypto.Keccak256([]byte(exp))[:4]
  503. if !bytes.Equal(m.Id(), idexp) {
  504. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  505. }
  506. uintt, _ := NewType("uint")
  507. m = Method{"foo", false, []Argument{Argument{"bar", uintt, false}}, nil}
  508. exp = "foo(uint256)"
  509. if m.Sig() != exp {
  510. t.Error("signature mismatch", exp, "!=", m.Sig())
  511. }
  512. }
  513. func TestMultiPack(t *testing.T) {
  514. abi, err := JSON(strings.NewReader(jsondata2))
  515. if err != nil {
  516. t.Error(err)
  517. t.FailNow()
  518. }
  519. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  520. sig = append(sig, make([]byte, 64)...)
  521. sig[35] = 10
  522. sig[67] = 11
  523. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  524. if err != nil {
  525. t.Error(err)
  526. t.FailNow()
  527. }
  528. if !bytes.Equal(packed, sig) {
  529. t.Errorf("expected %x got %x", sig, packed)
  530. }
  531. }
  532. func ExampleJSON() {
  533. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  534. abi, err := JSON(strings.NewReader(definition))
  535. if err != nil {
  536. log.Fatalln(err)
  537. }
  538. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  539. if err != nil {
  540. log.Fatalln(err)
  541. }
  542. fmt.Printf("%x\n", out)
  543. // Output:
  544. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  545. }
  546. func TestInputVariableInputLength(t *testing.T) {
  547. const definition = `[
  548. { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
  549. { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
  550. { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
  551. ]`
  552. abi, err := JSON(strings.NewReader(definition))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. // test one string
  557. strin := "hello world"
  558. strpack, err := abi.Pack("strOne", strin)
  559. if err != nil {
  560. t.Error(err)
  561. }
  562. offset := make([]byte, 32)
  563. offset[31] = 32
  564. length := make([]byte, 32)
  565. length[31] = byte(len(strin))
  566. value := common.RightPadBytes([]byte(strin), 32)
  567. exp := append(offset, append(length, value...)...)
  568. // ignore first 4 bytes of the output. This is the function identifier
  569. strpack = strpack[4:]
  570. if !bytes.Equal(strpack, exp) {
  571. t.Errorf("expected %x, got %x\n", exp, strpack)
  572. }
  573. // test one bytes
  574. btspack, err := abi.Pack("bytesOne", []byte(strin))
  575. if err != nil {
  576. t.Error(err)
  577. }
  578. // ignore first 4 bytes of the output. This is the function identifier
  579. btspack = btspack[4:]
  580. if !bytes.Equal(btspack, exp) {
  581. t.Errorf("expected %x, got %x\n", exp, btspack)
  582. }
  583. // test two strings
  584. str1 := "hello"
  585. str2 := "world"
  586. str2pack, err := abi.Pack("strTwo", str1, str2)
  587. if err != nil {
  588. t.Error(err)
  589. }
  590. offset1 := make([]byte, 32)
  591. offset1[31] = 64
  592. length1 := make([]byte, 32)
  593. length1[31] = byte(len(str1))
  594. value1 := common.RightPadBytes([]byte(str1), 32)
  595. offset2 := make([]byte, 32)
  596. offset2[31] = 128
  597. length2 := make([]byte, 32)
  598. length2[31] = byte(len(str2))
  599. value2 := common.RightPadBytes([]byte(str2), 32)
  600. exp2 := append(offset1, offset2...)
  601. exp2 = append(exp2, append(length1, value1...)...)
  602. exp2 = append(exp2, append(length2, value2...)...)
  603. // ignore first 4 bytes of the output. This is the function identifier
  604. str2pack = str2pack[4:]
  605. if !bytes.Equal(str2pack, exp2) {
  606. t.Errorf("expected %x, got %x\n", exp, str2pack)
  607. }
  608. // test two strings, first > 32, second < 32
  609. str1 = strings.Repeat("a", 33)
  610. str2pack, err = abi.Pack("strTwo", str1, str2)
  611. if err != nil {
  612. t.Error(err)
  613. }
  614. offset1 = make([]byte, 32)
  615. offset1[31] = 64
  616. length1 = make([]byte, 32)
  617. length1[31] = byte(len(str1))
  618. value1 = common.RightPadBytes([]byte(str1), 64)
  619. offset2[31] = 160
  620. exp2 = append(offset1, offset2...)
  621. exp2 = append(exp2, append(length1, value1...)...)
  622. exp2 = append(exp2, append(length2, value2...)...)
  623. // ignore first 4 bytes of the output. This is the function identifier
  624. str2pack = str2pack[4:]
  625. if !bytes.Equal(str2pack, exp2) {
  626. t.Errorf("expected %x, got %x\n", exp, str2pack)
  627. }
  628. // test two strings, first > 32, second >32
  629. str1 = strings.Repeat("a", 33)
  630. str2 = strings.Repeat("a", 33)
  631. str2pack, err = abi.Pack("strTwo", str1, str2)
  632. if err != nil {
  633. t.Error(err)
  634. }
  635. offset1 = make([]byte, 32)
  636. offset1[31] = 64
  637. length1 = make([]byte, 32)
  638. length1[31] = byte(len(str1))
  639. value1 = common.RightPadBytes([]byte(str1), 64)
  640. offset2 = make([]byte, 32)
  641. offset2[31] = 160
  642. length2 = make([]byte, 32)
  643. length2[31] = byte(len(str2))
  644. value2 = common.RightPadBytes([]byte(str2), 64)
  645. exp2 = append(offset1, offset2...)
  646. exp2 = append(exp2, append(length1, value1...)...)
  647. exp2 = append(exp2, append(length2, value2...)...)
  648. // ignore first 4 bytes of the output. This is the function identifier
  649. str2pack = str2pack[4:]
  650. if !bytes.Equal(str2pack, exp2) {
  651. t.Errorf("expected %x, got %x\n", exp, str2pack)
  652. }
  653. }
  654. func TestDefaultFunctionParsing(t *testing.T) {
  655. const definition = `[{ "name" : "balance" }]`
  656. abi, err := JSON(strings.NewReader(definition))
  657. if err != nil {
  658. t.Fatal(err)
  659. }
  660. if _, ok := abi.Methods["balance"]; !ok {
  661. t.Error("expected 'balance' to be present")
  662. }
  663. }
  664. func TestBareEvents(t *testing.T) {
  665. const definition = `[
  666. { "type" : "event", "name" : "balance" },
  667. { "type" : "event", "name" : "anon", "anonymous" : true},
  668. { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
  669. ]`
  670. arg0, _ := NewType("uint256")
  671. arg1, _ := NewType("address")
  672. expectedEvents := map[string]struct {
  673. Anonymous bool
  674. Args []Argument
  675. }{
  676. "balance": {false, nil},
  677. "anon": {true, nil},
  678. "args": {false, []Argument{
  679. Argument{Name: "arg0", Type: arg0, Indexed: false},
  680. Argument{Name: "arg1", Type: arg1, Indexed: true},
  681. }},
  682. }
  683. abi, err := JSON(strings.NewReader(definition))
  684. if err != nil {
  685. t.Fatal(err)
  686. }
  687. if len(abi.Events) != len(expectedEvents) {
  688. t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
  689. }
  690. for name, exp := range expectedEvents {
  691. got, ok := abi.Events[name]
  692. if !ok {
  693. t.Errorf("could not found event %s", name)
  694. continue
  695. }
  696. if got.Anonymous != exp.Anonymous {
  697. t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
  698. }
  699. if len(got.Inputs) != len(exp.Args) {
  700. t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
  701. continue
  702. }
  703. for i, arg := range exp.Args {
  704. if arg.Name != got.Inputs[i].Name {
  705. t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
  706. }
  707. if arg.Indexed != got.Inputs[i].Indexed {
  708. t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
  709. }
  710. if arg.Type.T != got.Inputs[i].Type.T {
  711. t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
  712. }
  713. }
  714. }
  715. }
  716. func TestMultiReturnWithStruct(t *testing.T) {
  717. const definition = `[
  718. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  719. abi, err := JSON(strings.NewReader(definition))
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. // using buff to make the code readable
  724. buff := new(bytes.Buffer)
  725. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  726. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  727. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  728. stringOut := "hello"
  729. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  730. var inter struct {
  731. Int *big.Int
  732. String string
  733. }
  734. err = abi.Unpack(&inter, "multi", buff.Bytes())
  735. if err != nil {
  736. t.Error(err)
  737. }
  738. if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
  739. t.Error("expected Int to be 1 got", inter.Int)
  740. }
  741. if inter.String != stringOut {
  742. t.Error("expected String to be", stringOut, "got", inter.String)
  743. }
  744. var reversed struct {
  745. String string
  746. Int *big.Int
  747. }
  748. err = abi.Unpack(&reversed, "multi", buff.Bytes())
  749. if err != nil {
  750. t.Error(err)
  751. }
  752. if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
  753. t.Error("expected Int to be 1 got", reversed.Int)
  754. }
  755. if reversed.String != stringOut {
  756. t.Error("expected String to be", stringOut, "got", reversed.String)
  757. }
  758. }
  759. func TestMultiReturnWithSlice(t *testing.T) {
  760. const definition = `[
  761. { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
  762. abi, err := JSON(strings.NewReader(definition))
  763. if err != nil {
  764. t.Fatal(err)
  765. }
  766. // using buff to make the code readable
  767. buff := new(bytes.Buffer)
  768. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  769. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  770. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  771. stringOut := "hello"
  772. buff.Write(common.RightPadBytes([]byte(stringOut), 32))
  773. var inter []interface{}
  774. err = abi.Unpack(&inter, "multi", buff.Bytes())
  775. if err != nil {
  776. t.Error(err)
  777. }
  778. if len(inter) != 2 {
  779. t.Fatal("expected 2 results got", len(inter))
  780. }
  781. if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 {
  782. t.Error("expected index 0 to be 1 got", num)
  783. }
  784. if str, ok := inter[1].(string); !ok || str != stringOut {
  785. t.Error("expected index 1 to be", stringOut, "got", str)
  786. }
  787. }
  788. func TestMarshalArrays(t *testing.T) {
  789. const definition = `[
  790. { "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  791. { "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] }
  792. ]`
  793. abi, err := JSON(strings.NewReader(definition))
  794. if err != nil {
  795. t.Fatal(err)
  796. }
  797. output := common.LeftPadBytes([]byte{1}, 32)
  798. var bytes10 [10]byte
  799. err = abi.Unpack(&bytes10, "bytes32", output)
  800. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  801. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  802. }
  803. var bytes32 [32]byte
  804. err = abi.Unpack(&bytes32, "bytes32", output)
  805. if err != nil {
  806. t.Error("didn't expect error:", err)
  807. }
  808. if !bytes.Equal(bytes32[:], output) {
  809. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  810. }
  811. type (
  812. B10 [10]byte
  813. B32 [32]byte
  814. )
  815. var b10 B10
  816. err = abi.Unpack(&b10, "bytes32", output)
  817. if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
  818. t.Error("expected error or bytes32 not be assignable to bytes10:", err)
  819. }
  820. var b32 B32
  821. err = abi.Unpack(&b32, "bytes32", output)
  822. if err != nil {
  823. t.Error("didn't expect error:", err)
  824. }
  825. if !bytes.Equal(b32[:], output) {
  826. t.Error("expected bytes32[31] to be 1 got", bytes32[31])
  827. }
  828. output[10] = 1
  829. var shortAssignLong [32]byte
  830. err = abi.Unpack(&shortAssignLong, "bytes10", output)
  831. if err != nil {
  832. t.Error("didn't expect error:", err)
  833. }
  834. if !bytes.Equal(output, shortAssignLong[:]) {
  835. t.Errorf("expected %x to be %x", shortAssignLong, output)
  836. }
  837. }
  838. func TestUnmarshal(t *testing.T) {
  839. const definition = `[
  840. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  841. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  842. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  843. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  844. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  845. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  846. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  847. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  848. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  849. abi, err := JSON(strings.NewReader(definition))
  850. if err != nil {
  851. t.Fatal(err)
  852. }
  853. buff := new(bytes.Buffer)
  854. // marshal int
  855. var Int *big.Int
  856. err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  857. if err != nil {
  858. t.Error(err)
  859. }
  860. if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
  861. t.Error("expected Int to be 1 got", Int)
  862. }
  863. // marshal bool
  864. var Bool bool
  865. err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  866. if err != nil {
  867. t.Error(err)
  868. }
  869. if !Bool {
  870. t.Error("expected Bool to be true")
  871. }
  872. // marshal dynamic bytes max length 32
  873. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  874. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  875. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  876. buff.Write(bytesOut)
  877. var Bytes []byte
  878. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  879. if err != nil {
  880. t.Error(err)
  881. }
  882. if !bytes.Equal(Bytes, bytesOut) {
  883. t.Errorf("expected %x got %x", bytesOut, Bytes)
  884. }
  885. // marshall dynamic bytes max length 64
  886. buff.Reset()
  887. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  888. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  889. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  890. buff.Write(bytesOut)
  891. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  892. if err != nil {
  893. t.Error(err)
  894. }
  895. if !bytes.Equal(Bytes, bytesOut) {
  896. t.Errorf("expected %x got %x", bytesOut, Bytes)
  897. }
  898. // marshall dynamic bytes max length 63
  899. buff.Reset()
  900. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  901. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  902. bytesOut = common.RightPadBytes([]byte("hello"), 63)
  903. buff.Write(bytesOut)
  904. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  905. if err != nil {
  906. t.Error(err)
  907. }
  908. if !bytes.Equal(Bytes, bytesOut) {
  909. t.Errorf("expected %x got %x", bytesOut, Bytes)
  910. }
  911. // marshal dynamic bytes output empty
  912. err = abi.Unpack(&Bytes, "bytes", nil)
  913. if err == nil {
  914. t.Error("expected error")
  915. }
  916. // marshal dynamic bytes length 5
  917. buff.Reset()
  918. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  919. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  920. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  921. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  922. if err != nil {
  923. t.Error(err)
  924. }
  925. if !bytes.Equal(Bytes, []byte("hello")) {
  926. t.Errorf("expected %x got %x", bytesOut, Bytes)
  927. }
  928. // marshal dynamic bytes length 5
  929. buff.Reset()
  930. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  931. var hash common.Hash
  932. err = abi.Unpack(&hash, "fixed", buff.Bytes())
  933. if err != nil {
  934. t.Error(err)
  935. }
  936. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  937. if hash != helloHash {
  938. t.Errorf("Expected %x to equal %x", hash, helloHash)
  939. }
  940. // marshal error
  941. buff.Reset()
  942. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  943. err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
  944. if err == nil {
  945. t.Error("expected error")
  946. }
  947. err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
  948. if err == nil {
  949. t.Error("expected error")
  950. }
  951. // marshal mixed bytes
  952. buff.Reset()
  953. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  954. fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")
  955. buff.Write(fixed)
  956. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  957. bytesOut = common.RightPadBytes([]byte("hello"), 32)
  958. buff.Write(bytesOut)
  959. var out []interface{}
  960. err = abi.Unpack(&out, "mixedBytes", buff.Bytes())
  961. if err != nil {
  962. t.Fatal("didn't expect error:", err)
  963. }
  964. if !bytes.Equal(bytesOut, out[0].([]byte)) {
  965. t.Errorf("expected %x, got %x", bytesOut, out[0])
  966. }
  967. if !bytes.Equal(fixed, out[1].([]byte)) {
  968. t.Errorf("expected %x, got %x", fixed, out[1])
  969. }
  970. buff.Reset()
  971. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  972. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  973. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  974. // marshal int array
  975. var intArray [3]*big.Int
  976. err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
  977. if err != nil {
  978. t.Error(err)
  979. }
  980. var testAgainstIntArray [3]*big.Int
  981. testAgainstIntArray[0] = big.NewInt(1)
  982. testAgainstIntArray[1] = big.NewInt(2)
  983. testAgainstIntArray[2] = big.NewInt(3)
  984. for i, Int := range intArray {
  985. if Int.Cmp(testAgainstIntArray[i]) != 0 {
  986. t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
  987. }
  988. }
  989. // marshal address slice
  990. buff.Reset()
  991. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  992. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  993. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  994. var outAddr []common.Address
  995. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  996. if err != nil {
  997. t.Fatal("didn't expect error:", err)
  998. }
  999. if len(outAddr) != 1 {
  1000. t.Fatal("expected 1 item, got", len(outAddr))
  1001. }
  1002. if outAddr[0] != (common.Address{1}) {
  1003. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  1004. }
  1005. // marshal multiple address slice
  1006. buff.Reset()
  1007. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  1008. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  1009. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  1010. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  1011. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  1012. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  1013. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  1014. var outAddrStruct struct {
  1015. A []common.Address
  1016. B []common.Address
  1017. }
  1018. err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
  1019. if err != nil {
  1020. t.Fatal("didn't expect error:", err)
  1021. }
  1022. if len(outAddrStruct.A) != 1 {
  1023. t.Fatal("expected 1 item, got", len(outAddrStruct.A))
  1024. }
  1025. if outAddrStruct.A[0] != (common.Address{1}) {
  1026. t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
  1027. }
  1028. if len(outAddrStruct.B) != 2 {
  1029. t.Fatal("expected 1 item, got", len(outAddrStruct.B))
  1030. }
  1031. if outAddrStruct.B[0] != (common.Address{2}) {
  1032. t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
  1033. }
  1034. if outAddrStruct.B[1] != (common.Address{3}) {
  1035. t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
  1036. }
  1037. // marshal invalid address slice
  1038. buff.Reset()
  1039. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  1040. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  1041. if err == nil {
  1042. t.Fatal("expected error:", err)
  1043. }
  1044. }