diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index 2dab89e02a..56dfffbebc 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -712,14 +712,6 @@ func TestPendingTxFilterDeadlock(t *testing.T) { } } -func flattenLogs(pl [][]*types.Log) []*types.Log { - var logs []*types.Log - for _, l := range pl { - logs = append(logs, l...) - } - return logs -} - type mockNotifier struct { c chan interface{} } @@ -733,38 +725,16 @@ func (n *mockNotifier) Notify(id rpc.ID, data interface{}) error { return nil } -func (n *mockNotifier) Closed() <-chan interface{} { - return nil -} - // TestLogsSubscription tests if a rpc subscription receives the correct logs func TestLogsSubscription(t *testing.T) { t.Parallel() var ( - db = rawdb.NewMemoryDatabase() - tdb = triedb.NewDatabase(db, triedb.HashDefaults) - signer = types.HomesteadSigner{} - key, _ = crypto.GenerateKey() - addr = crypto.PubkeyToAddress(key.PublicKey) - contract = common.HexToAddress("0000000000000000000000000000000000031ec7") - genesis = &core.Genesis{ - Config: params.TestChainConfig, - Alloc: core.GenesisAlloc{ - // // SPDX-License-Identifier: GPL-3.0 - // pragma solidity >=0.7.0 <0.9.0; - // - // contract Token { - // event Transfer(address indexed from, address indexed to, uint256 value); - // function transfer(address to, uint256 value) public returns (bool) { - // emit Transfer(msg.sender, to, value); - // return true; - // } - // } - contract: {Balance: big.NewInt(params.Ether), Code: common.FromHex("0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b60073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033")}, - addr: {Balance: big.NewInt(params.Ether)}, - }, - } + db = rawdb.NewMemoryDatabase() + tdb = triedb.NewDatabase(db, triedb.HashDefaults) + genesis = &core.Genesis{Config: params.TestChainConfig, Alloc: genesisAlloc} + backend, sys = newTestFilterSystem(db, Config{}) + api = NewFilterAPI(sys) ) // Hack: GenerateChainWithGenesis creates a new db. @@ -783,11 +753,24 @@ func TestLogsSubscription(t *testing.T) { if err != nil { t.Fatal(err) } + + // Hack: FilterSystem is using mock backend, i.e. blockchain events will be not received + // by it. Forward them manually. + var ( + bcLogsFeed = make(chan []*types.Log) + bcRmLogsFeed = make(chan core.RemovedLogsEvent) + ) + backend.forwardLogEvents(bcLogsFeed, bcRmLogsFeed) + bc.SubscribeLogsEvent(bcLogsFeed) + _, err = bc.InsertChain(blocks) if err != nil { t.Fatal(err) } + backend.startFilterMaps(0, false, filtermaps.RangeTestParams) + defer backend.stopFilterMaps() + // Generate pending block, logs for which // will be sent to subscription feed. _, preceipts := core.GenerateChain(genesis.Config, blocks[len(blocks)-1], ethash.NewFaker(), db, 1, func(i int, gen *core.BlockGen) { @@ -797,23 +780,12 @@ func TestLogsSubscription(t *testing.T) { gen.AddTx(tx) }) liveLogs := preceipts[0][0].Logs - var ( - backend, sys = newTestFilterSystem(db, Config{}) - api = NewFilterAPI(sys) - // Transfer(address indexed from, address indexed to, uint256 value); - topic = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef") - ) - - backend.startFilterMaps(0, false, filtermaps.RangeTestParams) - defer backend.stopFilterMaps() - - i2h := func(i int) common.Hash { return common.BigToHash(big.NewInt(int64(i))) } allLogs := []*types.Log{ - {Address: contract, Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(1)}, Data: i2h(11).Bytes(), BlockNumber: 1, BlockHash: blocks[0].Hash(), TxHash: blocks[0].Transactions()[0].Hash()}, - {Address: contract, Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(2)}, Data: i2h(12).Bytes(), BlockNumber: 2, BlockHash: blocks[1].Hash(), TxHash: blocks[1].Transactions()[0].Hash()}, - {Address: contract, Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(3)}, Data: i2h(13).Bytes(), BlockNumber: 3, BlockHash: blocks[2].Hash(), TxHash: blocks[2].Transactions()[0].Hash()}, - {Address: contract, Topics: []common.Hash{topic, common.HexToHash(addr.Hex()), i2h(4)}, Data: i2h(14).Bytes(), BlockNumber: 4, BlockHash: blocks[3].Hash(), TxHash: blocks[3].Transactions()[0].Hash()}, + {Address: contract, Topics: []common.Hash{topic, addrHash, i2h(1)}, Data: i2b(11), BlockNumber: 1, BlockTimestamp: 10, BlockHash: blocks[0].Hash(), TxHash: blocks[0].Transactions()[0].Hash()}, + {Address: contract, Topics: []common.Hash{topic, addrHash, i2h(2)}, Data: i2b(12), BlockNumber: 2, BlockTimestamp: 20, BlockHash: blocks[1].Hash(), TxHash: blocks[1].Transactions()[0].Hash()}, + {Address: contract, Topics: []common.Hash{topic, addrHash, i2h(3)}, Data: i2b(13), BlockNumber: 3, BlockTimestamp: 30, BlockHash: blocks[2].Hash(), TxHash: blocks[2].Transactions()[0].Hash()}, + {Address: contract, Topics: []common.Hash{topic, addrHash, i2h(4)}, Data: i2b(14), BlockNumber: 4, BlockTimestamp: 40, BlockHash: blocks[3].Hash(), TxHash: blocks[3].Transactions()[0].Hash()}, } testCases := []struct { crit FilterCriteria @@ -926,6 +898,7 @@ var ( signer = types.HomesteadSigner{} key, _ = crypto.GenerateKey() addr = crypto.PubkeyToAddress(key.PublicKey) + addrHash = common.HexToHash(addr.Hex()) contract = common.HexToAddress("0000000000000000000000000000000000031ec7") // Transfer(address indexed from, address indexed to, uint256 value); topic = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef") @@ -954,6 +927,7 @@ func makeTx(to int, value int, b *core.BlockGen) *types.Transaction { func i2h(i int) common.Hash { return common.BigToHash(big.NewInt(int64(i))) } func a2h(a common.Address) common.Hash { return common.HexToHash(a.Hex()) } +func i2b(i int) []byte { return i2h(i).Bytes() } func parseTransferLog(log *types.Log) (from, to common.Address, amount uint64) { from = common.BytesToAddress(log.Topics[1].Bytes())