Allow node to start a push notification server

This commit allows a node to start a push notification server.
If the config is set it will start a messenger with a corresponding pn
server.
This commit is contained in:
Andrea Maria Piana 2020-08-20 09:26:00 +02:00
parent ea47f07b96
commit d09eaeacb8
6 changed files with 70 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/google/uuid"
"github.com/okzk/sdnotify"
"golang.org/x/crypto/ssh/terminal"
@ -20,12 +21,16 @@ import (
gethmetrics "github.com/ethereum/go-ethereum/metrics"
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/appdatabase"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/metrics"
nodemetrics "github.com/status-im/status-go/metrics/node"
"github.com/status-im/status-go/node"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling"
"github.com/status-im/status-go/protocol"
)
const (
@ -150,6 +155,52 @@ func main() {
return
}
if config.PushNotificationServerConfig.Enabled {
if config.NodeKey == "" {
logger.Error("node key needs to be set if running a push notification server")
return
}
identity, err := crypto.HexToECDSA(config.NodeKey)
if err != nil {
logger.Error("node key is invalid", "error", err)
return
}
// Generate installationID from public key, so it's always the same
installationID, err := uuid.FromBytes(crypto.CompressPubkey(&identity.PublicKey)[:16])
if err != nil {
logger.Error("cannot create installation id", "error", err)
return
}
db, err := appdatabase.InitializeDB(config.DataDir+"/"+installationID.String()+".db", "")
if err != nil {
logger.Error("failed to initialize app db", "error", err)
return
}
options := []protocol.Option{
protocol.WithPushNotifications(),
protocol.WithPushNotificationServerConfig(&config.PushNotificationServerConfig),
protocol.WithDatabase(db),
}
messenger, err := protocol.NewMessenger(identity, gethbridge.NewNodeBridge(backend.StatusNode().GethNode()), installationID.String(), options...)
if err != nil {
logger.Error("failed to create messenger", "error", err)
return
}
// This will start the push notification server as well as
// the config is set to Enabled
err = messenger.Start()
if err != nil {
logger.Error("failed to start messenger", "error", err)
return
}
}
err = sdnotify.Ready()
if err == sdnotify.ErrSdNotifyNoSocket {
logger.Debug("sd_notify socket not available")

View File

@ -19,6 +19,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/static"
wakucommon "github.com/status-im/status-go/waku/common"
"github.com/status-im/status-go/whisper/v6"
@ -447,6 +448,9 @@ type NodeConfig struct {
// MailServerRegistryAddress is the MailServerRegistry contract address
MailServerRegistryAddress string
// PushNotificationServerConfig is the config for the push notification server
PushNotificationServerConfig pushnotificationserver.Config `json:"PushNotificationServerConfig"`
}
// WalletConfig extra configuration for wallet.Service.

View File

@ -206,7 +206,7 @@ func NewMessenger(
// Initialize push notification server
var pushNotificationServer *pushnotificationserver.Server
if c.pushNotificationServerConfig != nil {
if c.pushNotificationServerConfig != nil && c.pushNotificationServerConfig.Enabled {
c.pushNotificationServerConfig.Identity = identity
pushNotificationServerPersistence := pushnotificationserver.NewSQLitePersistence(database)
pushNotificationServer = pushnotificationserver.New(c.pushNotificationServerConfig, pushNotificationServerPersistence, processor)
@ -3306,6 +3306,7 @@ func (m *Messenger) StartPushNotificationsServer() error {
if m.pushNotificationServer == nil {
pushNotificationServerPersistence := pushnotificationserver.NewSQLitePersistence(m.database)
config := &pushnotificationserver.Config{
Enabled: true,
Logger: m.logger,
Identity: m.identity,
}

View File

@ -109,6 +109,7 @@ func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Wak
s.Require().NoError(err)
serverConfig := &pushnotificationserver.Config{
Enabled: true,
Logger: s.logger,
Identity: privateKey,
}

View File

@ -4,10 +4,10 @@ import (
"context"
"crypto/ecdsa"
"encoding/hex"
"errors"
"github.com/golang/protobuf/proto"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/status-im/status-go/eth-node/crypto"
@ -21,6 +21,7 @@ const encryptedPayloadKeyLength = 16
const defaultGorushURL = "https://gorush.status.im"
type Config struct {
Enabled bool
// Identity is our identity key
Identity *ecdsa.PrivateKey
// GorushUrl is the url for the gorush service
@ -44,6 +45,14 @@ func New(config *Config, persistence Persistence, messageProcessor *common.Messa
}
func (s *Server) Start() error {
if s.config.Logger == nil {
logger, err := zap.NewDevelopment()
if err != nil {
return errors.Wrap(err, "failed to create a logger")
}
s.config.Logger = logger
}
s.config.Logger.Info("starting push notification server")
if s.config.Identity == nil {
s.config.Logger.Info("Identity nil")

View File

@ -476,6 +476,7 @@ func buildMessengerOptions(
if settings.PushNotificationsServerEnabled {
config := &pushnotificationserver.Config{
Enabled: true,
Logger: logger,
}
options = append(options, protocol.WithPushNotificationServerConfig(config))