From d70a67abd1644101a7a8f27c85c49cab0e1046dd Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 20 Feb 2017 17:24:29 +0100 Subject: [PATCH] whisper: error handling changed --- cmd/wnode/main.go | 5 ++++- whisper/whisperv5/api.go | 3 +-- whisper/whisperv5/filter.go | 20 +++++++++----------- whisper/whisperv5/filter_test.go | 12 ++++++++++-- whisper/whisperv5/peer_test.go | 5 ++++- whisper/whisperv5/whisper.go | 2 +- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/cmd/wnode/main.go b/cmd/wnode/main.go index 4fd4df8f20..d002497fbf 100644 --- a/cmd/wnode/main.go +++ b/cmd/wnode/main.go @@ -309,7 +309,10 @@ func configureNode() { Topics: []whisper.TopicType{topic}, 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) } diff --git a/whisper/whisperv5/api.go b/whisper/whisperv5/api.go index d31cb64425..606d24c46f 100644 --- a/whisper/whisperv5/api.go +++ b/whisper/whisperv5/api.go @@ -215,8 +215,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (string, error) { } } - id := api.whisper.Watch(&filter) - return id, nil + return api.whisper.Watch(&filter) } // UninstallFilter disables and removes an existing filter. diff --git a/whisper/whisperv5/filter.go b/whisper/whisperv5/filter.go index 8dd1f04a77..832ebe3f6b 100644 --- a/whisper/whisperv5/filter.go +++ b/whisper/whisperv5/filter.go @@ -53,8 +53,7 @@ func NewFilters(w *Whisper) *Filters { } } -func (fs *Filters) generateRandomID() (id string) { - var err error +func (fs *Filters) generateRandomID() (id string, err error) { buf := make([]byte, 20) for i := 0; i < 3; i++ { _, err = crand.Read(buf) @@ -70,16 +69,13 @@ func (fs *Filters) generateRandomID() (id string) { err = fmt.Errorf("error in generateRandomID: generated same ID twice") continue } - return id + return id, err } - // at this point all the attempts to generate the random ID failed. - // this poses a serious danger, since whisper very much relies - // on random numbers. the program must be terminated. - panic(err) + return "", err } -func (fs *Filters) Install(watcher *Filter) string { +func (fs *Filters) Install(watcher *Filter) (string, error) { if watcher.Messages == nil { watcher.Messages = make(map[common.Hash]*ReceivedMessage) } @@ -87,9 +83,11 @@ func (fs *Filters) Install(watcher *Filter) string { fs.mutex.Lock() defer fs.mutex.Unlock() - id := fs.generateRandomID() - fs.watchers[id] = watcher - return id + id, err := fs.generateRandomID() + if err == nil { + fs.watchers[id] = watcher + } + return id, err } func (fs *Filters) Uninstall(id string) { diff --git a/whisper/whisperv5/filter_test.go b/whisper/whisperv5/filter_test.go index 259b22a0d7..d69fb40dbf 100644 --- a/whisper/whisperv5/filter_test.go +++ b/whisper/whisperv5/filter_test.go @@ -100,9 +100,13 @@ func TestInstallFilters(t *testing.T) { filters := NewFilters(w) tst := generateTestCases(t, SizeTestFilters) + var err error var j string 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 if len(j) != 40 { 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 e *Envelope var x, firstID string + var err error w := New() filters := NewFilters(w) tst := generateTestCases(t, NumFilters) for i = 0; i < NumFilters; i++ { 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 if len(firstID) == 0 { firstID = x diff --git a/whisper/whisperv5/peer_test.go b/whisper/whisperv5/peer_test.go index 223dd87620..cce2c92ba6 100644 --- a/whisper/whisperv5/peer_test.go +++ b/whisper/whisperv5/peer_test.go @@ -122,7 +122,10 @@ func initialize(t *testing.T) { topics := make([]TopicType, 0) topics = append(topics, sharedTopic) 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]) if err != nil { t.Fatalf("failed convert the key: %s.", keys[i]) diff --git a/whisper/whisperv5/whisper.go b/whisper/whisperv5/whisper.go index 87552b15ef..2a6ff5f409 100644 --- a/whisper/whisperv5/whisper.go +++ b/whisper/whisperv5/whisper.go @@ -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 // from the whisper network. -func (w *Whisper) Watch(f *Filter) string { +func (w *Whisper) Watch(f *Filter) (string, error) { return w.filters.Install(f) }