From 2e9d1502d488c9df383db59288ea585db438074a Mon Sep 17 00:00:00 2001 From: Lin Corey Date: Thu, 1 Nov 2018 10:45:37 +0800 Subject: [PATCH 1/2] whisperv6: remove a few duplicated code --- whisper/whisperv6/api_test.go | 16 +--------------- whisper/whisperv6/whisper.go | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/whisper/whisperv6/api_test.go b/whisper/whisperv6/api_test.go index cdbc7fab5e05..7d0c50cc17f8 100644 --- a/whisper/whisperv6/api_test.go +++ b/whisper/whisperv6/api_test.go @@ -18,26 +18,12 @@ package whisperv6 import ( "bytes" - "crypto/ecdsa" "testing" "time" - - mapset "github.com/deckarep/golang-set" - "github.com/ethereum/go-ethereum/common" ) func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) { - w := &Whisper{ - privateKeys: make(map[string]*ecdsa.PrivateKey), - symKeys: make(map[string][]byte), - envelopes: make(map[common.Hash]*Envelope), - expirations: make(map[uint32]mapset.Set), - peers: make(map[*Peer]struct{}), - messageQueue: make(chan *Envelope, messageQueueLimit), - p2pMsgQueue: make(chan *Envelope, messageQueueLimit), - quit: make(chan struct{}), - syncAllowance: DefaultSyncAllowance, - } + w := newDefaultWhisper() w.filters = NewFilters(w) keyID, err := w.GenerateSymKey() diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index eb713f84ee5c..68139d6a663f 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -96,17 +96,7 @@ func New(cfg *Config) *Whisper { cfg = &DefaultConfig } - whisper := &Whisper{ - privateKeys: make(map[string]*ecdsa.PrivateKey), - symKeys: make(map[string][]byte), - envelopes: make(map[common.Hash]*Envelope), - expirations: make(map[uint32]mapset.Set), - peers: make(map[*Peer]struct{}), - messageQueue: make(chan *Envelope, messageQueueLimit), - p2pMsgQueue: make(chan *Envelope, messageQueueLimit), - quit: make(chan struct{}), - syncAllowance: DefaultSyncAllowance, - } + whisper := newDefaultWhisper() whisper.filters = NewFilters(whisper) @@ -133,6 +123,20 @@ func New(cfg *Config) *Whisper { return whisper } +func newDefaultWhisper() *Whisper { + return &Whisper{ + privateKeys: make(map[string]*ecdsa.PrivateKey), + symKeys: make(map[string][]byte), + envelopes: make(map[common.Hash]*Envelope), + expirations: make(map[uint32]mapset.Set), + peers: make(map[*Peer]struct{}), + messageQueue: make(chan *Envelope, messageQueueLimit), + p2pMsgQueue: make(chan *Envelope, messageQueueLimit), + quit: make(chan struct{}), + syncAllowance: DefaultSyncAllowance, + } +} + // MinPow returns the PoW value required by this node. func (whisper *Whisper) MinPow() float64 { val, exist := whisper.settings.Load(minPowIdx) From 373b95a83383754f5041a79fa4be3736c7071ca1 Mon Sep 17 00:00:00 2001 From: CoreyLin <514971757@qq.com> Date: Mon, 3 Dec 2018 20:49:25 +0800 Subject: [PATCH 2/2] whisperv6: remove duplicated code from api_test.go --- whisper/whisperv6/api_test.go | 3 +-- whisper/whisperv6/whisper.go | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/whisper/whisperv6/api_test.go b/whisper/whisperv6/api_test.go index 7d0c50cc17f8..6d7157f57fd2 100644 --- a/whisper/whisperv6/api_test.go +++ b/whisper/whisperv6/api_test.go @@ -23,8 +23,7 @@ import ( ) func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) { - w := newDefaultWhisper() - w.filters = NewFilters(w) + w := New(nil) keyID, err := w.GenerateSymKey() if err != nil { diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index 68139d6a663f..eb713f84ee5c 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -96,7 +96,17 @@ func New(cfg *Config) *Whisper { cfg = &DefaultConfig } - whisper := newDefaultWhisper() + whisper := &Whisper{ + privateKeys: make(map[string]*ecdsa.PrivateKey), + symKeys: make(map[string][]byte), + envelopes: make(map[common.Hash]*Envelope), + expirations: make(map[uint32]mapset.Set), + peers: make(map[*Peer]struct{}), + messageQueue: make(chan *Envelope, messageQueueLimit), + p2pMsgQueue: make(chan *Envelope, messageQueueLimit), + quit: make(chan struct{}), + syncAllowance: DefaultSyncAllowance, + } whisper.filters = NewFilters(whisper) @@ -123,20 +133,6 @@ func New(cfg *Config) *Whisper { return whisper } -func newDefaultWhisper() *Whisper { - return &Whisper{ - privateKeys: make(map[string]*ecdsa.PrivateKey), - symKeys: make(map[string][]byte), - envelopes: make(map[common.Hash]*Envelope), - expirations: make(map[uint32]mapset.Set), - peers: make(map[*Peer]struct{}), - messageQueue: make(chan *Envelope, messageQueueLimit), - p2pMsgQueue: make(chan *Envelope, messageQueueLimit), - quit: make(chan struct{}), - syncAllowance: DefaultSyncAllowance, - } -} - // MinPow returns the PoW value required by this node. func (whisper *Whisper) MinPow() float64 { val, exist := whisper.settings.Load(minPowIdx)