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