Add mailservers to transport object

This commit is contained in:
Dmitry 2019-06-19 08:49:39 +03:00 committed by Dmitry Shulyak
parent 59b95d5420
commit f881f1d81f
2 changed files with 29 additions and 6 deletions

View File

@ -10,8 +10,8 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/ethereum/go-ethereum/p2p"
gethnode "github.com/ethereum/go-ethereum/node" gethnode "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/status-im/status-go/services/shhext" "github.com/status-im/status-go/services/shhext"
whisper "github.com/status-im/whisper/whisperv6" whisper "github.com/status-im/whisper/whisperv6"
@ -19,6 +19,11 @@ import (
"github.com/status-im/status-console-client/protocol/subscription" "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 { type WhisperServiceKeysManager struct {
shh *whisper.Whisper shh *whisper.Whisper
@ -64,10 +69,10 @@ func (m *WhisperServiceKeysManager) GetRawSymKey(id string) ([]byte, error) {
type WhisperServiceTransport struct { type WhisperServiceTransport struct {
node StatusNode // TODO: replace with an interface node StatusNode // TODO: replace with an interface
shh *whisper.Whisper shh *whisper.Whisper
shhextAPI *shhext.PublicAPI shhextAPI *shhext.PublicAPI
keysManager *WhisperServiceKeysManager keysManager *WhisperServiceKeysManager
mailservers []string mailservers []string
selectedMailServerEnode string selectedMailServerEnode string
} }
@ -87,9 +92,10 @@ func NewWhisperServiceTransport(
privateKey *ecdsa.PrivateKey, privateKey *ecdsa.PrivateKey,
) *WhisperServiceTransport { ) *WhisperServiceTransport {
return &WhisperServiceTransport{ return &WhisperServiceTransport{
node: node, node: node,
shh: shh, shh: shh,
shhextAPI: shhext.NewPublicAPI(shhextService), mailservers: mailservers,
shhextAPI: shhext.NewPublicAPI(shhextService),
keysManager: &WhisperServiceKeysManager{ keysManager: &WhisperServiceKeysManager{
shh: shh, shh: shh,
privateKey: privateKey, privateKey: privateKey,
@ -184,6 +190,9 @@ func (a *WhisperServiceTransport) selectAndAddMailServer() (string, error) {
if a.selectedMailServerEnode != "" { if a.selectedMailServerEnode != "" {
return a.selectedMailServerEnode, nil return a.selectedMailServerEnode, nil
} }
if len(a.mailservers) == 0 {
return "", ErrNoMailservers
}
enode := randomItem(a.mailservers) enode := randomItem(a.mailservers)
errCh := waitForPeerAsync( errCh := waitForPeerAsync(

View File

@ -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())
}