From 258ca757592699778282e554b38b0ea2d66f74ef Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 20 Mar 2017 17:34:58 +0100 Subject: [PATCH] whisper: allowed filtering for variable topic size --- whisper/whisperv5/api_test.go | 1 + whisper/whisperv5/filter.go | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/whisper/whisperv5/api_test.go b/whisper/whisperv5/api_test.go index e6ac1468bb..cc93261636 100644 --- a/whisper/whisperv5/api_test.go +++ b/whisper/whisperv5/api_test.go @@ -17,6 +17,7 @@ package whisperv5 import ( + "bytes" "encoding/json" "testing" "time" diff --git a/whisper/whisperv5/filter.go b/whisper/whisperv5/filter.go index 675340d03c..8230a26ff0 100644 --- a/whisper/whisperv5/filter.go +++ b/whisper/whisperv5/filter.go @@ -89,12 +89,12 @@ func (fs *Filters) Get(id string) *Filter { } func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) { - var j int var msg *ReceivedMessage fs.mutex.RLock() defer fs.mutex.RUnlock() + j := -1 for _, watcher := range fs.watchers { j++ if p2pMessage && !watcher.AllowP2P { @@ -118,6 +118,7 @@ func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) { } if match && msg != nil { + log.Trace(fmt.Sprintf("message decrypted [%x]", env.Hash())) watcher.Trigger(msg) } } @@ -202,10 +203,21 @@ func (f *Filter) MatchTopic(topic TopicType) bool { } for _, bt := range f.Topics { - for j, b := range bt { - if topic[j] != b { - return false - } + if MatchSingleTopic(topic, bt) { + return true + } + } + return false +} + +func MatchSingleTopic(topic TopicType, bt []byte) bool { + if len(bt) > 4 { + bt = bt[0:4] + } + + for j, b := range bt { + if topic[j] != b { + return false } } return true