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