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