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