api_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2016 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 filters
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. func TestUnmarshalJSONNewFilterArgs(t *testing.T) {
  25. var (
  26. fromBlock rpc.BlockNumber = 0x123435
  27. toBlock rpc.BlockNumber = 0xabcdef
  28. address0 = common.HexToAddress("70c87d191324e6712a591f304b4eedef6ad9bb9d")
  29. address1 = common.HexToAddress("9b2055d370f73ec7d8a03e965129118dc8f5bf83")
  30. topic0 = common.HexToHash("3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1ca")
  31. topic1 = common.HexToHash("9084a792d2f8b16a62b882fd56f7860c07bf5fa91dd8a2ae7e809e5180fef0b3")
  32. topic2 = common.HexToHash("6ccae1c4af4152f460ff510e573399795dfab5dcf1fa60d1f33ac8fdc1e480ce")
  33. )
  34. // default values
  35. var test0 FilterCriteria
  36. if err := json.Unmarshal([]byte("{}"), &test0); err != nil {
  37. t.Fatal(err)
  38. }
  39. if test0.FromBlock != nil {
  40. t.Fatalf("expected nil, got %d", test0.FromBlock)
  41. }
  42. if test0.ToBlock != nil {
  43. t.Fatalf("expected nil, got %d", test0.ToBlock)
  44. }
  45. if len(test0.Addresses) != 0 {
  46. t.Fatalf("expected 0 addresses, got %d", len(test0.Addresses))
  47. }
  48. if len(test0.Topics) != 0 {
  49. t.Fatalf("expected 0 topics, got %d topics", len(test0.Topics))
  50. }
  51. // from, to block number
  52. var test1 FilterCriteria
  53. vector := fmt.Sprintf(`{"fromBlock":"0x%x","toBlock":"0x%x"}`, fromBlock, toBlock)
  54. if err := json.Unmarshal([]byte(vector), &test1); err != nil {
  55. t.Fatal(err)
  56. }
  57. if test1.FromBlock.Int64() != fromBlock.Int64() {
  58. t.Fatalf("expected FromBlock %d, got %d", fromBlock, test1.FromBlock)
  59. }
  60. if test1.ToBlock.Int64() != toBlock.Int64() {
  61. t.Fatalf("expected ToBlock %d, got %d", toBlock, test1.ToBlock)
  62. }
  63. // single address
  64. var test2 FilterCriteria
  65. vector = fmt.Sprintf(`{"address": "%s"}`, address0.Hex())
  66. if err := json.Unmarshal([]byte(vector), &test2); err != nil {
  67. t.Fatal(err)
  68. }
  69. if len(test2.Addresses) != 1 {
  70. t.Fatalf("expected 1 address, got %d address(es)", len(test2.Addresses))
  71. }
  72. if test2.Addresses[0] != address0 {
  73. t.Fatalf("expected address %x, got %x", address0, test2.Addresses[0])
  74. }
  75. // multiple address
  76. var test3 FilterCriteria
  77. vector = fmt.Sprintf(`{"address": ["%s", "%s"]}`, address0.Hex(), address1.Hex())
  78. if err := json.Unmarshal([]byte(vector), &test3); err != nil {
  79. t.Fatal(err)
  80. }
  81. if len(test3.Addresses) != 2 {
  82. t.Fatalf("expected 2 addresses, got %d address(es)", len(test3.Addresses))
  83. }
  84. if test3.Addresses[0] != address0 {
  85. t.Fatalf("expected address %x, got %x", address0, test3.Addresses[0])
  86. }
  87. if test3.Addresses[1] != address1 {
  88. t.Fatalf("expected address %x, got %x", address1, test3.Addresses[1])
  89. }
  90. // single topic
  91. var test4 FilterCriteria
  92. vector = fmt.Sprintf(`{"topics": ["%s"]}`, topic0.Hex())
  93. if err := json.Unmarshal([]byte(vector), &test4); err != nil {
  94. t.Fatal(err)
  95. }
  96. if len(test4.Topics) != 1 {
  97. t.Fatalf("expected 1 topic, got %d", len(test4.Topics))
  98. }
  99. if len(test4.Topics[0]) != 1 {
  100. t.Fatalf("expected len(topics[0]) to be 1, got %d", len(test4.Topics[0]))
  101. }
  102. if test4.Topics[0][0] != topic0 {
  103. t.Fatalf("got %x, expected %x", test4.Topics[0][0], topic0)
  104. }
  105. // test multiple "AND" topics
  106. var test5 FilterCriteria
  107. vector = fmt.Sprintf(`{"topics": ["%s", "%s"]}`, topic0.Hex(), topic1.Hex())
  108. if err := json.Unmarshal([]byte(vector), &test5); err != nil {
  109. t.Fatal(err)
  110. }
  111. if len(test5.Topics) != 2 {
  112. t.Fatalf("expected 2 topics, got %d", len(test5.Topics))
  113. }
  114. if len(test5.Topics[0]) != 1 {
  115. t.Fatalf("expected 1 topic, got %d", len(test5.Topics[0]))
  116. }
  117. if test5.Topics[0][0] != topic0 {
  118. t.Fatalf("got %x, expected %x", test5.Topics[0][0], topic0)
  119. }
  120. if len(test5.Topics[1]) != 1 {
  121. t.Fatalf("expected 1 topic, got %d", len(test5.Topics[1]))
  122. }
  123. if test5.Topics[1][0] != topic1 {
  124. t.Fatalf("got %x, expected %x", test5.Topics[1][0], topic1)
  125. }
  126. // test optional topic
  127. var test6 FilterCriteria
  128. vector = fmt.Sprintf(`{"topics": ["%s", null, "%s"]}`, topic0.Hex(), topic2.Hex())
  129. if err := json.Unmarshal([]byte(vector), &test6); err != nil {
  130. t.Fatal(err)
  131. }
  132. if len(test6.Topics) != 3 {
  133. t.Fatalf("expected 3 topics, got %d", len(test6.Topics))
  134. }
  135. if len(test6.Topics[0]) != 1 {
  136. t.Fatalf("expected 1 topic, got %d", len(test6.Topics[0]))
  137. }
  138. if test6.Topics[0][0] != topic0 {
  139. t.Fatalf("got %x, expected %x", test6.Topics[0][0], topic0)
  140. }
  141. if len(test6.Topics[1]) != 0 {
  142. t.Fatalf("expected 0 topic, got %d", len(test6.Topics[1]))
  143. }
  144. if len(test6.Topics[2]) != 1 {
  145. t.Fatalf("expected 1 topic, got %d", len(test6.Topics[2]))
  146. }
  147. if test6.Topics[2][0] != topic2 {
  148. t.Fatalf("got %x, expected %x", test6.Topics[2][0], topic2)
  149. }
  150. // test OR topics
  151. var test7 FilterCriteria
  152. vector = fmt.Sprintf(`{"topics": [["%s", "%s"], null, ["%s", null]]}`, topic0.Hex(), topic1.Hex(), topic2.Hex())
  153. if err := json.Unmarshal([]byte(vector), &test7); err != nil {
  154. t.Fatal(err)
  155. }
  156. if len(test7.Topics) != 3 {
  157. t.Fatalf("expected 3 topics, got %d topics", len(test7.Topics))
  158. }
  159. if len(test7.Topics[0]) != 2 {
  160. t.Fatalf("expected 2 topics, got %d topics", len(test7.Topics[0]))
  161. }
  162. if test7.Topics[0][0] != topic0 || test7.Topics[0][1] != topic1 {
  163. t.Fatalf("invalid topics expected [%x,%x], got [%x,%x]",
  164. topic0, topic1, test7.Topics[0][0], test7.Topics[0][1],
  165. )
  166. }
  167. if len(test7.Topics[1]) != 0 {
  168. t.Fatalf("expected 0 topic, got %d topics", len(test7.Topics[1]))
  169. }
  170. if len(test7.Topics[2]) != 0 {
  171. t.Fatalf("expected 0 topics, got %d topics", len(test7.Topics[2]))
  172. }
  173. }