forked from ledgerwatch/erigon
/
eth_filters.go
283 lines (255 loc) · 7.44 KB
/
eth_filters.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package jsonrpc
import (
"context"
"strings"
"github.com/ledgerwatch/log/v3"
"github.com/nebojsa94/erigon/common/debug"
"github.com/nebojsa94/erigon/core/types"
"github.com/nebojsa94/erigon/eth/filters"
"github.com/nebojsa94/erigon/rpc"
"github.com/nebojsa94/erigon/turbo/rpchelper"
)
// NewPendingTransactionFilter new transaction filter
func (api *APIImpl) NewPendingTransactionFilter(_ context.Context) (string, error) {
if api.filters == nil {
return "", rpc.ErrNotificationsUnsupported
}
txsCh, id := api.filters.SubscribePendingTxs(32)
go func() {
for txs := range txsCh {
api.filters.AddPendingTxs(id, txs)
}
}()
return "0x" + string(id), nil
}
// NewBlockFilter implements eth_newBlockFilter. Creates a filter in the node, to notify when a new block arrives.
func (api *APIImpl) NewBlockFilter(_ context.Context) (string, error) {
if api.filters == nil {
return "", rpc.ErrNotificationsUnsupported
}
ch, id := api.filters.SubscribeNewHeads(32)
go func() {
for block := range ch {
api.filters.AddPendingBlock(id, block)
}
}()
return "0x" + string(id), nil
}
// NewFilter implements eth_newFilter. Creates an arbitrary filter object, based on filter options, to notify when the state changes (logs).
func (api *APIImpl) NewFilter(_ context.Context, crit filters.FilterCriteria) (string, error) {
if api.filters == nil {
return "", rpc.ErrNotificationsUnsupported
}
logs, id := api.filters.SubscribeLogs(256, crit)
go func() {
for lg := range logs {
api.filters.AddLogs(id, lg)
}
}()
return "0x" + string(id), nil
}
// UninstallFilter new transaction filter
func (api *APIImpl) UninstallFilter(_ context.Context, index string) (isDeleted bool, err error) {
if api.filters == nil {
return false, rpc.ErrNotificationsUnsupported
}
// remove 0x
cutIndex := strings.TrimPrefix(index, "0x")
if ok := api.filters.UnsubscribeHeads(rpchelper.HeadsSubID(cutIndex)); ok {
isDeleted = true
}
if ok := api.filters.UnsubscribePendingTxs(rpchelper.PendingTxsSubID(cutIndex)); ok {
isDeleted = true
}
if ok := api.filters.UnsubscribeLogs(rpchelper.LogsSubID(cutIndex)); ok {
isDeleted = true
}
return
}
// GetFilterChanges implements eth_getFilterChanges.
// Polling method for a previously-created filter
// returns an array of logs, block headers, or pending transactions which occurred since last poll.
func (api *APIImpl) GetFilterChanges(_ context.Context, index string) ([]any, error) {
if api.filters == nil {
return nil, rpc.ErrNotificationsUnsupported
}
stub := make([]any, 0)
// remove 0x
cutIndex := strings.TrimPrefix(index, "0x")
if blocks, ok := api.filters.ReadPendingBlocks(rpchelper.HeadsSubID(cutIndex)); ok {
for _, v := range blocks {
stub = append(stub, v.Hash())
}
return stub, nil
}
if txs, ok := api.filters.ReadPendingTxs(rpchelper.PendingTxsSubID(cutIndex)); ok {
if len(txs) > 0 {
for _, tx := range txs[0] {
stub = append(stub, tx.Hash())
}
return stub, nil
}
return stub, nil
}
if logs, ok := api.filters.ReadLogs(rpchelper.LogsSubID(cutIndex)); ok {
for _, v := range logs {
stub = append(stub, v)
}
return stub, nil
}
return stub, nil
}
// GetFilterLogs implements eth_getFilterLogs.
// Polling method for a previously-created filter
// returns an array of logs which occurred since last poll.
func (api *APIImpl) GetFilterLogs(_ context.Context, index string) ([]*types.Log, error) {
if api.filters == nil {
return nil, rpc.ErrNotificationsUnsupported
}
cutIndex := strings.TrimPrefix(index, "0x")
logs, ok := api.filters.ReadLogs(rpchelper.LogsSubID(cutIndex))
if len(logs) == 0 || !ok {
return []*types.Log{}, nil
}
return logs, nil
}
// NewHeads send a notification each time a new (header) block is appended to the chain.
func (api *APIImpl) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
if api.filters == nil {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
rpcSub := notifier.CreateSubscription()
go func() {
defer debug.LogPanic()
headers, id := api.filters.SubscribeNewHeads(32)
defer api.filters.UnsubscribeHeads(id)
for {
select {
case h, ok := <-headers:
if h != nil {
err := notifier.Notify(rpcSub.ID, h)
if err != nil {
log.Warn("[rpc] error while notifying subscription", "err", err)
}
}
if !ok {
log.Warn("[rpc] new heads channel was closed")
return
}
case <-rpcSub.Err():
return
}
}
}()
return rpcSub, nil
}
// NewPendingTransactions send a notification each time when a transaction had added into mempool.
func (api *APIImpl) NewPendingTransactions(ctx context.Context) (*rpc.Subscription, error) {
if api.filters == nil {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
rpcSub := notifier.CreateSubscription()
go func() {
defer debug.LogPanic()
txsCh, id := api.filters.SubscribePendingTxs(256)
defer api.filters.UnsubscribePendingTxs(id)
for {
select {
case txs, ok := <-txsCh:
for _, t := range txs {
if t != nil {
err := notifier.Notify(rpcSub.ID, t.Hash())
if err != nil {
log.Warn("[rpc] error while notifying subscription", "err", err)
}
}
}
if !ok {
log.Warn("[rpc] new pending transactions channel was closed")
return
}
case <-rpcSub.Err():
return
}
}
}()
return rpcSub, nil
}
// NewPendingTransactionsWithBody send a notification each time when a transaction had added into mempool.
func (api *APIImpl) NewPendingTransactionsWithBody(ctx context.Context) (*rpc.Subscription, error) {
if api.filters == nil {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
rpcSub := notifier.CreateSubscription()
go func() {
defer debug.LogPanic()
txsCh, id := api.filters.SubscribePendingTxs(512)
defer api.filters.UnsubscribePendingTxs(id)
for {
select {
case txs, ok := <-txsCh:
for _, t := range txs {
if t != nil {
err := notifier.Notify(rpcSub.ID, t)
if err != nil {
log.Warn("[rpc] error while notifying subscription", "err", err)
}
}
}
if !ok {
log.Warn("[rpc] new pending transactions channel was closed")
return
}
case <-rpcSub.Err():
return
}
}
}()
return rpcSub, nil
}
// Logs send a notification each time a new log appears.
func (api *APIImpl) Logs(ctx context.Context, crit filters.FilterCriteria) (*rpc.Subscription, error) {
if api.filters == nil {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
rpcSub := notifier.CreateSubscription()
go func() {
defer debug.LogPanic()
logs, id := api.filters.SubscribeLogs(128, crit)
defer api.filters.UnsubscribeLogs(id)
for {
select {
case h, ok := <-logs:
if h != nil {
err := notifier.Notify(rpcSub.ID, h)
if err != nil {
log.Warn("[rpc] error while notifying subscription", "err", err)
}
}
if !ok {
log.Warn("[rpc] log channel was closed")
return
}
case <-rpcSub.Err():
return
}
}
}()
return rpcSub, nil
}