mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
whisper, event/filter: mutexes for whisper keys and filter watchers
This commit is contained in:
parent
0f6f83a709
commit
d4f816a47c
2 changed files with 20 additions and 5 deletions
|
|
@ -17,7 +17,10 @@
|
||||||
// Package filter implements event filters.
|
// Package filter implements event filters.
|
||||||
package filter
|
package filter
|
||||||
|
|
||||||
import "reflect"
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Filter interface {
|
type Filter interface {
|
||||||
Compare(Filter) bool
|
Compare(Filter) bool
|
||||||
|
|
@ -30,9 +33,10 @@ type FilterEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
id int
|
id int
|
||||||
watchers map[int]Filter
|
watchers map[int]Filter
|
||||||
ch chan FilterEvent
|
watcherMu sync.RWMutex
|
||||||
|
ch chan FilterEvent
|
||||||
|
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
}
|
}
|
||||||
|
|
@ -58,14 +62,18 @@ func (self *Filters) Notify(filter Filter, data interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Filters) Install(watcher Filter) int {
|
func (self *Filters) Install(watcher Filter) int {
|
||||||
|
self.watcherMu.Lock()
|
||||||
self.watchers[self.id] = watcher
|
self.watchers[self.id] = watcher
|
||||||
self.id++
|
self.id++
|
||||||
|
self.watcherMu.Unlock()
|
||||||
|
|
||||||
return self.id - 1
|
return self.id - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Filters) Uninstall(id int) {
|
func (self *Filters) Uninstall(id int) {
|
||||||
|
self.watcherMu.Lock()
|
||||||
delete(self.watchers, id)
|
delete(self.watchers, id)
|
||||||
|
self.watcherMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Filters) loop() {
|
func (self *Filters) loop() {
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,8 @@ type Whisper struct {
|
||||||
protocol p2p.Protocol
|
protocol p2p.Protocol
|
||||||
filters *filter.Filters
|
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
|
messages map[common.Hash]*Envelope // Pool of messages currently tracked by this node
|
||||||
expirations map[uint32]*set.SetNonTS // Message expiration pool (TODO: something lighter)
|
expirations map[uint32]*set.SetNonTS // Message expiration pool (TODO: something lighter)
|
||||||
|
|
@ -129,7 +130,9 @@ func (self *Whisper) NewIdentity() *ecdsa.PrivateKey {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
self.keyMu.Lock()
|
||||||
self.keys[string(crypto.FromECDSAPub(&key.PublicKey))] = key
|
self.keys[string(crypto.FromECDSAPub(&key.PublicKey))] = key
|
||||||
|
self.keyMu.Unlock()
|
||||||
|
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
@ -300,15 +303,19 @@ func (self *Whisper) open(envelope *Envelope) *Message {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Iterate over the keys and try to decrypt the message
|
// Iterate over the keys and try to decrypt the message
|
||||||
|
self.keyMu.Lock()
|
||||||
for _, key := range self.keys {
|
for _, key := range self.keys {
|
||||||
message, err := envelope.Open(key)
|
message, err := envelope.Open(key)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
message.To = &key.PublicKey
|
message.To = &key.PublicKey
|
||||||
|
self.keyMu.Unlock()
|
||||||
return message
|
return message
|
||||||
} else if err == ecies.ErrInvalidPublicKey {
|
} else if err == ecies.ErrInvalidPublicKey {
|
||||||
|
self.keyMu.Unlock()
|
||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.keyMu.Unlock()
|
||||||
// Failed to decrypt, don't return anything
|
// Failed to decrypt, don't return anything
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue