github.com/lzl124631x/go-ethereum@v1.8.12-0.20180615081455-574378edb50c/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/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/consensus/ethash"
    28  	"github.com/ethereum/go-ethereum/core"
    29  	"github.com/ethereum/go-ethereum/core/rawdb"
    30  	"github.com/ethereum/go-ethereum/core/types"
    31  	"github.com/ethereum/go-ethereum/crypto"
    32  	"github.com/ethereum/go-ethereum/ethdb"
    33  	"github.com/ethereum/go-ethereum/event"
    34  	"github.com/ethereum/go-ethereum/params"
    35  )
    36  
    37  func makeReceipt(addr common.Address) *types.Receipt {
    38  	receipt := types.NewReceipt(nil, false, 0)
    39  	receipt.Logs = []*types.Log{
    40  		{Address: addr},
    41  	}
    42  	receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
    43  	return receipt
    44  }
    45  
    46  func BenchmarkFilters(b *testing.B) {
    47  	dir, err := ioutil.TempDir("", "filtertest")
    48  	if err != nil {
    49  		b.Fatal(err)
    50  	}
    51  	defer os.RemoveAll(dir)
    52  
    53  	var (
    54  		db, _      = ethdb.NewLDBDatabase(dir, 0, 0)
    55  		mux        = new(event.TypeMux)
    56  		txFeed     = new(event.Feed)
    57  		rmLogsFeed = new(event.Feed)
    58  		logsFeed   = new(event.Feed)
    59  		chainFeed  = new(event.Feed)
    60  		backend    = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed}
    61  		key1, _    = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    62  		addr1      = crypto.PubkeyToAddress(key1.PublicKey)
    63  		addr2      = common.BytesToAddress([]byte("jeff"))
    64  		addr3      = common.BytesToAddress([]byte("ethereum"))
    65  		addr4      = common.BytesToAddress([]byte("random addresses please"))
    66  	)
    67  	defer db.Close()
    68  
    69  	genesis := core.GenesisBlockForTesting(db, addr1, big.NewInt(1000000))
    70  	chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 100010, func(i int, gen *core.BlockGen) {
    71  		switch i {
    72  		case 2403:
    73  			receipt := makeReceipt(addr1)
    74  			gen.AddUncheckedReceipt(receipt)
    75  		case 1034:
    76  			receipt := makeReceipt(addr2)
    77  			gen.AddUncheckedReceipt(receipt)
    78  		case 34:
    79  			receipt := makeReceipt(addr3)
    80  			gen.AddUncheckedReceipt(receipt)
    81  		case 99999:
    82  			receipt := makeReceipt(addr4)
    83  			gen.AddUncheckedReceipt(receipt)
    84  
    85  		}
    86  	})
    87  	for i, block := range chain {
    88  		rawdb.WriteBlock(db, block)
    89  		rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
    90  		rawdb.WriteHeadBlockHash(db, block.Hash())
    91  		rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
    92  	}
    93  	b.ResetTimer()
    94  
    95  	filter := New(backend, 0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil)
    96  
    97  	for i := 0; i < b.N; i++ {
    98  		logs, _ := filter.Logs(context.Background())
    99  		if len(logs) != 4 {
   100  			b.Fatal("expected 4 logs, got", len(logs))
   101  		}
   102  	}
   103  }
   104  
   105  func TestFilters(t *testing.T) {
   106  	dir, err := ioutil.TempDir("", "filtertest")
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	defer os.RemoveAll(dir)
   111  
   112  	var (
   113  		db, _      = ethdb.NewLDBDatabase(dir, 0, 0)
   114  		mux        = new(event.TypeMux)
   115  		txFeed     = new(event.Feed)
   116  		rmLogsFeed = new(event.Feed)
   117  		logsFeed   = new(event.Feed)
   118  		chainFeed  = new(event.Feed)
   119  		backend    = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed}
   120  		key1, _    = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
   121  		addr       = crypto.PubkeyToAddress(key1.PublicKey)
   122  
   123  		hash1 = common.BytesToHash([]byte("topic1"))
   124  		hash2 = common.BytesToHash([]byte("topic2"))
   125  		hash3 = common.BytesToHash([]byte("topic3"))
   126  		hash4 = common.BytesToHash([]byte("topic4"))
   127  	)
   128  	defer db.Close()
   129  
   130  	genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000))
   131  	chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {
   132  		switch i {
   133  		case 1:
   134  			receipt := types.NewReceipt(nil, false, 0)
   135  			receipt.Logs = []*types.Log{
   136  				{
   137  					Address: addr,
   138  					Topics:  []common.Hash{hash1},
   139  				},
   140  			}
   141  			gen.AddUncheckedReceipt(receipt)
   142  		case 2:
   143  			receipt := types.NewReceipt(nil, false, 0)
   144  			receipt.Logs = []*types.Log{
   145  				{
   146  					Address: addr,
   147  					Topics:  []common.Hash{hash2},
   148  				},
   149  			}
   150  			gen.AddUncheckedReceipt(receipt)
   151  		case 998:
   152  			receipt := types.NewReceipt(nil, false, 0)
   153  			receipt.Logs = []*types.Log{
   154  				{
   155  					Address: addr,
   156  					Topics:  []common.Hash{hash3},
   157  				},
   158  			}
   159  			gen.AddUncheckedReceipt(receipt)
   160  		case 999:
   161  			receipt := types.NewReceipt(nil, false, 0)
   162  			receipt.Logs = []*types.Log{
   163  				{
   164  					Address: addr,
   165  					Topics:  []common.Hash{hash4},
   166  				},
   167  			}
   168  			gen.AddUncheckedReceipt(receipt)
   169  		}
   170  	})
   171  	for i, block := range chain {
   172  		rawdb.WriteBlock(db, block)
   173  		rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
   174  		rawdb.WriteHeadBlockHash(db, block.Hash())
   175  		rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i])
   176  	}
   177  
   178  	filter := New(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}})
   179  
   180  	logs, _ := filter.Logs(context.Background())
   181  	if len(logs) != 4 {
   182  		t.Error("expected 4 log, got", len(logs))
   183  	}
   184  
   185  	filter = New(backend, 900, 999, []common.Address{addr}, [][]common.Hash{{hash3}})
   186  	logs, _ = filter.Logs(context.Background())
   187  	if len(logs) != 1 {
   188  		t.Error("expected 1 log, got", len(logs))
   189  	}
   190  	if len(logs) > 0 && logs[0].Topics[0] != hash3 {
   191  		t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
   192  	}
   193  
   194  	filter = New(backend, 990, -1, []common.Address{addr}, [][]common.Hash{{hash3}})
   195  	logs, _ = filter.Logs(context.Background())
   196  	if len(logs) != 1 {
   197  		t.Error("expected 1 log, got", len(logs))
   198  	}
   199  	if len(logs) > 0 && logs[0].Topics[0] != hash3 {
   200  		t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0])
   201  	}
   202  
   203  	filter = New(backend, 1, 10, nil, [][]common.Hash{{hash1, hash2}})
   204  
   205  	logs, _ = filter.Logs(context.Background())
   206  	if len(logs) != 2 {
   207  		t.Error("expected 2 log, got", len(logs))
   208  	}
   209  
   210  	failHash := common.BytesToHash([]byte("fail"))
   211  	filter = New(backend, 0, -1, nil, [][]common.Hash{{failHash}})
   212  
   213  	logs, _ = filter.Logs(context.Background())
   214  	if len(logs) != 0 {
   215  		t.Error("expected 0 log, got", len(logs))
   216  	}
   217  
   218  	failAddr := common.BytesToAddress([]byte("failmenow"))
   219  	filter = New(backend, 0, -1, []common.Address{failAddr}, nil)
   220  
   221  	logs, _ = filter.Logs(context.Background())
   222  	if len(logs) != 0 {
   223  		t.Error("expected 0 log, got", len(logs))
   224  	}
   225  
   226  	filter = New(backend, 0, -1, nil, [][]common.Hash{{failHash}, {hash1}})
   227  
   228  	logs, _ = filter.Logs(context.Background())
   229  	if len(logs) != 0 {
   230  		t.Error("expected 0 log, got", len(logs))
   231  	}
   232  }