whisper: Filter refactoring

This commit is contained in:
Vlad 2016-11-02 11:45:50 +01:00
parent 36956da4d2
commit 2010c20a15
3 changed files with 14 additions and 21 deletions

View file

@ -46,7 +46,9 @@ const (
messagesCode = 1 messagesCode = 1
p2pCode = 2 p2pCode = 2
mailRequestCode = 3 mailRequestCode = 3
NumberOfMessageCodes = 4 bloomExchangeCode = 4
reservedCode = 5
NumberOfMessageCodes = 6
paddingMask = byte(3) paddingMask = byte(3)
signatureFlag = byte(4) signatureFlag = byte(4)

View file

@ -143,18 +143,9 @@ func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
} }
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() { if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
// if Dst match, ignore the topic return isPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst) && f.MatchTopic(msg.Topic)
return isPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst)
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() { } else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
// check if that both the key and the topic match return f.SymKeyHash == msg.SymKeyHash && f.MatchTopic(msg.Topic)
if f.SymKeyHash == msg.SymKeyHash {
for _, t := range f.Topics {
if t == msg.Topic {
return true
}
}
return false
}
} }
return false return false
} }
@ -175,14 +166,15 @@ func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
encryptionMethodMatch = true encryptionMethodMatch = true
} }
if encryptionMethodMatch { return encryptionMethodMatch && f.MatchTopic(envelope.Topic)
}
func (f *Filter) MatchTopic(topic TopicType) bool {
for _, t := range f.Topics { for _, t := range f.Topics {
if t == envelope.Topic { if t == topic {
return true return true
} }
} }
}
return false return false
} }

View file

@ -487,10 +487,9 @@ func TestMatchMessageAsym(x *testing.T) {
return return
} }
// topic mismatch, but still match, because for asymmetric encryption // topic mismatch
// only private key matters (in case the message is already decrypted)
f.Topics[index][0]++ f.Topics[index][0]++
if !f.MatchMessage(msg) { if f.MatchMessage(msg) {
x.Errorf("failed test case 8 with seed %d.", seed) x.Errorf("failed test case 8 with seed %d.", seed)
return return
} }