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