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