-
Notifications
You must be signed in to change notification settings - Fork 244
/
logs_filter.go
161 lines (144 loc) · 3.43 KB
/
logs_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package rpcfilters
import (
"context"
"fmt"
"math/big"
"sync"
"time"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)
type logsFilter struct {
mu sync.RWMutex
logs []types.Log
crit ethereum.FilterQuery // will be modified and different from original
originalCrit ethereum.FilterQuery // not modified version of the criteria
logsCache *cache
id rpc.ID
timer *time.Timer
ctx context.Context
cancel context.CancelFunc
done chan struct{}
}
func (f *logsFilter) criteria() ethereum.FilterQuery {
f.mu.RLock()
defer f.mu.RUnlock()
return f.crit
}
func (f *logsFilter) add(data interface{}) error {
logs, ok := data.([]types.Log)
if !ok {
return fmt.Errorf("can't cast %v to types.Log", data)
}
filtered := filterLogs(logs, f.crit)
if len(filtered) > 0 {
f.mu.Lock()
defer f.mu.Unlock()
added, replaced, err := f.logsCache.add(filtered)
if err != nil {
return err
}
for _, log := range replaced {
log.Removed = true
f.logs = append(f.logs, log)
}
if len(added) > 0 {
f.logs = append(f.logs, added...)
}
// if there was no replaced logs - keep polling only latest logs
if len(replaced) == 0 {
adjustFromBlock(&f.crit)
} else {
// otherwise poll earliest known block in cache
earliest := f.logsCache.earliestBlockNum()
if earliest != 0 {
f.crit.FromBlock = new(big.Int).SetUint64(earliest)
}
}
}
return nil
}
func (f *logsFilter) pop() interface{} {
f.mu.Lock()
defer f.mu.Unlock()
rst := f.logs
f.logs = nil
return rst
}
func (f *logsFilter) stop() {
select {
case <-f.done:
return
default:
close(f.done)
if f.cancel != nil {
f.cancel()
}
}
}
func (f *logsFilter) deadline() *time.Timer {
return f.timer
}
// adjustFromBlock adjusts crit.FromBlock to latest to avoid querying same logs.
func adjustFromBlock(crit *ethereum.FilterQuery) {
latest := big.NewInt(rpc.LatestBlockNumber.Int64())
// don't adjust if filter is not interested in newer blocks
if crit.ToBlock != nil && crit.ToBlock.Cmp(latest) == 1 {
return
}
// don't adjust if from block is already pending
if crit.FromBlock != nil && crit.FromBlock.Cmp(latest) == -1 {
return
}
crit.FromBlock = latest
}
func includes(addresses []common.Address, a common.Address) bool {
for _, addr := range addresses {
if addr == a {
return true
}
}
return false
}
// filterLogs creates a slice of logs matching the given criteria.
func filterLogs(logs []types.Log, crit ethereum.FilterQuery) (
ret []types.Log) {
for _, log := range logs {
if matchLog(log, crit) {
ret = append(ret, log)
}
}
return
}
func matchLog(log types.Log, crit ethereum.FilterQuery) bool {
if crit.FromBlock != nil && crit.FromBlock.Int64() >= 0 && crit.FromBlock.Uint64() > log.BlockNumber {
return false
}
if crit.ToBlock != nil && crit.ToBlock.Int64() >= 0 && crit.ToBlock.Uint64() < log.BlockNumber {
return false
}
if len(crit.Addresses) > 0 && !includes(crit.Addresses, log.Address) {
return false
}
if len(crit.Topics) > len(log.Topics) {
return false
}
return matchTopics(log, crit.Topics)
}
func matchTopics(log types.Log, topics [][]common.Hash) bool {
for i, sub := range topics {
match := len(sub) == 0 // empty rule set == wildcard
for _, topic := range sub {
if log.Topics[i] == topic {
match = true
break
}
}
if !match {
return false
}
}
return true
}