This commit is contained in:
Johannes Hense 2016-09-21 08:05:35 +00:00 committed by GitHub
commit 4480d7c5b8
2 changed files with 20 additions and 5 deletions

View file

@ -17,7 +17,10 @@
// Package filter implements event filters.
package filter
import "reflect"
import (
"reflect"
"sync"
)
type Filter interface {
Compare(Filter) bool
@ -30,9 +33,10 @@ type FilterEvent struct {
}
type Filters struct {
id int
watchers map[int]Filter
ch chan FilterEvent
id int
watchers map[int]Filter
watcherMu sync.RWMutex
ch chan FilterEvent
quit chan struct{}
}
@ -58,14 +62,18 @@ func (self *Filters) Notify(filter Filter, data interface{}) {
}
func (self *Filters) Install(watcher Filter) int {
self.watcherMu.Lock()
self.watchers[self.id] = watcher
self.id++
self.watcherMu.Unlock()
return self.id - 1
}
func (self *Filters) Uninstall(id int) {
self.watcherMu.Lock()
delete(self.watchers, id)
self.watcherMu.Unlock()
}
func (self *Filters) loop() {

View file

@ -64,7 +64,8 @@ type Whisper struct {
protocol p2p.Protocol
filters *filter.Filters
keys map[string]*ecdsa.PrivateKey
keys map[string]*ecdsa.PrivateKey
keyMu sync.RWMutex
messages map[common.Hash]*Envelope // Pool of messages currently tracked by this node
expirations map[uint32]*set.SetNonTS // Message expiration pool (TODO: something lighter)
@ -129,7 +130,9 @@ func (self *Whisper) NewIdentity() *ecdsa.PrivateKey {
if err != nil {
panic(err)
}
self.keyMu.Lock()
self.keys[string(crypto.FromECDSAPub(&key.PublicKey))] = key
self.keyMu.Unlock()
return key
}
@ -300,15 +303,19 @@ func (self *Whisper) open(envelope *Envelope) *Message {
}
}
// Iterate over the keys and try to decrypt the message
self.keyMu.Lock()
for _, key := range self.keys {
message, err := envelope.Open(key)
if err == nil {
message.To = &key.PublicKey
self.keyMu.Unlock()
return message
} else if err == ecies.ErrInvalidPublicKey {
self.keyMu.Unlock()
return message
}
}
self.keyMu.Unlock()
// Failed to decrypt, don't return anything
return nil
}