abi_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. "strings"
  24. "testing"
  25. "reflect"
  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. ]`
  51. func TestReader(t *testing.T) {
  52. Uint256, _ := NewType("uint256")
  53. exp := ABI{
  54. Methods: map[string]Method{
  55. "balance": {
  56. "balance", true, nil, nil,
  57. },
  58. "send": {
  59. "send", false, []Argument{
  60. {"amount", Uint256, false},
  61. }, nil,
  62. },
  63. },
  64. }
  65. abi, err := JSON(strings.NewReader(jsondata))
  66. if err != nil {
  67. t.Error(err)
  68. }
  69. // deep equal fails for some reason
  70. for name, expM := range exp.Methods {
  71. gotM, exist := abi.Methods[name]
  72. if !exist {
  73. t.Errorf("Missing expected method %v", name)
  74. }
  75. if !reflect.DeepEqual(gotM, expM) {
  76. t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
  77. }
  78. }
  79. for name, gotM := range abi.Methods {
  80. expM, exist := exp.Methods[name]
  81. if !exist {
  82. t.Errorf("Found extra method %v", name)
  83. }
  84. if !reflect.DeepEqual(gotM, expM) {
  85. t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
  86. }
  87. }
  88. }
  89. func TestTestNumbers(t *testing.T) {
  90. abi, err := JSON(strings.NewReader(jsondata2))
  91. if err != nil {
  92. t.Error(err)
  93. t.FailNow()
  94. }
  95. if _, err := abi.Pack("balance"); err != nil {
  96. t.Error(err)
  97. }
  98. if _, err := abi.Pack("balance", 1); err == nil {
  99. t.Error("expected error for balance(1)")
  100. }
  101. if _, err := abi.Pack("doesntexist", nil); err == nil {
  102. t.Errorf("doesntexist shouldn't exist")
  103. }
  104. if _, err := abi.Pack("doesntexist", 1); err == nil {
  105. t.Errorf("doesntexist(1) shouldn't exist")
  106. }
  107. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  108. t.Error(err)
  109. }
  110. i := new(int)
  111. *i = 1000
  112. if _, err := abi.Pack("send", i); err == nil {
  113. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  114. }
  115. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  116. t.Error(err)
  117. }
  118. }
  119. func TestTestString(t *testing.T) {
  120. abi, err := JSON(strings.NewReader(jsondata2))
  121. if err != nil {
  122. t.Error(err)
  123. t.FailNow()
  124. }
  125. if _, err := abi.Pack("string", "hello world"); err != nil {
  126. t.Error(err)
  127. }
  128. }
  129. func TestTestBool(t *testing.T) {
  130. abi, err := JSON(strings.NewReader(jsondata2))
  131. if err != nil {
  132. t.Error(err)
  133. t.FailNow()
  134. }
  135. if _, err := abi.Pack("bool", true); err != nil {
  136. t.Error(err)
  137. }
  138. }
  139. func TestTestSlice(t *testing.T) {
  140. abi, err := JSON(strings.NewReader(jsondata2))
  141. if err != nil {
  142. t.Error(err)
  143. t.FailNow()
  144. }
  145. slice := make([]uint64, 2)
  146. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  147. t.Error(err)
  148. }
  149. if _, err := abi.Pack("uint64[]", slice); err != nil {
  150. t.Error(err)
  151. }
  152. }
  153. func TestMethodSignature(t *testing.T) {
  154. String, _ := NewType("string")
  155. m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
  156. exp := "foo(string,string)"
  157. if m.Sig() != exp {
  158. t.Error("signature mismatch", exp, "!=", m.Sig())
  159. }
  160. idexp := crypto.Keccak256([]byte(exp))[:4]
  161. if !bytes.Equal(m.Id(), idexp) {
  162. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  163. }
  164. uintt, _ := NewType("uint256")
  165. m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
  166. exp = "foo(uint256)"
  167. if m.Sig() != exp {
  168. t.Error("signature mismatch", exp, "!=", m.Sig())
  169. }
  170. }
  171. func TestMultiPack(t *testing.T) {
  172. abi, err := JSON(strings.NewReader(jsondata2))
  173. if err != nil {
  174. t.Error(err)
  175. t.FailNow()
  176. }
  177. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  178. sig = append(sig, make([]byte, 64)...)
  179. sig[35] = 10
  180. sig[67] = 11
  181. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  182. if err != nil {
  183. t.Error(err)
  184. t.FailNow()
  185. }
  186. if !bytes.Equal(packed, sig) {
  187. t.Errorf("expected %x got %x", sig, packed)
  188. }
  189. }
  190. func ExampleJSON() {
  191. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  192. abi, err := JSON(strings.NewReader(definition))
  193. if err != nil {
  194. log.Fatalln(err)
  195. }
  196. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  197. if err != nil {
  198. log.Fatalln(err)
  199. }
  200. fmt.Printf("%x\n", out)
  201. // Output:
  202. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  203. }
  204. func TestInputVariableInputLength(t *testing.T) {
  205. const definition = `[
  206. { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
  207. { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
  208. { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
  209. ]`
  210. abi, err := JSON(strings.NewReader(definition))
  211. if err != nil {
  212. t.Fatal(err)
  213. }
  214. // test one string
  215. strin := "hello world"
  216. strpack, err := abi.Pack("strOne", strin)
  217. if err != nil {
  218. t.Error(err)
  219. }
  220. offset := make([]byte, 32)
  221. offset[31] = 32
  222. length := make([]byte, 32)
  223. length[31] = byte(len(strin))
  224. value := common.RightPadBytes([]byte(strin), 32)
  225. exp := append(offset, append(length, value...)...)
  226. // ignore first 4 bytes of the output. This is the function identifier
  227. strpack = strpack[4:]
  228. if !bytes.Equal(strpack, exp) {
  229. t.Errorf("expected %x, got %x\n", exp, strpack)
  230. }
  231. // test one bytes
  232. btspack, err := abi.Pack("bytesOne", []byte(strin))
  233. if err != nil {
  234. t.Error(err)
  235. }
  236. // ignore first 4 bytes of the output. This is the function identifier
  237. btspack = btspack[4:]
  238. if !bytes.Equal(btspack, exp) {
  239. t.Errorf("expected %x, got %x\n", exp, btspack)
  240. }
  241. // test two strings
  242. str1 := "hello"
  243. str2 := "world"
  244. str2pack, err := abi.Pack("strTwo", str1, str2)
  245. if err != nil {
  246. t.Error(err)
  247. }
  248. offset1 := make([]byte, 32)
  249. offset1[31] = 64
  250. length1 := make([]byte, 32)
  251. length1[31] = byte(len(str1))
  252. value1 := common.RightPadBytes([]byte(str1), 32)
  253. offset2 := make([]byte, 32)
  254. offset2[31] = 128
  255. length2 := make([]byte, 32)
  256. length2[31] = byte(len(str2))
  257. value2 := common.RightPadBytes([]byte(str2), 32)
  258. exp2 := append(offset1, offset2...)
  259. exp2 = append(exp2, append(length1, value1...)...)
  260. exp2 = append(exp2, append(length2, value2...)...)
  261. // ignore first 4 bytes of the output. This is the function identifier
  262. str2pack = str2pack[4:]
  263. if !bytes.Equal(str2pack, exp2) {
  264. t.Errorf("expected %x, got %x\n", exp, str2pack)
  265. }
  266. // test two strings, first > 32, second < 32
  267. str1 = strings.Repeat("a", 33)
  268. str2pack, err = abi.Pack("strTwo", str1, str2)
  269. if err != nil {
  270. t.Error(err)
  271. }
  272. offset1 = make([]byte, 32)
  273. offset1[31] = 64
  274. length1 = make([]byte, 32)
  275. length1[31] = byte(len(str1))
  276. value1 = common.RightPadBytes([]byte(str1), 64)
  277. offset2[31] = 160
  278. exp2 = append(offset1, offset2...)
  279. exp2 = append(exp2, append(length1, value1...)...)
  280. exp2 = append(exp2, append(length2, value2...)...)
  281. // ignore first 4 bytes of the output. This is the function identifier
  282. str2pack = str2pack[4:]
  283. if !bytes.Equal(str2pack, exp2) {
  284. t.Errorf("expected %x, got %x\n", exp, str2pack)
  285. }
  286. // test two strings, first > 32, second >32
  287. str1 = strings.Repeat("a", 33)
  288. str2 = 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 = make([]byte, 32)
  299. offset2[31] = 160
  300. length2 = make([]byte, 32)
  301. length2[31] = byte(len(str2))
  302. value2 = common.RightPadBytes([]byte(str2), 64)
  303. exp2 = append(offset1, offset2...)
  304. exp2 = append(exp2, append(length1, value1...)...)
  305. exp2 = append(exp2, append(length2, value2...)...)
  306. // ignore first 4 bytes of the output. This is the function identifier
  307. str2pack = str2pack[4:]
  308. if !bytes.Equal(str2pack, exp2) {
  309. t.Errorf("expected %x, got %x\n", exp, str2pack)
  310. }
  311. }
  312. func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
  313. const definition = `[
  314. { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
  315. { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
  316. { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
  317. { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
  318. { "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]" } ] }
  319. ]`
  320. abi, err := JSON(strings.NewReader(definition))
  321. if err != nil {
  322. t.Error(err)
  323. }
  324. // test string, fixed array uint256[2]
  325. strin := "hello world"
  326. arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  327. fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin)
  328. if err != nil {
  329. t.Error(err)
  330. }
  331. // generate expected output
  332. offset := make([]byte, 32)
  333. offset[31] = 96
  334. length := make([]byte, 32)
  335. length[31] = byte(len(strin))
  336. strvalue := common.RightPadBytes([]byte(strin), 32)
  337. arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32)
  338. arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32)
  339. exp := append(offset, arrinvalue1...)
  340. exp = append(exp, arrinvalue2...)
  341. exp = append(exp, append(length, strvalue...)...)
  342. // ignore first 4 bytes of the output. This is the function identifier
  343. fixedArrStrPack = fixedArrStrPack[4:]
  344. if !bytes.Equal(fixedArrStrPack, exp) {
  345. t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack)
  346. }
  347. // test byte array, fixed array uint256[2]
  348. bytesin := []byte(strin)
  349. arrin = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  350. fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin)
  351. if err != nil {
  352. t.Error(err)
  353. }
  354. // generate expected output
  355. offset = make([]byte, 32)
  356. offset[31] = 96
  357. length = make([]byte, 32)
  358. length[31] = byte(len(strin))
  359. strvalue = common.RightPadBytes([]byte(strin), 32)
  360. arrinvalue1 = common.LeftPadBytes(arrin[0].Bytes(), 32)
  361. arrinvalue2 = common.LeftPadBytes(arrin[1].Bytes(), 32)
  362. exp = append(offset, arrinvalue1...)
  363. exp = append(exp, arrinvalue2...)
  364. exp = append(exp, append(length, strvalue...)...)
  365. // ignore first 4 bytes of the output. This is the function identifier
  366. fixedArrBytesPack = fixedArrBytesPack[4:]
  367. if !bytes.Equal(fixedArrBytesPack, exp) {
  368. t.Errorf("expected %x, got %x\n", exp, fixedArrBytesPack)
  369. }
  370. // test string, fixed array uint256[2], dynamic array uint256[]
  371. strin = "hello world"
  372. fixedarrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  373. dynarrin := []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  374. mixedArrStrPack, err := abi.Pack("mixedArrStr", strin, fixedarrin, dynarrin)
  375. if err != nil {
  376. t.Error(err)
  377. }
  378. // generate expected output
  379. stroffset := make([]byte, 32)
  380. stroffset[31] = 128
  381. strlength := make([]byte, 32)
  382. strlength[31] = byte(len(strin))
  383. strvalue = common.RightPadBytes([]byte(strin), 32)
  384. fixedarrinvalue1 := common.LeftPadBytes(fixedarrin[0].Bytes(), 32)
  385. fixedarrinvalue2 := common.LeftPadBytes(fixedarrin[1].Bytes(), 32)
  386. dynarroffset := make([]byte, 32)
  387. dynarroffset[31] = byte(160 + ((len(strin)/32)+1)*32)
  388. dynarrlength := make([]byte, 32)
  389. dynarrlength[31] = byte(len(dynarrin))
  390. dynarrinvalue1 := common.LeftPadBytes(dynarrin[0].Bytes(), 32)
  391. dynarrinvalue2 := common.LeftPadBytes(dynarrin[1].Bytes(), 32)
  392. dynarrinvalue3 := common.LeftPadBytes(dynarrin[2].Bytes(), 32)
  393. exp = append(stroffset, fixedarrinvalue1...)
  394. exp = append(exp, fixedarrinvalue2...)
  395. exp = append(exp, dynarroffset...)
  396. exp = append(exp, append(strlength, strvalue...)...)
  397. dynarrarg := append(dynarrlength, dynarrinvalue1...)
  398. dynarrarg = append(dynarrarg, dynarrinvalue2...)
  399. dynarrarg = append(dynarrarg, dynarrinvalue3...)
  400. exp = append(exp, dynarrarg...)
  401. // ignore first 4 bytes of the output. This is the function identifier
  402. mixedArrStrPack = mixedArrStrPack[4:]
  403. if !bytes.Equal(mixedArrStrPack, exp) {
  404. t.Errorf("expected %x, got %x\n", exp, mixedArrStrPack)
  405. }
  406. // test string, fixed array uint256[2], fixed array uint256[3]
  407. strin = "hello world"
  408. fixedarrin1 := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  409. fixedarrin2 := [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  410. doubleFixedArrStrPack, err := abi.Pack("doubleFixedArrStr", strin, fixedarrin1, fixedarrin2)
  411. if err != nil {
  412. t.Error(err)
  413. }
  414. // generate expected output
  415. stroffset = make([]byte, 32)
  416. stroffset[31] = 192
  417. strlength = make([]byte, 32)
  418. strlength[31] = byte(len(strin))
  419. strvalue = common.RightPadBytes([]byte(strin), 32)
  420. fixedarrin1value1 := common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
  421. fixedarrin1value2 := common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
  422. fixedarrin2value1 := common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
  423. fixedarrin2value2 := common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
  424. fixedarrin2value3 := common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
  425. exp = append(stroffset, fixedarrin1value1...)
  426. exp = append(exp, fixedarrin1value2...)
  427. exp = append(exp, fixedarrin2value1...)
  428. exp = append(exp, fixedarrin2value2...)
  429. exp = append(exp, fixedarrin2value3...)
  430. exp = append(exp, append(strlength, strvalue...)...)
  431. // ignore first 4 bytes of the output. This is the function identifier
  432. doubleFixedArrStrPack = doubleFixedArrStrPack[4:]
  433. if !bytes.Equal(doubleFixedArrStrPack, exp) {
  434. t.Errorf("expected %x, got %x\n", exp, doubleFixedArrStrPack)
  435. }
  436. // test string, fixed array uint256[2], dynamic array uint256[], fixed array uint256[3]
  437. strin = "hello world"
  438. fixedarrin1 = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  439. dynarrin = []*big.Int{big.NewInt(1), big.NewInt(2)}
  440. fixedarrin2 = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  441. multipleMixedArrStrPack, err := abi.Pack("multipleMixedArrStr", strin, fixedarrin1, dynarrin, fixedarrin2)
  442. if err != nil {
  443. t.Error(err)
  444. }
  445. // generate expected output
  446. stroffset = make([]byte, 32)
  447. stroffset[31] = 224
  448. strlength = make([]byte, 32)
  449. strlength[31] = byte(len(strin))
  450. strvalue = common.RightPadBytes([]byte(strin), 32)
  451. fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
  452. fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
  453. dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
  454. dynarrlength = make([]byte, 32)
  455. dynarrlength[31] = byte(len(dynarrin))
  456. dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
  457. dynarrinvalue2 = common.LeftPadBytes(dynarrin[1].Bytes(), 32)
  458. fixedarrin2value1 = common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
  459. fixedarrin2value2 = common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
  460. fixedarrin2value3 = common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
  461. exp = append(stroffset, fixedarrin1value1...)
  462. exp = append(exp, fixedarrin1value2...)
  463. exp = append(exp, dynarroffset...)
  464. exp = append(exp, fixedarrin2value1...)
  465. exp = append(exp, fixedarrin2value2...)
  466. exp = append(exp, fixedarrin2value3...)
  467. exp = append(exp, append(strlength, strvalue...)...)
  468. dynarrarg = append(dynarrlength, dynarrinvalue1...)
  469. dynarrarg = append(dynarrarg, dynarrinvalue2...)
  470. exp = append(exp, dynarrarg...)
  471. // ignore first 4 bytes of the output. This is the function identifier
  472. multipleMixedArrStrPack = multipleMixedArrStrPack[4:]
  473. if !bytes.Equal(multipleMixedArrStrPack, exp) {
  474. t.Errorf("expected %x, got %x\n", exp, multipleMixedArrStrPack)
  475. }
  476. }
  477. func TestDefaultFunctionParsing(t *testing.T) {
  478. const definition = `[{ "name" : "balance" }]`
  479. abi, err := JSON(strings.NewReader(definition))
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. if _, ok := abi.Methods["balance"]; !ok {
  484. t.Error("expected 'balance' to be present")
  485. }
  486. }
  487. func TestBareEvents(t *testing.T) {
  488. const definition = `[
  489. { "type" : "event", "name" : "balance" },
  490. { "type" : "event", "name" : "anon", "anonymous" : true},
  491. { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
  492. ]`
  493. arg0, _ := NewType("uint256")
  494. arg1, _ := NewType("address")
  495. expectedEvents := map[string]struct {
  496. Anonymous bool
  497. Args []Argument
  498. }{
  499. "balance": {false, nil},
  500. "anon": {true, nil},
  501. "args": {false, []Argument{
  502. {Name: "arg0", Type: arg0, Indexed: false},
  503. {Name: "arg1", Type: arg1, Indexed: true},
  504. }},
  505. }
  506. abi, err := JSON(strings.NewReader(definition))
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. if len(abi.Events) != len(expectedEvents) {
  511. t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
  512. }
  513. for name, exp := range expectedEvents {
  514. got, ok := abi.Events[name]
  515. if !ok {
  516. t.Errorf("could not found event %s", name)
  517. continue
  518. }
  519. if got.Anonymous != exp.Anonymous {
  520. t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
  521. }
  522. if len(got.Inputs) != len(exp.Args) {
  523. t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
  524. continue
  525. }
  526. for i, arg := range exp.Args {
  527. if arg.Name != got.Inputs[i].Name {
  528. t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
  529. }
  530. if arg.Indexed != got.Inputs[i].Indexed {
  531. t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
  532. }
  533. if arg.Type.T != got.Inputs[i].Type.T {
  534. t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
  535. }
  536. }
  537. }
  538. }
  539. // TestUnpackEvent is based on this contract:
  540. // contract T {
  541. // event received(address sender, uint amount, bytes memo);
  542. // event receivedAddr(address sender);
  543. // function receive(bytes memo) external payable {
  544. // received(msg.sender, msg.value, memo);
  545. // receivedAddr(msg.sender);
  546. // }
  547. // }
  548. // When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
  549. // receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
  550. func TestUnpackEvent(t *testing.T) {
  551. 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"}]`
  552. abi, err := JSON(strings.NewReader(abiJSON))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. const hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
  557. data, err := hex.DecodeString(hexdata)
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. if len(data)%32 == 0 {
  562. t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
  563. }
  564. type ReceivedEvent struct {
  565. Address common.Address
  566. Amount *big.Int
  567. Memo []byte
  568. }
  569. var ev ReceivedEvent
  570. err = abi.Unpack(&ev, "received", data)
  571. if err != nil {
  572. t.Error(err)
  573. } else {
  574. t.Logf("len(data): %d; received event: %+v", len(data), ev)
  575. }
  576. type ReceivedAddrEvent struct {
  577. Address common.Address
  578. }
  579. var receivedAddrEv ReceivedAddrEvent
  580. err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
  581. if err != nil {
  582. t.Error(err)
  583. } else {
  584. t.Logf("len(data): %d; received event: %+v", len(data), receivedAddrEv)
  585. }
  586. }
  587. func TestABI_MethodById(t *testing.T) {
  588. const abiJSON = `[
  589. {"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"},
  590. {"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"}]},
  591. {"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]},
  592. {"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]},
  593. {"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]},
  594. {"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]},
  595. {"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]"}]},
  596. {"type":"function","name":"balance","constant":true},
  597. {"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]},
  598. {"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]},
  599. {"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]},
  600. {"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]},
  601. {"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]},
  602. {"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]},
  603. {"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]},
  604. {"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]},
  605. {"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]},
  606. {"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]},
  607. {"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]},
  608. {"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]},
  609. {"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]}
  610. ]
  611. `
  612. abi, err := JSON(strings.NewReader(abiJSON))
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. for name, m := range abi.Methods {
  617. a := fmt.Sprintf("%v", m)
  618. m2, err := abi.MethodById(m.Id())
  619. if err != nil {
  620. t.Fatalf("Failed to look up ABI method: %v", err)
  621. }
  622. b := fmt.Sprintf("%v", m2)
  623. if a != b {
  624. t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
  625. }
  626. }
  627. }