abi_test.go 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. "encoding/hex"
  20. "fmt"
  21. "log"
  22. "math/big"
  23. "reflect"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. )
  29. const jsondata = `
  30. [
  31. { "type" : "function", "name" : "balance", "constant" : true },
  32. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
  33. ]`
  34. const jsondata2 = `
  35. [
  36. { "type" : "function", "name" : "balance", "constant" : true },
  37. { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
  38. { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
  39. { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
  40. { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
  41. { "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
  42. { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
  43. { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
  44. { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
  45. { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
  46. { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
  47. { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
  48. { "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
  49. { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] },
  50. { "type" : "function", "name" : "nestedArray", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint256[2][2]" }, { "name" : "b", "type" : "address[]" } ] },
  51. { "type" : "function", "name" : "nestedArray2", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][2]" } ] },
  52. { "type" : "function", "name" : "nestedSlice", "constant" : false, "inputs" : [ { "name" : "a", "type" : "uint8[][]" } ] }
  53. ]`
  54. func TestReader(t *testing.T) {
  55. Uint256, _ := NewType("uint256", nil)
  56. exp := ABI{
  57. Methods: map[string]Method{
  58. "balance": {
  59. "balance", true, nil, nil,
  60. },
  61. "send": {
  62. "send", false, []Argument{
  63. {"amount", Uint256, false},
  64. }, nil,
  65. },
  66. },
  67. }
  68. abi, err := JSON(strings.NewReader(jsondata))
  69. if err != nil {
  70. t.Error(err)
  71. }
  72. // deep equal fails for some reason
  73. for name, expM := range exp.Methods {
  74. gotM, exist := abi.Methods[name]
  75. if !exist {
  76. t.Errorf("Missing expected method %v", name)
  77. }
  78. if !reflect.DeepEqual(gotM, expM) {
  79. t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
  80. }
  81. }
  82. for name, gotM := range abi.Methods {
  83. expM, exist := exp.Methods[name]
  84. if !exist {
  85. t.Errorf("Found extra method %v", name)
  86. }
  87. if !reflect.DeepEqual(gotM, expM) {
  88. t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
  89. }
  90. }
  91. }
  92. func TestTestNumbers(t *testing.T) {
  93. abi, err := JSON(strings.NewReader(jsondata2))
  94. if err != nil {
  95. t.Error(err)
  96. t.FailNow()
  97. }
  98. if _, err := abi.Pack("balance"); err != nil {
  99. t.Error(err)
  100. }
  101. if _, err := abi.Pack("balance", 1); err == nil {
  102. t.Error("expected error for balance(1)")
  103. }
  104. if _, err := abi.Pack("doesntexist", nil); err == nil {
  105. t.Errorf("doesntexist shouldn't exist")
  106. }
  107. if _, err := abi.Pack("doesntexist", 1); err == nil {
  108. t.Errorf("doesntexist(1) shouldn't exist")
  109. }
  110. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  111. t.Error(err)
  112. }
  113. i := new(int)
  114. *i = 1000
  115. if _, err := abi.Pack("send", i); err == nil {
  116. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  117. }
  118. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  119. t.Error(err)
  120. }
  121. }
  122. func TestTestString(t *testing.T) {
  123. abi, err := JSON(strings.NewReader(jsondata2))
  124. if err != nil {
  125. t.Error(err)
  126. t.FailNow()
  127. }
  128. if _, err := abi.Pack("string", "hello world"); err != nil {
  129. t.Error(err)
  130. }
  131. }
  132. func TestTestBool(t *testing.T) {
  133. abi, err := JSON(strings.NewReader(jsondata2))
  134. if err != nil {
  135. t.Error(err)
  136. t.FailNow()
  137. }
  138. if _, err := abi.Pack("bool", true); err != nil {
  139. t.Error(err)
  140. }
  141. }
  142. func TestTestSlice(t *testing.T) {
  143. abi, err := JSON(strings.NewReader(jsondata2))
  144. if err != nil {
  145. t.Error(err)
  146. t.FailNow()
  147. }
  148. slice := make([]uint64, 2)
  149. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  150. t.Error(err)
  151. }
  152. if _, err := abi.Pack("uint64[]", slice); err != nil {
  153. t.Error(err)
  154. }
  155. }
  156. func TestMethodSignature(t *testing.T) {
  157. String, _ := NewType("string", nil)
  158. m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
  159. exp := "foo(string,string)"
  160. if m.Sig() != exp {
  161. t.Error("signature mismatch", exp, "!=", m.Sig())
  162. }
  163. idexp := crypto.Keccak256([]byte(exp))[:4]
  164. if !bytes.Equal(m.Id(), idexp) {
  165. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  166. }
  167. uintt, _ := NewType("uint256", nil)
  168. m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
  169. exp = "foo(uint256)"
  170. if m.Sig() != exp {
  171. t.Error("signature mismatch", exp, "!=", m.Sig())
  172. }
  173. // Method with tuple arguments
  174. s, _ := NewType("tuple", []ArgumentMarshaling{
  175. {Name: "a", Type: "int256"},
  176. {Name: "b", Type: "int256[]"},
  177. {Name: "c", Type: "tuple[]", Components: []ArgumentMarshaling{
  178. {Name: "x", Type: "int256"},
  179. {Name: "y", Type: "int256"},
  180. }},
  181. {Name: "d", Type: "tuple[2]", Components: []ArgumentMarshaling{
  182. {Name: "x", Type: "int256"},
  183. {Name: "y", Type: "int256"},
  184. }},
  185. })
  186. m = Method{"foo", false, []Argument{{"s", s, false}, {"bar", String, false}}, nil}
  187. exp = "foo((int256,int256[],(int256,int256)[],(int256,int256)[2]),string)"
  188. if m.Sig() != exp {
  189. t.Error("signature mismatch", exp, "!=", m.Sig())
  190. }
  191. }
  192. func TestMultiPack(t *testing.T) {
  193. abi, err := JSON(strings.NewReader(jsondata2))
  194. if err != nil {
  195. t.Error(err)
  196. t.FailNow()
  197. }
  198. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  199. sig = append(sig, make([]byte, 64)...)
  200. sig[35] = 10
  201. sig[67] = 11
  202. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  203. if err != nil {
  204. t.Error(err)
  205. t.FailNow()
  206. }
  207. if !bytes.Equal(packed, sig) {
  208. t.Errorf("expected %x got %x", sig, packed)
  209. }
  210. }
  211. func ExampleJSON() {
  212. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  213. abi, err := JSON(strings.NewReader(definition))
  214. if err != nil {
  215. log.Fatalln(err)
  216. }
  217. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  218. if err != nil {
  219. log.Fatalln(err)
  220. }
  221. fmt.Printf("%x\n", out)
  222. // Output:
  223. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  224. }
  225. func TestInputVariableInputLength(t *testing.T) {
  226. const definition = `[
  227. { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
  228. { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
  229. { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
  230. ]`
  231. abi, err := JSON(strings.NewReader(definition))
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. // test one string
  236. strin := "hello world"
  237. strpack, err := abi.Pack("strOne", strin)
  238. if err != nil {
  239. t.Error(err)
  240. }
  241. offset := make([]byte, 32)
  242. offset[31] = 32
  243. length := make([]byte, 32)
  244. length[31] = byte(len(strin))
  245. value := common.RightPadBytes([]byte(strin), 32)
  246. exp := append(offset, append(length, value...)...)
  247. // ignore first 4 bytes of the output. This is the function identifier
  248. strpack = strpack[4:]
  249. if !bytes.Equal(strpack, exp) {
  250. t.Errorf("expected %x, got %x\n", exp, strpack)
  251. }
  252. // test one bytes
  253. btspack, err := abi.Pack("bytesOne", []byte(strin))
  254. if err != nil {
  255. t.Error(err)
  256. }
  257. // ignore first 4 bytes of the output. This is the function identifier
  258. btspack = btspack[4:]
  259. if !bytes.Equal(btspack, exp) {
  260. t.Errorf("expected %x, got %x\n", exp, btspack)
  261. }
  262. // test two strings
  263. str1 := "hello"
  264. str2 := "world"
  265. str2pack, err := abi.Pack("strTwo", str1, str2)
  266. if err != nil {
  267. t.Error(err)
  268. }
  269. offset1 := make([]byte, 32)
  270. offset1[31] = 64
  271. length1 := make([]byte, 32)
  272. length1[31] = byte(len(str1))
  273. value1 := common.RightPadBytes([]byte(str1), 32)
  274. offset2 := make([]byte, 32)
  275. offset2[31] = 128
  276. length2 := make([]byte, 32)
  277. length2[31] = byte(len(str2))
  278. value2 := common.RightPadBytes([]byte(str2), 32)
  279. exp2 := append(offset1, offset2...)
  280. exp2 = append(exp2, append(length1, value1...)...)
  281. exp2 = append(exp2, append(length2, value2...)...)
  282. // ignore first 4 bytes of the output. This is the function identifier
  283. str2pack = str2pack[4:]
  284. if !bytes.Equal(str2pack, exp2) {
  285. t.Errorf("expected %x, got %x\n", exp, str2pack)
  286. }
  287. // test two strings, first > 32, second < 32
  288. str1 = strings.Repeat("a", 33)
  289. str2pack, err = abi.Pack("strTwo", str1, str2)
  290. if err != nil {
  291. t.Error(err)
  292. }
  293. offset1 = make([]byte, 32)
  294. offset1[31] = 64
  295. length1 = make([]byte, 32)
  296. length1[31] = byte(len(str1))
  297. value1 = common.RightPadBytes([]byte(str1), 64)
  298. offset2[31] = 160
  299. exp2 = append(offset1, offset2...)
  300. exp2 = append(exp2, append(length1, value1...)...)
  301. exp2 = append(exp2, append(length2, value2...)...)
  302. // ignore first 4 bytes of the output. This is the function identifier
  303. str2pack = str2pack[4:]
  304. if !bytes.Equal(str2pack, exp2) {
  305. t.Errorf("expected %x, got %x\n", exp, str2pack)
  306. }
  307. // test two strings, first > 32, second >32
  308. str1 = strings.Repeat("a", 33)
  309. str2 = strings.Repeat("a", 33)
  310. str2pack, err = abi.Pack("strTwo", str1, str2)
  311. if err != nil {
  312. t.Error(err)
  313. }
  314. offset1 = make([]byte, 32)
  315. offset1[31] = 64
  316. length1 = make([]byte, 32)
  317. length1[31] = byte(len(str1))
  318. value1 = common.RightPadBytes([]byte(str1), 64)
  319. offset2 = make([]byte, 32)
  320. offset2[31] = 160
  321. length2 = make([]byte, 32)
  322. length2[31] = byte(len(str2))
  323. value2 = common.RightPadBytes([]byte(str2), 64)
  324. exp2 = append(offset1, offset2...)
  325. exp2 = append(exp2, append(length1, value1...)...)
  326. exp2 = append(exp2, append(length2, value2...)...)
  327. // ignore first 4 bytes of the output. This is the function identifier
  328. str2pack = str2pack[4:]
  329. if !bytes.Equal(str2pack, exp2) {
  330. t.Errorf("expected %x, got %x\n", exp, str2pack)
  331. }
  332. }
  333. func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
  334. const definition = `[
  335. { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
  336. { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
  337. { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
  338. { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
  339. { "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] }
  340. ]`
  341. abi, err := JSON(strings.NewReader(definition))
  342. if err != nil {
  343. t.Error(err)
  344. }
  345. // test string, fixed array uint256[2]
  346. strin := "hello world"
  347. arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  348. fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin)
  349. if err != nil {
  350. t.Error(err)
  351. }
  352. // generate expected output
  353. offset := make([]byte, 32)
  354. offset[31] = 96
  355. length := make([]byte, 32)
  356. length[31] = byte(len(strin))
  357. strvalue := common.RightPadBytes([]byte(strin), 32)
  358. arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32)
  359. arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32)
  360. exp := append(offset, arrinvalue1...)
  361. exp = append(exp, arrinvalue2...)
  362. exp = append(exp, append(length, strvalue...)...)
  363. // ignore first 4 bytes of the output. This is the function identifier
  364. fixedArrStrPack = fixedArrStrPack[4:]
  365. if !bytes.Equal(fixedArrStrPack, exp) {
  366. t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack)
  367. }
  368. // test byte array, fixed array uint256[2]
  369. bytesin := []byte(strin)
  370. arrin = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  371. fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin)
  372. if err != nil {
  373. t.Error(err)
  374. }
  375. // generate expected output
  376. offset = make([]byte, 32)
  377. offset[31] = 96
  378. length = make([]byte, 32)
  379. length[31] = byte(len(strin))
  380. strvalue = common.RightPadBytes([]byte(strin), 32)
  381. arrinvalue1 = common.LeftPadBytes(arrin[0].Bytes(), 32)
  382. arrinvalue2 = common.LeftPadBytes(arrin[1].Bytes(), 32)
  383. exp = append(offset, arrinvalue1...)
  384. exp = append(exp, arrinvalue2...)
  385. exp = append(exp, append(length, strvalue...)...)
  386. // ignore first 4 bytes of the output. This is the function identifier
  387. fixedArrBytesPack = fixedArrBytesPack[4:]
  388. if !bytes.Equal(fixedArrBytesPack, exp) {
  389. t.Errorf("expected %x, got %x\n", exp, fixedArrBytesPack)
  390. }
  391. // test string, fixed array uint256[2], dynamic array uint256[]
  392. strin = "hello world"
  393. fixedarrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  394. dynarrin := []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  395. mixedArrStrPack, err := abi.Pack("mixedArrStr", strin, fixedarrin, dynarrin)
  396. if err != nil {
  397. t.Error(err)
  398. }
  399. // generate expected output
  400. stroffset := make([]byte, 32)
  401. stroffset[31] = 128
  402. strlength := make([]byte, 32)
  403. strlength[31] = byte(len(strin))
  404. strvalue = common.RightPadBytes([]byte(strin), 32)
  405. fixedarrinvalue1 := common.LeftPadBytes(fixedarrin[0].Bytes(), 32)
  406. fixedarrinvalue2 := common.LeftPadBytes(fixedarrin[1].Bytes(), 32)
  407. dynarroffset := make([]byte, 32)
  408. dynarroffset[31] = byte(160 + ((len(strin)/32)+1)*32)
  409. dynarrlength := make([]byte, 32)
  410. dynarrlength[31] = byte(len(dynarrin))
  411. dynarrinvalue1 := common.LeftPadBytes(dynarrin[0].Bytes(), 32)
  412. dynarrinvalue2 := common.LeftPadBytes(dynarrin[1].Bytes(), 32)
  413. dynarrinvalue3 := common.LeftPadBytes(dynarrin[2].Bytes(), 32)
  414. exp = append(stroffset, fixedarrinvalue1...)
  415. exp = append(exp, fixedarrinvalue2...)
  416. exp = append(exp, dynarroffset...)
  417. exp = append(exp, append(strlength, strvalue...)...)
  418. dynarrarg := append(dynarrlength, dynarrinvalue1...)
  419. dynarrarg = append(dynarrarg, dynarrinvalue2...)
  420. dynarrarg = append(dynarrarg, dynarrinvalue3...)
  421. exp = append(exp, dynarrarg...)
  422. // ignore first 4 bytes of the output. This is the function identifier
  423. mixedArrStrPack = mixedArrStrPack[4:]
  424. if !bytes.Equal(mixedArrStrPack, exp) {
  425. t.Errorf("expected %x, got %x\n", exp, mixedArrStrPack)
  426. }
  427. // test string, fixed array uint256[2], fixed array uint256[3]
  428. strin = "hello world"
  429. fixedarrin1 := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  430. fixedarrin2 := [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  431. doubleFixedArrStrPack, err := abi.Pack("doubleFixedArrStr", strin, fixedarrin1, fixedarrin2)
  432. if err != nil {
  433. t.Error(err)
  434. }
  435. // generate expected output
  436. stroffset = make([]byte, 32)
  437. stroffset[31] = 192
  438. strlength = make([]byte, 32)
  439. strlength[31] = byte(len(strin))
  440. strvalue = common.RightPadBytes([]byte(strin), 32)
  441. fixedarrin1value1 := common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
  442. fixedarrin1value2 := common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
  443. fixedarrin2value1 := common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
  444. fixedarrin2value2 := common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
  445. fixedarrin2value3 := common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
  446. exp = append(stroffset, fixedarrin1value1...)
  447. exp = append(exp, fixedarrin1value2...)
  448. exp = append(exp, fixedarrin2value1...)
  449. exp = append(exp, fixedarrin2value2...)
  450. exp = append(exp, fixedarrin2value3...)
  451. exp = append(exp, append(strlength, strvalue...)...)
  452. // ignore first 4 bytes of the output. This is the function identifier
  453. doubleFixedArrStrPack = doubleFixedArrStrPack[4:]
  454. if !bytes.Equal(doubleFixedArrStrPack, exp) {
  455. t.Errorf("expected %x, got %x\n", exp, doubleFixedArrStrPack)
  456. }
  457. // test string, fixed array uint256[2], dynamic array uint256[], fixed array uint256[3]
  458. strin = "hello world"
  459. fixedarrin1 = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  460. dynarrin = []*big.Int{big.NewInt(1), big.NewInt(2)}
  461. fixedarrin2 = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  462. multipleMixedArrStrPack, err := abi.Pack("multipleMixedArrStr", strin, fixedarrin1, dynarrin, fixedarrin2)
  463. if err != nil {
  464. t.Error(err)
  465. }
  466. // generate expected output
  467. stroffset = make([]byte, 32)
  468. stroffset[31] = 224
  469. strlength = make([]byte, 32)
  470. strlength[31] = byte(len(strin))
  471. strvalue = common.RightPadBytes([]byte(strin), 32)
  472. fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
  473. fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
  474. dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
  475. dynarrlength = make([]byte, 32)
  476. dynarrlength[31] = byte(len(dynarrin))
  477. dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
  478. dynarrinvalue2 = common.LeftPadBytes(dynarrin[1].Bytes(), 32)
  479. fixedarrin2value1 = common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
  480. fixedarrin2value2 = common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
  481. fixedarrin2value3 = common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
  482. exp = append(stroffset, fixedarrin1value1...)
  483. exp = append(exp, fixedarrin1value2...)
  484. exp = append(exp, dynarroffset...)
  485. exp = append(exp, fixedarrin2value1...)
  486. exp = append(exp, fixedarrin2value2...)
  487. exp = append(exp, fixedarrin2value3...)
  488. exp = append(exp, append(strlength, strvalue...)...)
  489. dynarrarg = append(dynarrlength, dynarrinvalue1...)
  490. dynarrarg = append(dynarrarg, dynarrinvalue2...)
  491. exp = append(exp, dynarrarg...)
  492. // ignore first 4 bytes of the output. This is the function identifier
  493. multipleMixedArrStrPack = multipleMixedArrStrPack[4:]
  494. if !bytes.Equal(multipleMixedArrStrPack, exp) {
  495. t.Errorf("expected %x, got %x\n", exp, multipleMixedArrStrPack)
  496. }
  497. }
  498. func TestDefaultFunctionParsing(t *testing.T) {
  499. const definition = `[{ "name" : "balance" }]`
  500. abi, err := JSON(strings.NewReader(definition))
  501. if err != nil {
  502. t.Fatal(err)
  503. }
  504. if _, ok := abi.Methods["balance"]; !ok {
  505. t.Error("expected 'balance' to be present")
  506. }
  507. }
  508. func TestBareEvents(t *testing.T) {
  509. const definition = `[
  510. { "type" : "event", "name" : "balance" },
  511. { "type" : "event", "name" : "anon", "anonymous" : true},
  512. { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] },
  513. { "type" : "event", "name" : "tuple", "inputs" : [{ "indexed":false, "name":"t", "type":"tuple", "components":[{"name":"a", "type":"uint256"}] }, { "indexed":true, "name":"arg1", "type":"address" }] }
  514. ]`
  515. arg0, _ := NewType("uint256", nil)
  516. arg1, _ := NewType("address", nil)
  517. tuple, _ := NewType("tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}})
  518. expectedEvents := map[string]struct {
  519. Anonymous bool
  520. Args []Argument
  521. }{
  522. "balance": {false, nil},
  523. "anon": {true, nil},
  524. "args": {false, []Argument{
  525. {Name: "arg0", Type: arg0, Indexed: false},
  526. {Name: "arg1", Type: arg1, Indexed: true},
  527. }},
  528. "tuple": {false, []Argument{
  529. {Name: "t", Type: tuple, Indexed: false},
  530. {Name: "arg1", Type: arg1, Indexed: true},
  531. }},
  532. }
  533. abi, err := JSON(strings.NewReader(definition))
  534. if err != nil {
  535. t.Fatal(err)
  536. }
  537. if len(abi.Events) != len(expectedEvents) {
  538. t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
  539. }
  540. for name, exp := range expectedEvents {
  541. got, ok := abi.Events[name]
  542. if !ok {
  543. t.Errorf("could not found event %s", name)
  544. continue
  545. }
  546. if got.Anonymous != exp.Anonymous {
  547. t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
  548. }
  549. if len(got.Inputs) != len(exp.Args) {
  550. t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
  551. continue
  552. }
  553. for i, arg := range exp.Args {
  554. if arg.Name != got.Inputs[i].Name {
  555. t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
  556. }
  557. if arg.Indexed != got.Inputs[i].Indexed {
  558. t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
  559. }
  560. if arg.Type.T != got.Inputs[i].Type.T {
  561. t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
  562. }
  563. }
  564. }
  565. }
  566. // TestUnpackEvent is based on this contract:
  567. // contract T {
  568. // event received(address sender, uint amount, bytes memo);
  569. // event receivedAddr(address sender);
  570. // function receive(bytes memo) external payable {
  571. // received(msg.sender, msg.value, memo);
  572. // receivedAddr(msg.sender);
  573. // }
  574. // }
  575. // When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
  576. // receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
  577. func TestUnpackEvent(t *testing.T) {
  578. const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
  579. abi, err := JSON(strings.NewReader(abiJSON))
  580. if err != nil {
  581. t.Fatal(err)
  582. }
  583. const hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
  584. data, err := hex.DecodeString(hexdata)
  585. if err != nil {
  586. t.Fatal(err)
  587. }
  588. if len(data)%32 == 0 {
  589. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  590. }
  591. type ReceivedEvent struct {
  592. Sender common.Address
  593. Amount *big.Int
  594. Memo []byte
  595. }
  596. var ev ReceivedEvent
  597. err = abi.Unpack(&ev, "received", data)
  598. if err != nil {
  599. t.Error(err)
  600. }
  601. type ReceivedAddrEvent struct {
  602. Sender common.Address
  603. }
  604. var receivedAddrEv ReceivedAddrEvent
  605. err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
  606. if err != nil {
  607. t.Error(err)
  608. }
  609. }
  610. func TestUnpackEventIntoMap(t *testing.T) {
  611. const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
  612. abi, err := JSON(strings.NewReader(abiJSON))
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. const hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
  617. data, err := hex.DecodeString(hexdata)
  618. if err != nil {
  619. t.Fatal(err)
  620. }
  621. if len(data)%32 == 0 {
  622. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  623. }
  624. receivedMap := map[string]interface{}{}
  625. expectedReceivedMap := map[string]interface{}{
  626. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  627. "amount": big.NewInt(1),
  628. "memo": []byte{88},
  629. }
  630. if err := abi.UnpackIntoMap(receivedMap, "received", data); err != nil {
  631. t.Error(err)
  632. }
  633. if len(receivedMap) != 3 {
  634. t.Error("unpacked `received` map expected to have length 3")
  635. }
  636. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  637. t.Error("unpacked `received` map does not match expected map")
  638. }
  639. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  640. t.Error("unpacked `received` map does not match expected map")
  641. }
  642. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  643. t.Error("unpacked `received` map does not match expected map")
  644. }
  645. receivedAddrMap := map[string]interface{}{}
  646. if err = abi.UnpackIntoMap(receivedAddrMap, "receivedAddr", data); err != nil {
  647. t.Error(err)
  648. }
  649. if len(receivedAddrMap) != 1 {
  650. t.Error("unpacked `receivedAddr` map expected to have length 1")
  651. }
  652. if receivedAddrMap["sender"] != expectedReceivedMap["sender"] {
  653. t.Error("unpacked `receivedAddr` map does not match expected map")
  654. }
  655. }
  656. func TestUnpackMethodIntoMap(t *testing.T) {
  657. const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
  658. abi, err := JSON(strings.NewReader(abiJSON))
  659. if err != nil {
  660. t.Fatal(err)
  661. }
  662. const hexdata = `00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000015800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000158000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001580000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000015800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000158`
  663. data, err := hex.DecodeString(hexdata)
  664. if err != nil {
  665. t.Fatal(err)
  666. }
  667. if len(data)%32 != 0 {
  668. t.Errorf("len(data) is %d, want a multiple of 32", len(data))
  669. }
  670. // Tests a method with no outputs
  671. receiveMap := map[string]interface{}{}
  672. if err = abi.UnpackIntoMap(receiveMap, "receive", data); err != nil {
  673. t.Error(err)
  674. }
  675. if len(receiveMap) > 0 {
  676. t.Error("unpacked `receive` map expected to have length 0")
  677. }
  678. // Tests a method with only outputs
  679. sendMap := map[string]interface{}{}
  680. if err = abi.UnpackIntoMap(sendMap, "send", data); err != nil {
  681. t.Error(err)
  682. }
  683. if len(sendMap) != 1 {
  684. t.Error("unpacked `send` map expected to have length 1")
  685. }
  686. if sendMap["amount"].(*big.Int).Cmp(big.NewInt(1)) != 0 {
  687. t.Error("unpacked `send` map expected `amount` value of 1")
  688. }
  689. // Tests a method with outputs and inputs
  690. getMap := map[string]interface{}{}
  691. if err = abi.UnpackIntoMap(getMap, "get", data); err != nil {
  692. t.Error(err)
  693. }
  694. if len(sendMap) != 1 {
  695. t.Error("unpacked `get` map expected to have length 1")
  696. }
  697. expectedBytes := []byte{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, 96, 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, 1, 88, 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, 96, 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, 1, 88, 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, 96, 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, 1, 88, 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, 96, 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, 1, 88, 0}
  698. if !bytes.Equal(getMap["hash"].([]byte), expectedBytes) {
  699. t.Errorf("unpacked `get` map expected `hash` value of %v", expectedBytes)
  700. }
  701. }
  702. func TestUnpackIntoMapNamingConflict(t *testing.T) {
  703. // Two methods have the same name
  704. var abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"get","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"send","outputs":[{"name":"amount","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"}],"name":"get","outputs":[{"name":"hash","type":"bytes"}],"payable":true,"stateMutability":"payable","type":"function"}]`
  705. abi, err := JSON(strings.NewReader(abiJSON))
  706. if err != nil {
  707. t.Fatal(err)
  708. }
  709. var hexdata = `00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
  710. data, err := hex.DecodeString(hexdata)
  711. if err != nil {
  712. t.Fatal(err)
  713. }
  714. if len(data)%32 == 0 {
  715. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  716. }
  717. getMap := map[string]interface{}{}
  718. if err = abi.UnpackIntoMap(getMap, "get", data); err == nil {
  719. t.Error("naming conflict between two methods; error expected")
  720. }
  721. // Two events have the same name
  722. abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"received","type":"event"}]`
  723. abi, err = JSON(strings.NewReader(abiJSON))
  724. if err != nil {
  725. t.Fatal(err)
  726. }
  727. hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
  728. data, err = hex.DecodeString(hexdata)
  729. if err != nil {
  730. t.Fatal(err)
  731. }
  732. if len(data)%32 == 0 {
  733. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  734. }
  735. receivedMap := map[string]interface{}{}
  736. if err = abi.UnpackIntoMap(receivedMap, "received", data); err != nil {
  737. t.Error("naming conflict between two events; no error expected")
  738. }
  739. if len(receivedMap) != 1 {
  740. t.Error("naming conflict between two events; event defined latest in the abi expected to be used")
  741. }
  742. // Method and event have the same name
  743. abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"received","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
  744. abi, err = JSON(strings.NewReader(abiJSON))
  745. if err != nil {
  746. t.Fatal(err)
  747. }
  748. if len(data)%32 == 0 {
  749. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  750. }
  751. if err = abi.UnpackIntoMap(receivedMap, "received", data); err == nil {
  752. t.Error("naming conflict between an event and a method; error expected")
  753. }
  754. // Conflict is case sensitive
  755. abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"received","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"Received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
  756. abi, err = JSON(strings.NewReader(abiJSON))
  757. if err != nil {
  758. t.Fatal(err)
  759. }
  760. if len(data)%32 == 0 {
  761. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  762. }
  763. expectedReceivedMap := map[string]interface{}{
  764. "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
  765. "amount": big.NewInt(1),
  766. "memo": []byte{88},
  767. }
  768. if err = abi.UnpackIntoMap(receivedMap, "Received", data); err != nil {
  769. t.Error(err)
  770. }
  771. if len(receivedMap) != 3 {
  772. t.Error("unpacked `received` map expected to have length 3")
  773. }
  774. if receivedMap["sender"] != expectedReceivedMap["sender"] {
  775. t.Error("unpacked `received` map does not match expected map")
  776. }
  777. if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 {
  778. t.Error("unpacked `received` map does not match expected map")
  779. }
  780. if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
  781. t.Error("unpacked `received` map does not match expected map")
  782. }
  783. }
  784. func TestABI_MethodById(t *testing.T) {
  785. const abiJSON = `[
  786. {"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"},
  787. {"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]},
  788. {"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]},
  789. {"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]},
  790. {"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]},
  791. {"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]},
  792. {"type":"function","name":"multipleMixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"},{"name":"fixedArr2","type":"uint256[3]"}]},
  793. {"type":"function","name":"balance","constant":true},
  794. {"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]},
  795. {"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]},
  796. {"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]},
  797. {"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]},
  798. {"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]},
  799. {"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]},
  800. {"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]},
  801. {"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]},
  802. {"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]},
  803. {"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]},
  804. {"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]},
  805. {"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]},
  806. {"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]}
  807. ]
  808. `
  809. abi, err := JSON(strings.NewReader(abiJSON))
  810. if err != nil {
  811. t.Fatal(err)
  812. }
  813. for name, m := range abi.Methods {
  814. a := fmt.Sprintf("%v", m)
  815. m2, err := abi.MethodById(m.Id())
  816. if err != nil {
  817. t.Fatalf("Failed to look up ABI method: %v", err)
  818. }
  819. b := fmt.Sprintf("%v", m2)
  820. if a != b {
  821. t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
  822. }
  823. }
  824. // Also test empty
  825. if _, err := abi.MethodById([]byte{0x00}); err == nil {
  826. t.Errorf("Expected error, too short to decode data")
  827. }
  828. if _, err := abi.MethodById([]byte{}); err == nil {
  829. t.Errorf("Expected error, too short to decode data")
  830. }
  831. if _, err := abi.MethodById(nil); err == nil {
  832. t.Errorf("Expected error, nil is short to decode data")
  833. }
  834. }