whisper: error handling changed

This commit is contained in:
Vlad 2017-02-20 17:24:29 +01:00
parent 2ac62c3857
commit d70a67abd1
6 changed files with 29 additions and 18 deletions

View file

@ -309,7 +309,10 @@ func configureNode() {
Topics: []whisper.TopicType{topic}, Topics: []whisper.TopicType{topic},
AcceptP2P: p2pAccept, AcceptP2P: p2pAccept,
} }
filterID = shh.Watch(&filter) filterID, err = shh.Watch(&filter)
if err != nil {
utils.Fatalf("Failed to install filter: %s", err)
}
fmt.Printf("Filter is configured for the topic: %x \n", topic) fmt.Printf("Filter is configured for the topic: %x \n", topic)
} }

View file

@ -215,8 +215,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (string, error) {
} }
} }
id := api.whisper.Watch(&filter) return api.whisper.Watch(&filter)
return id, nil
} }
// UninstallFilter disables and removes an existing filter. // UninstallFilter disables and removes an existing filter.

View file

@ -53,8 +53,7 @@ func NewFilters(w *Whisper) *Filters {
} }
} }
func (fs *Filters) generateRandomID() (id string) { func (fs *Filters) generateRandomID() (id string, err error) {
var err error
buf := make([]byte, 20) buf := make([]byte, 20)
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
_, err = crand.Read(buf) _, err = crand.Read(buf)
@ -70,16 +69,13 @@ func (fs *Filters) generateRandomID() (id string) {
err = fmt.Errorf("error in generateRandomID: generated same ID twice") err = fmt.Errorf("error in generateRandomID: generated same ID twice")
continue continue
} }
return id return id, err
} }
// at this point all the attempts to generate the random ID failed. return "", err
// this poses a serious danger, since whisper very much relies
// on random numbers. the program must be terminated.
panic(err)
} }
func (fs *Filters) Install(watcher *Filter) string { func (fs *Filters) Install(watcher *Filter) (string, error) {
if watcher.Messages == nil { if watcher.Messages == nil {
watcher.Messages = make(map[common.Hash]*ReceivedMessage) watcher.Messages = make(map[common.Hash]*ReceivedMessage)
} }
@ -87,9 +83,11 @@ func (fs *Filters) Install(watcher *Filter) string {
fs.mutex.Lock() fs.mutex.Lock()
defer fs.mutex.Unlock() defer fs.mutex.Unlock()
id := fs.generateRandomID() id, err := fs.generateRandomID()
if err == nil {
fs.watchers[id] = watcher fs.watchers[id] = watcher
return id }
return id, err
} }
func (fs *Filters) Uninstall(id string) { func (fs *Filters) Uninstall(id string) {

View file

@ -100,9 +100,13 @@ func TestInstallFilters(t *testing.T) {
filters := NewFilters(w) filters := NewFilters(w)
tst := generateTestCases(t, SizeTestFilters) tst := generateTestCases(t, SizeTestFilters)
var err error
var j string var j string
for i := 0; i < SizeTestFilters; i++ { for i := 0; i < SizeTestFilters; i++ {
j = filters.Install(tst[i].f) j, err = filters.Install(tst[i].f)
if err != nil {
t.Fatalf("seed %d: failed to install filter: %s", seed, err)
}
tst[i].id = j tst[i].id = j
if len(j) != 40 { if len(j) != 40 {
t.Fatalf("seed %d: wrong filter id size [%d]", seed, len(j)) t.Fatalf("seed %d: wrong filter id size [%d]", seed, len(j))
@ -519,13 +523,17 @@ func TestWatchers(t *testing.T) {
var j uint32 var j uint32
var e *Envelope var e *Envelope
var x, firstID string var x, firstID string
var err error
w := New() w := New()
filters := NewFilters(w) filters := NewFilters(w)
tst := generateTestCases(t, NumFilters) tst := generateTestCases(t, NumFilters)
for i = 0; i < NumFilters; i++ { for i = 0; i < NumFilters; i++ {
tst[i].f.Src = nil tst[i].f.Src = nil
x = filters.Install(tst[i].f) x, err = filters.Install(tst[i].f)
if err != nil {
t.Fatalf("failed to install filter with seed %d: %s.", seed, err)
}
tst[i].id = x tst[i].id = x
if len(firstID) == 0 { if len(firstID) == 0 {
firstID = x firstID = x

View file

@ -122,7 +122,10 @@ func initialize(t *testing.T) {
topics := make([]TopicType, 0) topics := make([]TopicType, 0)
topics = append(topics, sharedTopic) topics = append(topics, sharedTopic)
f := Filter{KeySym: sharedKey, Topics: topics} f := Filter{KeySym: sharedKey, Topics: topics}
node.filerId = node.shh.Watch(&f) node.filerId, err = node.shh.Watch(&f)
if err != nil {
t.Fatalf("failed to install the filter: %s.", err)
}
node.id, err = crypto.HexToECDSA(keys[i]) node.id, err = crypto.HexToECDSA(keys[i])
if err != nil { if err != nil {
t.Fatalf("failed convert the key: %s.", keys[i]) t.Fatalf("failed convert the key: %s.", keys[i])

View file

@ -272,7 +272,7 @@ func (w *Whisper) GetSymKey(name string) []byte {
// Watch installs a new message handler to run in case a matching packet arrives // Watch installs a new message handler to run in case a matching packet arrives
// from the whisper network. // from the whisper network.
func (w *Whisper) Watch(f *Filter) string { func (w *Whisper) Watch(f *Filter) (string, error) {
return w.filters.Install(f) return w.filters.Install(f)
} }