aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/filter.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-21 23:31:08 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-28 15:49:04 +0800
commitae4bfc3cfb3f1debad9dd0211950ce09038ffa90 (patch)
treed09f6c0291eab1b02cc1145b816542024e7c4bfa /whisper/filter.go
parent15586368e52f49a0f7ea28f890af49d196760846 (diff)
downloadgo-tangerine-ae4bfc3cfb3f1debad9dd0211950ce09038ffa90.tar.gz
go-tangerine-ae4bfc3cfb3f1debad9dd0211950ce09038ffa90.tar.zst
go-tangerine-ae4bfc3cfb3f1debad9dd0211950ce09038ffa90.zip
rpc, ui/qt/qwhisper, whisper, xeth: introduce complex topic filters
Diffstat (limited to 'whisper/filter.go')
-rw-r--r--whisper/filter.go53
1 files changed, 48 insertions, 5 deletions
diff --git a/whisper/filter.go b/whisper/filter.go
index 8fcc45afd..8a398ab76 100644
--- a/whisper/filter.go
+++ b/whisper/filter.go
@@ -2,12 +2,55 @@
package whisper
-import "crypto/ecdsa"
+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 watch messages on
- Fn func(*Message) // Handler in case of a match
+ 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
+}
+
+// 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
+ }
+ }
+ if !self.matcher.Matches(topics) {
+ return false
+ }
+ return true
+}
+
+// Trigger is called when a filter successfully matches an inbound message.
+func (self filterer) Trigger(data interface{}) {
+ self.fn(data)
}