github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/neatptc/filters/filter_test.go (about)

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