github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/eth/filters/filter_system_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 "context" 21 "errors" 22 "math/big" 23 "math/rand" 24 "reflect" 25 "runtime" 26 "testing" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/consensus/ethash" 31 "github.com/ethereum/go-ethereum/core" 32 "github.com/ethereum/go-ethereum/core/bloombits" 33 "github.com/ethereum/go-ethereum/core/rawdb" 34 "github.com/ethereum/go-ethereum/core/types" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/event" 37 "github.com/ethereum/go-ethereum/internal/ethapi" 38 "github.com/ethereum/go-ethereum/params" 39 "github.com/ethereum/go-ethereum/rpc" 40 ) 41 42 type testBackend struct { 43 db ethdb.Database 44 sections uint64 45 txFeed event.Feed 46 logsFeed event.Feed 47 rmLogsFeed event.Feed 48 chainFeed event.Feed 49 pendingBlock *types.Block 50 pendingReceipts types.Receipts 51 } 52 53 func (b *testBackend) ChainConfig() *params.ChainConfig { 54 return params.TestChainConfig 55 } 56 57 func (b *testBackend) CurrentHeader() *types.Header { 58 hdr, _ := b.HeaderByNumber(context.TODO(), rpc.LatestBlockNumber) 59 return hdr 60 } 61 62 func (b *testBackend) ChainDb() ethdb.Database { 63 return b.db 64 } 65 66 func (b *testBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) { 67 var ( 68 hash common.Hash 69 num uint64 70 ) 71 switch blockNr { 72 case rpc.LatestBlockNumber: 73 hash = rawdb.ReadHeadBlockHash(b.db) 74 number := rawdb.ReadHeaderNumber(b.db, hash) 75 if number == nil { 76 return nil, nil 77 } 78 num = *number 79 case rpc.FinalizedBlockNumber: 80 hash = rawdb.ReadFinalizedBlockHash(b.db) 81 number := rawdb.ReadHeaderNumber(b.db, hash) 82 if number == nil { 83 return nil, nil 84 } 85 num = *number 86 case rpc.SafeBlockNumber: 87 return nil, errors.New("safe block not found") 88 default: 89 num = uint64(blockNr) 90 hash = rawdb.ReadCanonicalHash(b.db, num) 91 } 92 return rawdb.ReadHeader(b.db, hash, num), nil 93 } 94 95 func (b *testBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { 96 number := rawdb.ReadHeaderNumber(b.db, hash) 97 if number == nil { 98 return nil, nil 99 } 100 return rawdb.ReadHeader(b.db, hash, *number), nil 101 } 102 103 func (b *testBackend) GetBody(ctx context.Context, hash common.Hash, number rpc.BlockNumber) (*types.Body, error) { 104 if body := rawdb.ReadBody(b.db, hash, uint64(number)); body != nil { 105 return body, nil 106 } 107 return nil, errors.New("block body not found") 108 } 109 110 func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 111 if number := rawdb.ReadHeaderNumber(b.db, hash); number != nil { 112 if header := rawdb.ReadHeader(b.db, hash, *number); header != nil { 113 return rawdb.ReadReceipts(b.db, hash, *number, header.Time, params.TestChainConfig), nil 114 } 115 } 116 return nil, nil 117 } 118 119 func (b *testBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { 120 logs := rawdb.ReadLogs(b.db, hash, number) 121 return logs, nil 122 } 123 124 func (b *testBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 125 return b.txFeed.Subscribe(ch) 126 } 127 128 func (b *testBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { 129 return b.rmLogsFeed.Subscribe(ch) 130 } 131 132 func (b *testBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 133 return b.logsFeed.Subscribe(ch) 134 } 135 136 func (b *testBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { 137 return b.chainFeed.Subscribe(ch) 138 } 139 140 func (b *testBackend) BloomStatus() (uint64, uint64) { 141 return params.BloomBitsBlocks, b.sections 142 } 143 144 func (b *testBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) { 145 requests := make(chan chan *bloombits.Retrieval) 146 147 go session.Multiplex(16, 0, requests) 148 go func() { 149 for { 150 // Wait for a service request or a shutdown 151 select { 152 case <-ctx.Done(): 153 return 154 155 case request := <-requests: 156 task := <-request 157 158 task.Bitsets = make([][]byte, len(task.Sections)) 159 for i, section := range task.Sections { 160 if rand.Int()%4 != 0 { // Handle occasional missing deliveries 161 head := rawdb.ReadCanonicalHash(b.db, (section+1)*params.BloomBitsBlocks-1) 162 task.Bitsets[i], _ = rawdb.ReadBloomBits(b.db, task.Bit, section, head) 163 } 164 } 165 request <- task 166 } 167 } 168 }() 169 } 170 171 func (b *testBackend) setPending(block *types.Block, receipts types.Receipts) { 172 b.pendingBlock = block 173 b.pendingReceipts = receipts 174 } 175 176 func newTestFilterSystem(t testing.TB, db ethdb.Database, cfg Config) (*testBackend, *FilterSystem) { 177 backend := &testBackend{db: db} 178 sys := NewFilterSystem(backend, cfg) 179 return backend, sys 180 } 181 182 // TestBlockSubscription tests if a block subscription returns block hashes for posted chain events. 183 // It creates multiple subscriptions: 184 // - one at the start and should receive all posted chain events and a second (blockHashes) 185 // - one that is created after a cutoff moment and uninstalled after a second cutoff moment (blockHashes[cutoff1:cutoff2]) 186 // - one that is created after the second cutoff moment (blockHashes[cutoff2:]) 187 func TestBlockSubscription(t *testing.T) { 188 t.Parallel() 189 190 var ( 191 db = rawdb.NewMemoryDatabase() 192 backend, sys = newTestFilterSystem(t, db, Config{}) 193 api = NewFilterAPI(sys) 194 genesis = &core.Genesis{ 195 Config: params.TestChainConfig, 196 BaseFee: big.NewInt(params.InitialBaseFee), 197 } 198 _, chain, _ = core.GenerateChainWithGenesis(genesis, ethash.NewFaker(), 10, func(i int, gen *core.BlockGen) {}) 199 chainEvents []core.ChainEvent 200 ) 201 202 for _, blk := range chain { 203 chainEvents = append(chainEvents, core.ChainEvent{Hash: blk.Hash(), Block: blk}) 204 } 205 206 chan0 := make(chan *types.Header) 207 sub0 := api.events.SubscribeNewHeads(chan0) 208 chan1 := make(chan *types.Header) 209 sub1 := api.events.SubscribeNewHeads(chan1) 210 211 go func() { // simulate client 212 i1, i2 := 0, 0 213 for i1 != len(chainEvents) || i2 != len(chainEvents) { 214 select { 215 case header := <-chan0: 216 if chainEvents[i1].Hash != header.Hash() { 217 t.Errorf("sub0 received invalid hash on index %d, want %x, got %x", i1, chainEvents[i1].Hash, header.Hash()) 218 } 219 i1++ 220 case header := <-chan1: 221 if chainEvents[i2].Hash != header.Hash() { 222 t.Errorf("sub1 received invalid hash on index %d, want %x, got %x", i2, chainEvents[i2].Hash, header.Hash()) 223 } 224 i2++ 225 } 226 } 227 228 sub0.Unsubscribe() 229 sub1.Unsubscribe() 230 }() 231 232 time.Sleep(1 * time.Second) 233 for _, e := range chainEvents { 234 backend.chainFeed.Send(e) 235 } 236 237 <-sub0.Err() 238 <-sub1.Err() 239 } 240 241 // TestPendingTxFilter tests whether pending tx filters retrieve all pending transactions that are posted to the event mux. 242 func TestPendingTxFilter(t *testing.T) { 243 t.Parallel() 244 245 var ( 246 db = rawdb.NewMemoryDatabase() 247 backend, sys = newTestFilterSystem(t, db, Config{}) 248 api = NewFilterAPI(sys) 249 250 transactions = []*types.Transaction{ 251 types.NewTransaction(0, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 252 types.NewTransaction(1, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 253 types.NewTransaction(2, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 254 types.NewTransaction(3, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 255 types.NewTransaction(4, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 256 } 257 258 hashes []common.Hash 259 ) 260 261 fid0 := api.NewPendingTransactionFilter(nil) 262 263 time.Sleep(1 * time.Second) 264 backend.txFeed.Send(core.NewTxsEvent{Txs: transactions}) 265 266 timeout := time.Now().Add(1 * time.Second) 267 for { 268 results, err := api.GetFilterChanges(fid0) 269 if err != nil { 270 t.Fatalf("Unable to retrieve logs: %v", err) 271 } 272 273 h := results.([]common.Hash) 274 hashes = append(hashes, h...) 275 if len(hashes) >= len(transactions) { 276 break 277 } 278 // check timeout 279 if time.Now().After(timeout) { 280 break 281 } 282 283 time.Sleep(100 * time.Millisecond) 284 } 285 286 if len(hashes) != len(transactions) { 287 t.Errorf("invalid number of transactions, want %d transactions(s), got %d", len(transactions), len(hashes)) 288 return 289 } 290 for i := range hashes { 291 if hashes[i] != transactions[i].Hash() { 292 t.Errorf("hashes[%d] invalid, want %x, got %x", i, transactions[i].Hash(), hashes[i]) 293 } 294 } 295 } 296 297 // TestPendingTxFilterFullTx tests whether pending tx filters retrieve all pending transactions that are posted to the event mux. 298 func TestPendingTxFilterFullTx(t *testing.T) { 299 t.Parallel() 300 301 var ( 302 db = rawdb.NewMemoryDatabase() 303 backend, sys = newTestFilterSystem(t, db, Config{}) 304 api = NewFilterAPI(sys) 305 306 transactions = []*types.Transaction{ 307 types.NewTransaction(0, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 308 types.NewTransaction(1, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 309 types.NewTransaction(2, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 310 types.NewTransaction(3, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 311 types.NewTransaction(4, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil), 312 } 313 314 txs []*ethapi.RPCTransaction 315 ) 316 317 fullTx := true 318 fid0 := api.NewPendingTransactionFilter(&fullTx) 319 320 time.Sleep(1 * time.Second) 321 backend.txFeed.Send(core.NewTxsEvent{Txs: transactions}) 322 323 timeout := time.Now().Add(1 * time.Second) 324 for { 325 results, err := api.GetFilterChanges(fid0) 326 if err != nil { 327 t.Fatalf("Unable to retrieve logs: %v", err) 328 } 329 330 tx := results.([]*ethapi.RPCTransaction) 331 txs = append(txs, tx...) 332 if len(txs) >= len(transactions) { 333 break 334 } 335 // check timeout 336 if time.Now().After(timeout) { 337 break 338 } 339 340 time.Sleep(100 * time.Millisecond) 341 } 342 343 if len(txs) != len(transactions) { 344 t.Errorf("invalid number of transactions, want %d transactions(s), got %d", len(transactions), len(txs)) 345 return 346 } 347 for i := range txs { 348 if txs[i].Hash != transactions[i].Hash() { 349 t.Errorf("hashes[%d] invalid, want %x, got %x", i, transactions[i].Hash(), txs[i].Hash) 350 } 351 } 352 } 353 354 // TestLogFilterCreation test whether a given filter criteria makes sense. 355 // If not it must return an error. 356 func TestLogFilterCreation(t *testing.T) { 357 var ( 358 db = rawdb.NewMemoryDatabase() 359 _, sys = newTestFilterSystem(t, db, Config{}) 360 api = NewFilterAPI(sys) 361 362 testCases = []struct { 363 crit FilterCriteria 364 success bool 365 }{ 366 // defaults 367 {FilterCriteria{}, true}, 368 // valid block number range 369 {FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(2)}, true}, 370 // "mined" block range to pending 371 {FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, true}, 372 // from block "higher" than to block 373 {FilterCriteria{FromBlock: big.NewInt(2), ToBlock: big.NewInt(1)}, false}, 374 // from block "higher" than to block 375 {FilterCriteria{FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64()), ToBlock: big.NewInt(100)}, false}, 376 // from block "higher" than to block 377 {FilterCriteria{FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(100)}, false}, 378 // from block "higher" than to block 379 {FilterCriteria{FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, false}, 380 // topics more than 4 381 {FilterCriteria{Topics: [][]common.Hash{{}, {}, {}, {}, {}}}, false}, 382 } 383 ) 384 385 for i, test := range testCases { 386 id, err := api.NewFilter(test.crit) 387 if err != nil && test.success { 388 t.Errorf("expected filter creation for case %d to success, got %v", i, err) 389 } 390 if err == nil { 391 api.UninstallFilter(id) 392 if !test.success { 393 t.Errorf("expected testcase %d to fail with an error", i) 394 } 395 } 396 } 397 } 398 399 // TestInvalidLogFilterCreation tests whether invalid filter log criteria results in an error 400 // when the filter is created. 401 func TestInvalidLogFilterCreation(t *testing.T) { 402 t.Parallel() 403 404 var ( 405 db = rawdb.NewMemoryDatabase() 406 _, sys = newTestFilterSystem(t, db, Config{}) 407 api = NewFilterAPI(sys) 408 ) 409 410 // different situations where log filter creation should fail. 411 // Reason: fromBlock > toBlock 412 testCases := []FilterCriteria{ 413 0: {FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, 414 1: {FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(100)}, 415 2: {FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64()), ToBlock: big.NewInt(100)}, 416 3: {Topics: [][]common.Hash{{}, {}, {}, {}, {}}}, 417 } 418 419 for i, test := range testCases { 420 if _, err := api.NewFilter(test); err == nil { 421 t.Errorf("Expected NewFilter for case #%d to fail", i) 422 } 423 } 424 } 425 426 // TestInvalidGetLogsRequest tests invalid getLogs requests 427 func TestInvalidGetLogsRequest(t *testing.T) { 428 t.Parallel() 429 430 var ( 431 db = rawdb.NewMemoryDatabase() 432 _, sys = newTestFilterSystem(t, db, Config{}) 433 api = NewFilterAPI(sys) 434 blockHash = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111") 435 ) 436 437 // Reason: Cannot specify both BlockHash and FromBlock/ToBlock) 438 testCases := []FilterCriteria{ 439 0: {BlockHash: &blockHash, FromBlock: big.NewInt(100)}, 440 1: {BlockHash: &blockHash, ToBlock: big.NewInt(500)}, 441 2: {BlockHash: &blockHash, FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, 442 3: {BlockHash: &blockHash, Topics: [][]common.Hash{{}, {}, {}, {}, {}}}, 443 } 444 445 for i, test := range testCases { 446 if _, err := api.GetLogs(context.Background(), test); err == nil { 447 t.Errorf("Expected Logs for case #%d to fail", i) 448 } 449 } 450 } 451 452 // TestInvalidGetRangeLogsRequest tests getLogs with invalid block range 453 func TestInvalidGetRangeLogsRequest(t *testing.T) { 454 t.Parallel() 455 456 var ( 457 db = rawdb.NewMemoryDatabase() 458 _, sys = newTestFilterSystem(t, db, Config{}) 459 api = NewFilterAPI(sys) 460 ) 461 462 if _, err := api.GetLogs(context.Background(), FilterCriteria{FromBlock: big.NewInt(2), ToBlock: big.NewInt(1)}); err != errInvalidBlockRange { 463 t.Errorf("Expected Logs for invalid range return error, but got: %v", err) 464 } 465 } 466 467 // TestLogFilter tests whether log filters match the correct logs that are posted to the event feed. 468 func TestLogFilter(t *testing.T) { 469 t.Parallel() 470 471 var ( 472 db = rawdb.NewMemoryDatabase() 473 backend, sys = newTestFilterSystem(t, db, Config{}) 474 api = NewFilterAPI(sys) 475 476 firstAddr = common.HexToAddress("0x1111111111111111111111111111111111111111") 477 secondAddr = common.HexToAddress("0x2222222222222222222222222222222222222222") 478 thirdAddress = common.HexToAddress("0x3333333333333333333333333333333333333333") 479 notUsedAddress = common.HexToAddress("0x9999999999999999999999999999999999999999") 480 firstTopic = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111") 481 secondTopic = common.HexToHash("0x2222222222222222222222222222222222222222222222222222222222222222") 482 notUsedTopic = common.HexToHash("0x9999999999999999999999999999999999999999999999999999999999999999") 483 484 // posted twice, once as regular logs and once as pending logs. 485 allLogs = []*types.Log{ 486 {Address: firstAddr}, 487 {Address: firstAddr, Topics: []common.Hash{firstTopic}, BlockNumber: 1}, 488 {Address: secondAddr, Topics: []common.Hash{firstTopic}, BlockNumber: 1}, 489 {Address: thirdAddress, Topics: []common.Hash{secondTopic}, BlockNumber: 2}, 490 {Address: thirdAddress, Topics: []common.Hash{secondTopic}, BlockNumber: 3}, 491 } 492 493 testCases = []struct { 494 crit FilterCriteria 495 expected []*types.Log 496 id rpc.ID 497 }{ 498 // match all 499 0: {FilterCriteria{}, allLogs, ""}, 500 // match none due to no matching addresses 501 1: {FilterCriteria{Addresses: []common.Address{{}, notUsedAddress}, Topics: [][]common.Hash{nil}}, []*types.Log{}, ""}, 502 // match logs based on addresses, ignore topics 503 2: {FilterCriteria{Addresses: []common.Address{firstAddr}}, allLogs[:2], ""}, 504 // match none due to no matching topics (match with address) 505 3: {FilterCriteria{Addresses: []common.Address{secondAddr}, Topics: [][]common.Hash{{notUsedTopic}}}, []*types.Log{}, ""}, 506 // match logs based on addresses and topics 507 4: {FilterCriteria{Addresses: []common.Address{thirdAddress}, Topics: [][]common.Hash{{firstTopic, secondTopic}}}, allLogs[3:5], ""}, 508 // match logs based on multiple addresses and "or" topics 509 5: {FilterCriteria{Addresses: []common.Address{secondAddr, thirdAddress}, Topics: [][]common.Hash{{firstTopic, secondTopic}}}, allLogs[2:5], ""}, 510 // all "mined" logs with block num >= 2 511 6: {FilterCriteria{FromBlock: big.NewInt(2), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, allLogs[3:], ""}, 512 // all "mined" logs 513 7: {FilterCriteria{ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())}, allLogs, ""}, 514 // all "mined" logs with 1>= block num <=2 and topic secondTopic 515 8: {FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(2), Topics: [][]common.Hash{{secondTopic}}}, allLogs[3:4], ""}, 516 // match all logs due to wildcard topic 517 9: {FilterCriteria{Topics: [][]common.Hash{nil}}, allLogs[1:], ""}, 518 } 519 ) 520 521 // create all filters 522 for i := range testCases { 523 testCases[i].id, _ = api.NewFilter(testCases[i].crit) 524 } 525 526 // raise events 527 time.Sleep(1 * time.Second) 528 if nsend := backend.logsFeed.Send(allLogs); nsend == 0 { 529 t.Fatal("Logs event not delivered") 530 } 531 532 for i, tt := range testCases { 533 var fetched []*types.Log 534 timeout := time.Now().Add(1 * time.Second) 535 for { // fetch all expected logs 536 results, err := api.GetFilterChanges(tt.id) 537 if err != nil { 538 t.Fatalf("test %d: unable to fetch logs: %v", i, err) 539 } 540 541 fetched = append(fetched, results.([]*types.Log)...) 542 if len(fetched) >= len(tt.expected) { 543 break 544 } 545 // check timeout 546 if time.Now().After(timeout) { 547 break 548 } 549 550 time.Sleep(100 * time.Millisecond) 551 } 552 553 if len(fetched) != len(tt.expected) { 554 t.Errorf("invalid number of logs for case %d, want %d log(s), got %d", i, len(tt.expected), len(fetched)) 555 return 556 } 557 558 for l := range fetched { 559 if fetched[l].Removed { 560 t.Errorf("expected log not to be removed for log %d in case %d", l, i) 561 } 562 if !reflect.DeepEqual(fetched[l], tt.expected[l]) { 563 t.Errorf("invalid log on index %d for case %d", l, i) 564 } 565 } 566 } 567 } 568 569 // TestPendingTxFilterDeadlock tests if the event loop hangs when pending 570 // txes arrive at the same time that one of multiple filters is timing out. 571 // Please refer to #22131 for more details. 572 func TestPendingTxFilterDeadlock(t *testing.T) { 573 t.Parallel() 574 timeout := 100 * time.Millisecond 575 576 var ( 577 db = rawdb.NewMemoryDatabase() 578 backend, sys = newTestFilterSystem(t, db, Config{Timeout: timeout}) 579 api = NewFilterAPI(sys) 580 done = make(chan struct{}) 581 ) 582 583 go func() { 584 // Bombard feed with txes until signal was received to stop 585 i := uint64(0) 586 for { 587 select { 588 case <-done: 589 return 590 default: 591 } 592 593 tx := types.NewTransaction(i, common.HexToAddress("0xb794f5ea0ba39494ce83a213fffba74279579268"), new(big.Int), 0, new(big.Int), nil) 594 backend.txFeed.Send(core.NewTxsEvent{Txs: []*types.Transaction{tx}}) 595 i++ 596 } 597 }() 598 599 // Create a bunch of filters that will 600 // timeout either in 100ms or 200ms 601 subs := make([]*Subscription, 20) 602 for i := 0; i < len(subs); i++ { 603 fid := api.NewPendingTransactionFilter(nil) 604 f, ok := api.filters[fid] 605 if !ok { 606 t.Fatalf("Filter %s should exist", fid) 607 } 608 subs[i] = f.s 609 // Wait for at least one tx to arrive in filter 610 for { 611 hashes, err := api.GetFilterChanges(fid) 612 if err != nil { 613 t.Fatalf("Filter should exist: %v\n", err) 614 } 615 if len(hashes.([]common.Hash)) > 0 { 616 break 617 } 618 runtime.Gosched() 619 } 620 } 621 622 // Wait until filters have timed out and have been uninstalled. 623 for _, sub := range subs { 624 select { 625 case <-sub.Err(): 626 case <-time.After(1 * time.Second): 627 t.Fatalf("Filter timeout is hanging") 628 } 629 } 630 }