From f881f1d81f9875c5b4c5cd0717361503b8e40e44 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 19 Jun 2019 08:49:39 +0300 Subject: [PATCH] Add mailservers to transport object --- protocol/transport/whisper_service.go | 21 +++++++++++++++------ protocol/transport/whisper_service_test.go | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 protocol/transport/whisper_service_test.go diff --git a/protocol/transport/whisper_service.go b/protocol/transport/whisper_service.go index 665f4e7..d55e449 100644 --- a/protocol/transport/whisper_service.go +++ b/protocol/transport/whisper_service.go @@ -10,8 +10,8 @@ import ( "github.com/pkg/errors" - "github.com/ethereum/go-ethereum/p2p" gethnode "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/p2p" "github.com/status-im/status-go/services/shhext" whisper "github.com/status-im/whisper/whisperv6" @@ -19,6 +19,11 @@ import ( "github.com/status-im/status-console-client/protocol/subscription" ) +var ( + // ErrNoMailservers returned if there is no configured mailservers that can be used. + ErrNoMailservers = errors.New("no configured mailservers") +) + type WhisperServiceKeysManager struct { shh *whisper.Whisper @@ -64,10 +69,10 @@ func (m *WhisperServiceKeysManager) GetRawSymKey(id string) ([]byte, error) { type WhisperServiceTransport struct { node StatusNode // TODO: replace with an interface shh *whisper.Whisper - shhextAPI *shhext.PublicAPI + shhextAPI *shhext.PublicAPI keysManager *WhisperServiceKeysManager - mailservers []string + mailservers []string selectedMailServerEnode string } @@ -87,9 +92,10 @@ func NewWhisperServiceTransport( privateKey *ecdsa.PrivateKey, ) *WhisperServiceTransport { return &WhisperServiceTransport{ - node: node, - shh: shh, - shhextAPI: shhext.NewPublicAPI(shhextService), + node: node, + shh: shh, + mailservers: mailservers, + shhextAPI: shhext.NewPublicAPI(shhextService), keysManager: &WhisperServiceKeysManager{ shh: shh, privateKey: privateKey, @@ -184,6 +190,9 @@ func (a *WhisperServiceTransport) selectAndAddMailServer() (string, error) { if a.selectedMailServerEnode != "" { return a.selectedMailServerEnode, nil } + if len(a.mailservers) == 0 { + return "", ErrNoMailservers + } enode := randomItem(a.mailservers) errCh := waitForPeerAsync( diff --git a/protocol/transport/whisper_service_test.go b/protocol/transport/whisper_service_test.go new file mode 100644 index 0000000..3c39f57 --- /dev/null +++ b/protocol/transport/whisper_service_test.go @@ -0,0 +1,14 @@ +package transport + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSelectAndAddNoMailservers(t *testing.T) { + svc := &WhisperServiceTransport{} + rst, err := svc.selectAndAddMailServer() + require.Empty(t, rst) + require.EqualError(t, ErrNoMailservers, err.Error()) +}