github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/eth/filters/filter_test.go (about) 1 // Copyright 2015 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 "context" 21 "io/ioutil" 22 "math/big" 23 "os" 24 "testing" 25 26 "github.com/kisexp/xdchain/common" 27 "github.com/kisexp/xdchain/consensus/ethash" 28 "github.com/kisexp/xdchain/core" 29 "github.com/kisexp/xdchain/core/rawdb" 30 "github.com/kisexp/xdchain/core/types" 31 "github.com/kisexp/xdchain/crypto" 32 "github.com/kisexp/xdchain/params" 33 "github.com/kisexp/xdchain/rpc" 34 ) 35 36 func makeReceipt(addr common.Address) *types.Receipt { 37 receipt := types.NewReceipt(nil, false, 0) 38 receipt.Logs = []*types.Log{ 39 {Address: addr}, 40 } 41 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 42 return receipt 43 } 44 45 func BenchmarkFilters(b *testing.B) { 46 dir, err := ioutil.TempDir("", "filtertest") 47 if err != nil { 48 b.Fatal(err) 49 } 50 defer os.RemoveAll(dir) 51 52 var ( 53 db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "") 54 backend = &testBackend{db: db} 55 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 56 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 57 addr2 = common.BytesToAddress([]byte("jeff")) 58 addr3 = common.BytesToAddress([]byte("ethereum")) 59 addr4 = common.BytesToAddress([]byte("random addresses please")) 60 ) 61 defer db.Close() 62 63 genesis := core.GenesisBlockForTesting(db, addr1, big.NewInt(1000000)) 64 chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 100010, func(i int, gen *core.BlockGen) { 65 switch i { 66 case 2403: 67 receipt := makeReceipt(addr1) 68 gen.AddUncheckedReceipt(receipt) 69 case 1034: 70 receipt := makeReceipt(addr2) 71 gen.AddUncheckedReceipt(receipt) 72 case 34: 73 receipt := makeReceipt(addr3) 74 gen.AddUncheckedReceipt(receipt) 75 case 99999: 76 receipt := makeReceipt(addr4) 77 gen.AddUncheckedReceipt(receipt) 78 79 } 80 }) 81 for i, block := range chain { 82 rawdb.WriteBlock(db, block) 83 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 84 rawdb.WriteHeadBlockHash(db, block.Hash()) 85 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) 86 } 87 b.ResetTimer() 88 89 filter := NewRangeFilter(backend, 0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil, "") 90 91 for i := 0; i < b.N; i++ { 92 logs, _ := filter.Logs(context.Background()) 93 if len(logs) != 4 { 94 b.Fatal("expected 4 logs, got", len(logs)) 95 } 96 } 97 } 98 99 func TestFilters(t *testing.T) { 100 dir, err := ioutil.TempDir("", "filtertest") 101 if err != nil { 102 t.Fatal(err) 103 } 104 defer os.RemoveAll(dir) 105 106 var ( 107 db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "") 108 backend = &testBackend{db: db} 109 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 110 addr = crypto.PubkeyToAddress(key1.PublicKey) 111 112 hash1 = common.BytesToHash([]byte("topic1")) 113 hash2 = common.BytesToHash([]byte("topic2")) 114 hash3 = common.BytesToHash([]byte("topic3")) 115 hash4 = common.BytesToHash([]byte("topic4")) 116 hash5 = common.BytesToHash([]byte("privateTopic")) 117 ) 118 defer db.Close() 119 120 genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000)) 121 chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) { 122 switch i { 123 case 1: 124 receipt := types.NewReceipt(nil, false, 0) 125 receipt.Logs = []*types.Log{ 126 { 127 Address: addr, 128 Topics: []common.Hash{hash1}, 129 }, 130 } 131 gen.AddUncheckedReceipt(receipt) 132 gen.AddUncheckedTx(types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)) 133 case 2: 134 receipt := types.NewReceipt(nil, false, 0) 135 receipt.Logs = []*types.Log{ 136 { 137 Address: addr, 138 Topics: []common.Hash{hash2}, 139 }, 140 } 141 gen.AddUncheckedReceipt(receipt) 142 gen.AddUncheckedTx(types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)) 143 144 case 998: 145 receipt := types.NewReceipt(nil, false, 0) 146 receipt.Logs = []*types.Log{ 147 { 148 Address: addr, 149 Topics: []common.Hash{hash3}, 150 }, 151 } 152 gen.AddUncheckedReceipt(receipt) 153 gen.AddUncheckedTx(types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, big.NewInt(998), nil)) 154 // Add pseudo Quorum private transaction 155 privateReceipt := types.NewReceipt(nil, false, 0) 156 privateReceipt.Logs = []*types.Log{ 157 { 158 Address: addr, 159 Topics: []common.Hash{hash5}, 160 }, 161 } 162 if err := rawdb.WritePrivateBlockBloom(db, 999, []*types.Receipt{privateReceipt}); err != nil { 163 t.Fatal(err) 164 } 165 gen.AddUncheckedReceipt(privateReceipt) 166 gen.AddUncheckedTx(types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, big.NewInt(998), nil)) 167 case 999: 168 receipt := types.NewReceipt(nil, false, 0) 169 receipt.Logs = []*types.Log{ 170 { 171 Address: addr, 172 Topics: []common.Hash{hash4}, 173 }, 174 } 175 gen.AddUncheckedReceipt(receipt) 176 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, big.NewInt(999), nil)) 177 } 178 }) 179 for i, block := range chain { 180 rawdb.WriteBlock(db, block) 181 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 182 rawdb.WriteHeadBlockHash(db, block.Hash()) 183 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) 184 } 185 186 filter := NewRangeFilter(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}, "") 187 188 logs, _ := filter.Logs(context.Background()) 189 if len(logs) != 4 { 190 t.Error("expected 4 log, got", len(logs)) 191 } 192 193 filter = NewRangeFilter(backend, 900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}, "") 194 logs, _ = filter.Logs(context.Background()) 195 if len(logs) != 1 { 196 t.Error("expected 1 log, got", len(logs)) 197 } 198 if len(logs) > 0 && logs[0].Topics[0] != hash3 { 199 t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) 200 } 201 202 filter = NewRangeFilter(backend, 990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}, "") 203 logs, _ = filter.Logs(context.Background()) 204 if len(logs) != 1 { 205 t.Error("expected 1 log, got", len(logs)) 206 } 207 if len(logs) > 0 && logs[0].Topics[0] != hash3 { 208 t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) 209 } 210 211 filter = NewRangeFilter(backend, 1, 10, nil, [][]common.Hash{{hash1, hash2}}, "") 212 213 logs, _ = filter.Logs(context.Background()) 214 if len(logs) != 2 { 215 t.Error("expected 2 log, got", len(logs)) 216 } 217 218 failHash := common.BytesToHash([]byte("fail")) 219 filter = NewRangeFilter(backend, 0, -1, nil, [][]common.Hash{{failHash}}, "") 220 221 logs, _ = filter.Logs(context.Background()) 222 if len(logs) != 0 { 223 t.Error("expected 0 log, got", len(logs)) 224 } 225 226 failAddr := common.BytesToAddress([]byte("failmenow")) 227 filter = NewRangeFilter(backend, 0, -1, []common.Address{failAddr}, nil, "") 228 229 logs, _ = filter.Logs(context.Background()) 230 if len(logs) != 0 { 231 t.Error("expected 0 log, got", len(logs)) 232 } 233 234 filter = NewRangeFilter(backend, 0, -1, nil, [][]common.Hash{{failHash}, {hash1}}, "") 235 236 logs, _ = filter.Logs(context.Background()) 237 if len(logs) != 0 { 238 t.Error("expected 0 log, got", len(logs)) 239 } 240 241 // Quorum 242 243 // Test individual private log with NewBlockFilter (query filter with block hash) 244 filter = NewBlockFilter(backend, chain[998].Hash(), nil, [][]common.Hash{{hash5}}, "") 245 246 logs, _ = filter.Logs(context.Background()) 247 if len(logs) != 1 { 248 t.Error("expected 1 log, got", len(logs)) 249 } 250 if len(logs) > 0 && logs[0].Topics[0] != hash5 { 251 t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash5, logs[0].Topics[0]) 252 } 253 254 // Test a mix of public and private logs with NewBlockFilter (query filter with block hash) 255 filter = NewBlockFilter(backend, chain[998].Hash(), nil, [][]common.Hash{{hash3, hash5}}, "") 256 257 logs, _ = filter.Logs(context.Background()) 258 if len(logs) != 2 { 259 t.Error("expected 2 log, got", len(logs)) 260 } 261 262 } 263 264 func TestMPSFilters(t *testing.T) { 265 dir, err := ioutil.TempDir("", "filtermpstest") 266 if err != nil { 267 t.Fatal(err) 268 } 269 defer os.RemoveAll(dir) 270 271 var ( 272 db, _ = rawdb.NewLevelDBDatabase(dir, 0, 0, "") 273 backend = &testBackend{db: db} 274 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 275 addr = crypto.PubkeyToAddress(key1.PublicKey) 276 hash1 = common.BytesToHash([]byte("topic1")) 277 ) 278 defer db.Close() 279 280 noPSILog := []*types.Log{ 281 { 282 Address: addr, 283 Topics: []common.Hash{hash1}, 284 }, 285 } 286 287 psi2PSILog := []*types.Log{ 288 { 289 Address: addr, 290 Topics: []common.Hash{hash1}, 291 PSI: types.PrivateStateIdentifier("psi2"), 292 }, 293 } 294 psi1PSILog := []*types.Log{ 295 { 296 Address: addr, 297 Topics: []common.Hash{hash1}, 298 PSI: types.PrivateStateIdentifier("psi1"), 299 }, 300 } 301 302 genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000)) 303 chain, receipts := core.GenerateChain(params.QuorumMPSTestChainConfig, genesis, ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) { 304 switch i { 305 case 1: 306 //log on private transaction 307 //has "private" psi receipt 308 tx := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) 309 tx.SetPrivate() 310 privateReceipt := types.NewReceipt(nil, false, 0) 311 privateReceipt.Logs = noPSILog 312 privateReceipt.PSReceipts = make(map[types.PrivateStateIdentifier]*types.Receipt) 313 psiReceipt := types.NewReceipt(nil, false, 0) 314 psiReceipt.Logs = psi2PSILog 315 privateReceipt.PSReceipts[types.PrivateStateIdentifier("psi2")] = psiReceipt 316 if err := rawdb.WritePrivateBlockBloom(db, 2, []*types.Receipt{privateReceipt}); err != nil { 317 t.Fatal(err) 318 } 319 gen.AddUncheckedReceipt(privateReceipt) 320 gen.AddUncheckedTx(tx) 321 case 2: 322 //no log on private transaction 323 //has "psi1" receipt 324 tx := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) 325 tx.SetPrivate() 326 privateReceipt := types.NewReceipt(nil, false, 0) 327 privateReceipt.PSReceipts = make(map[types.PrivateStateIdentifier]*types.Receipt) 328 psiReceipt := types.NewReceipt(nil, false, 0) 329 psiReceipt.Logs = psi1PSILog 330 privateReceipt.PSReceipts[types.PrivateStateIdentifier("psi1")] = psiReceipt 331 if err := rawdb.WritePrivateBlockBloom(db, 3, []*types.Receipt{privateReceipt}); err != nil { 332 t.Fatal(err) 333 } 334 gen.AddUncheckedReceipt(privateReceipt) 335 gen.AddUncheckedTx(tx) 336 case 998: 337 //no log on private transaction 338 //has "psi2" psi receipt 339 tx := types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, big.NewInt(998), nil) 340 tx.SetPrivate() 341 privateReceipt := types.NewReceipt(nil, false, 0) 342 privateReceipt.PSReceipts = make(map[types.PrivateStateIdentifier]*types.Receipt) 343 psiReceipt := types.NewReceipt(nil, false, 0) 344 psiReceipt.Logs = psi2PSILog 345 privateReceipt.PSReceipts[types.PrivateStateIdentifier("psi2")] = psiReceipt 346 if err := rawdb.WritePrivateBlockBloom(db, 999, []*types.Receipt{privateReceipt}); err != nil { 347 t.Fatal(err) 348 } 349 gen.AddUncheckedReceipt(privateReceipt) 350 gen.AddUncheckedTx(tx) 351 case 999: 352 //log on private transaction 353 //no psi receipt 354 tx := types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, big.NewInt(999), nil) 355 tx.SetPrivate() 356 privateReceipt := types.NewReceipt(nil, false, 0) 357 privateReceipt.Logs = noPSILog 358 if err := rawdb.WritePrivateBlockBloom(db, 1000, []*types.Receipt{privateReceipt}); err != nil { 359 t.Fatal(err) 360 } 361 gen.AddUncheckedReceipt(privateReceipt) 362 gen.AddUncheckedTx(tx) 363 } 364 }) 365 for i, block := range chain { 366 rawdb.WriteBlock(db, block) 367 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 368 rawdb.WriteHeadBlockHash(db, block.Hash()) 369 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) 370 } 371 372 //no psi filter: but only gets top level private receipt logs(no psi) 373 filter := NewRangeFilter(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1}}, "") 374 logs, _ := filter.Logs(context.Background()) 375 if len(logs) != 2 { 376 t.Error("expected 2 logs, got", len(logs)) 377 } 378 379 //test filtering "psi2" logs: gets psi2 logs and top level private receipt logs(no psi) 380 filter = NewRangeFilter(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1}}, types.ToPrivateStateIdentifier("psi2")) 381 ctx := rpc.WithPrivateStateIdentifier(context.Background(), types.ToPrivateStateIdentifier("psi2")) 382 logs, _ = filter.Logs(ctx) 383 if len(logs) != 3 { 384 t.Error("expected 3 logs, got", len(logs)) 385 } 386 387 //test filtering "psi1" logs: gets psi1 logs and top level private receipt logs(no psi) 388 filter = NewRangeFilter(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1}}, types.PrivateStateIdentifier("psi1")) 389 ctx = rpc.WithPrivateStateIdentifier(context.Background(), types.ToPrivateStateIdentifier("psi1")) 390 logs, _ = filter.Logs(ctx) 391 if len(logs) != 3 { 392 t.Error("expected 3 logs, got", len(logs)) 393 } 394 }