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