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