abi_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package abi
  17. import (
  18. "bytes"
  19. "fmt"
  20. "log"
  21. "math/big"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. )
  28. 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. if !reflect.DeepEqual(abi, exp) {
  71. t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp)
  72. }
  73. }
  74. func TestTestNumbers(t *testing.T) {
  75. abi, err := JSON(strings.NewReader(jsondata2))
  76. if err != nil {
  77. t.Error(err)
  78. t.FailNow()
  79. }
  80. if _, err := abi.Pack("balance"); err != nil {
  81. t.Error(err)
  82. }
  83. if _, err := abi.Pack("balance", 1); err == nil {
  84. t.Error("expected error for balance(1)")
  85. }
  86. if _, err := abi.Pack("doesntexist", nil); err == nil {
  87. t.Errorf("doesntexist shouldn't exist")
  88. }
  89. if _, err := abi.Pack("doesntexist", 1); err == nil {
  90. t.Errorf("doesntexist(1) shouldn't exist")
  91. }
  92. if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
  93. t.Error(err)
  94. }
  95. i := new(int)
  96. *i = 1000
  97. if _, err := abi.Pack("send", i); err == nil {
  98. t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
  99. }
  100. if _, err := abi.Pack("test", uint32(1000)); err != nil {
  101. t.Error(err)
  102. }
  103. }
  104. func TestTestString(t *testing.T) {
  105. abi, err := JSON(strings.NewReader(jsondata2))
  106. if err != nil {
  107. t.Error(err)
  108. t.FailNow()
  109. }
  110. if _, err := abi.Pack("string", "hello world"); err != nil {
  111. t.Error(err)
  112. }
  113. }
  114. func TestTestBool(t *testing.T) {
  115. abi, err := JSON(strings.NewReader(jsondata2))
  116. if err != nil {
  117. t.Error(err)
  118. t.FailNow()
  119. }
  120. if _, err := abi.Pack("bool", true); err != nil {
  121. t.Error(err)
  122. }
  123. }
  124. func TestTestSlice(t *testing.T) {
  125. abi, err := JSON(strings.NewReader(jsondata2))
  126. if err != nil {
  127. t.Error(err)
  128. t.FailNow()
  129. }
  130. slice := make([]uint64, 2)
  131. if _, err := abi.Pack("uint64[2]", slice); err != nil {
  132. t.Error(err)
  133. }
  134. if _, err := abi.Pack("uint64[]", slice); err != nil {
  135. t.Error(err)
  136. }
  137. }
  138. func TestMethodSignature(t *testing.T) {
  139. String, _ := NewType("string")
  140. m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
  141. exp := "foo(string,string)"
  142. if m.Sig() != exp {
  143. t.Error("signature mismatch", exp, "!=", m.Sig())
  144. }
  145. idexp := crypto.Keccak256([]byte(exp))[:4]
  146. if !bytes.Equal(m.Id(), idexp) {
  147. t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
  148. }
  149. uintt, _ := NewType("uint256")
  150. m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
  151. exp = "foo(uint256)"
  152. if m.Sig() != exp {
  153. t.Error("signature mismatch", exp, "!=", m.Sig())
  154. }
  155. }
  156. func TestMultiPack(t *testing.T) {
  157. abi, err := JSON(strings.NewReader(jsondata2))
  158. if err != nil {
  159. t.Error(err)
  160. t.FailNow()
  161. }
  162. sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
  163. sig = append(sig, make([]byte, 64)...)
  164. sig[35] = 10
  165. sig[67] = 11
  166. packed, err := abi.Pack("bar", uint32(10), uint16(11))
  167. if err != nil {
  168. t.Error(err)
  169. t.FailNow()
  170. }
  171. if !bytes.Equal(packed, sig) {
  172. t.Errorf("expected %x got %x", sig, packed)
  173. }
  174. }
  175. func ExampleJSON() {
  176. const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
  177. abi, err := JSON(strings.NewReader(definition))
  178. if err != nil {
  179. log.Fatalln(err)
  180. }
  181. out, err := abi.Pack("isBar", common.HexToAddress("01"))
  182. if err != nil {
  183. log.Fatalln(err)
  184. }
  185. fmt.Printf("%x\n", out)
  186. // Output:
  187. // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
  188. }
  189. func TestInputVariableInputLength(t *testing.T) {
  190. const definition = `[
  191. { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
  192. { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
  193. { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
  194. ]`
  195. abi, err := JSON(strings.NewReader(definition))
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. // test one string
  200. strin := "hello world"
  201. strpack, err := abi.Pack("strOne", strin)
  202. if err != nil {
  203. t.Error(err)
  204. }
  205. offset := make([]byte, 32)
  206. offset[31] = 32
  207. length := make([]byte, 32)
  208. length[31] = byte(len(strin))
  209. value := common.RightPadBytes([]byte(strin), 32)
  210. exp := append(offset, append(length, value...)...)
  211. // ignore first 4 bytes of the output. This is the function identifier
  212. strpack = strpack[4:]
  213. if !bytes.Equal(strpack, exp) {
  214. t.Errorf("expected %x, got %x\n", exp, strpack)
  215. }
  216. // test one bytes
  217. btspack, err := abi.Pack("bytesOne", []byte(strin))
  218. if err != nil {
  219. t.Error(err)
  220. }
  221. // ignore first 4 bytes of the output. This is the function identifier
  222. btspack = btspack[4:]
  223. if !bytes.Equal(btspack, exp) {
  224. t.Errorf("expected %x, got %x\n", exp, btspack)
  225. }
  226. // test two strings
  227. str1 := "hello"
  228. str2 := "world"
  229. str2pack, err := abi.Pack("strTwo", str1, str2)
  230. if err != nil {
  231. t.Error(err)
  232. }
  233. offset1 := make([]byte, 32)
  234. offset1[31] = 64
  235. length1 := make([]byte, 32)
  236. length1[31] = byte(len(str1))
  237. value1 := common.RightPadBytes([]byte(str1), 32)
  238. offset2 := make([]byte, 32)
  239. offset2[31] = 128
  240. length2 := make([]byte, 32)
  241. length2[31] = byte(len(str2))
  242. value2 := common.RightPadBytes([]byte(str2), 32)
  243. exp2 := append(offset1, offset2...)
  244. exp2 = append(exp2, append(length1, value1...)...)
  245. exp2 = append(exp2, append(length2, value2...)...)
  246. // ignore first 4 bytes of the output. This is the function identifier
  247. str2pack = str2pack[4:]
  248. if !bytes.Equal(str2pack, exp2) {
  249. t.Errorf("expected %x, got %x\n", exp, str2pack)
  250. }
  251. // test two strings, first > 32, second < 32
  252. str1 = strings.Repeat("a", 33)
  253. str2pack, err = abi.Pack("strTwo", str1, str2)
  254. if err != nil {
  255. t.Error(err)
  256. }
  257. offset1 = make([]byte, 32)
  258. offset1[31] = 64
  259. length1 = make([]byte, 32)
  260. length1[31] = byte(len(str1))
  261. value1 = common.RightPadBytes([]byte(str1), 64)
  262. offset2[31] = 160
  263. exp2 = append(offset1, offset2...)
  264. exp2 = append(exp2, append(length1, value1...)...)
  265. exp2 = append(exp2, append(length2, value2...)...)
  266. // ignore first 4 bytes of the output. This is the function identifier
  267. str2pack = str2pack[4:]
  268. if !bytes.Equal(str2pack, exp2) {
  269. t.Errorf("expected %x, got %x\n", exp, str2pack)
  270. }
  271. // test two strings, first > 32, second >32
  272. str1 = strings.Repeat("a", 33)
  273. str2 = strings.Repeat("a", 33)
  274. str2pack, err = abi.Pack("strTwo", str1, str2)
  275. if err != nil {
  276. t.Error(err)
  277. }
  278. offset1 = make([]byte, 32)
  279. offset1[31] = 64
  280. length1 = make([]byte, 32)
  281. length1[31] = byte(len(str1))
  282. value1 = common.RightPadBytes([]byte(str1), 64)
  283. offset2 = make([]byte, 32)
  284. offset2[31] = 160
  285. length2 = make([]byte, 32)
  286. length2[31] = byte(len(str2))
  287. value2 = common.RightPadBytes([]byte(str2), 64)
  288. exp2 = append(offset1, offset2...)
  289. exp2 = append(exp2, append(length1, value1...)...)
  290. exp2 = append(exp2, append(length2, value2...)...)
  291. // ignore first 4 bytes of the output. This is the function identifier
  292. str2pack = str2pack[4:]
  293. if !bytes.Equal(str2pack, exp2) {
  294. t.Errorf("expected %x, got %x\n", exp, str2pack)
  295. }
  296. }
  297. func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
  298. const definition = `[
  299. { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
  300. { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] }
  301. ]`
  302. abi, err := JSON(strings.NewReader(definition))
  303. if err != nil {
  304. t.Error(err)
  305. }
  306. // test fixed array of uint256 and a string
  307. arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
  308. strin := "hello world"
  309. fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin)
  310. if err != nil {
  311. t.Error(err)
  312. }
  313. offset := make([]byte, 32)
  314. offset[31] = 96
  315. length := make([]byte, 32)
  316. length[31] = byte(len(strin))
  317. strvalue := common.RightPadBytes([]byte(strin), 32)
  318. arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32)
  319. arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32)
  320. exp := append(offset, arrinvalue1...)
  321. exp = append(exp, arrinvalue2...)
  322. exp = append(exp, append(length, strvalue...)...)
  323. // ignore first 4 bytes of the output. This is the function identifier
  324. fixedArrStrPack = fixedArrStrPack[4:]
  325. if !bytes.Equal(fixedArrStrPack, exp) {
  326. t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack)
  327. }
  328. // test fixed array of uint256 and a byte array
  329. bytesin := []byte(strin)
  330. fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin)
  331. if err != nil {
  332. t.Error(err)
  333. }
  334. // ignore first 4 bytes of the output. This is the function identifier
  335. fixedArrBytesPack = fixedArrBytesPack[4:]
  336. if !bytes.Equal(fixedArrBytesPack, exp) {
  337. t.Errorf("expected %x, got %x\n", exp, fixedArrBytesPack)
  338. }
  339. }
  340. func TestDefaultFunctionParsing(t *testing.T) {
  341. const definition = `[{ "name" : "balance" }]`
  342. abi, err := JSON(strings.NewReader(definition))
  343. if err != nil {
  344. t.Fatal(err)
  345. }
  346. if _, ok := abi.Methods["balance"]; !ok {
  347. t.Error("expected 'balance' to be present")
  348. }
  349. }
  350. func TestBareEvents(t *testing.T) {
  351. const definition = `[
  352. { "type" : "event", "name" : "balance" },
  353. { "type" : "event", "name" : "anon", "anonymous" : true},
  354. { "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
  355. ]`
  356. arg0, _ := NewType("uint256")
  357. arg1, _ := NewType("address")
  358. expectedEvents := map[string]struct {
  359. Anonymous bool
  360. Args []Argument
  361. }{
  362. "balance": {false, nil},
  363. "anon": {true, nil},
  364. "args": {false, []Argument{
  365. {Name: "arg0", Type: arg0, Indexed: false},
  366. {Name: "arg1", Type: arg1, Indexed: true},
  367. }},
  368. }
  369. abi, err := JSON(strings.NewReader(definition))
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. if len(abi.Events) != len(expectedEvents) {
  374. t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
  375. }
  376. for name, exp := range expectedEvents {
  377. got, ok := abi.Events[name]
  378. if !ok {
  379. t.Errorf("could not found event %s", name)
  380. continue
  381. }
  382. if got.Anonymous != exp.Anonymous {
  383. t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
  384. }
  385. if len(got.Inputs) != len(exp.Args) {
  386. t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
  387. continue
  388. }
  389. for i, arg := range exp.Args {
  390. if arg.Name != got.Inputs[i].Name {
  391. t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
  392. }
  393. if arg.Indexed != got.Inputs[i].Indexed {
  394. t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
  395. }
  396. if arg.Type.T != got.Inputs[i].Type.T {
  397. t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
  398. }
  399. }
  400. }
  401. }