github.com/status-im/status-go@v1.1.0/services/rpcfilters/logs_filter_test.go (about)

     1  package rpcfilters
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	ethereum "github.com/ethereum/go-ethereum"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/core/types"
    12  )
    13  
    14  func TestFilterLogs(t *testing.T) {
    15  	logs := []types.Log{
    16  		{
    17  			BlockNumber: 1,
    18  			BlockHash:   common.Hash{1, 1},
    19  			Address:     common.Address{1, 1, 1},
    20  			Topics: []common.Hash{
    21  				{1},
    22  				{1, 1},
    23  			},
    24  		},
    25  		{
    26  			BlockNumber: 2,
    27  			BlockHash:   common.Hash{2, 2},
    28  			Address:     common.Address{2, 2, 2},
    29  			Topics: []common.Hash{
    30  				{1},
    31  				{2, 2},
    32  			},
    33  		},
    34  	}
    35  
    36  	type testCase struct {
    37  		description  string
    38  		crit         ethereum.FilterQuery
    39  		expectedLogs []types.Log
    40  	}
    41  
    42  	for _, tc := range []testCase{
    43  		{
    44  			description:  "All",
    45  			crit:         ethereum.FilterQuery{},
    46  			expectedLogs: []types.Log{logs[0], logs[1]},
    47  		},
    48  		{
    49  			description:  "LimitedByBlock",
    50  			crit:         ethereum.FilterQuery{ToBlock: big.NewInt(1)},
    51  			expectedLogs: []types.Log{logs[0]},
    52  		},
    53  		{
    54  			description:  "LimitedByAddress",
    55  			crit:         ethereum.FilterQuery{Addresses: []common.Address{logs[1].Address}},
    56  			expectedLogs: []types.Log{logs[1]},
    57  		},
    58  		{
    59  			description:  "LimitedByAddress",
    60  			crit:         ethereum.FilterQuery{Addresses: []common.Address{logs[1].Address}},
    61  			expectedLogs: []types.Log{logs[1]},
    62  		},
    63  		{
    64  			description: "MoreTopicsThanInLogs",
    65  			crit:        ethereum.FilterQuery{Topics: make([][]common.Hash, 3)},
    66  		},
    67  		{
    68  			description:  "Wildcard",
    69  			crit:         ethereum.FilterQuery{Topics: make([][]common.Hash, 1)},
    70  			expectedLogs: []types.Log{logs[0], logs[1]},
    71  		},
    72  		{
    73  			description:  "LimitedBySecondTopic",
    74  			crit:         ethereum.FilterQuery{Topics: [][]common.Hash{{}, logs[1].Topics}},
    75  			expectedLogs: []types.Log{logs[1]},
    76  		},
    77  	} {
    78  		tc := tc
    79  		t.Run(tc.description, func(t *testing.T) {
    80  			t.Parallel()
    81  			rst := filterLogs(logs, tc.crit)
    82  			require.Equal(t, tc.expectedLogs, rst)
    83  		})
    84  	}
    85  }
    86  
    87  func TestAdjustFromBlock(t *testing.T) {
    88  	type testCase struct {
    89  		description string
    90  		initial     ethereum.FilterQuery
    91  		result      ethereum.FilterQuery
    92  	}
    93  
    94  	for _, tc := range []testCase{
    95  		{
    96  			"ToBlockHigherThenLatest",
    97  			ethereum.FilterQuery{ToBlock: big.NewInt(10)},
    98  			ethereum.FilterQuery{ToBlock: big.NewInt(10)},
    99  		},
   100  		{
   101  			"FromBlockIsPending",
   102  			ethereum.FilterQuery{FromBlock: big.NewInt(-2)},
   103  			ethereum.FilterQuery{FromBlock: big.NewInt(-2)},
   104  		},
   105  		{
   106  			"FromBlockIsOlderThenLatest",
   107  			ethereum.FilterQuery{FromBlock: big.NewInt(10)},
   108  			ethereum.FilterQuery{FromBlock: big.NewInt(-1)},
   109  		},
   110  		{
   111  			"NotInterestedInLatestBlocks",
   112  			ethereum.FilterQuery{FromBlock: big.NewInt(10), ToBlock: big.NewInt(15)},
   113  			ethereum.FilterQuery{FromBlock: big.NewInt(10), ToBlock: big.NewInt(15)},
   114  		},
   115  	} {
   116  		tc := tc
   117  		t.Run(tc.description, func(t *testing.T) {
   118  			t.Parallel()
   119  			adjustFromBlock(&tc.initial)
   120  			require.Equal(t, tc.result, tc.initial)
   121  		})
   122  	}
   123  }