-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.go
282 lines (240 loc) · 8.87 KB
/
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
// Copyright 2021 Evmos Foundation
// This file is part of Evmos' Ethermint library.
//
// The Ethermint library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Ethermint library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Ethermint library. If not, see https://github.com/Timwood0x10/sei-chain/blob/main/LICENSE
package filters
import (
"context"
"encoding/binary"
"fmt"
"math/big"
"github.com/Timwood0x10/sei-chain/rpc/backend"
"github.com/Timwood0x10/sei-chain/rpc/types"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/log"
tmrpctypes "github.com/tendermint/tendermint/rpc/coretypes"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/filters"
)
// BloomIV represents the bit indexes and value inside the bloom filter that belong
// to some key.
type BloomIV struct {
I [3]uint
V [3]byte
}
// Filter can be used to retrieve and filter logs.
type Filter struct {
logger log.Logger
backend Backend
criteria filters.FilterCriteria
bloomFilters [][]BloomIV // Filter the system is matching for
}
// NewBlockFilter creates a new filter which directly inspects the contents of
// a block to figure out whether it is interesting or not.
func NewBlockFilter(logger log.Logger, backend Backend, criteria filters.FilterCriteria) *Filter {
// Create a generic filter and convert it into a block filter
return newFilter(logger, backend, criteria, nil)
}
// NewRangeFilter creates a new filter which uses a bloom filter on blocks to
// figure out whether a particular block is interesting or not.
func NewRangeFilter(logger log.Logger, backend Backend, begin, end int64, addresses []common.Address, topics [][]common.Hash) *Filter {
// Flatten the address and topic filter clauses into a single bloombits filter
// system. Since the bloombits are not positional, nil topics are permitted,
// which get flattened into a nil byte slice.
var filtersBz [][][]byte //nolint: prealloc
if len(addresses) > 0 {
filter := make([][]byte, len(addresses))
for i, address := range addresses {
filter[i] = address.Bytes()
}
filtersBz = append(filtersBz, filter)
}
for _, topicList := range topics {
filter := make([][]byte, len(topicList))
for i, topic := range topicList {
filter[i] = topic.Bytes()
}
filtersBz = append(filtersBz, filter)
}
// Create a generic filter and convert it into a range filter
criteria := filters.FilterCriteria{
FromBlock: big.NewInt(begin),
ToBlock: big.NewInt(end),
Addresses: addresses,
Topics: topics,
}
return newFilter(logger, backend, criteria, createBloomFilters(filtersBz, logger))
}
// newFilter returns a new Filter
func newFilter(logger log.Logger, backend Backend, criteria filters.FilterCriteria, bloomFilters [][]BloomIV) *Filter {
return &Filter{
logger: logger,
backend: backend,
criteria: criteria,
bloomFilters: bloomFilters,
}
}
const (
maxToOverhang = 600
)
// Logs searches the blockchain for matching log entries, returning all from the
// first block that contains matches, updating the start of the filter accordingly.
func (f *Filter) Logs(_ context.Context, logLimit int, blockLimit int64) ([]*ethtypes.Log, error) {
logs := []*ethtypes.Log{}
var err error
// If we're doing singleton block filtering, execute and return
if f.criteria.BlockHash != nil && *f.criteria.BlockHash != (common.Hash{}) {
resBlock, err := f.backend.TendermintBlockByHash(*f.criteria.BlockHash)
if err != nil {
return nil, fmt.Errorf("failed to fetch header by hash %s: %w", f.criteria.BlockHash, err)
}
blockRes, err := f.backend.TendermintBlockResultByNumber(&resBlock.Block.Height)
if err != nil {
f.logger.Debug("failed to fetch block result from Tendermint", "height", resBlock.Block.Height, "error", err.Error())
return nil, nil
}
bloom, err := f.backend.BlockBloom(blockRes)
if err != nil {
return nil, err
}
return f.blockLogs(blockRes, bloom)
}
// Figure out the limits of the filter range
header, err := f.backend.HeaderByNumber(types.EthLatestBlockNumber)
if err != nil {
return nil, fmt.Errorf("failed to fetch header by number (latest): %w", err)
}
if header == nil || header.Number == nil {
f.logger.Debug("header not found or has no number")
return nil, nil
}
head := header.Number.Int64()
if f.criteria.FromBlock.Int64() < 0 {
f.criteria.FromBlock = big.NewInt(head)
} else if f.criteria.FromBlock.Int64() == 0 {
f.criteria.FromBlock = big.NewInt(1)
}
if f.criteria.ToBlock.Int64() < 0 {
f.criteria.ToBlock = big.NewInt(head)
} else if f.criteria.ToBlock.Int64() == 0 {
f.criteria.ToBlock = big.NewInt(1)
}
if f.criteria.ToBlock.Int64()-f.criteria.FromBlock.Int64() > blockLimit {
return nil, fmt.Errorf("maximum [from, to] blocks distance: %d", blockLimit)
}
// check bounds
if f.criteria.FromBlock.Int64() > head {
return []*ethtypes.Log{}, nil
} else if f.criteria.ToBlock.Int64() > head+maxToOverhang {
f.criteria.ToBlock = big.NewInt(head + maxToOverhang)
}
from := f.criteria.FromBlock.Int64()
to := f.criteria.ToBlock.Int64()
for height := from; height <= to; height++ {
blockRes, err := f.backend.TendermintBlockResultByNumber(&height)
if err != nil {
f.logger.Debug("failed to fetch block result from Tendermint", "height", height, "error", err.Error())
return nil, nil
}
bloom, err := f.backend.BlockBloom(blockRes)
if err != nil {
return nil, err
}
filtered, err := f.blockLogs(blockRes, bloom)
if err != nil {
return nil, errors.Wrapf(err, "failed to fetch block by number %d", height)
}
// check logs limit
if len(logs)+len(filtered) > logLimit {
return nil, fmt.Errorf("query returned more than %d results", logLimit)
}
logs = append(logs, filtered...)
}
return logs, nil
}
// blockLogs returns the logs matching the filter criteria within a single block.
func (f *Filter) blockLogs(blockRes *tmrpctypes.ResultBlockResults, bloom ethtypes.Bloom) ([]*ethtypes.Log, error) {
if !bloomFilter(bloom, f.criteria.Addresses, f.criteria.Topics) {
return []*ethtypes.Log{}, nil
}
logsList, err := backend.GetLogsFromBlockResults(blockRes)
if err != nil {
return []*ethtypes.Log{}, errors.Wrapf(err, "failed to fetch logs block number %d", blockRes.Height)
}
unfiltered := make([]*ethtypes.Log, 0)
for _, logs := range logsList {
unfiltered = append(unfiltered, logs...)
}
logs := FilterLogs(unfiltered, nil, nil, f.criteria.Addresses, f.criteria.Topics)
if len(logs) == 0 {
return []*ethtypes.Log{}, nil
}
return logs, nil
}
func createBloomFilters(filters [][][]byte, logger log.Logger) [][]BloomIV {
bloomFilters := make([][]BloomIV, 0)
for _, filter := range filters {
// Gather the bit indexes of the filter rule, special casing the nil filter
if len(filter) == 0 {
continue
}
bloomIVs := make([]BloomIV, len(filter))
// Transform the filter rules (the addresses and topics) to the bloom index and value arrays
// So it can be used to compare with the bloom of the block header. If the rule has any nil
// clauses. The rule will be ignored.
for i, clause := range filter {
if clause == nil {
bloomIVs = nil
break
}
iv, err := calcBloomIVs(clause)
if err != nil {
bloomIVs = nil
logger.Error("calcBloomIVs error: %v", err)
break
}
bloomIVs[i] = iv
}
// Accumulate the filter rules if no nil rule was within
if bloomIVs != nil {
bloomFilters = append(bloomFilters, bloomIVs)
}
}
return bloomFilters
}
// calcBloomIVs returns BloomIV for the given data,
// revised from https://github.com/ethereum/go-ethereum/blob/401354976bb44f0ad4455ca1e0b5c0dc31d9a5f5/core/types/bloom9.go#L139
func calcBloomIVs(data []byte) (BloomIV, error) {
hashbuf := make([]byte, 6)
biv := BloomIV{}
sha := crypto.NewKeccakState()
sha.Reset()
if _, err := sha.Write(data); err != nil {
return BloomIV{}, err
}
if _, err := sha.Read(hashbuf); err != nil {
return BloomIV{}, err
}
// The actual bits to flip
biv.V[0] = byte(1 << (hashbuf[1] & 0x7))
biv.V[1] = byte(1 << (hashbuf[3] & 0x7))
biv.V[2] = byte(1 << (hashbuf[5] & 0x7))
// The indices for the bytes to OR in
biv.I[0] = ethtypes.BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf)&0x7ff)>>3) - 1
biv.I[1] = ethtypes.BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[2:])&0x7ff)>>3) - 1
biv.I[2] = ethtypes.BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[4:])&0x7ff)>>3) - 1
return biv, nil
}