Skip to content

Commit

Permalink
Local notifications service (#2026)
Browse files Browse the repository at this point in the history
Also adds implementation for eth transactions notifications
  • Loading branch information
Ferossgp authored Oct 28, 2020
1 parent 3e446ee commit d04e54e
Show file tree
Hide file tree
Showing 22 changed files with 967 additions and 72 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.62.15
0.62.16
87 changes: 83 additions & 4 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/status-im/status-go/rpc"
accountssvc "github.com/status-im/status-go/services/accounts"
"github.com/status-im/status-go/services/browsers"
localnotifications "github.com/status-im/status-go/services/local-notifications"
"github.com/status-im/status-go/services/mailservers"
"github.com/status-im/status-go/services/permissions"
"github.com/status-im/status-go/services/personal"
Expand Down Expand Up @@ -96,6 +97,7 @@ func NewGethStatusBackend() *GethStatusBackend {
transactor := transactions.NewTransactor()
personalAPI := personal.NewAPI()
rpcFilters := rpcfilters.New(statusNode)

return &GethStatusBackend{
statusNode: statusNode,
accountManager: accountManager,
Expand Down Expand Up @@ -339,10 +341,12 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
WatchAddresses: watchAddrs,
MainAccount: walletAddr,
}

err = b.StartNode(conf)
if err != nil {
return err
}

err = b.SelectAccount(login)
if err != nil {
return err
Expand All @@ -351,6 +355,7 @@ func (b *GethStatusBackend) startNodeWithAccount(acc multiaccounts.Account, pass
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -514,6 +519,12 @@ func (b *GethStatusBackend) walletService(network uint64, accountsFeed *event.Fe
}
}

func (b *GethStatusBackend) localNotificationsService(network uint64) gethnode.ServiceConstructor {
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
return localnotifications.NewService(b.appDB, network), nil
}
}

func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -542,6 +553,7 @@ func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
services = appendIf(config.PermissionsConfig.Enabled, services, b.permissionsService())
services = appendIf(config.MailserversConfig.Enabled, services, b.mailserversService())
services = appendIf(config.WalletConfig.Enabled, services, b.walletService(config.NetworkID, accountsFeed))
services = appendIf(config.LocalNotificationsConfig.Enabled, services, b.localNotificationsService(config.NetworkID))

manager := b.accountManager.GetManager()
if manager == nil {
Expand Down Expand Up @@ -889,17 +901,27 @@ func (b *GethStatusBackend) AppStateChange(state string) {
}
}
} else if s == appStateBackground && !b.forceStopWallet {
localNotifications, err := b.statusNode.LocalNotificationsService()
if err != nil {
b.log.Error("Retrieving of local notifications service failed on app state change", "error", err)
}

wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on app state change to background", "error", err)
return
}
err = wallet.Stop()
if err != nil {
b.log.Error("Wallet service stop failed on app state change to background", "error", err)
return

if !localNotifications.IsWatchingWallet() {
err = wallet.Stop()

if err != nil {
b.log.Error("Wallet service stop failed on app state change to background", "error", err)
return
}
}
}

// TODO: put node in low-power mode if the app is in background (or inactive)
// and normal mode if the app is in foreground.
}
Expand Down Expand Up @@ -948,6 +970,62 @@ func (b *GethStatusBackend) StartWallet() error {
return nil
}

func (b *GethStatusBackend) StopLocalNotifications() error {
localPN, err := b.statusNode.LocalNotificationsService()
if err != nil {
b.log.Error("Retrieving of LocalNotifications service failed on StopLocalNotifications", "error", err)
return nil
}

if localPN.IsStarted() {
err = localPN.Stop()
if err != nil {
b.log.Error("LocalNotifications service stop failed on StopLocalNotifications", "error", err)
return nil
}
}

return nil
}

func (b *GethStatusBackend) StartLocalNotifications() error {
localPN, err := b.statusNode.LocalNotificationsService()

if err != nil {
b.log.Error("Retrieving of local notifications service failed on StartLocalNotifications", "error", err)
return nil
}

wallet, err := b.statusNode.WalletService()
if err != nil {
b.log.Error("Retrieving of wallet service failed on StartLocalNotifications", "error", err)
return nil
}

if !wallet.IsStarted() {
b.log.Error("Can't start local notifications service without wallet service")
return nil
}

if !localPN.IsStarted() {
err = localPN.Start(b.statusNode.Server())

if err != nil {
b.log.Error("LocalNotifications service start failed on StartLocalNotifications", "error", err)
return nil
}
}

err = localPN.SubscribeWallet(wallet.GetFeed())

if err != nil {
b.log.Error("LocalNotifications service could not subscribe to wallet on StartLocalNotifications", "error", err)
return nil
}

return nil
}

// Logout clears whisper identities.
func (b *GethStatusBackend) Logout() error {
b.mu.Lock()
Expand Down Expand Up @@ -1118,6 +1196,7 @@ func (b *GethStatusBackend) startWallet() error {
}

watchAddresses := b.accountManager.WatchAddresses()

mainAccountAddress, err := b.accountManager.MainAccountAddress()
if err != nil {
return err
Expand Down
Loading

0 comments on commit d04e54e

Please sign in to comment.