-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
129 lines (116 loc) · 4.08 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
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Contains the message filter for fine grained subscriptions.
package whisperv2
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/event/filter"
)
// Filter is used to subscribe to specific types of whisper messages.
type Filter struct {
To *ecdsa.PublicKey // Recipient of the message
From *ecdsa.PublicKey // Sender of the message
Topics [][]Topic // Topics to filter messages with
Fn func(msg *Message) // Handler in case of a match
}
// NewFilterTopics creates a 2D topic array used by whisper.Filter from binary
// data elements.
func NewFilterTopics(data ...[][]byte) [][]Topic {
filter := make([][]Topic, len(data))
for i, condition := range data {
// Handle the special case of condition == [[]byte{}]
if len(condition) == 1 && len(condition[0]) == 0 {
filter[i] = []Topic{}
continue
}
// Otherwise flatten normally
filter[i] = NewTopics(condition...)
}
return filter
}
// NewFilterTopicsFlat creates a 2D topic array used by whisper.Filter from flat
// binary data elements.
func NewFilterTopicsFlat(data ...[]byte) [][]Topic {
filter := make([][]Topic, len(data))
for i, element := range data {
// Only add non-wildcard topics
filter[i] = make([]Topic, 0, 1)
if len(element) > 0 {
filter[i] = append(filter[i], NewTopic(element))
}
}
return filter
}
// NewFilterTopicsFromStrings creates a 2D topic array used by whisper.Filter
// from textual data elements.
func NewFilterTopicsFromStrings(data ...[]string) [][]Topic {
filter := make([][]Topic, len(data))
for i, condition := range data {
// Handle the special case of condition == [""]
if len(condition) == 1 && condition[0] == "" {
filter[i] = []Topic{}
continue
}
// Otherwise flatten normally
filter[i] = NewTopicsFromStrings(condition...)
}
return filter
}
// NewFilterTopicsFromStringsFlat creates a 2D topic array used by whisper.Filter from flat
// binary data elements.
func NewFilterTopicsFromStringsFlat(data ...string) [][]Topic {
filter := make([][]Topic, len(data))
for i, element := range data {
// Only add non-wildcard topics
filter[i] = make([]Topic, 0, 1)
if element != "" {
filter[i] = append(filter[i], NewTopicFromString(element))
}
}
return filter
}
// filterer is the internal, fully initialized filter ready to match inbound
// messages to a variety of criteria.
type filterer struct {
to string // Recipient of the message
from string // Sender of the message
matcher *topicMatcher // Topics to filter messages with
fn func(data interface{}) // Handler in case of a match
}
// Compare checks if the specified filter matches the current one.
func (self filterer) Compare(f filter.Filter) bool {
filter := f.(filterer)
// Check the message sender and recipient
if len(self.to) > 0 && self.to != filter.to {
return false
}
if len(self.from) > 0 && self.from != filter.from {
return false
}
// Check the topic filtering
topics := make([]Topic, len(filter.matcher.conditions))
for i, group := range filter.matcher.conditions {
// Message should contain a single topic entry, extract
for topics[i] = range group {
break
}
}
return self.matcher.Matches(topics)
}
// Trigger is called when a filter successfully matches an inbound message.
func (self filterer) Trigger(data interface{}) {
self.fn(data)
}