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" "strings"
"time" "time"
"github.com/google/uuid"
"github.com/okzk/sdnotify" "github.com/okzk/sdnotify"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -20,12 +21,16 @@ import (
gethmetrics "github.com/ethereum/go-ethereum/metrics" gethmetrics "github.com/ethereum/go-ethereum/metrics"
"github.com/status-im/status-go/api" "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/logutils"
"github.com/status-im/status-go/metrics" "github.com/status-im/status-go/metrics"
nodemetrics "github.com/status-im/status-go/metrics/node" nodemetrics "github.com/status-im/status-go/metrics/node"
"github.com/status-im/status-go/node" "github.com/status-im/status-go/node"
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling" "github.com/status-im/status-go/profiling"
"github.com/status-im/status-go/protocol"
) )
const ( const (
@ -150,6 +155,52 @@ func main() {
return 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() err = sdnotify.Ready()
if err == sdnotify.ErrSdNotifyNoSocket { if err == sdnotify.ErrSdNotifyNoSocket {
logger.Debug("sd_notify socket not available") 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/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/static" "github.com/status-im/status-go/static"
wakucommon "github.com/status-im/status-go/waku/common" wakucommon "github.com/status-im/status-go/waku/common"
"github.com/status-im/status-go/whisper/v6" "github.com/status-im/status-go/whisper/v6"
@ -447,6 +448,9 @@ type NodeConfig struct {
// MailServerRegistryAddress is the MailServerRegistry contract address // MailServerRegistryAddress is the MailServerRegistry contract address
MailServerRegistryAddress string MailServerRegistryAddress string
// PushNotificationServerConfig is the config for the push notification server
PushNotificationServerConfig pushnotificationserver.Config `json:"PushNotificationServerConfig"`
} }
// WalletConfig extra configuration for wallet.Service. // WalletConfig extra configuration for wallet.Service.

View File

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

View File

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

View File

@ -4,10 +4,10 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"errors"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pkg/errors"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
@ -21,6 +21,7 @@ const encryptedPayloadKeyLength = 16
const defaultGorushURL = "https://gorush.status.im" const defaultGorushURL = "https://gorush.status.im"
type Config struct { type Config struct {
Enabled bool
// Identity is our identity key // Identity is our identity key
Identity *ecdsa.PrivateKey Identity *ecdsa.PrivateKey
// GorushUrl is the url for the gorush service // 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 { 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") s.config.Logger.Info("starting push notification server")
if s.config.Identity == nil { if s.config.Identity == nil {
s.config.Logger.Info("Identity nil") s.config.Logger.Info("Identity nil")

View File

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