github.com/status-im/status-go@v1.1.0/services/rpcfilters/logs_cache_test.go (about) 1 package rpcfilters 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/ethereum/go-ethereum/common" 10 "github.com/ethereum/go-ethereum/core/types" 11 ) 12 13 func TestAggregateLogs(t *testing.T) { 14 logs := []types.Log{} 15 for i := 1; i <= 15; i++ { 16 logs = append(logs, 17 types.Log{BlockNumber: uint64(i), BlockHash: common.Hash{byte(i)}}, 18 types.Log{BlockNumber: uint64(i), BlockHash: common.Hash{byte(i)}}) 19 } 20 aggregated := aggregateLogs(logs, 10) 21 start := 15 - len(aggregated) + 1 22 for _, record := range aggregated { 23 assert.Equal(t, start, int(record.block)) // numbers are small 24 assert.Len(t, record.logs, 2) 25 start++ 26 } 27 } 28 29 func TestAggregateLessThenFull(t *testing.T) { 30 logs := []types.Log{} 31 for i := 1; i <= 3; i++ { 32 logs = append(logs, 33 types.Log{BlockNumber: uint64(i), BlockHash: common.Hash{byte(i)}}) 34 } 35 aggregated := aggregateLogs(logs, 10) 36 start := 1 37 for _, record := range aggregated { 38 assert.Equal(t, start, int(record.block)) // numbers are small 39 assert.Len(t, record.logs, 1) 40 start++ 41 } 42 } 43 44 func TestMerge(t *testing.T) { 45 step1Logs := []types.Log{ 46 {BlockNumber: 1, BlockHash: common.Hash{1}}, 47 {BlockNumber: 2, BlockHash: common.Hash{2}}, 48 {BlockNumber: 3, BlockHash: common.Hash{3}}, 49 } 50 step2Logs := []types.Log{ 51 {BlockNumber: 2, BlockHash: common.Hash{2}}, 52 {BlockNumber: 3, BlockHash: common.Hash{3}}, 53 {BlockNumber: 4, BlockHash: common.Hash{4}}, 54 } 55 reorg := []types.Log{ 56 {BlockNumber: 2, BlockHash: common.Hash{2, 2}}, 57 {BlockNumber: 3, BlockHash: common.Hash{3, 3}}, 58 {BlockNumber: 4, BlockHash: common.Hash{4, 4}}, 59 {BlockNumber: 5, BlockHash: common.Hash{5, 4}}, 60 } 61 62 limit := 7 63 cache := make([]cacheRecord, 0, limit) 64 cache, added, replaced := merge(0, cache, aggregateLogs(step1Logs, limit)) 65 require.Len(t, added, 3) 66 require.Empty(t, replaced) 67 require.Equal(t, 3, int(cache[2].block)) 68 cache, added, replaced = merge(1, cache, aggregateLogs(step2Logs, limit)) 69 require.Len(t, added, 1) 70 require.Empty(t, replaced) 71 require.Equal(t, 4, int(cache[3].block)) 72 _, added, replaced = merge(1, cache, aggregateLogs(reorg, limit)) 73 require.Len(t, added, 4) 74 require.Len(t, replaced, 3) 75 } 76 77 func TestMergeFull(t *testing.T) { 78 old := []cacheRecord{ 79 {block: 1, hash: common.Hash{1}}, 80 {block: 2, hash: common.Hash{2}}, 81 {block: 3, hash: common.Hash{3}}, 82 } 83 new := []cacheRecord{ 84 {block: 4, hash: common.Hash{4}}, 85 {block: 5, hash: common.Hash{5}}, 86 } 87 old, _, _ = merge(0, old, new) 88 require.Len(t, old, 5) 89 require.Equal(t, int(old[2].block), 3) 90 require.Equal(t, int(old[3].block), 4) 91 require.Equal(t, int(old[4].block), 5) 92 } 93 94 func TestAddLogs(t *testing.T) { 95 c := newCache(7) 96 step1Logs := []types.Log{ 97 {BlockNumber: 1, BlockHash: common.Hash{1}}, 98 {BlockNumber: 2, BlockHash: common.Hash{2}}, 99 {BlockNumber: 3, BlockHash: common.Hash{3}}, 100 } 101 step2Logs := []types.Log{ 102 {BlockNumber: 2, BlockHash: common.Hash{2}}, 103 {BlockNumber: 3, BlockHash: common.Hash{3}}, 104 {BlockNumber: 4, BlockHash: common.Hash{4}}, 105 } 106 added, replaced, err := c.add(step1Logs) 107 require.NoError(t, err) 108 require.Len(t, added, 3) 109 require.Empty(t, replaced) 110 added, replaced, err = c.add(step2Logs) 111 require.NoError(t, err) 112 require.Len(t, added, 1) 113 require.Empty(t, replaced) 114 } 115 116 func TestAddLogsNotInOrder(t *testing.T) { 117 c := newCache(7) 118 logs := []types.Log{{BlockNumber: 1, BlockHash: common.Hash{1}}, {BlockNumber: 3, BlockHash: common.Hash{3}}} 119 _, _, err := c.add(logs) 120 require.EqualError(t, err, "logs must be delivered straight in order. gaps between blocks '1' and '3'") 121 }