diff --git a/api/geth_backend.go b/api/geth_backend.go index 525096e96..1c3318c16 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -84,6 +84,7 @@ type GethStatusBackend struct { selectedAccountKeyID string log log.Logger allowAllRPC bool // used only for tests, disables api method restrictions + forceStopWallet bool } // NewGethStatusBackend create a new GethStatusBackend instance @@ -269,9 +270,6 @@ func (b *GethStatusBackend) startNodeWithKey(acc multiaccounts.Account, password if err != nil { return err } - if err := b.startWallet(); err != nil { - return err - } err = b.multiaccountsDB.UpdateAccountTimestamp(acc.KeyUID, time.Now().Unix()) if err != nil { return err @@ -856,7 +854,7 @@ func (b *GethStatusBackend) AppStateChange(state string) { b.log.Info("App State changed", "new-state", s) b.appState = s - if s == appStateForeground { + if s == appStateForeground && !b.forceStopWallet { wallet, err := b.statusNode.WalletService() if err != nil { b.log.Error("Retrieving of wallet service failed on app state change to active", "error", err) @@ -875,7 +873,7 @@ func (b *GethStatusBackend) AppStateChange(state string) { return } } - } else if s == appStateBackground { + } else if s == appStateBackground && !b.forceStopWallet { wallet, err := b.statusNode.WalletService() if err != nil { b.log.Error("Retrieving of wallet service failed on app state change to background", "error", err) @@ -891,6 +889,50 @@ func (b *GethStatusBackend) AppStateChange(state string) { // and normal mode if the app is in foreground. } +func (b *GethStatusBackend) StopWallet() error { + wallet, err := b.statusNode.WalletService() + if err != nil { + b.log.Error("Retrieving of wallet service failed on StopWallet", "error", err) + return nil + } + if wallet.IsStarted() { + err = wallet.Stop() + if err != nil { + b.log.Error("Wallet service stop failed on StopWallet", "error", err) + return nil + } + } + + b.forceStopWallet = true + + return nil +} + +func (b *GethStatusBackend) StartWallet() error { + wallet, err := b.statusNode.WalletService() + if err != nil { + b.log.Error("Retrieving of wallet service failed on StartWallet", "error", err) + return nil + } + if !wallet.IsStarted() { + err = wallet.Start(b.statusNode.Server()) + if err != nil { + b.log.Error("Wallet service start failed on StartWallet", "error", err) + return nil + } + + err = b.startWallet() + if err != nil { + b.log.Error("Wallet reactor start failed on StartWallet", "error", err) + return nil + } + } + + b.forceStopWallet = false + + return nil +} + // Logout clears whisper identities. func (b *GethStatusBackend) Logout() error { b.mu.Lock() @@ -940,9 +982,11 @@ func (b *GethStatusBackend) cleanupServices() error { switch err { case node.ErrServiceUnknown: case nil: - err = wallet.StopReactor() - if err != nil { - return err + if wallet.IsStarted() { + err = wallet.Stop() + if err != nil { + return err + } } default: return err @@ -981,10 +1025,6 @@ func (b *GethStatusBackend) SelectAccount(loginParams account.LoginParams) error return err } - if err := b.startWallet(); err != nil { - return err - } - return nil } diff --git a/mobile/status.go b/mobile/status.go index e2257b566..0756e9a4c 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -624,6 +624,18 @@ func AppStateChange(state string) { statusBackend.AppStateChange(state) } +// StopWallet +func StopWallet() string { + err := statusBackend.StopWallet() + return makeJSONResponse(err) +} + +// StartWallet +func StartWallet() string { + err := statusBackend.StartWallet() + return makeJSONResponse(err) +} + // SetMobileSignalHandler setup geth callback to notify about new signal // used for gomobile builds func SetMobileSignalHandler(handler SignalHandler) { diff --git a/services/wallet/service.go b/services/wallet/service.go index 38266bf05..2ae71d65f 100644 --- a/services/wallet/service.go +++ b/services/wallet/service.go @@ -64,8 +64,10 @@ func (s *Service) StopReactor() error { return nil } s.reactor.Stop() - s.group.Stop() - s.group.Wait() + if s.group != nil { + s.group.Stop() + s.group.Wait() + } return nil }