abi_test.go 22 KB

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