-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter.go
189 lines (174 loc) · 4.22 KB
/
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package seth
import (
"encoding/json"
"log"
"math"
"math/big"
"strconv"
"sync"
"time"
)
// Filter represents a Log filter
type Filter struct {
id int64
c *Client
out chan *Log
lock sync.Mutex // guards below
exit chan struct{}
err error
closed bool
poll bool // continue polling after first fetch
}
// Out returns the channel of output logs.
// The output channel will be closed when the
// filter is closed, or when the filter encounters
// an error, in which case (*Filter).Err() will be non-nil.
func (f *Filter) Out() <-chan *Log { return f.out }
func (f *Filter) seterr(err error) {
f.lock.Lock()
f.err = err
f.lock.Unlock()
}
// Err returns an error if the filter
// has encountered an error while fetching logs.
func (f *Filter) Err() error {
f.lock.Lock()
err := f.err
f.lock.Unlock()
return err
}
// Close will close the filter. Closing
// the filter will cause the output channel
// to be closed. Close is safe to call from
// any goroutine.
func (f *Filter) Close() {
f.lock.Lock()
if !f.closed {
close(f.exit)
f.closed = true
f.c.deleteFilter(f.id)
}
f.lock.Unlock()
}
type newFilterReq struct {
FromBlock json.RawMessage `json:"fromBlock,omitempty"`
ToBlock json.RawMessage `json:"toBlock,omitempty"`
Address *Address `json:"address,omitempty"`
Topics []*Hash `json:"topics,omitempty"`
}
func frecv(f *Filter) {
logs, err := f.c.getLogs(f.id)
if err != nil {
f.seterr(err)
goto done
}
for i := range logs {
f.out <- &logs[i]
}
if !f.poll {
goto done
}
for {
ticker := time.NewTicker(time.Second)
select {
case <-f.exit:
ticker.Stop()
goto done
case <-ticker.C:
logs, err := f.c.getUpdates(f.id)
if err != nil {
f.seterr(err)
ticker.Stop()
goto done
}
for i := range logs {
f.out <- &logs[i]
}
}
}
done:
close(f.out)
}
func (c *Client) getLogs(id int64) ([]Log, error) {
var o []Log
p := []json.RawMessage{itox(id)}
err := c.Do("eth_getFilterLogs", p, &o)
return o, err
}
func (c *Client) getUpdates(id int64) ([]Log, error) {
var o []Log
p := []json.RawMessage{itox(id)}
err := c.Do("eth_getFilterChanges", p, &o)
return o, err
}
func (c *Client) deleteFilter(id int64) {
var out bool
err := c.Do("eth_uninstallFilter", []json.RawMessage{itox(id)}, &out)
if err != nil || !out {
log.Printf("uninstallFilter: %s %v", err, out)
}
}
func itox(i int64) json.RawMessage {
o := make([]byte, 3, 64)
o[0] = '"'
o[1] = '0'
o[2] = 'x'
o = strconv.AppendInt(o, i, 16)
return append(o, '"')
}
// FilterTopics creates a log filter that matches the given topics. (Topics are order-dependent.)
// If 'addr' is non-nil, only logs generated from that address are yielded by the filter.
// If 'start' and 'end' are non-negative, then they specify the range of blocks in which to
// search. Otherwise, the filter starts at the latest block.
func (c *Client) FilterTopics(topics []*Hash, addr *Address, start, end int64) (*Filter, error) {
_ = math.MaxInt64
req := &newFilterReq{
Address: addr,
Topics: topics,
}
poll := false
if start < 0 {
poll = true
req.FromBlock = rawlatest
} else {
req.FromBlock = itox(start)
}
if end < 0 {
req.ToBlock = rawlatest
} else {
req.ToBlock = itox(end)
}
buf, err := json.Marshal(req)
if err != nil {
return nil, err
}
var out Int
err = c.Do("eth_newFilter", []json.RawMessage{buf}, &out)
if err != nil {
return nil, err
}
id := (*big.Int)(&out).Int64()
f := &Filter{c: c, out: make(chan *Log, 20), exit: make(chan struct{}, 1), id: id, poll: poll}
go frecv(f)
return f, nil
}
// TokenTransfers returns a filter that searches for token transfers
// matching the given arguments. If any of the argments are nil, the
// filter matches that argument as a wildcard. In other words,
// if from, to, and tok are all nil, this filter finds all token
// transfers in the given block range.
func (c *Client) TokenTransfers(from *Address, to *Address, tok *Address, start, end int64) (*Filter, error) {
var hashes [3]Hash
var arg0 [3]*Hash
hashes[0] = ERC20Transfer
arg0[0] = &hashes[0]
if from != nil {
copy(hashes[1][12:], from[:])
arg0[1] = &hashes[1]
}
if to != nil {
copy(hashes[2][12:], to[:])
arg0[2] = &hashes[2]
}
return c.FilterTopics(arg0[:], tok, start, end)
}